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