Files: 14d54b356c1ef57cc1dca625f3b0886b7c31de34 / modules_basic / message.js
2206 bytesRaw
1 | var fs = require('fs') |
2 | var Path = require('path') |
3 | var pull = require('pull-stream') |
4 | var u = require('../util') |
5 | var h = require('../h') |
6 | |
7 | exports.needs = { |
8 | avatar_name: 'first', |
9 | avatar_link: 'first', |
10 | message_meta: 'map', |
11 | message_action: 'map', |
12 | message_link: 'first', |
13 | message_backlinks: 'first', |
14 | message_author: 'first', |
15 | message_content: 'first', |
16 | message_content_mini: 'first', |
17 | message_footer: 'first' |
18 | } |
19 | |
20 | exports.gives = { |
21 | message_render: true, |
22 | mcss: true |
23 | } |
24 | |
25 | exports.create = function (api) { |
26 | return { |
27 | message_render, |
28 | mcss: () => fs.readFileSync(Path.join(__dirname, 'message.mcss')) |
29 | } |
30 | |
31 | function message_render (msg, sbot) { |
32 | var content = api.message_content_mini(msg) |
33 | if (content) return mini(msg, content) |
34 | |
35 | var content = api.message_content(msg) |
36 | if (!content) return mini(msg, message_content_mini_fallback(msg)) |
37 | |
38 | return h('Message', { |
39 | 'ev-keydown': navigateToMessageOnEnter, |
40 | attributes: { |
41 | tabindex: '0' |
42 | } |
43 | }, [ |
44 | h('hr'), |
45 | h('header.author', api.message_author(msg)), |
46 | h('section.meta', api.message_meta(msg)), |
47 | h('section.content', content), |
48 | h('section.action', api.message_action(msg)), |
49 | h('footer.backlinks', api.message_backlinks(msg)) |
50 | ]) |
51 | |
52 | function navigateToMessageOnEnter (ev) { |
53 | // on enter, hit first meta. |
54 | if(ev.keyCode == 13) { |
55 | |
56 | // unless in an input |
57 | if (ev.target.nodeName === 'INPUT' |
58 | || ev.target.nodeName === 'TEXTAREA') return |
59 | |
60 | // HACK! (mw) |
61 | // there's no exported api to open a new tab. :/ |
62 | // it's only done in `app.js` module in an`onhashchange` handler. |
63 | // sooooooo yeah this shit for now :) |
64 | var wtf = h('a', { href: `#${msg.key}` }) |
65 | msgEl.appendChild(wtf) |
66 | wtf.click() |
67 | } |
68 | } |
69 | } |
70 | |
71 | function mini(msg, el) { |
72 | return h('Message -mini', { |
73 | attributes: { |
74 | tabindex: '0' |
75 | } |
76 | }, [ |
77 | h('hr'), |
78 | h('header.author', api.message_author(msg, { size: 0 })), |
79 | h('section.meta', api.message_meta(msg)), |
80 | h('section.content', el) |
81 | ]) |
82 | } |
83 | } |
84 | |
85 | |
86 | function message_content_mini_fallback(msg) { |
87 | return h('code', msg.value.content.type) |
88 | } |
89 |
Built with git-ssb-web