git ssb

2+

ev / mvd



Tree: 3bf3c7aca4c5ebf780ce5daee70c769f82f5ca26

Files: 3bf3c7aca4c5ebf780ce5daee70c769f82f5ca26 / compose.js

3611 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
12function 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
38module.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.value.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 if (opts.type == 'post')
73 var header = tools.header(msg)
74 if (opts.type == 'update')
75 var header = h('div.timestamp', 'Edited:', h('a', {href: msg.key}, human(new Date(msg.value.timestamp))))
76
77 var preview = h('div',
78 header,
79 h('div.message__content', tools.markdown(msg.value.content.text)),
80 h('button.btn', 'Publish', {
81 onclick: function () {
82 sbot.publish(msg.value.content, function (err, msg) {
83 if(err) throw err
84 console.log('Published!', msg)
85 window.location.reload()
86 if(cb) cb(err, msg)
87 })
88 }
89 }),
90 h('button.btn', 'Cancel', {
91 onclick: function () {
92 composer.replaceChild(container, composer.firstChild)
93 container.appendChild(textarea)
94 container.appendChild(initialButtons)
95 }
96 })
97 )
98 composer.replaceChild(preview, composer.firstChild)
99 }
100 }),
101 file_input(function (file) {
102 files.push(file)
103 filesById[file.link] = file
104 var embed = file.type.indexOf('image/') === 0 ? '!' : ''
105 textarea.value += embed + '['+file.name+']('+file.link+')'
106 }),
107 h('button.btn', 'Cancel', {
108 onclick: function () {
109 var message = document.getElementById(opts.branch.substring(0,10))
110 if (opts.updated)
111 message.parentNode.removeChild(message)
112 else
113 message.parentNode.removeChild(message)
114 }
115 })
116 )
117
118 composer.appendChild(container)
119 container.appendChild(textarea)
120 container.appendChild(initialButtons)
121
122 return composer
123}
124
125

Built with git-ssb-web