git ssb

2+

mixmix / ticktack



Tree: 26ec4bc385c250f6b9b1d0eb534ceaff0f876083

Files: 26ec4bc385c250f6b9b1d0eb534ceaff0f876083 / message / html / compose.js

5748 bytesRaw
1const nest = require('depnest')
2const { h, when, send, resolve, Value, computed } = 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 filesById = {},
34 prepublish
35 } = options
36
37 const strings = api.translations.sync.strings()
38 const getUserSuggestions = api.about.async.suggest()
39 const getChannelSuggestions = api.channel.async.suggest()
40 const getEmojiSuggestions = api.emoji.async.suggest()
41
42 placeholder = placeholder || strings.writeMessage
43
44 var textAreaFocused = Value(false)
45 var focused = textAreaFocused
46 var hasContent = Value(false)
47
48 var blurTimeout = null
49
50 var expanded = computed([shrink, focused, hasContent], (shrink, focused, hasContent) => {
51 if (!shrink || hasContent) return true
52
53 return focused
54 })
55
56 var textRaw = meta.text || Value('')
57 var textArea = h('textarea', {
58 value: computed(textRaw, t => t),
59 'ev-input': () => textRaw.set(textArea.value),
60 'ev-blur': () => {
61 clearTimeout(blurTimeout)
62 blurTimeout = setTimeout(() => textAreaFocused.set(false), 200)
63 },
64 'ev-focus': send(textAreaFocused.set, true),
65 placeholder
66 })
67 textRaw(text => hasContent.set(!!text))
68
69 textArea.publish = () => publish({ filesById }) // TODO: fix - clunky api for the keyboard shortcut to target
70
71 var fileInput
72 if (!meta.recps) {
73 fileInput = api.blob.html.input(file => {
74 filesById[file.link] = file
75
76 var imgPrefix = file.type.match(/^image/) ? '!' : ''
77 var spacer = imgPrefix ? '\n' : ' '
78 var insertLink = spacer + imgPrefix + '[' + file.name + ']' + '(' + file.link + ')' + spacer
79
80 var pos = textArea.selectionStart
81 var newText = textRaw().slice(0, pos) + insertLink + textRaw().slice(pos)
82 textArea.value = newText
83 textRaw.set(newText)
84
85 console.log('added:', file)
86 })
87
88 fileInput.onclick = () => hasContent.set(true)
89 }
90 // if fileInput is null, send button moves to the left side
91 // and we don't want that.
92 else { fileInput = h('input', { style: {visibility: 'hidden'} }) }
93
94 function PreviewSetup (strings) {
95 var showPreview = Value(false)
96 var previewBtn = h('Button',
97 {
98 className: when(showPreview, '-strong', '-subtle'),
99 'ev-click': () => showPreview.set(!showPreview())
100 },
101 when(showPreview, strings.blogNew.actions.edit, strings.blogNew.actions.preview)
102 )
103 return { previewBtn, showPreview }
104 }
105 var { previewBtn, showPreview } = PreviewSetup(strings)
106 var preview = computed(textRaw, text => api.message.html.markdown(text))
107
108 var publishBtn = h('Button -primary', { 'ev-click': () => publish({ filesById }) }, strings.sendMessage)
109
110 var actions = h('section.actions', [
111 canAttach ? fileInput : '',
112 canPreview ? previewBtn : '',
113 publishBtn
114 ])
115
116 var composer = h('Compose', { classList: when(expanded, '-expanded', '-contracted') }, [
117 when(showPreview, preview, textArea),
118 actions
119 ])
120
121 addSuggest(textArea, (inputText, cb) => {
122 const char = inputText[0]
123 const wordFragment = inputText.slice(1)
124
125 if (char === '@') cb(null, getUserSuggestions(wordFragment, feedIdsInThread))
126 if (char === '#') cb(null, getChannelSuggestions(wordFragment))
127 if (char === ':') cb(null, getEmojiSuggestions(wordFragment))
128 }, {cls: 'PatchSuggest'})
129
130 return composer
131
132 // scoped
133
134 function publish ({ filesById }) {
135 if (publishBtn.disabled) return
136
137 const text = resolve(textRaw)
138 if (isEmpty(text)) return
139
140 publishBtn.disabled = true
141
142 const mentions = ssbMentions(text).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 var content = assign({}, resolve(meta), {
153 text,
154 mentions
155 })
156 for (var k in content) { content[k] = resolve(content[k]) }
157
158 if (!content.channel) delete content.channel
159 if (!mentions.length) delete content.mentions
160 if (content.recps && content.recps.length === 0) delete content.recps
161
162 if (typeof prepublish === 'function') {
163 prepublish(content, function (err, content) {
164 if (err) handleErr(err)
165 else api.message.async.publish(content, done)
166 })
167 } else { api.message.async.publish(content, done) }
168
169 function done (err, msg) {
170 publishBtn.disabled = false
171 if (err) handleErr(err)
172 else if (msg) {
173 textRaw.set('')
174 textArea.value = ''
175 }
176 if (cb) cb(err, msg)
177 }
178
179 function handleErr (err) {
180 publishBtn.disabled = false
181 if (cb) cb(err)
182 else throw err
183 }
184 }
185 }
186}
187

Built with git-ssb-web