Files: ad76660a40cc006f0ccf04dd43afa04529f1b38c / modules / message / html / compose.js
2895 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 | |
11 | exports.needs = nest({ |
12 | 'blob.html.input': 'first', |
13 | 'message.async.publish': 'first' |
14 | }) |
15 | |
16 | exports.gives = nest('message.html.compose') |
17 | |
18 | exports.create = function (api) { |
19 | return nest('message.html.compose', function ({shrink = true, meta, prepublish, placeholder = 'Write a message'}, cb) { |
20 | var files = [] |
21 | var filesById = {} |
22 | var focused = Value(false) |
23 | var hasContent = Value(false) |
24 | var blurTimeout = null |
25 | |
26 | var expanded = computed([shrink, focused, hasContent], (shrink, focused, hasContent) => { |
27 | if (!shrink || hasContent) { |
28 | return true |
29 | } else { |
30 | return focused |
31 | } |
32 | }) |
33 | |
34 | var textArea = h('textarea', { |
35 | 'ev-input': function () { |
36 | hasContent.set(!!textArea.value) |
37 | }, |
38 | 'ev-blur': () => { |
39 | clearTimeout(blurTimeout) |
40 | blurTimeout = setTimeout(() => focused.set(false), 200) |
41 | }, |
42 | 'ev-focus': send(focused.set, true), |
43 | placeholder |
44 | }) |
45 | |
46 | var fileInput = api.blob.html.input(file => { |
47 | files.push(file) |
48 | filesById[file.link] = file |
49 | |
50 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
51 | |
52 | textArea.value += embed + `[${file.name}](${file.link})` |
53 | console.log('added:', file) |
54 | }) |
55 | |
56 | fileInput.onclick = function () { |
57 | hasContent.set(true) |
58 | } |
59 | |
60 | var publishBtn = h('button', { 'ev-click': publish }, 'Publish') |
61 | |
62 | var actions = h('section.actions', [ |
63 | fileInput, |
64 | publishBtn |
65 | ]) |
66 | |
67 | var composer = h('Compose', { |
68 | classList: [ |
69 | when(expanded, '-expanded', '-contracted') |
70 | ] |
71 | }, [ |
72 | textArea, |
73 | actions |
74 | ]) |
75 | |
76 | return composer |
77 | |
78 | // scoped |
79 | |
80 | function publish () { |
81 | publishBtn.disabled = true |
82 | |
83 | meta = extend(resolve(meta), { |
84 | text: textArea.value, |
85 | mentions: mentions(textArea.value).map(mention => { |
86 | // merge markdown-detected mention with file info |
87 | var file = filesById[mention.link] |
88 | if (file) { |
89 | if (file.type) mention.type = file.type |
90 | if (file.size) mention.size = file.size |
91 | } |
92 | return mention |
93 | }) |
94 | }) |
95 | |
96 | try { |
97 | if (typeof prepublish === 'function') { |
98 | meta = prepublish(meta) |
99 | } |
100 | } catch (err) { |
101 | publishBtn.disabled = false |
102 | if (cb) cb(err) |
103 | else throw err |
104 | } |
105 | |
106 | return api.message.async.publish(meta, done) |
107 | |
108 | function done (err, msg) { |
109 | publishBtn.disabled = false |
110 | if (err) throw err |
111 | else if (msg) textArea.value = '' |
112 | if (cb) cb(err, msg) |
113 | } |
114 | } |
115 | }) |
116 | } |
117 |
Built with git-ssb-web