git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Commit 81141f56a31ef09f5c3d5281526bd97540f12508

get buttons working, kind of

Ev Bogue committed on 5/23/2018, 9:20:38 PM
Parent: d09d2a856ec4fe57fc11895e72383da4dc65c128

Files changed

render.jschanged
compose.jsdeleted
render.jsView
@@ -2,22 +2,136 @@
22 var pull = require('pull-stream')
33 var human = require('human-time')
44
55 var sbot = require('./scuttlebot')
6-var composer = require('./compose')
76 var tools = require('./tools')
87
98 var config = require('./config')()
109 var id = require('./keys').id
1110
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+
12127 module.exports = function (msg) {
13128 var edit = {}
14129 var reply = {}
15130
16131 var message = h('div.message')
17132
18133 if (msg.value.content.type == 'post') {
19-
20134 reply.type = 'post'
21135 reply.branch = msg.key
22136
23137 if (msg.value.content.root)
@@ -49,30 +163,17 @@
49163 h('span.timestamp', 'Edited: ', h('a', {href: '#' + update.key}, human(new Date(update.value.timestamp))))
50164 )
51165 var num = message.childNodes.length
52166 if (msg.value.author == id)
53- var act = num - 3
167+ var act = num - 2
54168 else
55169 var act = num - 2
56170 message.replaceChild(latest, message.childNodes[act])
57171 edit.messageText = update.value.content.text
58172 edit.original = msg.value.content.original
59173 })
60174 )
61-
62- message.appendChild(h('button.btn', 'Reply', {
63- onclick: function () {
64- var compose = composer(reply)
65- message.replaceChild(compose, message.lastElementChild)
66- }
67- }))
68- if (msg.value.author == id)
69- message.appendChild(h('button.btn', 'Edit', {
70- onclick: function () {
71- var compose = h('div.message', composer(edit))
72- message.parentNode.replaceChild(compose, message)
73- }
74- }))
175+ message.appendChild(defaultButtons(msg, reply, edit))
75176 return message
76177
77178 } else if (msg.value.content.type == 'vote') {
78179 message.appendChild(tools.header(msg))
compose.jsView
@@ -1,108 +1,0 @@
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- "root": opts.root
60- }
61- }
62- if (opts.original)
63- msg.value.content.original = opts.original
64- if (opts.updated)
65- msg.value.content.updated = opts.updated
66-
67- msg.value.content.text = textarea.value
68- console.log(msg)
69-
70- var preview = h('div',
71- tools.header(msg),
72- h('div.message__content', tools.markdown(msg.value.content.text)),
73- h('button.btn', 'Publish', {
74- onclick: function () {
75- sbot.publish(msg.value.content, function (err, msg) {
76- if(err) throw err
77- console.log('Published!', msg)
78- window.location.reload()
79- if(cb) cb(err, msg)
80- })
81- }
82- }),
83- h('button.btn', 'Cancel', {
84- onclick: function () {
85- composer.replaceChild(container, composer.firstChild)
86- container.appendChild(textarea)
87- container.appendChild(initialButtons)
88- }
89- })
90- )
91- composer.replaceChild(preview, composer.firstChild)
92- }
93- }),
94- file_input(function (file) {
95- files.push(file)
96- filesById[file.link] = file
97- var embed = file.type.indexOf('image/') === 0 ? '!' : ''
98- textarea.value += embed + '['+file.name+']('+file.link+')'
99- })
100- )
101-
102- composer.appendChild(container)
103- container.appendChild(textarea)
104- container.appendChild(initialButtons)
105-
106- return composer
107-}
108-

Built with git-ssb-web