Files: 7cef8be38fc2c78136c3f41f4660b0f3fc977aa6 / compose.js
5221 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 | var route = require('./views') |
13 | |
14 | function file_input (onAdded) { |
15 | return h('label.btn', 'Upload file', |
16 | h('input', { type: 'file', hidden: true, |
17 | onchange: function (ev) { |
18 | var file = ev.target.files[0] |
19 | if (!file) return |
20 | var reader = new FileReader() |
21 | reader.onload = function () { |
22 | pull( |
23 | pull.values(split(new Buffer(reader.result), 64*1024)), |
24 | sbot.addblob(function (err, blob) { |
25 | if(err) return console.error(err) |
26 | onAdded({ |
27 | link: blob, |
28 | name: file.name, |
29 | size: reader.result.length || reader.result.byteLength, |
30 | type: mime(file.name) |
31 | }) |
32 | }) |
33 | ) |
34 | } |
35 | reader.readAsArrayBuffer(file) |
36 | } |
37 | })) |
38 | } |
39 | |
40 | module.exports = function (opts, fallback) { |
41 | var files = [] |
42 | var filesById = {} |
43 | |
44 | var composer = h('div.composer') |
45 | var container = h('div.container') |
46 | |
47 | if (opts.type == 'post') |
48 | var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a message'}) |
49 | else |
50 | var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a message'}, fallback.messageText) |
51 | |
52 | var cancelBtn = h('button.btn', 'Cancel', { |
53 | onclick: function () { |
54 | var cancel |
55 | console.log(opts) |
56 | if (opts.type == 'edit') { |
57 | cancel = document.getElementById('edit:' + opts.branch.substring(0,44)) |
58 | var oldMessage = h('div.message__body', tools.markdown(fallback.messageText)) |
59 | cancel.parentNode.replaceChild(oldMessage, cancel) |
60 | oldMessage.parentNode.appendChild(fallback.buttons) |
61 | } else if (opts.branch) { |
62 | //cancel reply composer |
63 | cancel = document.getElementById('re:' + opts.branch.substring(0,44)) |
64 | cancel.parentNode.removeChild(cancel) |
65 | } else { |
66 | // cancel generic composer |
67 | cancel = document.getElementById('composer') |
68 | cancel.parentNode.removeChild(cancel) |
69 | } |
70 | } |
71 | |
72 | }) |
73 | |
74 | var initialButtons = h('span', |
75 | h('button.btn', 'Preview', { |
76 | onclick: function () { |
77 | if (textarea.value) { |
78 | var msg = {} |
79 | msg.value = { |
80 | "author": id, |
81 | "content": { |
82 | "type": opts.type |
83 | } |
84 | } |
85 | |
86 | if (opts.root) |
87 | msg.value.content.root = opts.root |
88 | if (opts.original) |
89 | msg.value.content.original = opts.original |
90 | if (opts.updated) |
91 | msg.value.content.updated = opts.updated |
92 | |
93 | msg.value.content.text = textarea.value |
94 | |
95 | if (opts.type == 'post') |
96 | var header = tools.header(msg) |
97 | if (opts.type == 'update') |
98 | var header = tools.timestamp(msg, {edited: true}) |
99 | var preview = h('div', |
100 | header, |
101 | h('div.message__content', tools.markdown(msg.value.content.text)), |
102 | h('button.btn', 'Publish', { |
103 | onclick: function () { |
104 | if (msg.value.content) { |
105 | sbot.publish(msg.value.content, function (err, msg) { |
106 | if(err) throw err |
107 | console.log('Published!', msg) |
108 | if (opts.type == 'edit') { |
109 | var message = document.getElementById(opts.branch.substring(0,44)) |
110 | fallback.messageText = msg.value.content.text |
111 | var editBody = h('div.message__body', |
112 | tools.timestamp(msg, {edited: true}), |
113 | h('div', tools.markdown(msg.value.content.text)) |
114 | ) |
115 | |
116 | message.replaceChild(editBody, message.childNodes[message.childNodes.length - 1]) |
117 | editBody.parentNode.appendChild(fallback.buttons) |
118 | } else { |
119 | if (opts.branch) |
120 | cancel = document.getElementById('re:' + opts.branch.substring(0,44)) |
121 | else |
122 | cancel = document.getElementById('composer') |
123 | cancel.parentNode.removeChild(cancel) |
124 | } |
125 | }) |
126 | } |
127 | } |
128 | }), |
129 | h('button.btn', 'Cancel', { |
130 | onclick: function () { |
131 | composer.replaceChild(container, composer.firstChild) |
132 | container.appendChild(textarea) |
133 | container.appendChild(initialButtons) |
134 | } |
135 | }) |
136 | ) |
137 | composer.replaceChild(preview, composer.firstChild) |
138 | } |
139 | } |
140 | }), |
141 | file_input(function (file) { |
142 | files.push(file) |
143 | filesById[file.link] = file |
144 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
145 | textarea.value += embed + '['+file.name+']('+file.link+')' |
146 | }), |
147 | cancelBtn |
148 | ) |
149 | |
150 | composer.appendChild(container) |
151 | container.appendChild(textarea) |
152 | container.appendChild(initialButtons) |
153 | |
154 | return composer |
155 | } |
156 | |
157 |
Built with git-ssb-web