Files: 5ab138f415f014e52c4f2489a0807515aa58340e / message / html / compose.js
6109 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, when, send, resolve, Value, computed } = 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.sync.names': 'first', |
14 | 'emoji.sync.url': 'first', |
15 | 'message.async.publish': 'first', |
16 | // 'message.html.confirm': 'first' |
17 | 'translations.sync.strings': 'first' |
18 | }) |
19 | |
20 | exports.create = function (api) { |
21 | return nest('message.html.compose', compose) |
22 | |
23 | function compose ({ shrink = true, meta, prepublish, placeholder = 'Write a message' }, cb) { |
24 | const strings = api.translations.sync.strings() |
25 | |
26 | var files = [] |
27 | var filesById = {} |
28 | var channelInputFocused = Value(false) |
29 | var textAreaFocused = Value(false) |
30 | var focused = computed([channelInputFocused, textAreaFocused], (a, b) => a || b) |
31 | var hasContent = Value(false) |
32 | var getProfileSuggestions = api.about.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) return true |
39 | |
40 | return focused |
41 | }) |
42 | |
43 | // var channelInput = h('input.channel', { |
44 | // 'ev-input': () => hasContent.set(!!channelInput.value), |
45 | // 'ev-keyup': ev => { |
46 | // ev.target.value = ev.target.value.replace(/^#*([\w@%&])/, '#$1') |
47 | // }, |
48 | // 'ev-blur': () => { |
49 | // clearTimeout(blurTimeout) |
50 | // blurTimeout = setTimeout(() => channelInputFocused.set(false), 200) |
51 | // }, |
52 | // 'ev-focus': send(channelInputFocused.set, true), |
53 | // placeholder: '#channel (optional)', |
54 | // value: computed(meta.channel, ch => ch ? '#' + ch : null), |
55 | // disabled: when(meta.channel, true), |
56 | // title: when(meta.channel, 'Reply is in same channel as original message') |
57 | // }) |
58 | |
59 | var textArea = h('textarea', { |
60 | 'ev-input': () => hasContent.set(!!textArea.value), |
61 | 'ev-blur': () => { |
62 | clearTimeout(blurTimeout) |
63 | blurTimeout = setTimeout(() => textAreaFocused.set(false), 200) |
64 | }, |
65 | 'ev-focus': send(textAreaFocused.set, true), |
66 | placeholder |
67 | }) |
68 | textArea.publish = publish // TODO: fix - clunky api for the keyboard shortcut to target |
69 | |
70 | var fileInput = api.blob.html.input(file => { |
71 | files.push(file) |
72 | filesById[file.link] = file |
73 | |
74 | var embed = file.type.match(/^image/) ? '!' : '' |
75 | var spacer = embed ? '\n' : ' ' |
76 | var insertLink = spacer + embed + '[' + file.name + ']' + '(' + file.link + ')' + spacer |
77 | |
78 | var pos = textArea.selectionStart |
79 | textArea.value = textArea.value.slice(0, pos) + insertLink + textArea.value.slice(pos) |
80 | |
81 | console.log('added:', file) |
82 | }) |
83 | |
84 | fileInput.onclick = () => hasContent.set(true) |
85 | |
86 | var publishBtn = h('Button -primary', { 'ev-click': publish }, strings.sendMessage) |
87 | |
88 | var actions = h('section.actions', [ |
89 | fileInput, |
90 | publishBtn |
91 | ]) |
92 | |
93 | var composer = h('Compose', { |
94 | classList: when(expanded, '-expanded', '-contracted') |
95 | }, [ |
96 | // channelInput, |
97 | textArea, |
98 | actions |
99 | ]) |
100 | |
101 | // addSuggest(channelInput, (inputText, cb) => { |
102 | // if (inputText[0] === '#') { |
103 | // cb(null, getChannelSuggestions(inputText.slice(1))) |
104 | // } |
105 | // }, {cls: 'SuggestBox'}) |
106 | // channelInput.addEventListener('suggestselect', ev => { |
107 | // channelInput.value = ev.detail.id // HACK : this over-rides the markdown value |
108 | // }) |
109 | |
110 | addSuggest(textArea, (inputText, cb) => { |
111 | if (inputText[0] === '@') { |
112 | cb(null, getProfileSuggestions(inputText.slice(1))) |
113 | // } else if (inputText[0] === '#') { |
114 | // cb(null, getChannelSuggestions(inputText.slice(1))) |
115 | } else if (inputText[0] === ':') { |
116 | // suggest emojis |
117 | var word = inputText.slice(1) |
118 | if (word[word.length - 1] === ':') { |
119 | word = word.slice(0, -1) |
120 | } |
121 | // TODO: when no emoji typed, list some default ones |
122 | cb(null, api.emoji.sync.names().filter(function (name) { |
123 | return name.slice(0, word.length) === word |
124 | }).slice(0, 100).map(function (emoji) { |
125 | return { |
126 | image: api.emoji.sync.url(emoji), |
127 | title: emoji, |
128 | subtitle: emoji, |
129 | value: ':' + emoji + ':' |
130 | } |
131 | })) |
132 | } |
133 | }, {cls: 'SuggestBox'}) |
134 | |
135 | return composer |
136 | |
137 | // scoped |
138 | |
139 | function publish () { |
140 | publishBtn.disabled = true |
141 | |
142 | // const channel = channelInput.value.startsWith('#') |
143 | // ? channelInput.value.substr(1).trim() |
144 | // : channelInput.value.trim() |
145 | const mentions = ssbMentions(textArea.value).map(mention => { |
146 | // merge markdown-detected mention with file info |
147 | var file = filesById[mention.link] |
148 | if (file) { |
149 | if (file.type) mention.type = file.type |
150 | if (file.size) mention.size = file.size |
151 | } |
152 | return mention |
153 | }) |
154 | |
155 | var content = assign({}, resolve(meta), { |
156 | text: textArea.value, |
157 | // channel, |
158 | mentions |
159 | }) |
160 | |
161 | // if (!channel) delete content.channel |
162 | if (!content.channel) delete content.channel |
163 | if (!mentions.length) delete content.mentions |
164 | if (content.recps && content.recps.length === 0) delete content.recps |
165 | |
166 | try { |
167 | if (typeof prepublish === 'function') { |
168 | content = prepublish(content) |
169 | } |
170 | } catch (err) { |
171 | publishBtn.disabled = false |
172 | handleErr(err) |
173 | } |
174 | |
175 | debugger |
176 | |
177 | return api.message.async.publish(content, done) |
178 | // return api.message.html.confirm(content, done) |
179 | |
180 | function done (err, msg) { |
181 | publishBtn.disabled = false |
182 | if (err) handleErr(err) |
183 | else if (msg) textArea.value = '' |
184 | if (cb) cb(err, msg) |
185 | } |
186 | |
187 | function handleErr (err) { |
188 | if (cb) cb(err) |
189 | else throw err |
190 | } |
191 | } |
192 | } |
193 | } |
194 | |
195 |
Built with git-ssb-web