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