git ssb

2+

ev / mvd



Commit c4a5bbce3ba975d7c10582da85771ea6fe6d9f0e

make a little progress on buttons

Ev Bogue committed on 5/24/2018, 5:55:12 PM
Parent: 4766a62576fe0849742086a4833348bca8a8f26d

Files changed

render.jschanged
compose.jsadded
render.jsView
@@ -2,136 +2,22 @@
22 var pull = require('pull-stream')
33 var human = require('human-time')
44
55 var sbot = require('./scuttlebot')
6 +var composer = require('./compose')
67 var tools = require('./tools')
78
89 var config = require('./config')()
910 var id = require('./keys').id
1011
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- /*var cancelBtn = h('button.btn', 'Cancel' {
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- //buttons.appendChild(cancelBtn)
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-
12712 module.exports = function (msg) {
12813 var edit = {}
12914 var reply = {}
13015
13116 var message = h('div.message')
13217
13318 if (msg.value.content.type == 'post') {
19 +
13420 reply.type = 'post'
13521 reply.branch = msg.key
13622
13723 if (msg.value.content.root)
@@ -161,19 +47,32 @@
16147 var latest = h('div.message__body',
16248 tools.markdown(update.value.content.text),
16349 h('span.timestamp', 'Edited: ', h('a', {href: '#' + update.key}, human(new Date(update.value.timestamp))))
16450 )
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)
17152 edit.messageText = update.value.content.text
17253 edit.original = msg.value.content.original
17354 })
17455 )
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)
17675 return message
17776
17877 } else if (msg.value.content.type == 'vote') {
17978 message.appendChild(tools.header(msg))
compose.jsView
@@ -1,0 +1,110 @@
1 +var h = require('hyperscript')
2 +var pull = require('pull-stream')
3 +var sbot = require('./scuttlebot')
4 +
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 +function file_input (onAdded) {
13 + return h('label.btn', 'Upload file',
14 + h('input', { type: 'file', hidden: true,
15 + onchange: function (ev) {
16 + var file = ev.target.files[0]
17 + if (!file) return
18 + var reader = new FileReader()
19 + reader.onload = function () {
20 + pull(
21 + pull.values(split(new Buffer(reader.result), 64*1024)),
22 + sbot.addblob(function (err, blob) {
23 + if(err) return console.error(err)
24 + onAdded({
25 + link: blob,
26 + name: file.name,
27 + size: reader.result.length || reader.result.byteLength,
28 + type: mime(file.name)
29 + })
30 + })
31 + )
32 + }
33 + reader.readAsArrayBuffer(file)
34 + }
35 + }))
36 +}
37 +
38 +module.exports = function (opts) {
39 + var files = []
40 + var filesById = {}
41 +
42 + var composer = h('div.composer')
43 + var container = h('div.container')
44 +
45 + if (opts.messageText)
46 + var textarea = h('textarea.compose', opts.messageText)
47 + else
48 + var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a message'})
49 +
50 + var initialButtons = h('span',
51 + h('button.btn', 'Preview', {
52 + onclick: function () {
53 +
54 + var msg = {}
55 + msg.value = {
56 + "author": id,
57 + "content": {
58 + "type": opts.type
59 + }
60 + }
61 +
62 + if (opts.root)
63 + msg.valeu.content.root = opts.root
64 + if (opts.original)
65 + msg.value.content.original = opts.original
66 + if (opts.updated)
67 + msg.value.content.updated = opts.updated
68 +
69 + msg.value.content.text = textarea.value
70 + console.log(msg)
71 +
72 + var preview = h('div',
73 + tools.header(msg),
74 + h('div.message__content', tools.markdown(msg.value.content.text)),
75 + h('button.btn', 'Publish', {
76 + onclick: function () {
77 + sbot.publish(msg.value.content, function (err, msg) {
78 + if(err) throw err
79 + console.log('Published!', msg)
80 + window.location.reload()
81 + if(cb) cb(err, msg)
82 + })
83 + }
84 + }),
85 + h('button.btn', 'Cancel', {
86 + onclick: function () {
87 + composer.replaceChild(container, composer.firstChild)
88 + container.appendChild(textarea)
89 + container.appendChild(initialButtons)
90 + }
91 + })
92 + )
93 + composer.replaceChild(preview, composer.firstChild)
94 + }
95 + }),
96 + file_input(function (file) {
97 + files.push(file)
98 + filesById[file.link] = file
99 + var embed = file.type.indexOf('image/') === 0 ? '!' : ''
100 + textarea.value += embed + '['+file.name+']('+file.link+')'
101 + })
102 + )
103 +
104 + composer.appendChild(container)
105 + container.appendChild(textarea)
106 + container.appendChild(initialButtons)
107 +
108 + return composer
109 +}
110 +

Built with git-ssb-web