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