Files: c493e24e3dd3c7732099cf62acbabe7501d136f3 / message / html / compose.js
8058 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 | |
9 | exports.gives = nest('message.html.compose') |
10 | |
11 | exports.needs = nest({ |
12 | 'about.async.suggest': 'first', |
13 | 'channel.async.suggest': 'first', |
14 | 'emoji.async.suggest': 'first', |
15 | 'meme.async.suggest': 'first', |
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 | 'ev-paste': ev => { |
87 | const files = get(ev, 'clipboardData.files') |
88 | if (!files || !files.length) return |
89 | const opts = { |
90 | stripExif: api.settings.obs.get('patchbay.removeExif', true), |
91 | isPrivate |
92 | } |
93 | blobFiles(files, api.sbot.obs.connection, opts, afterBlobed) |
94 | }, |
95 | placeholder |
96 | }) |
97 | textArea.publish = publish // TODO: fix - clunky api for the keyboard shortcut to target |
98 | |
99 | // load draft |
100 | let draft = api.drafts.sync.get(draftLocation) |
101 | if (typeof draft === 'string') { |
102 | textArea.value = draft |
103 | hasContent.set(true) |
104 | } |
105 | |
106 | var isPrivate = location.page === 'private' || |
107 | (location.key && !location.value) || |
108 | (location.value && location.value.private) |
109 | |
110 | var warningMessages = MutantArray([]) |
111 | var warning = computed(warningMessages, msgs => { |
112 | if (!msgs.length) return |
113 | |
114 | return h('section.warnings', msgs.map((m, i) => { |
115 | return h('div.warning', [ |
116 | h('i.fa.fa-exclamation-triangle'), |
117 | h('div.message', m), |
118 | h('i.fa.fa-times', { 'ev-click': () => warningMessages.deleteAt(i) }) |
119 | ]) |
120 | })) |
121 | }) |
122 | |
123 | var fileInput = h('input', { |
124 | type: 'file', |
125 | // accept, |
126 | attributes: { multiple: true }, |
127 | 'ev-click': () => hasContent.set(true), |
128 | 'ev-change': (ev) => { |
129 | warningMessages.set([]) |
130 | |
131 | const files = ev.target.files |
132 | const opts = { |
133 | stripExif: api.settings.obs.get('patchbay.removeExif', true), |
134 | isPrivate |
135 | } |
136 | blobFiles(files, api.sbot.obs.connection, opts, afterBlobed) |
137 | } |
138 | }) |
139 | function afterBlobed (err, result) { |
140 | if (err) { |
141 | console.error(err) |
142 | warningMessages.push(err.message) |
143 | return |
144 | } |
145 | |
146 | files.push(result) |
147 | filesById[result.link] = result |
148 | |
149 | const pos = textArea.selectionStart |
150 | const embed = result.type.match(/^image/) ? '!' : '' |
151 | const spacer = embed ? '\n' : ' ' |
152 | const insertLink = spacer + embed + '[' + result.name + ']' + '(' + result.link + ')' + spacer |
153 | |
154 | textArea.value = textArea.value.slice(0, pos) + insertLink + textArea.value.slice(pos) |
155 | |
156 | console.log('added:', result) |
157 | } |
158 | |
159 | var publishBtn = h('button', { 'ev-click': publish }, isPrivate ? 'Reply' : 'Publish') |
160 | |
161 | var actions = h('section.actions', [ |
162 | fileInput, |
163 | publishBtn |
164 | ]) |
165 | |
166 | var composer = h('Compose', { |
167 | classList: when(expanded, '-expanded', '-contracted') |
168 | }, [ |
169 | channelInput, |
170 | textArea, |
171 | warning, |
172 | actions |
173 | ]) |
174 | |
175 | composer.addQuote = function (data) { |
176 | try { |
177 | if (typeof data.content.text === 'string') { |
178 | var text = data.content.text |
179 | textArea.value += '> ' + text.replace(/\r\n|\r|\n/g, '\n> ') + '\r\n\n' |
180 | hasContent.set(!!textArea.value) |
181 | } |
182 | } catch (err) { |
183 | // object not have text or content |
184 | } |
185 | } |
186 | |
187 | if (location.action === 'quote') { composer.addQuote(location.value) } |
188 | |
189 | addSuggest(channelInput, (inputText, cb) => { |
190 | if (inputText[0] === '#') { |
191 | api.channel.async.suggest(inputText.slice(1), cb) |
192 | } |
193 | }, { cls: 'PatchSuggest' }) |
194 | channelInput.addEventListener('suggestselect', ev => { |
195 | channelInput.value = ev.detail.id // HACK : this over-rides the markdown value |
196 | }) |
197 | |
198 | addSuggest(textArea, (inputText, cb) => { |
199 | const char = inputText[0] |
200 | const wordFragment = inputText.slice(1) |
201 | |
202 | if (char === '@') api.about.async.suggest(wordFragment, feedIdsInThread, cb) |
203 | if (char === '#') api.channel.async.suggest(wordFragment, cb) |
204 | if (char === ':') api.emoji.async.suggest(wordFragment, cb) |
205 | if (char === '&') api.meme.async.suggest(wordFragment, cb) |
206 | }, { cls: 'PatchSuggest' }) |
207 | |
208 | return composer |
209 | |
210 | // scoped |
211 | |
212 | function publish () { |
213 | publishBtn.disabled = true |
214 | |
215 | const channel = channelInput.value.startsWith('#') |
216 | ? channelInput.value.substr(1).trim() |
217 | : channelInput.value.trim() |
218 | const mentions = ssbMentions(textArea.value).map(mention => { |
219 | // merge markdown-detected mention with file info |
220 | var file = filesById[mention.link] |
221 | if (file) { |
222 | if (file.type) mention.type = file.type |
223 | if (file.size) mention.size = file.size |
224 | } |
225 | return mention |
226 | }) |
227 | |
228 | var content = extend(resolve(meta), { |
229 | text: textArea.value, |
230 | channel, |
231 | mentions |
232 | }) |
233 | |
234 | if (!channel) delete content.channel |
235 | if (!mentions.length) delete content.mentions |
236 | if (content.recps && content.recps.length === 0) delete content.recps |
237 | |
238 | try { |
239 | if (typeof prepublish === 'function') { |
240 | content = prepublish(content) |
241 | } |
242 | } catch (err) { |
243 | publishBtn.disabled = false |
244 | if (cb) cb(err) |
245 | else throw err |
246 | } |
247 | |
248 | return api.message.html.confirm(content, done) |
249 | |
250 | function done (err, msg) { |
251 | publishBtn.disabled = false |
252 | if (err) throw err |
253 | else if (msg) { |
254 | textArea.value = '' |
255 | api.drafts.sync.remove(draftLocation) |
256 | } |
257 | if (cb) cb(err, msg) |
258 | } |
259 | } |
260 | } |
261 | } |
262 |
Built with git-ssb-web