Files: 9660479589cb3e6a2d8fe371854da4ce7a38522b / message / html / compose.js
5606 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, when, send, resolve, Value, computed, map } = require('mutant') |
3 | const assign = require('lodash/assign') |
4 | const ssbMentions = require('ssb-mentions') |
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 | 'blob.html.input': 'first', |
12 | 'channel.async.suggest': 'first', |
13 | 'emoji.async.suggest': 'first', |
14 | 'emoji.sync.names': 'first', |
15 | 'emoji.sync.url': 'first', |
16 | 'message.async.publish': 'first', |
17 | 'message.html.markdown': 'first', |
18 | // 'message.html.confirm': 'first' |
19 | 'translations.sync.strings': 'first' |
20 | }) |
21 | |
22 | exports.create = function (api) { |
23 | return nest('message.html.compose', compose) |
24 | |
25 | function compose (options, cb) { |
26 | var { |
27 | meta, // required |
28 | feedIdsInThread = [], |
29 | placeholder, |
30 | shrink = true, |
31 | canAttach = true, canPreview = true, |
32 | prepublish, |
33 | } = options |
34 | |
35 | const strings = api.translations.sync.strings() |
36 | const getUserSuggestions = api.about.async.suggest() |
37 | const getChannelSuggestions = api.channel.async.suggest() |
38 | const getEmojiSuggestions = api.emoji.async.suggest() |
39 | |
40 | placeholder = placeholder || strings.writeMessage |
41 | |
42 | var files = [] |
43 | var filesById = {} |
44 | var textAreaFocused = Value(false) |
45 | var focused = textAreaFocused |
46 | var hasContent = Value(false) |
47 | |
48 | var blurTimeout = null |
49 | |
50 | var expanded = computed([shrink, focused, hasContent], (shrink, focused, hasContent) => { |
51 | if (!shrink || hasContent) return true |
52 | |
53 | return focused |
54 | }) |
55 | |
56 | var textRaw = Value('') |
57 | var textArea = h('textarea', { |
58 | 'ev-input': () => textRaw.set(textArea.value), |
59 | 'ev-blur': () => { |
60 | clearTimeout(blurTimeout) |
61 | blurTimeout = setTimeout(() => textAreaFocused.set(false), 200) |
62 | }, |
63 | 'ev-focus': send(textAreaFocused.set, true), |
64 | placeholder |
65 | }) |
66 | textRaw(text => hasContent.set(!!text)) |
67 | |
68 | textArea.publish = publish // TODO: fix - clunky api for the keyboard shortcut to target |
69 | |
70 | var fileInput |
71 | if(!meta.recps) { |
72 | fileInput = api.blob.html.input(file => { |
73 | files.push(file) |
74 | filesById[file.link] = file |
75 | |
76 | var imgPrefix = file.type.match(/^image/) ? '!' : '' |
77 | var spacer = imgPrefix ? '\n' : ' ' |
78 | var insertLink = spacer + imgPrefix + '[' + file.name + ']' + '(' + file.link + ')' + spacer |
79 | |
80 | var pos = textArea.selectionStart |
81 | var newText = textRaw().slice(0, pos) + insertLink + textRaw().slice(pos) |
82 | textArea.value = newText |
83 | textRaw.set(newText) |
84 | |
85 | console.log('added:', file) |
86 | }) |
87 | |
88 | fileInput.onclick = () => hasContent.set(true) |
89 | } |
90 | // if fileInput is null, send button moves to the left side |
91 | // and we don't want that. |
92 | else |
93 | fileInput = h('input', { style: {visibility: 'hidden'} }) |
94 | |
95 | function PreviewSetup (strings) { |
96 | var showPreview = Value(false) |
97 | var previewBtn = h('Button', |
98 | { |
99 | className: when(showPreview, '-strong', '-subtle'), |
100 | 'ev-click': () => showPreview.set(!showPreview()) |
101 | }, |
102 | when(showPreview, strings.blogNew.actions.edit, strings.blogNew.actions.preview) |
103 | ) |
104 | return { previewBtn, showPreview } |
105 | } |
106 | var { previewBtn, showPreview } = PreviewSetup(strings) |
107 | var preview = computed(textRaw, text => api.message.html.markdown(text)) |
108 | |
109 | var publishBtn = h('Button -primary', { 'ev-click': publish }, strings.sendMessage) |
110 | |
111 | var actions = h('section.actions', [ |
112 | canAttach ? fileInput : '', |
113 | canPreview ? previewBtn : '', |
114 | publishBtn |
115 | ]) |
116 | |
117 | var composer = h('Compose', { |
118 | classList: when(expanded, '-expanded', '-contracted') |
119 | }, [ |
120 | when(showPreview, preview, textArea), |
121 | actions |
122 | ]) |
123 | |
124 | addSuggest(textArea, (inputText, cb) => { |
125 | const char = inputText[0] |
126 | const wordFragment = inputText.slice(1) |
127 | |
128 | if (char === '@') cb(null, getUserSuggestions(wordFragment, feedIdsInThread)) |
129 | if (char === '#') cb(null, getChannelSuggestions(wordFragment)) |
130 | if (char === ':') cb(null, getEmojiSuggestions(wordFragment)) |
131 | }, {cls: 'PatchSuggest'}) |
132 | |
133 | return composer |
134 | |
135 | // scoped |
136 | |
137 | function publish () { |
138 | publishBtn.disabled = true |
139 | const text = resolve(textRaw) |
140 | |
141 | const mentions = ssbMentions(text).map(mention => { |
142 | // merge markdown-detected mention with file info |
143 | var file = filesById[mention.link] |
144 | if (file) { |
145 | if (file.type) mention.type = file.type |
146 | if (file.size) mention.size = file.size |
147 | } |
148 | return mention |
149 | }) |
150 | |
151 | var content = assign({}, resolve(meta), { |
152 | text, |
153 | mentions |
154 | }) |
155 | for(var k in content) |
156 | content[k] = resolve(content[k]) |
157 | |
158 | if (!content.channel) delete content.channel |
159 | if (!mentions.length) delete content.mentions |
160 | if (content.recps && content.recps.length === 0) delete content.recps |
161 | |
162 | if (typeof prepublish === 'function') { |
163 | prepublish(content, function (err, content) { |
164 | if(err) handleErr(err) |
165 | else api.message.async.publish(content, done) |
166 | }) |
167 | } |
168 | else |
169 | api.message.async.publish(content, done) |
170 | |
171 | function done (err, msg) { |
172 | publishBtn.disabled = false |
173 | if (err) handleErr(err) |
174 | else if (msg) { |
175 | textRaw.set('') |
176 | textArea.value = '' |
177 | } |
178 | if (cb) cb(err, msg) |
179 | } |
180 | |
181 | function handleErr (err) { |
182 | publishBtn.disabled = false |
183 | if (cb) cb(err) |
184 | else throw err |
185 | } |
186 | } |
187 | } |
188 | } |
189 | |
190 | |
191 |
Built with git-ssb-web