git ssb

16+

Dominic / patchbay



Tree: 7c22ec2ce201b04fa7c787e4f7833eb28065327b

Files: 7c22ec2ce201b04fa7c787e4f7833eb28065327b / modules_basic / message / render.js

2292 bytesRaw
1const fs = require('fs')
2const h = require('../../h')
3
4exports.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
17exports.gives = {
18 message_render: true,
19 mcss: true
20}
21
22exports.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.raw-content'),
46 h('section.action', api.message_action(msg)),
47 h('footer.backlinks', api.message_backlinks(msg))
48 ])
49 return msgEl
50
51 function navigateToMessageOnEnter (ev) {
52 // on enter (or 'o'), hit first meta.
53 if(ev.keyCode == 13 || ev.keyCode == 79) {
54
55 // unless in an input
56 if (ev.target.nodeName === 'INPUT'
57 || ev.target.nodeName === 'TEXTAREA') return
58
59 // HACK! (mw)
60 // there's no exported api to open a new tab. :/
61 // it's only done in `app.js` module in an`onhashchange` handler.
62 // sooooooo yeah this shit for now :)
63 var wtf = h('a', { href: `#${msg.key}` })
64 msgEl.appendChild(wtf)
65 wtf.click()
66 msgEl.removeChild(wtf)
67 }
68 }
69 }
70
71 function mini(msg, el) {
72 return h('Message -mini', {
73 attributes: {
74 tabindex: '0'
75 }
76 }, [
77 h('header.author', api.message_author(msg, { size: 'mini' })),
78 h('section.meta', api.message_meta(msg)),
79 h('section.content', el),
80 h('section.raw-content')
81 ])
82 }
83}
84
85
86function message_content_mini_fallback(msg) {
87 return h('code', msg.value.content.type)
88}
89
90

Built with git-ssb-web