git ssb

2+

mixmix / ticktack



Tree: 909b4b2756a7aaa99f6114938b240b53beb129c8

Files: 909b4b2756a7aaa99f6114938b240b53beb129c8 / message / html / compose.js

5743 bytesRaw
1const nest = require('depnest')
2const { h, when, send, resolve, Value, computed, map } = require('mutant')
3const assign = require('lodash/assign')
4const isEmpty = require('lodash/isEmpty')
5const ssbMentions = require('ssb-mentions')
6const addSuggest = require('suggest-box')
7
8exports.gives = nest('message.html.compose')
9
10exports.needs = nest({
11 'about.async.suggest': 'first',
12 'blob.html.input': 'first',
13 'channel.async.suggest': 'first',
14 'emoji.async.suggest': 'first',
15 'emoji.sync.names': 'first',
16 'emoji.sync.url': 'first',
17 'message.async.publish': 'first',
18 'message.html.markdown': 'first',
19 // 'message.html.confirm': 'first'
20 'translations.sync.strings': 'first'
21})
22
23exports.create = function (api) {
24 return nest('message.html.compose', compose)
25
26 function compose (options, cb) {
27 var {
28 meta, // required
29 feedIdsInThread = [],
30 placeholder,
31 shrink = true,
32 canAttach = true, canPreview = true,
33 prepublish
34 } = options
35
36 const strings = api.translations.sync.strings()
37 const getUserSuggestions = api.about.async.suggest()
38 const getChannelSuggestions = api.channel.async.suggest()
39 const getEmojiSuggestions = api.emoji.async.suggest()
40
41 placeholder = placeholder || strings.writeMessage
42
43 var files = []
44 var filesById = {}
45 var textAreaFocused = Value(false)
46 var focused = textAreaFocused
47 var hasContent = Value(false)
48
49 var blurTimeout = null
50
51 var expanded = computed([shrink, focused, hasContent], (shrink, focused, hasContent) => {
52 if (!shrink || hasContent) return true
53
54 return focused
55 })
56
57 var textRaw = meta.text || Value('')
58 var textArea = h('textarea', {
59 value: computed(textRaw, t => t),
60 'ev-input': () => textRaw.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 textRaw(text => hasContent.set(!!text))
69
70 textArea.publish = publish // TODO: fix - clunky api for the keyboard shortcut to target
71
72 var fileInput
73 if (!meta.recps) {
74 fileInput = api.blob.html.input(file => {
75 files.push(file)
76 filesById[file.link] = file
77
78 var imgPrefix = file.type.match(/^image/) ? '!' : ''
79 var spacer = imgPrefix ? '\n' : ' '
80 var insertLink = spacer + imgPrefix + '[' + file.name + ']' + '(' + file.link + ')' + spacer
81
82 var pos = textArea.selectionStart
83 var newText = textRaw().slice(0, pos) + insertLink + textRaw().slice(pos)
84 textArea.value = newText
85 textRaw.set(newText)
86
87 console.log('added:', file)
88 })
89
90 fileInput.onclick = () => hasContent.set(true)
91 }
92 // if fileInput is null, send button moves to the left side
93 // and we don't want that.
94 else { fileInput = h('input', { style: {visibility: 'hidden'} }) }
95
96 function PreviewSetup (strings) {
97 var showPreview = Value(false)
98 var previewBtn = h('Button',
99 {
100 className: when(showPreview, '-strong', '-subtle'),
101 'ev-click': () => showPreview.set(!showPreview())
102 },
103 when(showPreview, strings.blogNew.actions.edit, strings.blogNew.actions.preview)
104 )
105 return { previewBtn, showPreview }
106 }
107 var { previewBtn, showPreview } = PreviewSetup(strings)
108 var preview = computed(textRaw, text => api.message.html.markdown(text))
109
110 var publishBtn = h('Button -primary', { 'ev-click': publish }, strings.sendMessage)
111
112 var actions = h('section.actions', [
113 canAttach ? fileInput : '',
114 canPreview ? previewBtn : '',
115 publishBtn
116 ])
117
118 var composer = h('Compose', { classList: when(expanded, '-expanded', '-contracted') }, [
119 when(showPreview, preview, textArea),
120 actions
121 ])
122
123 addSuggest(textArea, (inputText, cb) => {
124 const char = inputText[0]
125 const wordFragment = inputText.slice(1)
126
127 if (char === '@') cb(null, getUserSuggestions(wordFragment, feedIdsInThread))
128 if (char === '#') cb(null, getChannelSuggestions(wordFragment))
129 if (char === ':') cb(null, getEmojiSuggestions(wordFragment))
130 }, {cls: 'PatchSuggest'})
131
132 return composer
133
134 // scoped
135
136 function publish () {
137 if (publishBtn.disabled) return
138
139 const text = resolve(textRaw)
140 if (isEmpty(text)) return
141
142 publishBtn.disabled = true
143
144 const mentions = ssbMentions(text).map(mention => {
145 // merge markdown-detected mention with file info
146 var file = filesById[mention.link]
147 if (file) {
148 if (file.type) mention.type = file.type
149 if (file.size) mention.size = file.size
150 }
151 return mention
152 })
153
154 var content = assign({}, resolve(meta), {
155 text,
156 mentions
157 })
158 for (var k in content) { content[k] = resolve(content[k]) }
159
160 if (!content.channel) delete content.channel
161 if (!mentions.length) delete content.mentions
162 if (content.recps && content.recps.length === 0) delete content.recps
163
164 if (typeof prepublish === 'function') {
165 prepublish(content, function (err, content) {
166 if (err) handleErr(err)
167 else api.message.async.publish(content, done)
168 })
169 } else { api.message.async.publish(content, done) }
170
171 function done (err, msg) {
172 publishBtn.disabled = false
173 if (err) handleErr(err)
174 else if (msg) {
175 textRaw.set('')
176 textArea.value = ''
177 }
178 if (cb) cb(err, msg)
179 }
180
181 function handleErr (err) {
182 publishBtn.disabled = false
183 if (cb) cb(err)
184 else throw err
185 }
186 }
187 }
188}
189

Built with git-ssb-web