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