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