git ssb

2+

ev / mvd



Tree: 9de4a12dc04dd73d6845202b57055758655aba3a

Files: 9de4a12dc04dd73d6845202b57055758655aba3a / compose.js

4047 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 if (window.location.hash.substring(1) != 'compose')
51 var cancelBtn = h('button.btn', 'Cancel', {
52 onclick: function () {
53 var cancel
54 if (opts.updated) {
55 cancel = document.getElementById(opts.updated.substring(0,10))
56 var oldMessage = h('div.message__body', tools.markdown(opts.messageText))
57 cancel.parentNode.replaceChild(oldMessage, cancel)
58 console.log(opts.buttons)
59 oldMessage.parentNode.appendChild(opts.buttons)
60 } else if (opts.branch) {
61 cancel = document.getElementById(opts.branch.substring(0,10))
62 cancel.parentNode.removeChild(cancel)
63 }
64 }
65
66 })
67
68 else
69 var cancel = h('span', '')
70
71 var initialButtons = h('span',
72 h('button.btn', 'Preview', {
73 onclick: function () {
74
75 var msg = {}
76 msg.value = {
77 "author": id,
78 "content": {
79 "type": opts.type
80 }
81 }
82
83 if (opts.root)
84 msg.value.content.root = opts.root
85 if (opts.original)
86 msg.value.content.original = opts.original
87 if (opts.updated)
88 msg.value.content.updated = opts.updated
89
90 msg.value.content.text = textarea.value
91 console.log(msg)
92
93 if (opts.type == 'post')
94 var header = tools.header(msg)
95 if (opts.type == 'update')
96 var header = h('div.timestamp', 'Edited:', h('a', {href: msg.key}, human(new Date(msg.value.timestamp))))
97
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 sbot.publish(msg.value.content, function (err, msg) {
104 if(err) throw err
105 console.log('Published!', msg)
106 window.location.reload()
107 if(cb) cb(err, msg)
108 })
109 }
110 }),
111 h('button.btn', 'Cancel', {
112 onclick: function () {
113 composer.replaceChild(container, composer.firstChild)
114 container.appendChild(textarea)
115 container.appendChild(initialButtons)
116 }
117 })
118 )
119 composer.replaceChild(preview, composer.firstChild)
120 }
121 }),
122 file_input(function (file) {
123 files.push(file)
124 filesById[file.link] = file
125 var embed = file.type.indexOf('image/') === 0 ? '!' : ''
126 textarea.value += embed + '['+file.name+']('+file.link+')'
127 }),
128 cancelBtn
129 )
130
131 composer.appendChild(container)
132 container.appendChild(textarea)
133 container.appendChild(initialButtons)
134
135 return composer
136}
137
138

Built with git-ssb-web