Files: d50d366baa6cc2cb6d544429e5f100b7b0c6d604 / modules_basic / message / render.js
2395 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 | 'data-key': msg.key, |
40 | 'data-text': msg.value.content.text |
41 | } |
42 | }, [ |
43 | h('header.author', api.message_author(msg)), |
44 | h('section.title', api.message_title(msg)), |
45 | h('section.meta', api.message_meta(msg)), |
46 | h('section.content', content), |
47 | h('section.raw-content'), |
48 | h('section.action', api.message_action(msg)), |
49 | h('footer.backlinks', api.message_backlinks(msg)) |
50 | ]) |
51 | return msgEl |
52 | |
53 | function navigateToMessageOnEnter (ev) { |
54 | // on enter (or 'o'), hit first meta. |
55 | if(ev.keyCode == 13 || ev.keyCode == 79) { |
56 | |
57 | // unless in an input |
58 | if (ev.target.nodeName === 'INPUT' |
59 | || ev.target.nodeName === 'TEXTAREA') return |
60 | |
61 | // HACK! (mw) |
62 | // there's no exported api to open a new tab. :/ |
63 | // it's only done in `app.js` module in an`onhashchange` handler. |
64 | // sooooooo yeah this shit for now :) |
65 | var wtf = h('a', { href: `#${msg.key}` }) |
66 | msgEl.appendChild(wtf) |
67 | wtf.click() |
68 | msgEl.removeChild(wtf) |
69 | } |
70 | } |
71 | } |
72 | |
73 | function mini(msg, el) { |
74 | return h('Message -mini', { |
75 | attributes: { |
76 | tabindex: '0', |
77 | 'data-key': msg.key |
78 | } |
79 | }, [ |
80 | h('header.author', api.message_author(msg, { size: 'mini' })), |
81 | h('section.meta', api.message_meta(msg)), |
82 | h('section.content', el), |
83 | h('section.raw-content') |
84 | ]) |
85 | } |
86 | } |
87 | |
88 | |
89 | function message_content_mini_fallback(msg) { |
90 | return h('code', msg.value.content.type) |
91 | } |
92 | |
93 |
Built with git-ssb-web