Files: 4879a173de93a45b824f4093ab59eafc12602a47 / message / html / compose.js
6504 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 | }) |
18 | |
19 | exports.create = function (api) { |
20 | return nest({ 'message.html.compose': compose }) |
21 | |
22 | function compose ({ location, shrink = true, meta, prepublish, placeholder = 'Write a message' }, cb) { |
23 | var files = [] |
24 | var filesById = {} |
25 | var channelInputFocused = Value(false) |
26 | var textAreaFocused = Value(false) |
27 | var focused = computed([channelInputFocused, textAreaFocused], (a, b) => a || b) |
28 | var hasContent = Value(false) |
29 | |
30 | var getProfileSuggestions = api.about.async.suggest() |
31 | var getChannelSuggestions = api.channel.async.suggest() |
32 | var getEmojiSuggestions = api.emoji.async.suggest() |
33 | |
34 | var blurTimeout = null |
35 | |
36 | var expanded = computed([shrink, focused, hasContent], (shrink, focused, hasContent) => { |
37 | if (!shrink || hasContent) return true |
38 | |
39 | return focused |
40 | }) |
41 | |
42 | var channelInput = h('input.channel', { |
43 | 'ev-input': () => hasContent.set(!!channelInput.value), |
44 | 'ev-keyup': ev => { |
45 | ev.target.value = ev.target.value.replace(/^#*([\w@%&])/, '#$1') |
46 | }, |
47 | 'ev-blur': () => { |
48 | clearTimeout(blurTimeout) |
49 | blurTimeout = setTimeout(() => channelInputFocused.set(false), 200) |
50 | }, |
51 | 'ev-focus': send(channelInputFocused.set, true), |
52 | placeholder: '#channel (optional)', |
53 | value: computed(meta.channel, ch => ch ? '#' + ch : null), |
54 | disabled: when(meta.channel, true), |
55 | title: when(meta.channel, 'Reply is in same channel as original message') |
56 | }) |
57 | |
58 | var draftPerstTimeout = null |
59 | var draftLocation = resolve(meta).root |
60 | if (!draftLocation) { |
61 | draftLocation = location.page |
62 | if (draftLocation === '/channel') draftLocation = draftLocation + ':' + meta.channel |
63 | } |
64 | var textArea = h('textarea', { |
65 | 'ev-input': () => { |
66 | hasContent.set(!!textArea.value) |
67 | clearTimeout(draftPerstTimeout) |
68 | draftPerstTimeout = setTimeout(() => { |
69 | api.drafts.sync.set(draftLocation, textArea.value) |
70 | }, 200) |
71 | }, |
72 | 'ev-blur': () => { |
73 | clearTimeout(blurTimeout) |
74 | blurTimeout = setTimeout(() => textAreaFocused.set(false), 200) |
75 | }, |
76 | 'ev-focus': send(textAreaFocused.set, true), |
77 | placeholder |
78 | }) |
79 | textArea.publish = publish // TODO: fix - clunky api for the keyboard shortcut to target |
80 | |
81 | // load draft |
82 | let draft = api.drafts.sync.get(draftLocation) |
83 | if (typeof draft === 'string') { |
84 | textArea.value = draft |
85 | } |
86 | |
87 | var warningMessage = Value(null) |
88 | var warning = h('section.warning', |
89 | { className: when(warningMessage, '-open', '-closed') }, |
90 | [ |
91 | h('div.warning', warningMessage), |
92 | h('div.close', { 'ev-click': () => warningMessage.set(null) }, 'x') |
93 | ] |
94 | ) |
95 | var fileInput = api.blob.html.input(file => { |
96 | const megabytes = file.size / 1024 / 1024 |
97 | if (megabytes >= 5) { |
98 | const rounded = Math.floor(megabytes*100)/100 |
99 | warningMessage.set([ |
100 | h('i.fa.fa-exclamation-triangle'), |
101 | h('strong', file.name), |
102 | ` is ${rounded}MB - the current limit is 5MB` |
103 | ]) |
104 | return |
105 | } |
106 | |
107 | files.push(file) |
108 | filesById[file.link] = file |
109 | |
110 | const pos = textArea.selectionStart |
111 | const embed = file.type.match(/^image/) ? '!' : '' |
112 | const spacer = embed ? '\n' : ' ' |
113 | const insertLink = spacer + embed + '[' + file.name + ']' + '(' + file.link + ')' + spacer |
114 | |
115 | textArea.value = textArea.value.slice(0, pos) + insertLink + textArea.value.slice(pos) |
116 | |
117 | console.log('added:', file) |
118 | }) |
119 | |
120 | fileInput.onclick = () => hasContent.set(true) |
121 | |
122 | var publishBtn = h('button', { 'ev-click': publish }, 'Publish') |
123 | |
124 | var actions = h('section.actions', [ |
125 | fileInput, |
126 | publishBtn |
127 | ]) |
128 | |
129 | var composer = h('Compose', { |
130 | classList: when(expanded, '-expanded', '-contracted') |
131 | }, [ |
132 | channelInput, |
133 | textArea, |
134 | warning, |
135 | actions |
136 | ]) |
137 | |
138 | addSuggest(channelInput, (inputText, cb) => { |
139 | if (inputText[0] === '#') { |
140 | cb(null, getChannelSuggestions(inputText.slice(1))) |
141 | } |
142 | }, {cls: 'PatchSuggest'}) |
143 | channelInput.addEventListener('suggestselect', ev => { |
144 | channelInput.value = ev.detail.id // HACK : this over-rides the markdown value |
145 | }) |
146 | |
147 | addSuggest(textArea, (inputText, cb) => { |
148 | const char = inputText[0] |
149 | const wordFragment = inputText.slice(1) |
150 | |
151 | if (char === '@') cb(null, getProfileSuggestions(wordFragment)) |
152 | if (char === '#') cb(null, getChannelSuggestions(wordFragment)) |
153 | if (char === ':') cb(null, getEmojiSuggestions(wordFragment)) |
154 | }, {cls: 'PatchSuggest'}) |
155 | |
156 | return composer |
157 | |
158 | // scoped |
159 | |
160 | function publish () { |
161 | publishBtn.disabled = true |
162 | |
163 | const channel = channelInput.value.startsWith('#') |
164 | ? channelInput.value.substr(1).trim() |
165 | : channelInput.value.trim() |
166 | const mentions = ssbMentions(textArea.value).map(mention => { |
167 | // merge markdown-detected mention with file info |
168 | var file = filesById[mention.link] |
169 | if (file) { |
170 | if (file.type) mention.type = file.type |
171 | if (file.size) mention.size = file.size |
172 | } |
173 | return mention |
174 | }) |
175 | |
176 | var content = extend(resolve(meta), { |
177 | text: textArea.value, |
178 | channel, |
179 | mentions |
180 | }) |
181 | |
182 | if (!channel) delete content.channel |
183 | if (!mentions.length) delete content.mentions |
184 | if (content.recps && content.recps.length === 0) delete content.recps |
185 | |
186 | try { |
187 | if (typeof prepublish === 'function') { |
188 | content = prepublish(content) |
189 | } |
190 | } catch (err) { |
191 | publishBtn.disabled = false |
192 | if (cb) cb(err) |
193 | else throw err |
194 | } |
195 | |
196 | return api.message.html.confirm(content, done) |
197 | |
198 | function done (err, msg) { |
199 | publishBtn.disabled = false |
200 | if (err) throw err |
201 | else if (msg) { |
202 | textArea.value = '' |
203 | api.drafts.sync.set(draftLocation, '') |
204 | } |
205 | if (cb) cb(err, msg) |
206 | } |
207 | } |
208 | } |
209 | } |
210 |
Built with git-ssb-web