Files: 83ae3109eb162b5f5b5c962e4f2cff738ee0f75d / message / html / compose.js
6750 bytesRaw
1 | const { h, when, send, resolve, Value, 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 | |
7 | exports.gives = nest('message.html.compose') |
8 | |
9 | exports.needs = nest({ |
10 | 'about.async.suggest': 'first', |
11 | 'channel.async.suggest': 'first', |
12 | 'emoji.async.suggest': 'first', |
13 | 'blob.html.input': 'first', |
14 | 'message.html.confirm': 'first', |
15 | 'drafts.sync.get': 'first', |
16 | 'drafts.sync.set': 'first', |
17 | 'drafts.sync.remove': 'first' |
18 | }) |
19 | |
20 | exports.create = function (api) { |
21 | return nest({ 'message.html.compose': compose }) |
22 | |
23 | function compose (options, cb) { |
24 | const { |
25 | meta, |
26 | location, |
27 | feedIdsInThread = [], |
28 | prepublish, |
29 | placeholder = 'Write a message', |
30 | shrink = true |
31 | } = options |
32 | |
33 | if (typeof resolve(meta) !== 'object') throw new Error('Compose needs meta data about what sort of message composer you are making') |
34 | if (!location) throw new Error('Compose expects a unique location so it can save drafts of messages') |
35 | |
36 | var files = [] |
37 | var filesById = {} |
38 | var channelInputFocused = Value(false) |
39 | var textAreaFocused = Value(false) |
40 | var focused = computed([channelInputFocused, textAreaFocused], (a, b) => a || b) |
41 | var hasContent = Value(false) |
42 | |
43 | var getProfileSuggestions = api.about.async.suggest() |
44 | var getChannelSuggestions = api.channel.async.suggest() |
45 | var getEmojiSuggestions = api.emoji.async.suggest() |
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: when(meta.channel, true), |
68 | title: when(meta.channel, 'Reply is in same channel as original message') |
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 warningMessage = Value(null) |
98 | var warning = h('section.warning', |
99 | { className: when(warningMessage, '-open', '-closed') }, |
100 | [ |
101 | h('div.warning', warningMessage), |
102 | h('div.close', { 'ev-click': () => warningMessage.set(null) }, 'x') |
103 | ] |
104 | ) |
105 | var fileInput = api.blob.html.input(file => { |
106 | const megabytes = file.size / 1024 / 1024 |
107 | if (megabytes >= 5) { |
108 | const rounded = Math.floor(megabytes * 100) / 100 |
109 | warningMessage.set([ |
110 | h('i.fa.fa-exclamation-triangle'), |
111 | h('strong', file.name), |
112 | ` is ${rounded}MB - the current limit is 5MB` |
113 | ]) |
114 | return |
115 | } |
116 | |
117 | files.push(file) |
118 | filesById[file.link] = file |
119 | |
120 | const pos = textArea.selectionStart |
121 | const embed = file.type.match(/^image/) ? '!' : '' |
122 | const spacer = embed ? '\n' : ' ' |
123 | const insertLink = spacer + embed + '[' + file.name + ']' + '(' + file.link + ')' + spacer |
124 | |
125 | textArea.value = textArea.value.slice(0, pos) + insertLink + textArea.value.slice(pos) |
126 | |
127 | console.log('added:', file) |
128 | }) |
129 | |
130 | fileInput.onclick = () => hasContent.set(true) |
131 | |
132 | var publishBtn = h('button', { 'ev-click': publish }, 'Publish') |
133 | |
134 | var actions = h('section.actions', [ |
135 | fileInput, |
136 | publishBtn |
137 | ]) |
138 | |
139 | var composer = h('Compose', { |
140 | classList: when(expanded, '-expanded', '-contracted') |
141 | }, [ |
142 | channelInput, |
143 | textArea, |
144 | warning, |
145 | actions |
146 | ]) |
147 | |
148 | addSuggest(channelInput, (inputText, cb) => { |
149 | if (inputText[0] === '#') { |
150 | cb(null, getChannelSuggestions(inputText.slice(1))) |
151 | } |
152 | }, {cls: 'PatchSuggest'}) |
153 | channelInput.addEventListener('suggestselect', ev => { |
154 | channelInput.value = ev.detail.id // HACK : this over-rides the markdown value |
155 | }) |
156 | |
157 | addSuggest(textArea, (inputText, cb) => { |
158 | const char = inputText[0] |
159 | const wordFragment = inputText.slice(1) |
160 | |
161 | if (char === '@') cb(null, getProfileSuggestions(wordFragment, feedIdsInThread)) |
162 | if (char === '#') cb(null, getChannelSuggestions(wordFragment)) |
163 | if (char === ':') cb(null, getEmojiSuggestions(wordFragment)) |
164 | }, {cls: 'PatchSuggest'}) |
165 | |
166 | return composer |
167 | |
168 | // scoped |
169 | |
170 | function publish () { |
171 | publishBtn.disabled = true |
172 | |
173 | const channel = channelInput.value.startsWith('#') |
174 | ? channelInput.value.substr(1).trim() |
175 | : channelInput.value.trim() |
176 | const mentions = ssbMentions(textArea.value).map(mention => { |
177 | // merge markdown-detected mention with file info |
178 | var file = filesById[mention.link] |
179 | if (file) { |
180 | if (file.type) mention.type = file.type |
181 | if (file.size) mention.size = file.size |
182 | } |
183 | return mention |
184 | }) |
185 | |
186 | var content = extend(resolve(meta), { |
187 | text: textArea.value, |
188 | channel, |
189 | mentions |
190 | }) |
191 | |
192 | if (!channel) delete content.channel |
193 | if (!mentions.length) delete content.mentions |
194 | if (content.recps && content.recps.length === 0) delete content.recps |
195 | |
196 | try { |
197 | if (typeof prepublish === 'function') { |
198 | content = prepublish(content) |
199 | } |
200 | } catch (err) { |
201 | publishBtn.disabled = false |
202 | if (cb) cb(err) |
203 | else throw err |
204 | } |
205 | |
206 | return api.message.html.confirm(content, done) |
207 | |
208 | function done (err, msg) { |
209 | publishBtn.disabled = false |
210 | if (err) throw err |
211 | else if (msg) { |
212 | textArea.value = '' |
213 | api.drafts.sync.remove(draftLocation) |
214 | } |
215 | if (cb) cb(err, msg) |
216 | } |
217 | } |
218 | } |
219 | } |
220 |
Built with git-ssb-web