git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: 7f0e32707cd24ca1c8721a7b8a6028e13958630e

Files: 7f0e32707cd24ca1c8721a7b8a6028e13958630e / modules / page / html / render / message.js

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

Built with git-ssb-web