Files: abd49a5d6c84f499180e6bdbb52e5188d42df75e / modules_basic / message.js
2261 bytesRaw
1 | const fs = require('fs') |
2 | const pull = require('pull-stream') |
3 | const u = require('../util') |
4 | const h = require('../h') |
5 | |
6 | exports.needs = { |
7 | avatar_name: 'first', |
8 | avatar_link: 'first', |
9 | message_action: 'map', |
10 | message_author: 'first', |
11 | message_backlinks: 'first', |
12 | message_content: 'first', |
13 | message_content_mini: 'first', |
14 | message_title: 'first', |
15 | message_link: 'first', |
16 | message_meta: 'map', |
17 | } |
18 | |
19 | exports.gives = { |
20 | message_render: true, |
21 | mcss: true |
22 | } |
23 | |
24 | exports.create = function (api) { |
25 | return { |
26 | message_render, |
27 | mcss: () => fs.readFileSync(__filename.replace(/js$/, 'mcss'), 'utf8') |
28 | } |
29 | |
30 | function message_render (msg) { |
31 | var content = api.message_content_mini(msg) |
32 | if (content) return mini(msg, content) |
33 | |
34 | content = api.message_content(msg) |
35 | if (!content) return mini(msg, message_content_mini_fallback(msg)) |
36 | |
37 | var msgEl = h('Message', { |
38 | 'ev-keydown': navigateToMessageOnEnter, |
39 | attributes: { |
40 | tabindex: '0' |
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.action', api.message_action(msg)), |
48 | h('footer.backlinks', api.message_backlinks(msg)) |
49 | ]) |
50 | return msgEl |
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 | msgEl.removeChild(wtf) |
68 | } |
69 | } |
70 | } |
71 | |
72 | function mini(msg, el) { |
73 | return h('Message -mini', { |
74 | attributes: { |
75 | tabindex: '0' |
76 | } |
77 | }, [ |
78 | h('header.author', api.message_author(msg, { size: 'mini' })), |
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 | |
90 |
Built with git-ssb-web