git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: c9880ee4fe3c93b8f7cb8c05078abebb502e097c

Files: c9880ee4fe3c93b8f7cb8c05078abebb502e097c / compose.js

5009 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
94 if (opts.type == 'post')
95 var header = tools.header(msg)
96 if (opts.type == 'update')
97 var header = tools.timestamp(msg, {edited: true})
98 var preview = h('div',
99 header,
100 h('div.message__content', tools.markdown(msg.value.content.text)),
101 h('button.btn', 'Publish', {
102 onclick: function () {
103 if (msg.value.content) {
104 sbot.publish(msg.value.content, function (err, msg) {
105 if(err) throw err
106 console.log('Published!', msg)
107 if (opts.type == 'update') {
108 var originalMessage = document.getElementById(opts.updated.substring(0,10))
109 opts.messageText = msg.value.content.text
110 var newMessage = h('div.message__body',
111 tools.timestamp(msg, {edited: true}),
112 h('div', tools.markdown(msg.value.content.text))
113 )
114 originalMessage.parentNode.replaceChild(newMessage, originalMessage)
115 newMessage.parentNode.appendChild(buttons)
116 } else {
117 if (opts.branch)
118 cancel = document.getElementById(opts.branch.substring(0,10))
119 else
120 cancel = document.getElementById('composer')
121 cancel.parentNode.removeChild(cancel)
122 }
123 })
124 }
125 }
126 }),
127 h('button.btn', 'Cancel', {
128 onclick: function () {
129 composer.replaceChild(container, composer.firstChild)
130 container.appendChild(textarea)
131 container.appendChild(initialButtons)
132 }
133 })
134 )
135 composer.replaceChild(preview, composer.firstChild)
136 }
137 }
138 }),
139 file_input(function (file) {
140 files.push(file)
141 filesById[file.link] = file
142 var embed = file.type.indexOf('image/') === 0 ? '!' : ''
143 textarea.value += embed + '['+file.name+']('+file.link+')'
144 }),
145 cancelBtn
146 )
147
148 composer.appendChild(container)
149 container.appendChild(textarea)
150 container.appendChild(initialButtons)
151
152 return composer
153}
154
155

Built with git-ssb-web