Files: 12a75517dfb361b5f8a4df0c91372c0c68f1f164 / compose.js
2823 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 | |
46 | var textarea = h('textarea.compose', {placeholder: 'Write a message' || opts.placeholder}) |
47 | |
48 | var initialButtons = h('span', |
49 | h('button.btn', 'Preview', { |
50 | onclick: function () { |
51 | |
52 | var msg = {} |
53 | msg.value = { |
54 | "author": id, |
55 | "content": { |
56 | "type": opts.type, |
57 | "root": opts.root, |
58 | "branch": opts.branch |
59 | } |
60 | } |
61 | msg.value.content.text = textarea.value |
62 | console.log(msg) |
63 | |
64 | var preview = h('div', |
65 | tools.header(msg), |
66 | h('div.message__content', tools.markdown(msg.value.content.text)), |
67 | h('button.btn', 'Publish', { |
68 | onclick: function () { |
69 | sbot.publish(msg.value.content, function (err, msg) { |
70 | if(err) throw err |
71 | console.log('Published!', msg) |
72 | window.location.reload() |
73 | if(cb) cb(err, msg) |
74 | }) |
75 | } |
76 | }), |
77 | h('button.btn', 'Cancel', { |
78 | onclick: function () { |
79 | composer.replaceChild(container, composer.firstChild) |
80 | container.appendChild(textarea) |
81 | container.appendChild(initialButtons) |
82 | } |
83 | }) |
84 | ) |
85 | composer.replaceChild(preview, composer.firstChild) |
86 | } |
87 | }), |
88 | file_input(function (file) { |
89 | files.push(file) |
90 | filesById[file.link] = file |
91 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
92 | textarea.value += embed + '['+file.name+']('+file.link+')' |
93 | }) |
94 | ) |
95 | |
96 | composer.appendChild(container) |
97 | container.appendChild(textarea) |
98 | container.appendChild(initialButtons) |
99 | |
100 | return composer |
101 | } |
102 | |
103 |
Built with git-ssb-web