Files: e3b6a2cde0471d83afe35e6ab1514feee59047cd / compose.js
4766 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, buttons) { |
41 | var files = [] |
42 | var filesById = {} |
43 | |
44 | var composer = h('div.composer') |
45 | var container = h('div.container') |
46 | |
47 | if (opts.messageText) |
48 | var textarea = h('textarea.compose', opts.messageText) |
49 | else |
50 | var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a message'}) |
51 | |
52 | if (window.location.hash.substring(1) != 'compose') |
53 | var cancelBtn = h('button.btn', 'Cancel', { |
54 | onclick: function () { |
55 | var cancel |
56 | if (opts.updated) { |
57 | cancel = document.getElementById(opts.updated.substring(0,10)) |
58 | var oldMessage = h('div.message__body', tools.markdown(opts.messageText)) |
59 | cancel.parentNode.replaceChild(oldMessage, cancel) |
60 | oldMessage.parentNode.appendChild(buttons) |
61 | } else if (opts.branch) { |
62 | cancel = document.getElementById(opts.branch.substring(0,10)) |
63 | cancel.parentNode.removeChild(cancel) |
64 | } else { |
65 | cancel = document.getElementById('composer') |
66 | cancel.parentNode.removeChild(cancel) |
67 | } |
68 | } |
69 | |
70 | }) |
71 | |
72 | else |
73 | var cancel = h('span', '') |
74 | |
75 | var initialButtons = h('span', |
76 | h('button.btn', 'Preview', { |
77 | onclick: function () { |
78 | |
79 | var msg = {} |
80 | msg.value = { |
81 | "author": id, |
82 | "content": { |
83 | "type": opts.type |
84 | } |
85 | } |
86 | |
87 | if (opts.root) |
88 | msg.value.content.root = opts.root |
89 | if (opts.original) |
90 | msg.value.content.original = opts.original |
91 | if (opts.updated) |
92 | msg.value.content.updated = opts.updated |
93 | |
94 | msg.value.content.text = textarea.value |
95 | console.log(msg) |
96 | |
97 | if (opts.type == 'post') |
98 | var header = tools.header(msg) |
99 | if (opts.type == 'update') |
100 | var header = h('div.timestamp', 'Edited: ', h('a', {href: msg.key}, human(new Date(msg.value.timestamp)))) |
101 | |
102 | var preview = h('div', |
103 | header, |
104 | h('div.message__content', tools.markdown(msg.value.content.text)), |
105 | h('button.btn', 'Publish', { |
106 | onclick: function () { |
107 | sbot.publish(msg.value.content, function (err, msg) { |
108 | if(err) throw err |
109 | console.log('Published!', msg) |
110 | if (opts.type == 'update') { |
111 | var originalMessage = document.getElementById(opts.updated.substring(0,10)) |
112 | console.log(originalMessage) |
113 | opts.messageText = msg.value.content.text |
114 | var newMessage = h('div.message__body', |
115 | h('div.timestamp', 'Edited: ', h('a', {href: msg.key}, human(new Date(msg.value.timestamp)))), |
116 | h('div', tools.markdown(msg.value.content.text)) |
117 | ) |
118 | originalMessage.parentNode.replaceChild(newMessage, originalMessage) |
119 | newMessage.parentNode.appendChild(buttons) |
120 | } |
121 | }) |
122 | } |
123 | }), |
124 | h('button.btn', 'Cancel', { |
125 | onclick: function () { |
126 | composer.replaceChild(container, composer.firstChild) |
127 | container.appendChild(textarea) |
128 | container.appendChild(initialButtons) |
129 | } |
130 | }) |
131 | ) |
132 | composer.replaceChild(preview, composer.firstChild) |
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