git ssb

3+

dangerousbeans / scuttle-vue



Tree: 8dc25143a3f37f1618f7a92b3c46dec512b43bfd

Files: 8dc25143a3f37f1618f7a92b3c46dec512b43bfd / modules / compose.js

3773 bytesRaw
1'use strict'
2var h = require('hyperscript')
3var u = require('../util')
4var suggest = require('suggest-box')
5var mentions = require('ssb-mentions')
6var lightbox = require('hyperlightbox')
7var cont = require('cont')
8
9exports.needs = {
10 suggest_mentions: 'map',
11 publish: 'first',
12 message_content: 'first',
13 message_confirm: 'first',
14 file_input: 'first'
15}
16
17exports.gives = 'message_compose'
18
19function id (e) { return e }
20
21/*
22 opts can take
23
24 placeholder: string. placeholder text, defaults to "Write a message"
25 prepublish: function. called before publishing a message.
26 shrink: boolean. set to false, to make composer not shrink (or hide controls) when unfocused.
27*/
28
29exports.create = function (api) {
30
31 return function (meta, opts, cb) {
32 if('function' === typeof cb) {
33 if('function' === typeof opts)
34 opts = {prepublish: opts}
35 }
36
37 if(!opts) opts = {}
38 opts.prepublish = opts.prepublish || id
39
40 var accessories
41 meta = meta || {}
42 if(!meta.type) throw new Error('message must have type')
43
44 var publishBtn = h('button.compose__button', 'Preview', {onclick: publish})
45
46 var ta = h('textarea', {
47 placeholder: opts.placeholder || 'Write a message',
48 style: {height: opts.shrink === false ? '200px' : ''}
49 })
50
51 accessories = h('div.row.compose__controls',
52 {style: {display: 'none'}},
53 api.file_input(function (file) {
54 files.push(file)
55 filesById[file.link] = file
56 var embed = file.type.indexOf('image/') === 0 ? '!' : ''
57 ta.value += embed + '['+file.name+']('+file.link+')'
58 console.log('added:', file)
59 }),
60 publishBtn)
61
62
63 if(opts.shrink !== false) {
64 var blur
65 ta.addEventListener('focus', function () {
66 clearTimeout(blur)
67 if(!ta.value) {
68 ta.style.height = '200px'
69 }
70 accessories.style.display = 'block'
71 })
72 ta.addEventListener('blur', function () {
73 if(ta.value) return
74 ta.style.height = '50px'
75 accessories.style.display = 'none'
76 })
77 }
78
79 //ta.addEventListener('keydown', function (ev) {
80 // if(ev.keyCode === 13 && ev.ctrlKey) publish()
81 //})
82
83 var files = []
84 var filesById = {}
85
86 function publish() {
87 publishBtn.disabled = true
88 var content
89 try {
90 content = JSON.parse(ta.value)
91 } catch (err) {
92 meta.text = ta.value
93 //meta.channel = (channel.value.startsWith('#') ?
94 // channel.value.substr(1).trim() : channel.value.trim()) || null
95 meta.mentions = mentions(ta.value).map(function (mention) {
96 // merge markdown-detected mention with file info
97 var file = filesById[mention.link]
98 if (file) {
99 if (file.type) mention.type = file.type
100 if (file.size) mention.size = file.size
101 }
102 return mention
103 })
104 try {
105 meta = opts.prepublish(meta)
106 } catch (err) {
107 publishBtn.disabled = false
108 if (cb) cb(err)
109 else alert(err.message)
110 }
111 return api.message_confirm(meta, done)
112 }
113
114 api.message_confirm(content, done)
115
116 function done (err, msg) {
117 publishBtn.disabled = false
118 if(err) return alert(err.stack)
119 else if (msg) ta.value = ''
120
121 if (cb) cb(err, msg)
122 }
123 }
124
125
126 var composer =
127 h('div.compose',
128 h('div.column',
129 ta,
130 accessories
131 )
132 )
133
134 suggest(ta, function (name, cb) {
135 cont.para(api.suggest_mentions(name))
136 (function (err, ary) {
137 cb(null, ary.reduce(function (a, b) {
138 if(!b) return a
139 return a.concat(b)
140 }, []))
141 })
142 }, {})
143
144 return composer
145 }
146
147}
148
149
150

Built with git-ssb-web