Files: 56281fc6758e5edb39a262b90900a7e5f0bed393 / modules / compose.js
3449 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', {placeholder: 'Write a message'}) |
27 | |
28 | var blur |
29 | ta.addEventListener('focus', function () { |
30 | clearTimeout(blur) |
31 | if(!ta.value) { |
32 | ta.style.height = '200px' |
33 | } |
34 | accessories.style.display = 'block' |
35 | }) |
36 | ta.addEventListener('blur', function () { |
37 | //don't shrink right away, so there is time |
38 | //to click the publish button. |
39 | clearTimeout(blur) |
40 | blur = setTimeout(function () { |
41 | if(ta.value) return |
42 | ta.style.height = '50px' |
43 | accessories.style.display = 'none' |
44 | }, 200) |
45 | }) |
46 | |
47 | ta.addEventListener('keydown', function (ev) { |
48 | if(ev.keyCode === 13 && ev.ctrlKey) publish() |
49 | }) |
50 | |
51 | var files = [] |
52 | var filesById = {} |
53 | |
54 | function publish() { |
55 | publishBtn.disabled = true |
56 | var content |
57 | try { |
58 | content = JSON.parse(ta.value) |
59 | } catch (err) { |
60 | meta.text = ta.value |
61 | meta.mentions = mentions(ta.value).map(function (mention) { |
62 | // merge markdown-detected mention with file info |
63 | var file = filesById[mention.link] |
64 | if (file) { |
65 | if (file.type) mention.type = file.type |
66 | if (file.size) mention.size = file.size |
67 | } |
68 | return mention |
69 | }) |
70 | try { |
71 | meta = prepublish(meta) |
72 | } catch (err) { |
73 | publishBtn.disabled = false |
74 | if (cb) cb(err) |
75 | else alert(err.message) |
76 | } |
77 | return message_confirm(meta, done) |
78 | } |
79 | message_confirm(content, done) |
80 | |
81 | function done (err, msg) { |
82 | publishBtn.disabled = false |
83 | if(err) return alert(err.stack) |
84 | else if (msg) ta.value = '' |
85 | |
86 | if (cb) cb(err, msg) |
87 | } |
88 | } |
89 | |
90 | |
91 | var publishBtn = h('button', 'Publish', {onclick: publish}) |
92 | var composer = |
93 | h('div.compose', h('div.column', ta, |
94 | accessories = h('div.row.compose__controls', |
95 | //hidden until you focus the textarea |
96 | {style: {display: 'none'}}, |
97 | file_input(function (file) { |
98 | files.push(file) |
99 | filesById[file.link] = file |
100 | |
101 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
102 | ta.value += embed + '['+file.name+']('+file.link+')' |
103 | console.log('added:', file) |
104 | }), |
105 | publishBtn) |
106 | ) |
107 | ) |
108 | |
109 | suggest(ta, function (word, cb) { |
110 | cont.para(exports.suggest.map(function (fn) { |
111 | return function (cb) { fn(word, cb) } |
112 | })) |
113 | (function (err, results) { |
114 | if(err) console.error(err) |
115 | results = results.reduce(function (ary, item) { |
116 | return ary.concat(item) |
117 | }, []).sort(function (a, b) { |
118 | return b.rank - a.rank |
119 | }).filter(Boolean) |
120 | |
121 | cb(null, results) |
122 | }) |
123 | }, {}) |
124 | |
125 | return composer |
126 | |
127 | } |
128 | |
129 |
Built with git-ssb-web