Files: 64fbc06fcc7bfe5024a9dbaa85e9f21a0133c20a / modules / message / html / compose.js
5549 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 | |
12 | exports.needs = nest({ |
13 | 'blob.html.input': 'first', |
14 | 'profile.async.suggest': 'first', |
15 | 'channel.async.suggest': 'first', |
16 | 'message.async.publish': 'first', |
17 | 'emoji.sync.names': 'first', |
18 | 'emoji.sync.url': 'first', |
19 | 'intl.sync.i18n': 'first', |
20 | }) |
21 | |
22 | exports.gives = nest('message.html.compose') |
23 | |
24 | exports.create = function (api) { |
25 | const i18n = api.intl.sync.i18n |
26 | return nest('message.html.compose', function ({shrink = true, isPrivate, meta, hooks, prepublish, placeholder = 'Write a message'}, cb) { |
27 | var files = [] |
28 | var filesById = {} |
29 | var focused = Value(false) |
30 | var hasContent = Value(false) |
31 | var publishing = Value(false) |
32 | var getProfileSuggestions = api.profile.async.suggest() |
33 | var getChannelSuggestions = api.channel.async.suggest() |
34 | |
35 | var blurTimeout = null |
36 | |
37 | var expanded = computed([shrink, focused, hasContent], (shrink, focused, hasContent) => { |
38 | if (!shrink || hasContent) { |
39 | return true |
40 | } else { |
41 | return focused |
42 | } |
43 | }) |
44 | |
45 | var textArea = h('textarea', { |
46 | 'ev-input': function () { |
47 | hasContent.set(!!textArea.value) |
48 | }, |
49 | 'ev-blur': () => { |
50 | clearTimeout(blurTimeout) |
51 | blurTimeout = setTimeout(() => focused.set(false), 200) |
52 | }, |
53 | 'ev-focus': send(focused.set, true), |
54 | disabled: publishing, |
55 | placeholder |
56 | }) |
57 | |
58 | var warningMessage = Value(null) |
59 | var warning = h('section.warning', |
60 | { className: when(warningMessage, '-open', '-closed') }, |
61 | [ |
62 | h('div.warning', warningMessage), |
63 | h('div.close', { 'ev-click': () => warningMessage.set(null) }, 'x') |
64 | ] |
65 | ) |
66 | var fileInput = api.blob.html.input(file => { |
67 | const megabytes = file.size / 1024 / 1024 |
68 | if (megabytes >= 5) { |
69 | const rounded = Math.floor(megabytes*100)/100 |
70 | warningMessage.set([ |
71 | h('i.fa.fa-exclamation-triangle'), |
72 | h('strong', file.name), |
73 | ` is ${rounded}MB - the current limit is 5MB` |
74 | ]) |
75 | return |
76 | } |
77 | |
78 | files.push(file) |
79 | filesById[file.link] = file |
80 | |
81 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
82 | var pos = textArea.selectionStart |
83 | var before = textArea.value.slice(0, pos) |
84 | var after = textArea.value.slice(pos) |
85 | |
86 | var spacer = embed ? '\n' : ' ' |
87 | if (before && !before.endsWith(spacer)) before += spacer |
88 | if (!after.startsWith(spacer)) after = spacer + after |
89 | |
90 | textArea.value = `${before}${embed}[${file.name}](${file.link})${after}` |
91 | console.log('added:', file) |
92 | }) |
93 | |
94 | fileInput.onclick = function () { |
95 | hasContent.set(true) |
96 | } |
97 | |
98 | var publishBtn = h('button', { |
99 | 'ev-click': publish, |
100 | classList: [ |
101 | when(isPrivate, '-private') |
102 | ], |
103 | disabled: publishing |
104 | }, when(publishing, |
105 | i18n('Publishing...'), |
106 | when(isPrivate, i18n('Publish Privately'), i18n('Publish')) |
107 | )) |
108 | |
109 | var actions = h('section.actions', [ |
110 | fileInput, |
111 | publishBtn |
112 | ]) |
113 | |
114 | var composer = h('Compose', { |
115 | hooks, |
116 | classList: [ |
117 | when(expanded, '-expanded', '-contracted') |
118 | ] |
119 | }, [ |
120 | textArea, |
121 | warning, |
122 | actions |
123 | ]) |
124 | |
125 | composer.focus = function () { |
126 | textArea.focus() |
127 | } |
128 | |
129 | composer.setText = function (value) { |
130 | textArea.value = value |
131 | hasContent.set(!!textArea.value) |
132 | } |
133 | |
134 | addSuggest(textArea, (inputText, cb) => { |
135 | if (inputText[0] === '@') { |
136 | cb(null, getProfileSuggestions(inputText.slice(1))) |
137 | } else if (inputText[0] === '#') { |
138 | cb(null, getChannelSuggestions(inputText.slice(1))) |
139 | } else if (inputText[0] === ':') { |
140 | // suggest emojis |
141 | var word = inputText.slice(1) |
142 | if (word[word.length - 1] === ':') { |
143 | word = word.slice(0, -1) |
144 | } |
145 | // TODO: when no emoji typed, list some default ones |
146 | cb(null, api.emoji.sync.names().filter(function (name) { |
147 | return name.slice(0, word.length) === word |
148 | }).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 | publishing.set(false) |
185 | if (cb) cb(err) |
186 | else throw err |
187 | } |
188 | |
189 | return api.message.async.publish(content, done) |
190 | |
191 | function done (err, msg) { |
192 | publishing.set(false) |
193 | if (err) throw err |
194 | else if (msg) textArea.value = '' |
195 | if (cb) cb(err, msg) |
196 | } |
197 | } |
198 | }) |
199 | } |
200 |
Built with git-ssb-web