git ssb

10+

Matt McKegg / patchwork



Tree: 01d1fe807ecac5563dd29fe54cc93a07904823e2

Files: 01d1fe807ecac5563dd29fe54cc93a07904823e2 / modules / message / html / compose.js

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

Built with git-ssb-web