Files: 5404eb9aac0d3f76707c6bf2fa5b57c55f46cb64 / modules / compose.js
3054 bytesRaw
1 | var h = require('hyperscript') |
2 | var u = require('../util') |
3 | var suggest = require('suggest-box') |
4 | var cont = require('cont') |
5 | var mentions = require('ssb-mentions') |
6 | var lightbox = require('hyperlightbox') |
7 | |
8 | var plugs = require('../plugs') |
9 | |
10 | //var suggest = plugs.map(exports.suggest = []) |
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 | exports.suggest = [] |
17 | |
18 | function id (e) { return e } |
19 | |
20 | exports.message_compose = function (meta, prepublish, cb) { |
21 | if('function' !== typeof prepublish) |
22 | sbot = prepublish, prepublish = id |
23 | var accessories |
24 | meta = meta || {} |
25 | if(!meta.type) throw new Error('message must have type') |
26 | var ta = h('textarea') |
27 | |
28 | var blur |
29 | ta.addEventListener('focus', function () { |
30 | clearTimeout(blur) |
31 | ta.style.height = '200px' |
32 | accessories.style.display = 'block' |
33 | }) |
34 | ta.addEventListener('blur', function () { |
35 | //don't shrink right away, so there is time |
36 | //to click the publish button. |
37 | clearTimeout(blur) |
38 | blur = setTimeout(function () { |
39 | if(ta.value) return |
40 | ta.style.height = '50px' |
41 | accessories.style.display = 'none' |
42 | }, 200) |
43 | }) |
44 | |
45 | ta.addEventListener('keydown', function (ev) { |
46 | if(ev.keyCode === 13 && ev.ctrlKey) publish() |
47 | }) |
48 | |
49 | var files = [] |
50 | |
51 | function publish() { |
52 | publishBtn.disabled = true |
53 | var content |
54 | try { |
55 | content = JSON.parse(ta.value) |
56 | } catch (err) { |
57 | meta.text = ta.value |
58 | meta.mentions = mentions(ta.value).concat(files) |
59 | try { |
60 | meta = prepublish(meta) |
61 | } catch (err) { |
62 | publishBtn.disabled = false |
63 | if (cb) cb(err) |
64 | else alert(err.message) |
65 | } |
66 | return message_confirm(meta, done) |
67 | } |
68 | message_confirm(content, done) |
69 | |
70 | function done (err, msg) { |
71 | publishBtn.disabled = false |
72 | if(err) return alert(err.stack) |
73 | else if (msg) ta.value = '' |
74 | |
75 | if (cb) cb(err, msg) |
76 | } |
77 | } |
78 | |
79 | |
80 | var publishBtn = h('button', 'Publish', {onclick: publish}) |
81 | var composer = |
82 | h('div.compose', h('div.column', ta, |
83 | accessories = h('div.row.compose__controls', |
84 | //hidden until you focus the textarea |
85 | {style: {display: 'none'}}, |
86 | file_input(function (file) { |
87 | files.push(file) |
88 | |
89 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
90 | ta.value += embed + '['+file.name+']('+file.link+')' |
91 | console.log('added:', file) |
92 | }), |
93 | publishBtn) |
94 | ) |
95 | ) |
96 | |
97 | suggest(ta, function (word, cb) { |
98 | cont.para(exports.suggest.map(function (fn) { |
99 | return function (cb) { fn(word, cb) } |
100 | })) |
101 | (function (err, results) { |
102 | if(err) console.error(err) |
103 | results = results.reduce(function (ary, item) { |
104 | return ary.concat(item) |
105 | }, []).sort(function (a, b) { |
106 | return b.rank - a.rank |
107 | }).filter(Boolean) |
108 | |
109 | cb(null, results) |
110 | }) |
111 | }, {}) |
112 | |
113 | return composer |
114 | |
115 | } |
116 | |
117 |
Built with git-ssb-web