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