git ssb

2+

mixmix / ticktack



Tree: e13726c12dd37e510f6ffbc69ef6e0bae607bc89

Files: e13726c12dd37e510f6ffbc69ef6e0bae607bc89 / message / html / compose.js

6094 bytesRaw
1const nest = require('depnest')
2const { h, when, send, resolve, Value, computed } = require('mutant')
3const assign = require('lodash/assign')
4const ssbMentions = require('ssb-mentions')
5const addSuggest = require('suggest-box')
6
7exports.gives = nest('message.html.compose')
8
9exports.needs = nest({
10 'about.async.suggest': 'first',
11 'blob.html.input': 'first',
12 // 'channel.async.suggest': 'first',
13 'emoji.sync.names': 'first',
14 'emoji.sync.url': 'first',
15 'message.async.publish': 'first',
16 // 'message.html.confirm': 'first'
17 'translations.sync.strings': 'first'
18})
19
20exports.create = function (api) {
21 return nest('message.html.compose', compose)
22
23 function compose ({ shrink = true, meta, prepublish, placeholder = 'Write a message' }, cb) {
24 const strings = api.translations.sync.strings()
25
26 var files = []
27 var filesById = {}
28 var channelInputFocused = Value(false)
29 var textAreaFocused = Value(false)
30 var focused = computed([channelInputFocused, textAreaFocused], (a, b) => a || b)
31 var hasContent = Value(false)
32 var getProfileSuggestions = api.about.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) return true
39
40 return focused
41 })
42
43 // var channelInput = h('input.channel', {
44 // 'ev-input': () => hasContent.set(!!channelInput.value),
45 // 'ev-keyup': ev => {
46 // ev.target.value = ev.target.value.replace(/^#*([\w@%&])/, '#$1')
47 // },
48 // 'ev-blur': () => {
49 // clearTimeout(blurTimeout)
50 // blurTimeout = setTimeout(() => channelInputFocused.set(false), 200)
51 // },
52 // 'ev-focus': send(channelInputFocused.set, true),
53 // placeholder: '#channel (optional)',
54 // value: computed(meta.channel, ch => ch ? '#' + ch : null),
55 // disabled: when(meta.channel, true),
56 // title: when(meta.channel, 'Reply is in same channel as original message')
57 // })
58
59 var textArea = h('textarea', {
60 'ev-input': () => hasContent.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 textArea.publish = publish // TODO: fix - clunky api for the keyboard shortcut to target
69
70 var fileInput = api.blob.html.input(file => {
71 files.push(file)
72 filesById[file.link] = file
73
74 var embed = file.type.match(/^image/) ? '!' : ''
75 var spacer = embed ? '\n' : ' '
76 var insertLink = spacer + embed + '[' + file.name + ']' + '(' + file.link + ')' + spacer
77
78 var pos = textArea.selectionStart
79 textArea.value = textArea.value.slice(0, pos) + insertLink + textArea.value.slice(pos)
80
81 console.log('added:', file)
82 })
83
84 fileInput.onclick = () => hasContent.set(true)
85
86 var publishBtn = h('Button -primary', { 'ev-click': publish }, strings.sendMessage)
87
88 var actions = h('section.actions', [
89 fileInput,
90 publishBtn
91 ])
92
93 var composer = h('Compose', {
94 classList: when(expanded, '-expanded', '-contracted')
95 }, [
96 // channelInput,
97 textArea,
98 actions
99 ])
100
101 // addSuggest(channelInput, (inputText, cb) => {
102 // if (inputText[0] === '#') {
103 // cb(null, getChannelSuggestions(inputText.slice(1)))
104 // }
105 // }, {cls: 'SuggestBox'})
106 // channelInput.addEventListener('suggestselect', ev => {
107 // channelInput.value = ev.detail.id // HACK : this over-rides the markdown value
108 // })
109
110 addSuggest(textArea, (inputText, cb) => {
111 if (inputText[0] === '@') {
112 cb(null, getProfileSuggestions(inputText.slice(1)))
113 // } else if (inputText[0] === '#') {
114 // cb(null, getChannelSuggestions(inputText.slice(1)))
115 } else if (inputText[0] === ':') {
116 // suggest emojis
117 var word = inputText.slice(1)
118 if (word[word.length - 1] === ':') {
119 word = word.slice(0, -1)
120 }
121 // TODO: when no emoji typed, list some default ones
122 cb(null, api.emoji.sync.names().filter(function (name) {
123 return name.slice(0, word.length) === word
124 }).slice(0, 100).map(function (emoji) {
125 return {
126 image: api.emoji.sync.url(emoji),
127 title: emoji,
128 subtitle: emoji,
129 value: ':' + emoji + ':'
130 }
131 }))
132 }
133 }, {cls: 'SuggestBox'})
134
135 return composer
136
137 // scoped
138
139 function publish () {
140 publishBtn.disabled = true
141
142 // const channel = channelInput.value.startsWith('#')
143 // ? channelInput.value.substr(1).trim()
144 // : channelInput.value.trim()
145 const mentions = ssbMentions(textArea.value).map(mention => {
146 // merge markdown-detected mention with file info
147 var file = filesById[mention.link]
148 if (file) {
149 if (file.type) mention.type = file.type
150 if (file.size) mention.size = file.size
151 }
152 return mention
153 })
154
155 var content = assign({}, resolve(meta), {
156 text: textArea.value,
157 // channel,
158 mentions
159 })
160
161 // if (!channel) delete content.channel
162 if (!content.channel) delete content.channel
163 if (!mentions.length) delete content.mentions
164 if (content.recps && content.recps.length === 0) delete content.recps
165
166 try {
167 if (typeof prepublish === 'function') {
168 content = prepublish(content)
169 }
170 } catch (err) {
171 publishBtn.disabled = false
172 handleErr(err)
173 }
174
175 return api.message.async.publish(content, done)
176 // return api.message.html.confirm(content, done)
177
178 function done (err, msg) {
179 publishBtn.disabled = false
180 if (err) handleErr(err)
181 else if (msg) textArea.value = ''
182 if (cb) cb(err, msg)
183 }
184
185 function handleErr (err) {
186 if (cb) cb(err)
187 else throw err
188 }
189 }
190 }
191}
192
193
194

Built with git-ssb-web