git ssb

16+

Dominic / patchbay



Tree: f6372a0b47a15d936a760ae37971796e6d433f76

Files: f6372a0b47a15d936a760ae37971796e6d433f76 / modules / compose.js

2468 bytesRaw
1var h = require('hyperscript')
2var u = require('../util')
3var suggest = require('suggest-box')
4var cont = require('cont')
5var mentions = require('ssb-mentions')
6var lightbox = require('hyperlightbox')
7
8var plugs = require('../plugs')
9
10//var suggest = plugs.map(exports.suggest = [])
11var publish = plugs.first(exports.publish = [])
12var message_content = plugs.first(exports.message_content = [])
13var message_confirm = plugs.first(exports.message_confirm = [])
14var file_input = plugs.first(exports.file_input = [])
15
16exports.suggest = []
17
18//this decorator expects to be the first
19
20function id (e) { return e }
21
22exports.message_compose = function (meta, prepublish, cb) {
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, function (err, msg) {
58 ta.value = ''
59 cb(err, msg)
60 })
61 }
62
63
64 var composer =
65 h('div', h('div.column', ta,
66 h('div.row',
67 file_input(function (file) {
68 files.push(file)
69
70 var embed = file.type.indexOf('image/') === 0 ? '!' : ''
71 ta.value += embed + '['+file.name+']('+file.link+')'
72 console.log('added:', file)
73 }),
74 h('button', 'publish', {onclick: publish}))
75 )
76 )
77
78 suggest(ta, function (word, cb) {
79 cont.para(exports.suggest.map(function (fn) {
80 return function (cb) { fn(word, cb) }
81 }))
82 (function (err, results) {
83 if(err) console.error(err)
84 results = results.reduce(function (ary, item) {
85 return ary.concat(item)
86 }, []).sort(function (a, b) {
87 return b.rank - a.rank
88 }).filter(Boolean)
89
90 cb(null, results)
91 })
92 }, {})
93
94 return composer
95
96}
97
98

Built with git-ssb-web