Files: e5ad753b8414d998113f9305e0826f4cf1035fe4 / modules / message / html / compose.js
5241 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 | var appRoot = require('app-root-path'); |
13 | var i18n = require(appRoot + '/lib/i18n').i18n |
14 | |
15 | exports.needs = nest({ |
16 | 'blob.html.input': 'first', |
17 | 'profile.async.suggest': 'first', |
18 | 'channel.async.suggest': 'first', |
19 | 'message.async.publish': 'first', |
20 | 'emoji.sync.names': 'first', |
21 | 'emoji.sync.url': 'first' |
22 | }) |
23 | |
24 | exports.gives = nest('message.html.compose') |
25 | |
26 | exports.create = function (api) { |
27 | return nest('message.html.compose', function ({shrink = true, meta, prepublish, placeholder = 'Write a message'}, cb) { |
28 | var files = [] |
29 | var filesById = {} |
30 | var focused = Value(false) |
31 | var hasContent = Value(false) |
32 | var publishing = Value(false) |
33 | var getProfileSuggestions = api.profile.async.suggest() |
34 | var getChannelSuggestions = api.channel.async.suggest() |
35 | |
36 | var blurTimeout = null |
37 | |
38 | var expanded = computed([shrink, focused, hasContent], (shrink, focused, hasContent) => { |
39 | if (!shrink || hasContent) { |
40 | return true |
41 | } else { |
42 | return focused |
43 | } |
44 | }) |
45 | |
46 | var textArea = h('textarea', { |
47 | 'ev-input': function () { |
48 | hasContent.set(!!textArea.value) |
49 | }, |
50 | 'ev-blur': () => { |
51 | clearTimeout(blurTimeout) |
52 | blurTimeout = setTimeout(() => focused.set(false), 200) |
53 | }, |
54 | 'ev-focus': send(focused.set, true), |
55 | disabled: publishing, |
56 | placeholder |
57 | }) |
58 | |
59 | var warningMessage = Value(null) |
60 | var warning = h('section.warning', |
61 | { className: when(warningMessage, '-open', '-closed') }, |
62 | [ |
63 | h('div.warning', warningMessage), |
64 | h('div.close', { 'ev-click': () => warningMessage.set(null) }, 'x') |
65 | ] |
66 | ) |
67 | var fileInput = api.blob.html.input(file => { |
68 | const megabytes = file.size / 1024 / 1024 |
69 | if (megabytes >= 5) { |
70 | const rounded = Math.floor(megabytes*100)/100 |
71 | warningMessage.set([ |
72 | h('i.fa.fa-exclamation-triangle'), |
73 | h('strong', file.name), |
74 | ` is ${rounded}MB - the current limit is 5MB` |
75 | ]) |
76 | return |
77 | } |
78 | |
79 | files.push(file) |
80 | filesById[file.link] = file |
81 | |
82 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
83 | var pos = textArea.selectionStart |
84 | var before = textArea.value.slice(0, pos) |
85 | var after = textArea.value.slice(pos) |
86 | |
87 | var spacer = embed ? '\n' : ' ' |
88 | if (before && !before.endsWith(spacer)) before += spacer |
89 | if (!after.startsWith(spacer)) after = spacer + after |
90 | |
91 | textArea.value = `${before}${embed}[${file.name}](${file.link})${after}` |
92 | console.log('added:', file) |
93 | }) |
94 | |
95 | fileInput.onclick = function () { |
96 | hasContent.set(true) |
97 | } |
98 | |
99 | var publishBtn = h('button', { |
100 | 'ev-click': publish, |
101 | disabled: publishing |
102 | }, when(publishing, i18n.__('Publishing...'), i18n.__('Publish'))) |
103 | |
104 | var actions = h('section.actions', [ |
105 | fileInput, |
106 | publishBtn |
107 | ]) |
108 | |
109 | var composer = h('Compose', { |
110 | classList: [ |
111 | when(expanded, '-expanded', '-contracted') |
112 | ] |
113 | }, [ |
114 | textArea, |
115 | warning, |
116 | actions |
117 | ]) |
118 | |
119 | addSuggest(textArea, (inputText, cb) => { |
120 | if (inputText[0] === '@') { |
121 | cb(null, getProfileSuggestions(inputText.slice(1))) |
122 | } else if (inputText[0] === '#') { |
123 | cb(null, getChannelSuggestions(inputText.slice(1))) |
124 | } else if (inputText[0] === ':') { |
125 | // suggest emojis |
126 | var word = inputText.slice(1) |
127 | if (word[word.length - 1] === ':') { |
128 | word = word.slice(0, -1) |
129 | } |
130 | // TODO: when no emoji typed, list some default ones |
131 | cb(null, api.emoji.sync.names().filter(function (name) { |
132 | return name.slice(0, word.length) === word |
133 | }).slice(0, 100).map(function (emoji) { |
134 | return { |
135 | image: api.emoji.sync.url(emoji), |
136 | title: emoji, |
137 | subtitle: emoji, |
138 | value: ':' + emoji + ':' |
139 | } |
140 | })) |
141 | } |
142 | }, {cls: 'SuggestBox'}) |
143 | |
144 | return composer |
145 | |
146 | // scoped |
147 | |
148 | function publish () { |
149 | publishing.set(true) |
150 | |
151 | var content = extend(resolve(meta), { |
152 | text: textArea.value, |
153 | mentions: mentions(textArea.value).map(mention => { |
154 | // merge markdown-detected mention with file info |
155 | var file = filesById[mention.link] |
156 | if (file) { |
157 | if (file.type) mention.type = file.type |
158 | if (file.size) mention.size = file.size |
159 | } |
160 | return mention |
161 | }) |
162 | }) |
163 | |
164 | try { |
165 | if (typeof prepublish === 'function') { |
166 | content = prepublish(content) |
167 | } |
168 | } catch (err) { |
169 | publishing.set(false) |
170 | if (cb) cb(err) |
171 | else throw err |
172 | } |
173 | |
174 | return api.message.async.publish(content, done) |
175 | |
176 | function done (err, msg) { |
177 | publishing.set(false) |
178 | if (err) throw err |
179 | else if (msg) textArea.value = '' |
180 | if (cb) cb(err, msg) |
181 | } |
182 | } |
183 | }) |
184 | } |
185 |
Built with git-ssb-web