git ssb

16+

Dominic / patchbay



Tree: dd6a5bf5f2c82fb52a866d1576c7f04a83e9678e

Files: dd6a5bf5f2c82fb52a866d1576c7f04a83e9678e / message / html / compose.js

7290 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})
20
21exports.create = function (api) {
22 return nest({ 'message.html.compose': compose })
23
24 function compose (options, cb) {
25 const {
26 meta,
27 location,
28 feedIdsInThread = [],
29 prepublish,
30 placeholder = 'Write a message',
31 shrink = true
32 } = options
33
34 if (typeof resolve(meta) !== 'object') throw new Error('Compose needs meta data about what sort of message composer you are making')
35 if (!location) throw new Error('Compose expects a unique location so it can save drafts of messages')
36
37 var files = []
38 var filesById = {}
39 var channelInputFocused = Value(false)
40 var textAreaFocused = Value(false)
41 var focused = computed([channelInputFocused, textAreaFocused], (a, b) => a || b)
42 var hasContent = Value(false)
43
44 var blurTimeout = null
45
46 var expanded = computed([shrink, focused, hasContent], (shrink, focused, hasContent) => {
47 if (!shrink || hasContent) return true
48
49 return focused
50 })
51
52 var channelInput = h('input.channel', {
53 'ev-input': () => hasContent.set(!!channelInput.value),
54 'ev-keyup': ev => {
55 ev.target.value = ev.target.value.replace(/^#*([\w@%&])/, '#$1')
56 },
57 'ev-blur': () => {
58 clearTimeout(blurTimeout)
59 blurTimeout = setTimeout(() => channelInputFocused.set(false), 200)
60 },
61 'ev-focus': send(channelInputFocused.set, true),
62 placeholder: '#channel (optional)',
63 value: computed(meta.channel, ch => ch ? '#' + ch : null),
64 disabled: when(meta.channel, true),
65 title: when(meta.channel, 'Reply is in same channel as original message')
66 })
67
68 var draftPerstTimeout = null
69 var draftLocation = location
70 var textArea = h('textarea', {
71 'ev-input': () => {
72 hasContent.set(!!textArea.value)
73 clearTimeout(draftPerstTimeout)
74 draftPerstTimeout = setTimeout(() => {
75 api.drafts.sync.set(draftLocation, textArea.value)
76 }, 200)
77 },
78 'ev-blur': () => {
79 clearTimeout(blurTimeout)
80 blurTimeout = setTimeout(() => textAreaFocused.set(false), 200)
81 },
82 'ev-focus': send(textAreaFocused.set, true),
83 placeholder
84 })
85 textArea.publish = publish // TODO: fix - clunky api for the keyboard shortcut to target
86
87 // load draft
88 let draft = api.drafts.sync.get(draftLocation)
89 if (typeof draft === 'string') {
90 textArea.value = draft
91 hasContent.set(true)
92 }
93
94 var isPrivate = location.page == 'private' ||
95 (location.key && !location.value) ||
96 (location.value && location.value.private)
97
98 var warningMessage = Value(null)
99 var warning = h('section.warning',
100 { className: when(warningMessage, '-open', '-closed') },
101 [
102 h('div.warning', warningMessage),
103 h('div.close', { 'ev-click': () => warningMessage.set(null) }, 'x')
104 ]
105 )
106 var fileInput = api.blob.html.input(file => {
107 const megabytes = file.size / 1024 / 1024
108 if (megabytes >= 5) {
109 const rounded = Math.floor(megabytes * 100) / 100
110 warningMessage.set([
111 h('i.fa.fa-exclamation-triangle'),
112 h('strong', file.name),
113 ` is ${rounded}MB - the current limit is 5MB`
114 ])
115 return
116 }
117
118 files.push(file)
119 filesById[file.link] = file
120
121 const pos = textArea.selectionStart
122 const embed = file.type.match(/^image/) ? '!' : ''
123 const spacer = embed ? '\n' : ' '
124 const insertLink = spacer + embed + '[' + file.name + ']' + '(' + file.link + ')' + spacer
125
126 textArea.value = textArea.value.slice(0, pos) + insertLink + textArea.value.slice(pos)
127
128 console.log('added:', file)
129 }, { private: isPrivate })
130
131 fileInput.onclick = () => hasContent.set(true)
132
133 var publishBtn = h('button', { 'ev-click': publish }, 'Publish')
134
135 var actions = h('section.actions', [
136 fileInput,
137 publishBtn
138 ])
139
140 var composer = h('Compose', {
141 classList: when(expanded, '-expanded', '-contracted')
142 }, [
143 channelInput,
144 textArea,
145 warning,
146 actions
147 ])
148
149 composer.addQuote = function (data) {
150 try {
151 if (typeof data.content.text === 'string') {
152 var text = data.content.text
153 textArea.value += '> ' + text.replace(/\r\n|\r|\n/g, '\n> ') + '\r\n\n'
154 hasContent.set(!!textArea.value)
155 }
156 } catch (err) {
157 // object not have text or content
158 }
159 }
160
161 if (location.action == 'quote') {
162 composer.addQuote(location.value)
163 }
164
165 addSuggest(channelInput, (inputText, cb) => {
166 if (inputText[0] === '#') {
167 cb(null, api.channel.async.suggest(inputText.slice(1)))
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