git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: 86661677a7623472280bc05e563bb733054a0f01

Files: 86661677a7623472280bc05e563bb733054a0f01 / compose.js

5192 bytesRaw
1var h = require('hyperscript')
2var pull = require('pull-stream')
3var sbot = require('./scuttlebot')
4var human = require('human-time')
5var id = require('./keys').id
6
7var tools = require('./tools')
8
9var mime = require('simple-mime')('application/octect-stream')
10var split = require('split-buffer')
11
12var route = require('./views')
13
14function 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
40module.exports = function (opts, buttons) {
41 var files = []
42 var filesById = {}
43
44 console.log(opts)
45
46 var composer = h('div.composer')
47 var container = h('div.container')
48
49 if (opts.messageText)
50 var textarea = h('textarea.compose', opts.messageText)
51 else
52 var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a message'})
53
54 var cancelBtn = h('button.btn', 'Cancel', {
55 onclick: function () {
56 var cancel
57 if (opts.updated) {
58 cancel = document.getElementById(opts.updated.substring(0,10))
59 var oldMessage = h('div.message__body', tools.markdown(opts.messageText))
60 cancel.parentNode.replaceChild(oldMessage, cancel)
61 oldMessage.parentNode.appendChild(buttons)
62 } else if (opts.branch) {
63 cancel = document.getElementById(opts.branch.substring(0,10))
64 cancel.parentNode.removeChild(cancel)
65 } else {
66 cancel = document.getElementById('composer')
67 cancel.parentNode.removeChild(cancel)
68 }
69 }
70
71 })
72
73 var initialButtons = h('span',
74 h('button.btn', 'Preview', {
75 onclick: function () {
76 if (textarea.value) {
77 var msg = {}
78 msg.value = {
79 "author": id,
80 "content": {
81 "type": opts.type
82 }
83 }
84
85 if (opts.root)
86 msg.value.content.root = opts.root
87 if (opts.original)
88 msg.value.content.original = opts.original
89 if (opts.updated)
90 msg.value.content.updated = opts.updated
91
92 msg.value.content.text = textarea.value
93 console.log(msg)
94
95 if (opts.type == 'post')
96 var header = tools.header(msg)
97 if (opts.type == 'update')
98 var header = h('div.timestamp', 'Edited: ', h('a', {href: msg.key}, human(new Date(msg.value.timestamp))))
99
100 var preview = h('div',
101 header,
102 h('div.message__content', tools.markdown(msg.value.content.text)),
103 h('button.btn', 'Publish', {
104 onclick: function () {
105 if (msg.value.content) {
106 sbot.publish(msg.value.content, function (err, msg) {
107 if(err) throw err
108 console.log('Published!', msg)
109 if (opts.type == 'update') {
110 var originalMessage = document.getElementById(opts.updated.substring(0,10))
111 console.log(originalMessage)
112 opts.messageText = msg.value.content.text
113 var newMessage = h('div.message__body',
114 h('div.timestamp', 'Edited: ', h('a', {href: msg.key}, human(new Date(msg.value.timestamp)))),
115 h('div', tools.markdown(msg.value.content.text))
116 )
117 originalMessage.parentNode.replaceChild(newMessage, originalMessage)
118 newMessage.parentNode.appendChild(buttons)
119 } else {
120 if (opts.branch)
121 cancel = document.getElementById(opts.branch.substring(0,10))
122 else
123 cancel = document.getElementById('composer')
124 cancel.parentNode.removeChild(cancel)
125 }
126 })
127 }
128 }
129 }),
130 h('button.btn', 'Cancel', {
131 onclick: function () {
132 composer.replaceChild(container, composer.firstChild)
133 container.appendChild(textarea)
134 container.appendChild(initialButtons)
135 }
136 })
137 )
138 composer.replaceChild(preview, composer.firstChild)
139 }
140 }
141 }),
142 file_input(function (file) {
143 files.push(file)
144 filesById[file.link] = file
145 var embed = file.type.indexOf('image/') === 0 ? '!' : ''
146 textarea.value += embed + '['+file.name+']('+file.link+')'
147 }),
148 cancelBtn
149 )
150
151 composer.appendChild(container)
152 container.appendChild(textarea)
153 container.appendChild(initialButtons)
154
155 return composer
156}
157
158

Built with git-ssb-web