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