git ssb

2+

ev / mvd



Tree: b0af4a5b196f6a3c00d7b9e44537ccdf1248230e

Files: b0af4a5b196f6a3c00d7b9e44537ccdf1248230e / compose.js

4976 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, fallback) {
41 var files = []
42 var filesById = {}
43
44 var composer = h('div.composer')
45 var container = h('div.container')
46
47 if (opts.type == 'post')
48 var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a message'})
49 else
50 var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a message'}, fallback.messageText)
51
52 var cancelBtn = h('button.btn', 'Cancel', {
53 onclick: function () {
54 var cancel
55 console.log(opts)
56 if (opts.type == 'edit') {
57 cancel = document.getElementById('edit:' + opts.branch.substring(0,44))
58 var oldMessage = h('div.message__body', tools.markdown(fallback.messageText))
59 cancel.parentNode.replaceChild(oldMessage, cancel)
60 oldMessage.parentNode.appendChild(fallback.buttons)
61 } else if (opts.branch) {
62 //cancel reply composer
63 cancel = document.getElementById('re:' + opts.branch.substring(0,44))
64 cancel.parentNode.removeChild(cancel)
65 } else {
66 // cancel generic composer
67 cancel = document.getElementById('composer')
68 cancel.parentNode.removeChild(cancel)
69 }
70 }
71
72 })
73
74 var initialButtons = h('span',
75 h('button.btn', 'Preview', {
76 onclick: function () {
77 if (textarea.value) {
78 var msg = {}
79
80 msg.value = {
81 "author": id,
82 "content": opts
83 }
84
85 msg.value.content.text = textarea.value
86 console.log(msg)
87 if (opts.type == 'post')
88 var header = tools.header(msg)
89 if (opts.type == 'update')
90 var header = tools.timestamp(msg, {edited: true})
91 var preview = h('div',
92 header,
93 h('div.message__content', tools.markdown(msg.value.content.text)),
94 h('button.btn', 'Publish', {
95 onclick: function () {
96 if (msg.value.content) {
97 sbot.publish(msg.value.content, function (err, msg) {
98 if(err) throw err
99 console.log('Published!', msg)
100 if (opts.type == 'edit') {
101 var message = document.getElementById(opts.branch.substring(0,44))
102 fallback.messageText = msg.value.content.text
103 var editBody = h('div.message__body',
104 tools.timestamp(msg, {edited: true}),
105 h('div', tools.markdown(msg.value.content.text))
106 )
107
108 message.replaceChild(editBody, message.childNodes[message.childNodes.length - 1])
109 editBody.parentNode.appendChild(fallback.buttons)
110 } else {
111 if (opts.branch)
112 cancel = document.getElementById('re:' + opts.branch.substring(0,44))
113 else
114 cancel = document.getElementById('composer')
115 cancel.parentNode.removeChild(cancel)
116 }
117 })
118 }
119 }
120 }),
121 h('button.btn', 'Cancel', {
122 onclick: function () {
123 composer.replaceChild(container, composer.firstChild)
124 container.appendChild(textarea)
125 container.appendChild(initialButtons)
126 }
127 })
128 )
129 composer.replaceChild(preview, composer.firstChild)
130 }
131 }
132 }),
133 file_input(function (file) {
134 files.push(file)
135 filesById[file.link] = file
136 var embed = file.type.indexOf('image/') === 0 ? '!' : ''
137 textarea.value += embed + '['+file.name+']('+file.link+')'
138 }),
139 cancelBtn
140 )
141
142 composer.appendChild(container)
143 container.appendChild(textarea)
144 container.appendChild(initialButtons)
145
146 return composer
147}
148
149

Built with git-ssb-web