Files: 98e148d13e4dcd1d5688c01b61c83da5039c4d4d / modules_basic / message.js
2849 bytesRaw
1 | var h = require('hyperscript') |
2 | var u = require('../util') |
3 | var pull = require('pull-stream') |
4 | |
5 | var plugs = require('../plugs') |
6 | var message_content = plugs.first(exports.message_content = []) |
7 | var message_content_mini = plugs.first(exports.message_content_mini = []) |
8 | var avatar = plugs.first(exports.avatar = []) |
9 | var avatar_name = plugs.first(exports.avatar_name = []) |
10 | var avatar_link = plugs.first(exports.avatar_link = []) |
11 | var message_meta = plugs.map(exports.message_meta = []) |
12 | var message_action = plugs.map(exports.message_action = []) |
13 | var message_link = plugs.first(exports.message_link = []) |
14 | |
15 | var sbot_links = plugs.first(exports.sbot_links = []) |
16 | |
17 | function mini(msg, el) { |
18 | var div = h('div.message.message--mini', |
19 | h('div.row', |
20 | h('div', |
21 | avatar_link(msg.value.author, avatar_name(msg.value.author)), |
22 | h('span.message_content', el)), |
23 | h('div.message_meta.row', message_meta(msg)) |
24 | ) |
25 | ) |
26 | div.setAttribute('tabindex', '0') |
27 | return div |
28 | } |
29 | |
30 | function message_content_mini_fallback(msg) { |
31 | return h('code', msg.value.content.type) |
32 | } |
33 | |
34 | exports.message_render = function (msg, sbot) { |
35 | var el = message_content_mini(msg) |
36 | if(el) return mini(msg, el) |
37 | |
38 | var el = message_content(msg) |
39 | if(!el) return mini(msg, message_content_mini_fallback(msg)) |
40 | |
41 | var links = [] |
42 | for(var k in CACHE) { |
43 | var _msg = CACHE[k] |
44 | if(Array.isArray(_msg.content.mentions)) { |
45 | for(var i = 0; i < _msg.content.mentions.length; i++) |
46 | if(_msg.content.mentions[i].link == msg.key) |
47 | links.push(k) |
48 | } |
49 | } |
50 | |
51 | var backlinks = h('div.backlinks') |
52 | if(links.length) |
53 | backlinks.appendChild(h('label', 'backlinks:', |
54 | h('div', links.map(function (key) { |
55 | return message_link(key) |
56 | })) |
57 | )) |
58 | |
59 | |
60 | // pull( |
61 | // sbot_links({dest: msg.key, rel: 'mentions', keys: true}), |
62 | // pull.collect(function (err, links) { |
63 | // if(links.length) |
64 | // backlinks.appendChild(h('label', 'backlinks:', |
65 | // h('div', links.map(function (link) { |
66 | // return message_link(link.key) |
67 | // })) |
68 | // )) |
69 | // }) |
70 | // ) |
71 | |
72 | var msg = h('div.message', |
73 | h('div.title.row', |
74 | h('div.avatar', avatar(msg.value.author, 'thumbnail')), |
75 | h('div.message_meta.row', message_meta(msg)) |
76 | ), |
77 | h('div.message_content', el), |
78 | h('div.message_actions.row', |
79 | h('div.actions', message_action(msg), |
80 | h('a', {href: '#' + msg.key}, 'Reply') |
81 | ) |
82 | ), |
83 | backlinks, |
84 | {onkeydown: function (ev) { |
85 | //on enter, hit first meta. |
86 | if(ev.keyCode == 13) { |
87 | |
88 | // unless in an input |
89 | if (ev.target.nodeName === 'INPUT' |
90 | || ev.target.nodeName === 'TEXTAREA') return |
91 | |
92 | msg.querySelector('.enter').click() |
93 | } |
94 | }} |
95 | ) |
96 | |
97 | // ); hyperscript does not seem to set attributes correctly. |
98 | msg.setAttribute('tabindex', '0') |
99 | |
100 | return msg |
101 | } |
102 | |
103 | |
104 | |
105 | |
106 | |
107 | |
108 | |
109 | |
110 |
Built with git-ssb-web