git ssb

16+

Dominic / patchbay



Tree: a1db279053aec5366d349c13b1b768184961ce89

Files: a1db279053aec5366d349c13b1b768184961ce89 / modules / compose.js

2647 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
18function id (e) { return e }
19
20exports.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.compose', h('div.column', ta,
67 accessories = h('div.row.compose__controls',
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

Built with git-ssb-web