Files: db07f3aa11263d691a9cda69238563a05b7c440e / app / html / comments.js
4263 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Array: MutantArray, Value, map, computed, when, resolve, throttle } = require('mutant') |
3 | const get = require('lodash/get') |
4 | |
5 | exports.gives = nest('app.html.comments') |
6 | |
7 | exports.needs = nest({ |
8 | 'about.html.avatar': 'first', |
9 | 'about.obs.name': 'first', |
10 | 'backlinks.obs.for': 'first', |
11 | 'feed.obs.thread': 'first', |
12 | 'message.html.compose': 'first', |
13 | 'message.html.markdown': 'first', |
14 | 'message.html.timeago': 'first', |
15 | 'message.html.likes': 'first', |
16 | 'unread.sync.markRead': 'first', |
17 | 'unread.sync.isUnread': 'first', |
18 | 'translations.sync.strings': 'first', |
19 | }) |
20 | |
21 | exports.create = (api) => { |
22 | return nest('app.html.comments', comments) |
23 | |
24 | function comments (root) { |
25 | const { messages, channel, lastId: branch } = api.feed.obs.thread(root) |
26 | const strings = api.translations.sync.strings() |
27 | |
28 | // TODO - move this up into Patchcore |
29 | const messagesTree = computed(throttle(messages, 200), msgs => { |
30 | return msgs |
31 | .filter(msg => forkOf(msg) === undefined) |
32 | .map(threadMsg => { |
33 | const nestedReplies = msgs.filter(msg => forkOf(msg) === threadMsg.key) |
34 | threadMsg.replies = nestedReplies |
35 | return threadMsg |
36 | }) |
37 | }) |
38 | |
39 | const meta = { |
40 | type: 'post', |
41 | root, |
42 | branch, |
43 | channel |
44 | } |
45 | // const twoComposers = computed(messages, messages => { |
46 | // return messages.length > 5 |
47 | // }) |
48 | const { compose } = api.message.html |
49 | |
50 | |
51 | return h('Comments', [ |
52 | // when(twoComposers, compose({ meta, shrink: true, canAttach: false })), |
53 | map(messagesTree, msg => Comment(msg, root, branch)), |
54 | compose({ meta, shrink: false, canAttach: false, placeholder: strings.writeComment }), |
55 | ]) |
56 | } |
57 | |
58 | function Comment (msgObs, root, branch) { |
59 | const strings = api.translations.sync.strings() |
60 | const msg = resolve(msgObs) |
61 | |
62 | const raw = get(msg, 'value.content.text') |
63 | var className = api.unread.sync.isUnread(msg) ? ' -unread' : ' -read' |
64 | api.unread.sync.markRead(msg) |
65 | |
66 | if (!get(msg, 'value.content.root')) return |
67 | |
68 | const { author, content } = msg.value |
69 | |
70 | // // TODO - move this upstream into patchcore:feed.obs.thread ?? |
71 | // // OR change strategy to use forks |
72 | // const backlinks = api.backlinks.obs.for(msg.key) |
73 | // const nestedReplies = computed(backlinks, backlinks => { |
74 | // return backlinks.filter(backlinker => { |
75 | // const { type, root } = backlinker.value.content |
76 | // return type === 'post' && root === msg.key |
77 | // }) |
78 | // }) |
79 | |
80 | var nestedReplyCompose = Value(false) |
81 | const toggleCompose = () => nestedReplyCompose.set(!nestedReplyCompose()) |
82 | const nestedReplyComposer = api.message.html.compose({ |
83 | meta: { |
84 | type: 'post', |
85 | root, |
86 | fork: msg.key, |
87 | branch, |
88 | channel: content.channel |
89 | }, |
90 | shrink: false, |
91 | canAttach: false, |
92 | canPreview: false, |
93 | placeholder: strings.writeComment |
94 | }, toggleCompose) |
95 | |
96 | return h('Comment', { className }, [ |
97 | h('div.left', api.about.html.avatar(author, 'tiny')), |
98 | h('div.right', [ |
99 | h('section.context', [ |
100 | h('div.name', api.about.obs.name(author)), |
101 | api.message.html.timeago(msg) |
102 | ]), |
103 | h('section.content', api.message.html.markdown(raw)), |
104 | when(msgObs.replies, |
105 | h('section.replies', |
106 | map(msgObs.replies, NestedComment) |
107 | ) |
108 | ), |
109 | h('section.actions', [ |
110 | h('div.reply', { 'ev-click': toggleCompose }, [ |
111 | h('i.fa.fa-commenting-o'), |
112 | ]), |
113 | api.message.html.likes(msg) |
114 | ]), |
115 | when(nestedReplyCompose, nestedReplyComposer), |
116 | ]) |
117 | ]) |
118 | } |
119 | |
120 | function NestedComment (msgObs) { |
121 | const msg = resolve(msgObs) |
122 | const raw = get(msg, 'value.content.text') |
123 | if (!raw) return |
124 | |
125 | const { author } = msg.value |
126 | |
127 | return h('Comment -nested', [ |
128 | h('div.left'), |
129 | h('div.right', [ |
130 | h('section.context', [ |
131 | h('div.name', api.about.obs.name(author)), |
132 | api.message.html.timeago(msg) |
133 | ]), |
134 | h('section.content', api.message.html.markdown(raw)), |
135 | ]) |
136 | ]) |
137 | |
138 | api.message.html.markdown(raw) |
139 | } |
140 | } |
141 | |
142 | function forkOf (msg) { |
143 | return get(msg, 'value.content.fork') |
144 | } |
145 | |
146 | |
147 |
Built with git-ssb-web