Files: de41793084dc0680dd57842ec2fec0c18ac702e9 / message / html / compose.js
4055 bytesRaw
1 | const { h, when, send, resolve, Value, computed } = require('mutant') |
2 | const nest = require('depnest') |
3 | const mentions = require('ssb-mentions') |
4 | const extend = require('xtend') |
5 | const addSuggest = require('suggest-box') |
6 | |
7 | exports.needs = nest({ |
8 | 'about.async.suggest': 'first', |
9 | 'blob.html.input': 'first', |
10 | 'channel.async.suggest': 'first', |
11 | 'emoji.sync': { |
12 | names: 'first', |
13 | url: 'first' |
14 | }, |
15 | 'message.async.publish': 'first' |
16 | }) |
17 | |
18 | exports.gives = nest('message.html.compose') |
19 | |
20 | exports.create = function (api) { |
21 | return nest({ 'message.html.compose': compose }) |
22 | |
23 | function compose ({ shrink = true, meta, prepublish, placeholder = 'Write a message' }, cb) { |
24 | var files = [] |
25 | var filesById = {} |
26 | var focused = Value(false) |
27 | var hasContent = Value(false) |
28 | var getProfileSuggestions = api.about.async.suggest() |
29 | var getChannelSuggestions = api.channel.async.suggest() |
30 | |
31 | var blurTimeout = null |
32 | |
33 | var expanded = computed([shrink, focused, hasContent], (shrink, focused, hasContent) => { |
34 | if (!shrink || hasContent) return true |
35 | |
36 | return focused |
37 | }) |
38 | |
39 | var textArea = h('textarea', { |
40 | 'ev-input': () => hasContent.set(!!textArea.value), |
41 | 'ev-blur': () => { |
42 | clearTimeout(blurTimeout) |
43 | blurTimeout = setTimeout(() => focused.set(false), 200) |
44 | }, |
45 | 'ev-focus': send(focused.set, true), |
46 | placeholder |
47 | }) |
48 | |
49 | var fileInput = api.blob.html.input(file => { |
50 | files.push(file) |
51 | filesById[file.link] = file |
52 | |
53 | var embed = file.type.match(/^image/) ? '!' : '' |
54 | var spacer = embed ? '\n' : ' ' |
55 | var insertLink = spacer + embed + '[' + file.name + ']' + '(' + file.link + ')' + spacer |
56 | |
57 | var pos = textArea.selectionStart |
58 | textArea.value = textArea.value.slice(0, pos) + insertLink + textArea.value.slice(pos) |
59 | |
60 | console.log('added:', file) |
61 | }) |
62 | |
63 | fileInput.onclick = () => hasContent.set(true) |
64 | |
65 | var publishBtn = h('button', { 'ev-click': publish }, 'Publish') |
66 | |
67 | var actions = h('section.actions', [ |
68 | fileInput, |
69 | publishBtn |
70 | ]) |
71 | |
72 | var composer = h('Compose', { |
73 | classList: when(expanded, '-expanded', '-contracted') |
74 | }, [ |
75 | textArea, |
76 | actions |
77 | ]) |
78 | |
79 | addSuggest(textArea, (inputText, cb) => { |
80 | if (inputText[0] === '@') { |
81 | cb(null, getProfileSuggestions(inputText.slice(1))) |
82 | } else if (inputText[0] === '#') { |
83 | cb(null, getChannelSuggestions(inputText.slice(1))) |
84 | } else if (inputText[0] === ':') { |
85 | // suggest emojis |
86 | var word = inputText.slice(1) |
87 | if (word[word.length - 1] === ':') { |
88 | word = word.slice(0, -1) |
89 | } |
90 | // TODO: when no emoji typed, list some default ones |
91 | cb(null, api.emoji.sync.names().filter(function (name) { |
92 | return name.slice(0, word.length) === word |
93 | }).slice(0, 100).map(function (emoji) { |
94 | return { |
95 | image: api.emoji.sync.url(emoji), |
96 | title: emoji, |
97 | subtitle: emoji, |
98 | value: ':' + emoji + ':' |
99 | } |
100 | })) |
101 | } |
102 | }, {cls: 'SuggestBox'}) |
103 | |
104 | return composer |
105 | |
106 | // scoped |
107 | |
108 | function publish () { |
109 | publishBtn.disabled = true |
110 | |
111 | meta = extend(resolve(meta), { |
112 | text: textArea.value, |
113 | mentions: mentions(textArea.value).map(mention => { |
114 | // merge markdown-detected mention with file info |
115 | var file = filesById[mention.link] |
116 | if (file) { |
117 | if (file.type) mention.type = file.type |
118 | if (file.size) mention.size = file.size |
119 | } |
120 | return mention |
121 | }) |
122 | }) |
123 | |
124 | try { |
125 | if (typeof prepublish === 'function') { |
126 | meta = prepublish(meta) |
127 | } |
128 | } catch (err) { |
129 | publishBtn.disabled = false |
130 | if (cb) cb(err) |
131 | else throw err |
132 | } |
133 | |
134 | return api.message.async.publish(meta, done) |
135 | |
136 | function done (err, msg) { |
137 | publishBtn.disabled = false |
138 | if (err) throw err |
139 | else if (msg) textArea.value = '' |
140 | if (cb) cb(err, msg) |
141 | } |
142 | } |
143 | } |
144 | } |
145 | |
146 |
Built with git-ssb-web