Files: 33543adec33f52f411c3d5f644850cc945f45abb / modules / compose.js
2892 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 | var content |
53 | try { |
54 | content = JSON.parse(ta.value) |
55 | } catch (err) { |
56 | meta.text = ta.value |
57 | meta.mentions = mentions(ta.value).concat(files) |
58 | try { |
59 | meta = prepublish(meta) |
60 | } catch (err) { |
61 | return alert(err.message) |
62 | } |
63 | return message_confirm(meta, done) |
64 | } |
65 | message_confirm(content, done) |
66 | |
67 | function done (err, msg) { |
68 | if(err) return alert(err.stack) |
69 | else ta.value = '' |
70 | |
71 | if (cb) cb(err, msg) |
72 | } |
73 | } |
74 | |
75 | |
76 | var composer = |
77 | h('div.compose', h('div.column', ta, |
78 | accessories = h('div.row.compose__controls', |
79 | //hidden until you focus the textarea |
80 | {style: {display: 'none'}}, |
81 | file_input(function (file) { |
82 | files.push(file) |
83 | |
84 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
85 | ta.value += embed + '['+file.name+']('+file.link+')' |
86 | console.log('added:', file) |
87 | }), |
88 | h('button', 'Publish', {onclick: publish})) |
89 | ) |
90 | ) |
91 | |
92 | suggest(ta, function (word, cb) { |
93 | cont.para(exports.suggest.map(function (fn) { |
94 | return function (cb) { fn(word, cb) } |
95 | })) |
96 | (function (err, results) { |
97 | if(err) console.error(err) |
98 | results = results.reduce(function (ary, item) { |
99 | return ary.concat(item) |
100 | }, []).sort(function (a, b) { |
101 | return b.rank - a.rank |
102 | }).filter(Boolean) |
103 | |
104 | cb(null, results) |
105 | }) |
106 | }, {}) |
107 | |
108 | return composer |
109 | |
110 | } |
111 | |
112 |
Built with git-ssb-web