Files: 9b55017793b50a57c274b7b60ab0eb512dfdeb2f / modules / compose.js
2622 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.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 | ta.style.height = '50px' |
40 | accessories.style.display = 'none' |
41 | }, 200) |
42 | }) |
43 | |
44 | ta.addEventListener('keydown', function (ev) { |
45 | if(ev.keyCode === 13 && ev.ctrlKey) publish() |
46 | }) |
47 | |
48 | var files = [] |
49 | |
50 | function publish() { |
51 | meta.text = ta.value |
52 | meta.mentions = mentions(ta.value).concat(files) |
53 | try { |
54 | meta = prepublish(meta) |
55 | } catch (err) { |
56 | return alert(err.message) |
57 | } |
58 | message_confirm(meta, function (err, msg) { |
59 | ta.value = '' |
60 | cb(err, msg) |
61 | }) |
62 | } |
63 | |
64 | |
65 | var composer = |
66 | h('div', h('div.column', ta, |
67 | accessories = h('div.row', |
68 | //hidden until you focus the textarea |
69 | {style: {display: 'none'}}, |
70 | file_input(function (file) { |
71 | files.push(file) |
72 | |
73 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
74 | ta.value += embed + '['+file.name+']('+file.link+')' |
75 | console.log('added:', file) |
76 | }), |
77 | h('button', 'publish', {onclick: publish})) |
78 | ) |
79 | ) |
80 | |
81 | suggest(ta, function (word, cb) { |
82 | cont.para(exports.suggest.map(function (fn) { |
83 | return function (cb) { fn(word, cb) } |
84 | })) |
85 | (function (err, results) { |
86 | if(err) console.error(err) |
87 | results = results.reduce(function (ary, item) { |
88 | return ary.concat(item) |
89 | }, []).sort(function (a, b) { |
90 | return b.rank - a.rank |
91 | }).filter(Boolean) |
92 | |
93 | cb(null, results) |
94 | }) |
95 | }, {}) |
96 | |
97 | return composer |
98 | |
99 | } |
100 | |
101 | |
102 |
Built with git-ssb-web