git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: c9ba3630ee9b32a904d47de7b87c1e101e5d59c5

Files: c9ba3630ee9b32a904d47de7b87c1e101e5d59c5 / modules / page / html / render / message.js

4317 bytesRaw
1var { h, when, map, Proxy, Struct, Value, computed } = require('mutant')
2var nest = require('depnest')
3var ref = require('ssb-ref')
4var AnchorHook = require('../../../../lib/anchor-hook')
5
6exports.needs = nest({
7 'keys.sync.id': 'first',
8 'feed.obs.thread': 'first',
9 'message.sync.unbox': 'first',
10 'message.sync.root': 'first',
11 'message.html': {
12 render: 'first',
13 compose: 'first'
14 },
15 'sbot.async.get': 'first',
16 'intl.sync.i18n': 'first',
17 'message.html.missing': 'first'
18})
19
20exports.gives = nest('page.html.render')
21
22exports.create = function (api) {
23 const i18n = api.intl.sync.i18n
24 return nest('page.html.render', function (id) {
25 if (!ref.isMsg(id)) return
26 var loader = h('div', {className: 'Loading -large'})
27
28 var result = Proxy(loader)
29 var anchor = Value()
30 var participants = Proxy([])
31
32 var meta = Struct({
33 type: 'post',
34 root: Proxy(id),
35 branch: Proxy(id),
36 reply: Value(undefined),
37 channel: Value(undefined),
38 recps: Value(undefined)
39 })
40
41 var compose = api.message.html.compose({
42 meta,
43 isPrivate: when(meta.recps, true),
44 shrink: false,
45 participants,
46 hooks: [
47 AnchorHook('reply', anchor, (el) => el.focus())
48 ],
49 placeholder: when(meta.recps, i18n('Write a private reply'), i18n('Write a public reply'))
50 })
51
52 api.sbot.async.get(id, (err, value) => {
53 if (err) {
54 return result.set(h('PageHeading', [
55 h('h1', i18n('Cannot load thread'))
56 ]))
57 }
58
59 if (typeof value.content === 'string') {
60 value = api.message.sync.unbox(value)
61 }
62
63 if (!value) {
64 return result.set(h('PageHeading', [
65 h('h1', i18n('Cannot display message.'))
66 ]))
67 }
68
69 // what happens in private stays in private!
70 meta.recps.set(value.content.recps)
71
72 var author = value.author
73 var root = api.message.sync.root({key: id, value}) || id
74 var isReply = id !== root
75 var thread = api.feed.obs.thread(id, {branch: isReply})
76
77 meta.channel.set(value.content.channel)
78 meta.root.set(root || thread.rootId)
79
80 // track message author for resolving missing messages and reply mentions
81 meta.reply.set({[id]: author})
82
83 // if root thread, reply to last post
84 meta.branch.set(isReply ? thread.branchId : thread.lastId)
85
86 participants.set(computed(thread.messages, messages => {
87 return messages.map(msg => msg && msg.value && msg.value.author)
88 }))
89
90 var container = h('Thread', [
91 h('div.messages', [
92 when(thread.branchId, h('a.full', {href: thread.rootId, anchor: id}, [i18n('View full thread')])),
93 map(thread.messages, (msg) => {
94 return computed([msg, thread.previousKey(msg)], (msg, previousId) => {
95 return h('div', {
96 hooks: [AnchorHook(msg.key, anchor, showContext)]
97 }, [
98 msg.key !== id ? api.message.html.missing(last(msg.value.content.branch), msg) : null,
99 api.message.html.render(msg, {
100 pageId: id,
101 previousId,
102 includeForks: msg.key !== id,
103 includeReferences: true
104 })
105 ])
106 })
107 })
108 ]),
109 compose
110 ])
111 result.set(when(thread.sync, container, loader))
112 })
113
114 var view = h('div', {className: 'SplitView'}, [
115 h('div.main', [
116 result
117 ])
118 ])
119
120 view.setAnchor = function (value) {
121 anchor.set(value)
122 }
123
124 return view
125 })
126}
127
128function showContext (element) {
129 var scrollParent = getScrollParent(element)
130 if (scrollParent) {
131 // ensure context is visible
132 scrollParent.scrollTop = Math.max(0, scrollParent.scrollTop - 100)
133 }
134}
135
136function getScrollParent (element) {
137 while (element.parentNode) {
138 if (element.parentNode.scrollTop > 10 && isScroller(element.parentNode)) {
139 return element.parentNode
140 } else {
141 element = element.parentNode
142 }
143 }
144}
145
146function isScroller (element) {
147 var value = window.getComputedStyle(element)['overflow-y']
148 return (value === 'auto' || value === 'scroll')
149}
150
151function last (array) {
152 if (Array.isArray(array)) {
153 return array[array.length - 1]
154 } else {
155 return array
156 }
157}
158

Built with git-ssb-web