git ssb

16+

Dominic / patchbay



Commit 86a733778b0428dc270a65273d76c6bb187dad06

implement attachments

Dominic Tarr committed on 5/29/2016, 11:16:27 AM
Parent: 705c393ea050af7928c41a20b9597870a3a264f0

Files changed

modules/compose.jschanged
modules/file-input.jsadded
modules/compose.jsView
@@ -8,21 +8,20 @@
88 exports.suggest = []
99 exports.publish = []
1010 exports.message_content = []
1111 exports.message_confirm = []
12+exports.file_input = []
1213
1314 //this decorator expects to be the first
1415
1516 function id (e) { return e }
1617
1718 exports.message_compose = function (meta, prepublish, sbot) {
1819 if('function' !== typeof prepublish)
19- sbot = prepublish, prepublish = id
20+ sbot = prepublish, prepublish = id
2021 meta = meta || {}
2122 if(!meta.type) throw new Error('message must have type')
2223 var ta = h('textarea')
23- //h('pre.editable.fixed', 'HELLO')
24- //ta.setAttribute('contenteditable', '')
2524
2625 var blur
2726 ta.addEventListener('focus', function () {
2827 clearTimeout(blur)
@@ -36,21 +35,40 @@
3635 ta.style.height = '50px'
3736 }, 200)
3837 })
3938
39+ ta.addEventListener('keydown', function (ev) {
40+ if(ev.keyCode === 13 && ev.ctrlKey) publish()
41+ })
42+
43+ var files = []
44+
45+ function publish() {
46+ meta.text = ta.value
47+ meta.mentions = mentions(ta.value).concat(files)
48+ try {
49+ meta = prepublish(meta)
50+ } catch (err) {
51+ return alert(err.message)
52+ }
53+ u.firstPlug(exports.message_confirm, meta, sbot)
54+ }
55+
56+
4057 var composer =
4158 h('div', h('div.column', ta,
42- h('button', 'publish', {onclick: function () {
43- meta.text = ta.value
44- meta.mentions = mentions(ta.value)
45- try {
46- meta = prepublish(meta)
47- } catch (err) {
48- return alert(err.message)
49- }
50- u.firstPlug(exports.message_confirm, meta, sbot)
51- }})))
59+ h('div.row',
60+ u.firstPlug(exports.file_input, function (file) {
61+ files.push(file)
5262
63+ var embed = file.type.indexOf('image/') === 0 ? '!' : ''
64+ ta.value += embed + '['+file.name+']('+file.link+')'
65+ console.log('added:', file)
66+ }),
67+ h('button', 'publish', {onclick: publish}))
68+ )
69+ )
70+
5371 suggest(ta, function (word, cb) {
5472 cont.para(exports.suggest.map(function (fn) {
5573 return function (cb) { fn(word, sbot, cb) }
5674 }))
@@ -69,8 +87,4 @@
6987 return composer
7088
7189 }
7290
73-
74-
75-
76-
modules/file-input.jsView
@@ -1,0 +1,45 @@
1+var u = require('../util')
2+var h = require('hyperscript')
3+var pull = require('pull-stream')
4+var mime = require('mime-types')
5+var split = require('split-buffer')
6+
7+function first(plug) {
8+ return function () {
9+ var args = [].slice.call(arguments)
10+ args.unshift(plug)
11+ return u.firstPlug.apply(null, args)
12+ }
13+}
14+
15+var add = first(exports.sbot_blobs_add = [])
16+
17+exports.file_input = function FileInput(onAdded) {
18+
19+ return h('input', { type: 'file',
20+ onchange: function (ev) {
21+ var file = ev.target.files[0]
22+ var reader = new FileReader()
23+ reader.onload = function () {
24+ pull(
25+ pull.values(split(new Buffer(reader.result), 64*1024)),
26+ add(function (err, blob) {
27+ if(err) return console.error(err)
28+ onAdded({
29+ link: blob,
30+ name: file.name,
31+ size: reader.result.length || reader.result.byteLength,
32+ type: mime.contentType(file.name)
33+ })
34+
35+ })
36+ )
37+ }
38+ reader.readAsArrayBuffer(file)
39+ }
40+ })
41+}
42+
43+
44+
45+

Built with git-ssb-web