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