Files: 7ede3c7a935ddf6b575e9a503f49956005a87a3d / compose.js
6395 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 | var mentions = require('ssb-mentions') |
7 | |
8 | var avatar = require('./avatar') |
9 | var tools = require('./tools') |
10 | |
11 | var mime = require('simple-mime')('application/octect-stream') |
12 | var split = require('split-buffer') |
13 | |
14 | var route = require('./views') |
15 | |
16 | function file_input (onAdded) { |
17 | return h('label.btn', 'Upload file', |
18 | h('input', { type: 'file', hidden: true, |
19 | onchange: function (ev) { |
20 | var file = ev.target.files[0] |
21 | if (!file) return |
22 | var reader = new FileReader() |
23 | reader.onload = function () { |
24 | pull( |
25 | pull.values(split(new Buffer(reader.result), 64*1024)), |
26 | sbot.addblob(function (err, blob) { |
27 | if(err) return console.error(err) |
28 | onAdded({ |
29 | link: blob, |
30 | name: file.name, |
31 | size: reader.result.length || reader.result.byteLength, |
32 | type: mime(file.name) |
33 | }) |
34 | }) |
35 | ) |
36 | } |
37 | reader.readAsArrayBuffer(file) |
38 | } |
39 | })) |
40 | } |
41 | |
42 | module.exports = function (opts, fallback) { |
43 | var files = [] |
44 | var filesById = {} |
45 | |
46 | var composer = h('div.composer') |
47 | var container = h('div.container') |
48 | if (opts.boostAuthor) { |
49 | var boostName = avatar.cachedName(opts.boostAuthor) |
50 | } |
51 | if (opts.boostContent) { |
52 | var textarea = h('textarea.compose') |
53 | var str = opts.boostContent |
54 | var lines = str.split("\n") |
55 | for(var i=0; i<lines.length; i++) { |
56 | lines[i] = "> " + lines[i] |
57 | } |
58 | var newContent = lines.join("\n") |
59 | var content = 'Boosting: ' + opts.boostKey + '\n\n' + newContent + '-[' + boostName.textContent + ']('+ opts.boostAuthor + ')' |
60 | textarea.value = content |
61 | } |
62 | |
63 | else if (opts.mentions) { |
64 | var textarea = h('textarea.compose', opts.mentions) |
65 | } |
66 | |
67 | else if (opts.type == 'wiki') |
68 | var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a wiki (anyone can edit)'}) |
69 | else if (opts.type == 'post') |
70 | var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a message (only you can edit)'}) |
71 | else |
72 | var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a message (only you can edit)'}, fallback.messageText) |
73 | |
74 | var cancelBtn = h('button.btn', 'Cancel', { |
75 | onclick: function () { |
76 | var cancel |
77 | console.log(opts) |
78 | |
79 | if (opts.type == 'edit') { |
80 | cancel = document.getElementById('edit:' + opts.branch.substring(0,44)) |
81 | var oldMessage = h('div.message__body', tools.markdown(fallback.messageText)) |
82 | cancel.parentNode.replaceChild(oldMessage, cancel) |
83 | oldMessage.parentNode.appendChild(fallback.buttons) |
84 | } else if (opts.branch) { |
85 | //cancel reply composer |
86 | cancel = document.getElementById('re:' + opts.branch.substring(0,44)) |
87 | cancel.parentNode.removeChild(cancel) |
88 | message = document.getElementById(opts.branch.substring(0,44)) |
89 | message.appendChild(fallback.buttons) |
90 | } else { |
91 | // cancel generic composer |
92 | cancel = document.getElementById('composer') |
93 | cancel.parentNode.removeChild(cancel) |
94 | } |
95 | } |
96 | |
97 | }) |
98 | |
99 | var initialButtons = h('span', |
100 | h('button.btn', 'Preview', { |
101 | onclick: function () { |
102 | if (textarea.value) { |
103 | var msg = {} |
104 | |
105 | msg.value = { |
106 | "author": id, |
107 | "content": opts |
108 | } |
109 | |
110 | msg.value.content.text = textarea.value |
111 | msg.value.content.mentions = mentions(textarea.value).map( |
112 | function (mention) { |
113 | var file = filesById[mention.link] |
114 | if (file) { |
115 | if (file.type) mention.type = file.type |
116 | if (file.size) mention.size = file.size |
117 | } |
118 | return mention |
119 | } |
120 | ) |
121 | |
122 | if (opts.recps) |
123 | msg.value.private = true |
124 | |
125 | console.log(msg) |
126 | if (opts.type == 'post' || opts.type == 'wiki') |
127 | var header = tools.header(msg) |
128 | if (opts.type == 'update') |
129 | var header = tools.timestamp(msg, {edited: true}) |
130 | var preview = h('div', |
131 | header, |
132 | h('div.message__content', tools.markdown(msg.value.content.text)), |
133 | h('button.btn', 'Publish', { |
134 | onclick: function () { |
135 | if (msg.value.content) { |
136 | sbot.publish(msg.value.content, function (err, msg) { |
137 | if(err) throw err |
138 | console.log('Published!', msg) |
139 | if (opts.type == 'edit') { |
140 | var message = document.getElementById(opts.branch.substring(0,44)) |
141 | fallback.messageText = msg.value.content.text |
142 | var editBody = h('div.message__body', |
143 | tools.timestamp(msg, {edited: true}), |
144 | h('div', tools.markdown(msg.value.content.text)) |
145 | ) |
146 | |
147 | message.replaceChild(editBody, message.childNodes[message.childNodes.length - 1]) |
148 | editBody.parentNode.appendChild(fallback.buttons) |
149 | } else { |
150 | if (opts.branch) |
151 | cancel = document.getElementById('re:' + opts.branch.substring(0,44)) |
152 | else |
153 | cancel = document.getElementById('composer') |
154 | cancel.parentNode.removeChild(cancel) |
155 | } |
156 | }) |
157 | } |
158 | } |
159 | }), |
160 | h('button.btn', 'Cancel', { |
161 | onclick: function () { |
162 | composer.replaceChild(container, composer.firstChild) |
163 | container.appendChild(textarea) |
164 | container.appendChild(initialButtons) |
165 | } |
166 | }) |
167 | ) |
168 | composer.replaceChild(preview, composer.firstChild) |
169 | } |
170 | } |
171 | }), |
172 | file_input(function (file) { |
173 | files.push(file) |
174 | filesById[file.link] = file |
175 | var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
176 | textarea.value += embed + '['+file.name+']('+file.link+')' |
177 | }), |
178 | cancelBtn |
179 | ) |
180 | |
181 | composer.appendChild(container) |
182 | container.appendChild(textarea) |
183 | container.appendChild(initialButtons) |
184 | |
185 | return composer |
186 | } |
187 | |
188 |
Built with git-ssb-web