Files: a692047f69bbb665d6be280233fdf5a8131c4594 / modules_basic / compose.js
3580 bytesRaw
1 | |
2 | var h = require('hyperscript') |
3 | var u = require('../util') |
4 | var suggest = require('suggest-box') |
5 | var mentions = require('ssb-mentions') |
6 | var lightbox = require('hyperlightbox') |
7 | |
8 | var plugs = require('../plugs') |
9 | |
10 | var suggest_mentions= plugs.asyncConcat(exports.suggest_mentions = []) |
11 | var publish = plugs.first(exports.sbot_publish = []) |
12 | var message_content = plugs.first(exports.message_content = []) |
13 | var message_confirm = plugs.first(exports.message_confirm = []) |
14 | var file_input = plugs.first(exports.file_input = []) |
15 | |
16 | function id (e) { return e } |
17 | |
18 | /* |
19 | opts can take |
20 | |
21 | placeholder: string. placeholder text, defaults to "Write a message" |
22 | prepublish: function. called before publishing a message. |
23 | shrink: boolean. set to false, to make composer not shrink (or hide controls) when unfocused. |
24 | */ |
25 | |
26 | exports.message_compose = function (meta, opts, cb) { |
27 | if('function' === typeof cb) { |
28 | if('function' === typeof opts) |
29 | opts = {prepublish: opts} |
30 | } |
31 | |
32 | if(!opts) opts = {} |
33 | opts.prepublish = opts.prepublish || id |
34 | |
35 | var accessories |
36 | meta = meta || {} |
37 | if(!meta.type) throw new Error('message must have type') |
38 | var ta = h('textarea', { |
39 | placeholder: opts.placeholder || 'Write a message', |
40 | style: {height: opts.shrink === false ? '200px' : ''} |
41 | }) |
42 | |
43 | if(opts.shrink !== false) { |
44 | var blur |
45 | ta.addEventListener('focus', function () { |
46 | clearTimeout(blur) |
47 | if(!ta.value) { |
48 | ta.style.height = '200px' |
49 | } |
50 | accessories.style.display = 'block' |
51 | }) |
52 | ta.addEventListener('blur', function () { |
53 | //don't shrink right away, so there is time |
54 | //to click the publish button. |
55 | clearTimeout(blur) |
56 | blur = setTimeout(function () { |
57 | if(ta.value) return |
58 | ta.style.height = '50px' |
59 | accessories.style.display = 'none' |
60 | }, 200) |
61 | }) |
62 | } |
63 | |
64 | ta.addEventListener('keydown', function (ev) { |
65 | if(ev.keyCode === 13 && ev.ctrlKey) publish() |
66 | }) |
67 | |
68 | var files = [] |
69 | var filesById = {} |
70 | |
71 | function publish() { |
72 | publishBtn.disabled = true |
73 | var content |
74 | try { |
75 | content = JSON.parse(ta.value) |
76 | } catch (err) { |
77 | meta.text = ta.value |
78 | meta.mentions = mentions(ta.value).map(function (mention) { |
79 | // merge markdown-detected mention with file info |
80 | var file = filesById[mention.link] |
81 | if (file) { |
82 | if (file.type) mention.type = file.type |
83 | if (file.size) mention.size = file.size |
84 | } |
85 | return mention |
86 | }) |
87 | try { |
88 | meta = opts.prepublish(meta) |
89 | } catch (err) { |
90 | publishBtn.disabled = false |
91 | if (cb) cb(err) |
92 | else alert(err.message) |
93 | } |
94 | return message_confirm(meta, done) |
95 | } |
96 | message_confirm(content, done) |
97 | |
98 | function done (err, msg) { |
99 | publishBtn.disabled = false |
100 | if(err) return alert(err.stack) |
101 | else if (msg) ta.value = '' |
102 | |
103 | if (cb) cb(err, msg) |
104 | } |
105 | } |
106 | |
107 | |
108 | var publishBtn = h('button', 'Publish', {onclick: publish}) |
109 | var composer = |
110 | h('div.compose', h('div.column', ta, |
111 | accessories = h('div.row.compose__controls', |
112 | //hidden until you focus the textarea |
113 | {style: {display: opts.shrink === false ? '' : 'none'}}, |
114 | file_input(function (file) { |
115 | files.push(file) |
116 | filesById[file.link] = file |
117 | |
118 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
119 | ta.value += embed + '['+file.name+']('+file.link+')' |
120 | console.log('added:', file) |
121 | }), |
122 | publishBtn) |
123 | ) |
124 | ) |
125 | |
126 | suggest(ta, suggest_mentions, {}) |
127 | |
128 | return composer |
129 | |
130 | } |
131 | |
132 | |
133 |
Built with git-ssb-web