Files: 820c6a9e100a474ed87382b2e85bf5efc6040e78 / message / html / compose.js
7790 bytesRaw
1 | const { h, when, send, resolve, Value, Array: MutantArray, computed } = require('mutant') |
2 | const nest = require('depnest') |
3 | const ssbMentions = require('ssb-mentions') |
4 | const extend = require('xtend') |
5 | const addSuggest = require('suggest-box') |
6 | const blobFiles = require('ssb-blob-files') |
7 | |
8 | exports.gives = nest('message.html.compose') |
9 | |
10 | exports.needs = nest({ |
11 | 'about.async.suggest': 'first', |
12 | 'channel.async.suggest': 'first', |
13 | 'emoji.async.suggest': 'first', |
14 | 'meme.async.suggest': 'first', |
15 | // 'blob.html.input': 'first', // TODO extract fileInput creator below out into patchcore |
16 | 'message.html.confirm': 'first', |
17 | 'drafts.sync.get': 'first', |
18 | 'drafts.sync.set': 'first', |
19 | 'drafts.sync.remove': 'first', |
20 | 'settings.obs.get': 'first', |
21 | 'sbot.obs.connection': 'first' |
22 | }) |
23 | |
24 | exports.create = function (api) { |
25 | return nest({ 'message.html.compose': compose }) |
26 | |
27 | function compose (options, cb) { |
28 | const { |
29 | meta, |
30 | location, |
31 | feedIdsInThread = [], |
32 | prepublish, |
33 | placeholder = 'Write a message', |
34 | shrink = true |
35 | } = options |
36 | |
37 | if (typeof resolve(meta) !== 'object') throw new Error('Compose needs meta data about what sort of message composer you are making') |
38 | if (!location) throw new Error('Compose expects a unique location so it can save drafts of messages') |
39 | |
40 | var files = [] |
41 | var filesById = {} |
42 | var channelInputFocused = Value(false) |
43 | var textAreaFocused = Value(false) |
44 | var focused = computed([channelInputFocused, textAreaFocused], (a, b) => a || b) |
45 | var hasContent = Value(false) |
46 | |
47 | var blurTimeout = null |
48 | |
49 | var expanded = computed([shrink, focused, hasContent], (shrink, focused, hasContent) => { |
50 | if (!shrink || hasContent) return true |
51 | |
52 | return focused |
53 | }) |
54 | |
55 | var channelInput = h('input.channel', { |
56 | 'ev-input': () => hasContent.set(!!channelInput.value), |
57 | 'ev-keyup': ev => { |
58 | ev.target.value = ev.target.value.replace(/^#*([\w@%&])/, '#$1') |
59 | }, |
60 | 'ev-blur': () => { |
61 | clearTimeout(blurTimeout) |
62 | blurTimeout = setTimeout(() => channelInputFocused.set(false), 200) |
63 | }, |
64 | 'ev-focus': send(channelInputFocused.set, true), |
65 | placeholder: '#channel (optional)', |
66 | value: computed(meta.channel, ch => ch ? '#' + ch : null), |
67 | disabled: meta.channel ? true : undefined, |
68 | title: meta.channel ? 'Reply is in same channel as original message' : undefined |
69 | }) |
70 | |
71 | var draftPerstTimeout = null |
72 | var draftLocation = location |
73 | var textArea = h('textarea', { |
74 | 'ev-input': () => { |
75 | hasContent.set(!!textArea.value) |
76 | clearTimeout(draftPerstTimeout) |
77 | draftPerstTimeout = setTimeout(() => { |
78 | api.drafts.sync.set(draftLocation, textArea.value) |
79 | }, 200) |
80 | }, |
81 | 'ev-blur': () => { |
82 | clearTimeout(blurTimeout) |
83 | blurTimeout = setTimeout(() => textAreaFocused.set(false), 200) |
84 | }, |
85 | 'ev-focus': send(textAreaFocused.set, true), |
86 | placeholder |
87 | }) |
88 | textArea.publish = publish // TODO: fix - clunky api for the keyboard shortcut to target |
89 | |
90 | // load draft |
91 | let draft = api.drafts.sync.get(draftLocation) |
92 | if (typeof draft === 'string') { |
93 | textArea.value = draft |
94 | hasContent.set(true) |
95 | } |
96 | |
97 | var isPrivate = location.page === 'private' || |
98 | (location.key && !location.value) || |
99 | (location.value && location.value.private) |
100 | |
101 | var warningMessages = MutantArray([]) |
102 | var warning = computed(warningMessages, msgs => { |
103 | if (!msgs.length) return |
104 | |
105 | return h('section.warnings', msgs.map((m, i) => { |
106 | return h('div.warning', [ |
107 | h('i.fa.fa-exclamation-triangle'), |
108 | h('div.message', m), |
109 | h('i.fa.fa-times', { 'ev-click': () => warningMessages.deleteAt(i) }) |
110 | ]) |
111 | })) |
112 | }) |
113 | |
114 | var fileInput = h('input', { |
115 | type: 'file', |
116 | // accept, |
117 | attributes: { multiple: true }, |
118 | 'ev-click': () => hasContent.set(true), |
119 | 'ev-change': (ev) => { |
120 | warningMessages.set([]) |
121 | |
122 | const files = ev.target.files |
123 | const opts = { |
124 | stripExif: api.settings.obs.get('patchbay.removeExif', true), |
125 | isPrivate |
126 | } |
127 | blobFiles(files, api.sbot.obs.connection, opts, afterBlobed) |
128 | } |
129 | }) |
130 | function afterBlobed (err, result) { |
131 | if (err) { |
132 | console.error(err) |
133 | warningMessages.push(err.message) |
134 | return |
135 | } |
136 | |
137 | files.push(result) |
138 | filesById[result.link] = result |
139 | |
140 | const pos = textArea.selectionStart |
141 | const embed = result.type.match(/^image/) ? '!' : '' |
142 | const spacer = embed ? '\n' : ' ' |
143 | const insertLink = spacer + embed + '[' + result.name + ']' + '(' + result.link + ')' + spacer |
144 | |
145 | textArea.value = textArea.value.slice(0, pos) + insertLink + textArea.value.slice(pos) |
146 | |
147 | console.log('added:', result) |
148 | } |
149 | |
150 | var publishBtn = h('button', { 'ev-click': publish }, isPrivate ? 'Reply' : 'Publish') |
151 | |
152 | var actions = h('section.actions', [ |
153 | fileInput, |
154 | publishBtn |
155 | ]) |
156 | |
157 | var composer = h('Compose', { |
158 | classList: when(expanded, '-expanded', '-contracted') |
159 | }, [ |
160 | channelInput, |
161 | textArea, |
162 | warning, |
163 | actions |
164 | ]) |
165 | |
166 | composer.addQuote = function (data) { |
167 | try { |
168 | if (typeof data.content.text === 'string') { |
169 | var text = data.content.text |
170 | textArea.value += '> ' + text.replace(/\r\n|\r|\n/g, '\n> ') + '\r\n\n' |
171 | hasContent.set(!!textArea.value) |
172 | } |
173 | } catch (err) { |
174 | // object not have text or content |
175 | } |
176 | } |
177 | |
178 | if (location.action === 'quote') { composer.addQuote(location.value) } |
179 | |
180 | addSuggest(channelInput, (inputText, cb) => { |
181 | if (inputText[0] === '#') { |
182 | api.channel.async.suggest(inputText.slice(1), cb) |
183 | } |
184 | }, { cls: 'PatchSuggest' }) |
185 | channelInput.addEventListener('suggestselect', ev => { |
186 | channelInput.value = ev.detail.id // HACK : this over-rides the markdown value |
187 | }) |
188 | |
189 | addSuggest(textArea, (inputText, cb) => { |
190 | const char = inputText[0] |
191 | const wordFragment = inputText.slice(1) |
192 | |
193 | if (char === '@') api.about.async.suggest(wordFragment, feedIdsInThread, cb) |
194 | if (char === '#') api.channel.async.suggest(wordFragment, cb) |
195 | if (char === ':') api.emoji.async.suggest(wordFragment, cb) |
196 | if (char === '&') api.meme.async.suggest(wordFragment, cb) |
197 | }, { cls: 'PatchSuggest' }) |
198 | |
199 | return composer |
200 | |
201 | // scoped |
202 | |
203 | function publish () { |
204 | publishBtn.disabled = true |
205 | |
206 | const channel = channelInput.value.startsWith('#') |
207 | ? channelInput.value.substr(1).trim() |
208 | : channelInput.value.trim() |
209 | const mentions = ssbMentions(textArea.value).map(mention => { |
210 | // merge markdown-detected mention with file info |
211 | var file = filesById[mention.link] |
212 | if (file) { |
213 | if (file.type) mention.type = file.type |
214 | if (file.size) mention.size = file.size |
215 | } |
216 | return mention |
217 | }) |
218 | |
219 | var content = extend(resolve(meta), { |
220 | text: textArea.value, |
221 | channel, |
222 | mentions |
223 | }) |
224 | |
225 | if (!channel) delete content.channel |
226 | if (!mentions.length) delete content.mentions |
227 | if (content.recps && content.recps.length === 0) delete content.recps |
228 | |
229 | try { |
230 | if (typeof prepublish === 'function') { |
231 | content = prepublish(content) |
232 | } |
233 | } catch (err) { |
234 | publishBtn.disabled = false |
235 | if (cb) cb(err) |
236 | else throw err |
237 | } |
238 | |
239 | return api.message.html.confirm(content, done) |
240 | |
241 | function done (err, msg) { |
242 | publishBtn.disabled = false |
243 | if (err) throw err |
244 | else if (msg) { |
245 | textArea.value = '' |
246 | api.drafts.sync.remove(draftLocation) |
247 | } |
248 | if (cb) cb(err, msg) |
249 | } |
250 | } |
251 | } |
252 | } |
253 |
Built with git-ssb-web