Files: fa68194a664b872178dcd7e28f7505eb33507917 / compose.js
5093 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 | message = document.getElementById(opts.branch.substring(0,44)) |
66 | message.appendChild(fallback.buttons) |
67 | } else { |
68 | // cancel generic composer |
69 | cancel = document.getElementById('composer') |
70 | cancel.parentNode.removeChild(cancel) |
71 | } |
72 | } |
73 | |
74 | }) |
75 | |
76 | var initialButtons = h('span', |
77 | h('button.btn', 'Preview', { |
78 | onclick: function () { |
79 | if (textarea.value) { |
80 | var msg = {} |
81 | |
82 | msg.value = { |
83 | "author": id, |
84 | "content": opts |
85 | } |
86 | |
87 | msg.value.content.text = textarea.value |
88 | console.log(msg) |
89 | if (opts.type == 'post') |
90 | var header = tools.header(msg) |
91 | if (opts.type == 'update') |
92 | var header = tools.timestamp(msg, {edited: true}) |
93 | var preview = h('div', |
94 | header, |
95 | h('div.message__content', tools.markdown(msg.value.content.text)), |
96 | h('button.btn', 'Publish', { |
97 | onclick: function () { |
98 | if (msg.value.content) { |
99 | sbot.publish(msg.value.content, function (err, msg) { |
100 | if(err) throw err |
101 | console.log('Published!', msg) |
102 | if (opts.type == 'edit') { |
103 | var message = document.getElementById(opts.branch.substring(0,44)) |
104 | fallback.messageText = msg.value.content.text |
105 | var editBody = h('div.message__body', |
106 | tools.timestamp(msg, {edited: true}), |
107 | h('div', tools.markdown(msg.value.content.text)) |
108 | ) |
109 | |
110 | message.replaceChild(editBody, message.childNodes[message.childNodes.length - 1]) |
111 | editBody.parentNode.appendChild(fallback.buttons) |
112 | } else { |
113 | if (opts.branch) |
114 | cancel = document.getElementById('re:' + opts.branch.substring(0,44)) |
115 | else |
116 | cancel = document.getElementById('composer') |
117 | cancel.parentNode.removeChild(cancel) |
118 | } |
119 | }) |
120 | } |
121 | } |
122 | }), |
123 | h('button.btn', 'Cancel', { |
124 | onclick: function () { |
125 | composer.replaceChild(container, composer.firstChild) |
126 | container.appendChild(textarea) |
127 | container.appendChild(initialButtons) |
128 | } |
129 | }) |
130 | ) |
131 | composer.replaceChild(preview, composer.firstChild) |
132 | } |
133 | } |
134 | }), |
135 | file_input(function (file) { |
136 | files.push(file) |
137 | filesById[file.link] = file |
138 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
139 | textarea.value += embed + '['+file.name+']('+file.link+')' |
140 | }), |
141 | cancelBtn |
142 | ) |
143 | |
144 | composer.appendChild(container) |
145 | container.appendChild(textarea) |
146 | container.appendChild(initialButtons) |
147 | |
148 | return composer |
149 | } |
150 | |
151 |
Built with git-ssb-web