git ssb

0+

dangerousbeans / patchbay-bootstrap



Tree: 079f7646fd62a11c7e71deabb533b888890f1093

Files: 079f7646fd62a11c7e71deabb533b888890f1093 / modules / compose.js

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

Built with git-ssb-web