git ssb

16+

Dominic / patchbay



Tree: 51cce4b91688bab9a65aaab93e4e95848d30f84b

Files: 51cce4b91688bab9a65aaab93e4e95848d30f84b / message / html / compose.js

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

Built with git-ssb-web