git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: cabc473e5efae9784e813e91373c35c0b8c5635f

Files: cabc473e5efae9784e813e91373c35c0b8c5635f / modules / message / html / compose.js

5211 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, meta, 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 disabled: publishing
101 }, when(publishing, i18n('Publishing...'), i18n('Publish')))
102
103 var actions = h('section.actions', [
104 fileInput,
105 publishBtn
106 ])
107
108 var composer = h('Compose', {
109 classList: [
110 when(expanded, '-expanded', '-contracted')
111 ]
112 }, [
113 textArea,
114 warning,
115 actions
116 ])
117
118 addSuggest(textArea, (inputText, cb) => {
119 if (inputText[0] === '@') {
120 cb(null, getProfileSuggestions(inputText.slice(1)))
121 } else if (inputText[0] === '#') {
122 cb(null, getChannelSuggestions(inputText.slice(1)))
123 } else if (inputText[0] === ':') {
124 // suggest emojis
125 var word = inputText.slice(1)
126 if (word[word.length - 1] === ':') {
127 word = word.slice(0, -1)
128 }
129 // TODO: when no emoji typed, list some default ones
130 cb(null, api.emoji.sync.names().filter(function (name) {
131 return name.slice(0, word.length) === word
132 }).slice(0, 100).map(function (emoji) {
133 return {
134 image: api.emoji.sync.url(emoji),
135 title: emoji,
136 subtitle: emoji,
137 value: ':' + emoji + ':'
138 }
139 }))
140 }
141 }, {cls: 'SuggestBox'})
142
143 return composer
144
145 // scoped
146
147 function publish () {
148 publishing.set(true)
149
150 var content = extend(resolve(meta), {
151 text: textArea.value,
152 mentions: mentions(textArea.value).map(mention => {
153 // merge markdown-detected mention with file info
154 var file = filesById[mention.link]
155 if (file) {
156 if (file.type) mention.type = file.type
157 if (file.size) mention.size = file.size
158 }
159 return mention
160 })
161 })
162
163 try {
164 if (typeof prepublish === 'function') {
165 content = prepublish(content)
166 }
167 } catch (err) {
168 publishing.set(false)
169 if (cb) cb(err)
170 else throw err
171 }
172
173 return api.message.async.publish(content, done)
174
175 function done (err, msg) {
176 publishing.set(false)
177 if (err) throw err
178 else if (msg) textArea.value = ''
179 if (cb) cb(err, msg)
180 }
181 }
182 })
183}
184

Built with git-ssb-web