render.jsView |
---|
2 | 2 | var pull = require('pull-stream') |
3 | 3 | var human = require('human-time') |
4 | 4 | |
5 | 5 | var sbot = require('./scuttlebot') |
| 6 | +var composer = require('./compose') |
6 | 7 | var tools = require('./tools') |
7 | 8 | |
8 | 9 | var config = require('./config')() |
9 | 10 | var id = require('./keys').id |
10 | 11 | |
11 | | -var mime = require('simple-mime')('application/octect-stream') |
12 | | -var split = require('split-buffer') |
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 | | -function composeButtons (msg, opts) { |
41 | | - var files = [] |
42 | | - var filesById = {} |
43 | | - |
44 | | - var buttons = h('div.controls') |
45 | | - |
46 | | - var previewBtn = h('button.btn', 'Preview', { |
47 | | - onclick: function () { |
48 | | - var draft = {} |
49 | | - draft.value = { |
50 | | - "author": id, |
51 | | - "content": { |
52 | | - "type": opts.type, |
53 | | - "root": opts.root |
54 | | - } |
55 | | - } |
56 | | - |
57 | | - if (opts.original) |
58 | | - msg.value.content.original = opts.original |
59 | | - if (opts.updated) |
60 | | - msg.value.content.updated = opts.updated |
61 | | - |
62 | | - draft.value.content.text = textarea.value |
63 | | - console.log(draft) |
64 | | - } |
65 | | - }) |
66 | | - |
67 | | - |
68 | | - onclick: function () { |
69 | | - |
70 | | - } |
71 | | - })*/ |
72 | | - |
73 | | - buttons.appendChild(previewBtn) |
74 | | - |
75 | | - buttons.appendChild( |
76 | | - file_input(function (file) { |
77 | | - files.push(file) |
78 | | - filesById[file.link] = file |
79 | | - var embed = file.type.indexOf('image/') === 0 ? '!' : '' |
80 | | - textarea.value += embed + '['+file.name+']('+file.link+')' |
81 | | - }) |
82 | | - ) |
83 | | - return buttons |
84 | | - |
85 | | -} |
86 | | - |
87 | | - |
88 | | -function defaultButtons (msg, reply, edit) { |
89 | | - |
90 | | - var buttons = h('div.controls') |
91 | | - |
92 | | - var replyBtn = h('button.btn', 'Reply', { |
93 | | - onclick: function () { |
94 | | - var textarea = h('textarea.compose', {placeholder: 'Reply to this message'}) |
95 | | - var reply = h('div.reply', |
96 | | - textarea, |
97 | | - composeButtons(msg, reply) |
98 | | - ) |
99 | | - |
100 | | - buttons.parentNode.replaceChild(reply, buttons) |
101 | | - } |
102 | | - }) |
103 | | - |
104 | | - var editBtn = h('button.btn', 'Edit', { |
105 | | - onclick: function () { |
106 | | - var textarea = h('textarea.compose', edit.messageText) |
107 | | - var editor = h('div.edit', |
108 | | - textarea, |
109 | | - composeButtons(msg, edit) |
110 | | - ) |
111 | | - |
112 | | - var prevMessage = buttons.parentNode.childNodes[2] |
113 | | - buttons.parentNode.replaceChild(editor, prevMessage) |
114 | | - buttons.parentNode.removeChild(buttons) |
115 | | - } |
116 | | - }) |
117 | | - |
118 | | - buttons.appendChild(replyBtn) |
119 | | - |
120 | | - if (msg.value.author == id) |
121 | | - buttons.appendChild(editBtn) |
122 | | - |
123 | | - return buttons |
124 | | -} |
125 | | - |
126 | | - |
127 | 12 | module.exports = function (msg) { |
128 | 13 | var edit = {} |
129 | 14 | var reply = {} |
130 | 15 | |
131 | 16 | var message = h('div.message') |
132 | 17 | |
133 | 18 | if (msg.value.content.type == 'post') { |
| 19 | + |
134 | 20 | reply.type = 'post' |
135 | 21 | reply.branch = msg.key |
136 | 22 | |
137 | 23 | if (msg.value.content.root) |
161 | 47 | var latest = h('div.message__body', |
162 | 48 | tools.markdown(update.value.content.text), |
163 | 49 | h('span.timestamp', 'Edited: ', h('a', {href: '#' + update.key}, human(new Date(update.value.timestamp)))) |
164 | 50 | ) |
165 | | - var num = message.childNodes.length |
166 | | - if (msg.value.author == id) |
167 | | - var act = num - 2 |
168 | | - else |
169 | | - var act = num - 2 |
170 | | - message.replaceChild(latest, message.childNodes[act]) |
| 51 | + message.replaceChild(latest, message.childNodes.length) |
171 | 52 | edit.messageText = update.value.content.text |
172 | 53 | edit.original = msg.value.content.original |
173 | 54 | }) |
174 | 55 | ) |
175 | | - message.appendChild(defaultButtons(msg, reply, edit)) |
| 56 | + var buttons = h('div.buttons') |
| 57 | + |
| 58 | + buttons.appendChild(h('button.btn', 'Reply', { |
| 59 | + onclick: function () { |
| 60 | + var compose = composer(reply) |
| 61 | + message.replaceChild(compose, message.lastElementChild) |
| 62 | + } |
| 63 | + })) |
| 64 | + if (msg.value.author == id) |
| 65 | + buttons.appendChild(h('button.btn', 'Edit', { |
| 66 | + onclick: function () { |
| 67 | + var compose = composer(edit) |
| 68 | + var r = message.childNodes.length - 1 |
| 69 | + message.removeChild(message.childNodes[r]) |
| 70 | + message.replaceChild(compose, message.lastElementChild) |
| 71 | + } |
| 72 | + })) |
| 73 | + |
| 74 | + message.appendChild(buttons) |
176 | 75 | return message |
177 | 76 | |
178 | 77 | } else if (msg.value.content.type == 'vote') { |
179 | 78 | message.appendChild(tools.header(msg)) |