Files: 793444db02289c9a688dd7301e70f700cedbdd3d / compose.js
3904 bytesRaw
1 | var h = require('hyperscript') |
2 | var pull = require('pull-stream') |
3 | var sbot = require('./scuttlebot') |
4 | var human = require('human-time') |
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 | var container = h('div.container') |
44 | |
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 | } |
60 | } |
61 | |
62 | if (opts.root) |
63 | msg.value.content.root = opts.root |
64 | if (opts.original) |
65 | msg.value.content.original = opts.original |
66 | if (opts.updated) |
67 | msg.value.content.updated = opts.updated |
68 | |
69 | msg.value.content.text = textarea.value |
70 | console.log(msg) |
71 | |
72 | if (opts.type == 'post') |
73 | var header = tools.header(msg) |
74 | if (opts.type == 'update') |
75 | var header = h('div.timestamp', 'Edited:', h('a', {href: msg.key}, human(new Date(msg.value.timestamp)))) |
76 | |
77 | var preview = h('div', |
78 | header, |
79 | h('div.message__content', tools.markdown(msg.value.content.text)), |
80 | h('button.btn', 'Publish', { |
81 | onclick: function () { |
82 | sbot.publish(msg.value.content, function (err, msg) { |
83 | if(err) throw err |
84 | console.log('Published!', msg) |
85 | window.location.reload() |
86 | if(cb) cb(err, msg) |
87 | }) |
88 | } |
89 | }), |
90 | h('button.btn', 'Cancel', { |
91 | onclick: function () { |
92 | composer.replaceChild(container, composer.firstChild) |
93 | container.appendChild(textarea) |
94 | container.appendChild(initialButtons) |
95 | } |
96 | }) |
97 | ) |
98 | composer.replaceChild(preview, composer.firstChild) |
99 | } |
100 | }), |
101 | file_input(function (file) { |
102 | files.push(file) |
103 | filesById[file.link] = file |
104 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
105 | textarea.value += embed + '['+file.name+']('+file.link+')' |
106 | }), |
107 | h('button.btn', 'Cancel', { |
108 | onclick: function () { |
109 | var cancel |
110 | if (opts.updated) { |
111 | cancel = document.getElementById(opts.updated.substring(0,10)) |
112 | var oldMessage = h('div.message__body', tools.markdown(opts.messageText)) |
113 | cancel.parentNode.replaceChild(oldMessage, cancel) |
114 | console.log(opts.buttons) |
115 | oldMessage.parentNode.appendChild(opts.buttons) |
116 | } else { |
117 | cancel = document.getElementById(opts.branch.substring(0,10)) |
118 | cancel.parentNode.removeChild(cancel) |
119 | } |
120 | } |
121 | |
122 | }) |
123 | ) |
124 | |
125 | composer.appendChild(container) |
126 | container.appendChild(textarea) |
127 | container.appendChild(initialButtons) |
128 | |
129 | return composer |
130 | } |
131 | |
132 |
Built with git-ssb-web