git ssb

10+

Matt McKegg / patchwork



Tree: 02eb138217bf2dd28c3be988febc79c8d62e190b

Files: 02eb138217bf2dd28c3be988febc79c8d62e190b / modules / page / html / render / message.js

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

Built with git-ssb-web