Files: 865ad391590f0190881008c270959f5bd1f8c6da / compose.js
1412 bytesRaw
1 | var h = require('hyperscript') |
2 | var pull = require('pull-stream') |
3 | var sbot = require('./scuttlebot') |
4 | |
5 | var mime = require('simple-mime')('application/octect-stream') |
6 | var split = require('split-buffer') |
7 | |
8 | function file_input (onAdded) { |
9 | return h('label.btn', 'Upload file', |
10 | h('input', { type: 'file', hidden: true, |
11 | onchange: function (ev) { |
12 | var file = ev.target.files[0] |
13 | if (!file) return |
14 | var reader = new FileReader() |
15 | reader.onload = function () { |
16 | pull( |
17 | pull.values(split(new Buffer(reader.result), 64*1024)), |
18 | sbot.addblob(function (err, blob) { |
19 | if(err) return console.error(err) |
20 | onAdded({ |
21 | link: blob, |
22 | name: file.name, |
23 | size: reader.result.length || reader.result.byteLength, |
24 | type: mime(file.name) |
25 | }) |
26 | }) |
27 | ) |
28 | } |
29 | reader.readAsArrayBuffer(file) |
30 | } |
31 | })) |
32 | } |
33 | |
34 | module.exports = function (opts) { |
35 | var files = [] |
36 | var filesById = {} |
37 | |
38 | var textarea = h('textarea.compose', {placeholder: 'Reply to this post'}) |
39 | |
40 | var composer = h('div', |
41 | textarea, |
42 | h('button.btn', 'Preview'), |
43 | file_input(function (file) { |
44 | files.push(file) |
45 | filesById[file.link] = file |
46 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
47 | textarea.value += embed + '['+file.name+']('+file.link+')' |
48 | }) |
49 | ) |
50 | return composer |
51 | } |
52 | |
53 |
Built with git-ssb-web