Files: 565af159eaf7a2f0d880ff7e4286e19e59da7ae7 / compose.js
3035 bytesRaw
1 | var h = require('hyperscript') |
2 | var pull = require('pull-stream') |
3 | var sbot = require('./scuttlebot') |
4 | |
5 | var id = require('./keys').id |
6 | |
7 | var tools = require('./tools') |
8 | |
9 | var mime = require('simple-mime')('application/octect-stream') |
10 | var split = require('split-buffer') |
11 | |
12 | function file_input (onAdded) { |
13 | return h('label.btn', 'Upload file', |
14 | h('input', { type: 'file', hidden: true, |
15 | onchange: function (ev) { |
16 | var file = ev.target.files[0] |
17 | if (!file) return |
18 | var reader = new FileReader() |
19 | reader.onload = function () { |
20 | pull( |
21 | pull.values(split(new Buffer(reader.result), 64*1024)), |
22 | sbot.addblob(function (err, blob) { |
23 | if(err) return console.error(err) |
24 | onAdded({ |
25 | link: blob, |
26 | name: file.name, |
27 | size: reader.result.length || reader.result.byteLength, |
28 | type: mime(file.name) |
29 | }) |
30 | }) |
31 | ) |
32 | } |
33 | reader.readAsArrayBuffer(file) |
34 | } |
35 | })) |
36 | } |
37 | |
38 | module.exports = function (opts) { |
39 | var files = [] |
40 | var filesById = {} |
41 | |
42 | var composer = h('div.composer') |
43 | |
44 | var container = h('div.container') |
45 | if (opts.messageText) |
46 | var textarea = h('textarea.compose', opts.messageText) |
47 | else |
48 | var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a message'}) |
49 | |
50 | var initialButtons = h('span', |
51 | h('button.btn', 'Preview', { |
52 | onclick: function () { |
53 | |
54 | var msg = {} |
55 | msg.value = { |
56 | "author": id, |
57 | "content": { |
58 | "type": opts.type, |
59 | "root": opts.root |
60 | } |
61 | } |
62 | if (opts.original) |
63 | msg.value.content.original = opts.original |
64 | if (opts.updated) |
65 | msg.value.content.updated = opts.updated |
66 | |
67 | msg.value.content.text = textarea.value |
68 | console.log(msg) |
69 | |
70 | var preview = h('div', |
71 | tools.header(msg), |
72 | h('div.message__content', tools.markdown(msg.value.content.text)), |
73 | h('button.btn', 'Publish', { |
74 | onclick: function () { |
75 | sbot.publish(msg.value.content, function (err, msg) { |
76 | if(err) throw err |
77 | console.log('Published!', msg) |
78 | window.location.reload() |
79 | if(cb) cb(err, msg) |
80 | }) |
81 | } |
82 | }), |
83 | h('button.btn', 'Cancel', { |
84 | onclick: function () { |
85 | composer.replaceChild(container, composer.firstChild) |
86 | container.appendChild(textarea) |
87 | container.appendChild(initialButtons) |
88 | } |
89 | }) |
90 | ) |
91 | composer.replaceChild(preview, composer.firstChild) |
92 | } |
93 | }), |
94 | file_input(function (file) { |
95 | files.push(file) |
96 | filesById[file.link] = file |
97 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
98 | textarea.value += embed + '['+file.name+']('+file.link+')' |
99 | }) |
100 | ) |
101 | |
102 | composer.appendChild(container) |
103 | container.appendChild(textarea) |
104 | container.appendChild(initialButtons) |
105 | |
106 | return composer |
107 | } |
108 | |
109 |
Built with git-ssb-web