Files: b0d0023156505d649f8c12faf827dd3d345a64d1 / modules / message / html / compose.js
6284 bytesRaw
1 | var h = require('mutant/h') |
2 | var when = require('mutant/when') |
3 | var send = require('mutant/send') |
4 | var resolve = require('mutant/resolve') |
5 | var Value = require('mutant/value') |
6 | var computed = require('mutant/computed') |
7 | var nest = require('depnest') |
8 | var mentions = require('ssb-mentions') |
9 | var extend = require('xtend') |
10 | var addSuggest = require('suggest-box') |
11 | var emoji = require('emojilib') |
12 | |
13 | exports.needs = nest({ |
14 | 'blob.html.input': 'first', |
15 | 'profile.async.suggest': 'first', |
16 | 'channel.async.suggest': 'first', |
17 | 'message.async.publish': 'first', |
18 | 'emoji.sync.names': 'first', |
19 | 'emoji.sync.url': 'first', |
20 | 'intl.sync.i18n': 'first' |
21 | }) |
22 | |
23 | exports.gives = nest('message.html.compose') |
24 | |
25 | exports.create = function (api) { |
26 | const i18n = api.intl.sync.i18n |
27 | return nest('message.html.compose', function ({shrink = true, isPrivate, participants, meta, hooks, prepublish, placeholder = 'Write a message'}, cb) { |
28 | var files = [] |
29 | var filesById = {} |
30 | var focused = Value(false) |
31 | var hasContent = Value(false) |
32 | var publishing = Value(false) |
33 | var getProfileSuggestions = api.profile.async.suggest() |
34 | var getChannelSuggestions = api.channel.async.suggest() |
35 | |
36 | var blurTimeout = null |
37 | |
38 | var expanded = computed([shrink, focused, hasContent], (shrink, focused, hasContent) => { |
39 | if (!shrink || hasContent) { |
40 | return true |
41 | } else { |
42 | return focused |
43 | } |
44 | }) |
45 | |
46 | var textArea = h('textarea', { |
47 | 'ev-input': function () { |
48 | hasContent.set(!!textArea.value) |
49 | }, |
50 | 'ev-blur': () => { |
51 | clearTimeout(blurTimeout) |
52 | blurTimeout = setTimeout(() => focused.set(false), 200) |
53 | }, |
54 | 'ev-focus': send(focused.set, true), |
55 | disabled: publishing, |
56 | placeholder |
57 | }) |
58 | |
59 | var warningMessage = Value(null) |
60 | var warning = h('section.warning', |
61 | { className: when(warningMessage, '-open', '-closed') }, |
62 | [ |
63 | h('div.warning', warningMessage), |
64 | h('div.close', { 'ev-click': () => warningMessage.set(null) }, 'x') |
65 | ] |
66 | ) |
67 | var fileInput = api.blob.html.input(file => { |
68 | const megabytes = file.size / 1024 / 1024 |
69 | if (megabytes >= 5) { |
70 | const rounded = Math.floor(megabytes * 100) / 100 |
71 | warningMessage.set([ |
72 | h('i.fa.fa-exclamation-triangle'), |
73 | h('strong', file.name), |
74 | ` is ${rounded}MB - the current limit is 5MB` |
75 | ]) |
76 | return |
77 | } |
78 | |
79 | files.push(file) |
80 | filesById[file.link] = file |
81 | |
82 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
83 | var pos = textArea.selectionStart |
84 | var before = textArea.value.slice(0, pos) |
85 | var after = textArea.value.slice(pos) |
86 | |
87 | var spacer = embed ? '\n' : ' ' |
88 | if (before && !before.endsWith(spacer)) before += spacer |
89 | if (!after.startsWith(spacer)) after = spacer + after |
90 | |
91 | textArea.value = `${before}${embed}[${file.name}](${file.link})${after}` |
92 | console.log('added:', file) |
93 | }, { |
94 | private: isPrivate |
95 | }) |
96 | |
97 | fileInput.onclick = function () { |
98 | hasContent.set(true) |
99 | } |
100 | |
101 | var publishBtn = h('button', { |
102 | 'ev-click': publish, |
103 | classList: [ |
104 | when(isPrivate, '-private') |
105 | ], |
106 | disabled: publishing |
107 | }, when(publishing, |
108 | i18n('Publishing...'), |
109 | when(isPrivate, i18n('Publish Privately'), i18n('Publish')) |
110 | )) |
111 | |
112 | var actions = h('section.actions', [ |
113 | fileInput, |
114 | publishBtn |
115 | ]) |
116 | |
117 | var composer = h('Compose', { |
118 | hooks, |
119 | classList: [ |
120 | when(expanded, '-expanded', '-contracted') |
121 | ] |
122 | }, [ |
123 | textArea, |
124 | warning, |
125 | actions |
126 | ]) |
127 | |
128 | composer.focus = function () { |
129 | textArea.focus() |
130 | } |
131 | |
132 | composer.setText = function (value) { |
133 | textArea.value = value |
134 | hasContent.set(!!textArea.value) |
135 | } |
136 | |
137 | addSuggest(textArea, (inputText, cb) => { |
138 | if (inputText[0] === '@') { |
139 | cb(null, getProfileSuggestions(inputText.slice(1), resolve(participants))) |
140 | } else if (inputText[0] === '#') { |
141 | cb(null, getChannelSuggestions(inputText.slice(1))) |
142 | } else if (inputText[0] === ':') { |
143 | // suggest emojis |
144 | var word = inputText.slice(1) |
145 | if (word[word.length - 1] === ':') { |
146 | word = word.slice(0, -1) |
147 | } |
148 | cb(null, suggestEmoji(word).slice(0, 100).map(function (emoji) { |
149 | return { |
150 | image: api.emoji.sync.url(emoji), |
151 | title: emoji, |
152 | subtitle: emoji, |
153 | value: ':' + emoji + ':' |
154 | } |
155 | })) |
156 | } |
157 | }, {cls: 'SuggestBox'}) |
158 | |
159 | return composer |
160 | |
161 | // scoped |
162 | |
163 | function publish () { |
164 | publishing.set(true) |
165 | |
166 | var content = extend(resolve(meta), { |
167 | text: textArea.value, |
168 | mentions: mentions(textArea.value).map(mention => { |
169 | // merge markdown-detected mention with file info |
170 | var file = filesById[mention.link] |
171 | if (file) { |
172 | if (file.type) mention.type = file.type |
173 | if (file.size) mention.size = file.size |
174 | } |
175 | return mention |
176 | }) |
177 | }) |
178 | |
179 | try { |
180 | if (typeof prepublish === 'function') { |
181 | content = prepublish(content) |
182 | } |
183 | } catch (err) { |
184 | return done(err) |
185 | } |
186 | |
187 | return api.message.async.publish(content, done) |
188 | |
189 | function done (err, msg) { |
190 | publishing.set(false) |
191 | if (err) { |
192 | if (cb) cb(err) |
193 | else { |
194 | showDialog({ |
195 | type: 'error', |
196 | title: i18n('Error'), |
197 | buttons: [i18n('OK')], |
198 | message: i18n('An error occured while publishing your message.'), |
199 | detail: err.message |
200 | }) |
201 | } |
202 | } else { |
203 | if (msg) textArea.value = '' |
204 | if (cb) cb(null, msg) |
205 | } |
206 | } |
207 | } |
208 | }) |
209 | |
210 | function suggestEmoji (prefix) { |
211 | var availableEmoji = api.emoji.sync.names() |
212 | return emoji.ordered.filter(key => { |
213 | if (!availableEmoji.includes(key)) return false |
214 | return key.startsWith(prefix) || key.includes('_' + prefix) || emoji.lib[key].keywords.some(word => word.startsWith(prefix) || word.startsWith(':' + prefix)) |
215 | }) |
216 | } |
217 | } |
218 | |
219 | function showDialog (opts) { |
220 | var electron = require('electron') |
221 | electron.remote.dialog.showMessageBox(electron.remote.getCurrentWindow(), opts) |
222 | } |
223 |
Built with git-ssb-web