Files: 86a733778b0428dc270a65273d76c6bb187dad06 / modules / compose.js
2204 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 | exports.suggest = [] |
9 | exports.publish = [] |
10 | exports.message_content = [] |
11 | exports.message_confirm = [] |
12 | exports.file_input = [] |
13 | |
14 | //this decorator expects to be the first |
15 | |
16 | function id (e) { return e } |
17 | |
18 | exports.message_compose = function (meta, prepublish, sbot) { |
19 | if('function' !== typeof prepublish) |
20 | sbot = prepublish, prepublish = id |
21 | meta = meta || {} |
22 | if(!meta.type) throw new Error('message must have type') |
23 | var ta = h('textarea') |
24 | |
25 | var blur |
26 | ta.addEventListener('focus', function () { |
27 | clearTimeout(blur) |
28 | ta.style.height = '200px' |
29 | }) |
30 | ta.addEventListener('blur', function () { |
31 | //don't shrink right away, so there is time |
32 | //to click the publish button. |
33 | clearTimeout(blur) |
34 | blur = setTimeout(function () { |
35 | ta.style.height = '50px' |
36 | }, 200) |
37 | }) |
38 | |
39 | ta.addEventListener('keydown', function (ev) { |
40 | if(ev.keyCode === 13 && ev.ctrlKey) publish() |
41 | }) |
42 | |
43 | var files = [] |
44 | |
45 | function publish() { |
46 | meta.text = ta.value |
47 | meta.mentions = mentions(ta.value).concat(files) |
48 | try { |
49 | meta = prepublish(meta) |
50 | } catch (err) { |
51 | return alert(err.message) |
52 | } |
53 | u.firstPlug(exports.message_confirm, meta, sbot) |
54 | } |
55 | |
56 | |
57 | var composer = |
58 | h('div', h('div.column', ta, |
59 | h('div.row', |
60 | u.firstPlug(exports.file_input, function (file) { |
61 | files.push(file) |
62 | |
63 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
64 | ta.value += embed + '['+file.name+']('+file.link+')' |
65 | console.log('added:', file) |
66 | }), |
67 | h('button', 'publish', {onclick: publish})) |
68 | ) |
69 | ) |
70 | |
71 | suggest(ta, function (word, cb) { |
72 | cont.para(exports.suggest.map(function (fn) { |
73 | return function (cb) { fn(word, sbot, cb) } |
74 | })) |
75 | (function (err, results) { |
76 | if(err) console.error(err) |
77 | results = results.reduce(function (ary, item) { |
78 | return ary.concat(item) |
79 | }, []).sort(function (a, b) { |
80 | return b.rank - a.rank |
81 | }).filter(Boolean) |
82 | |
83 | cb(null, results) |
84 | }) |
85 | }, {}) |
86 | |
87 | return composer |
88 | |
89 | } |
90 | |
91 |
Built with git-ssb-web