git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: 0655463e071d13cbb89dbb6968727e21229414e7

Files: 0655463e071d13cbb89dbb6968727e21229414e7 / compose.js

6369 bytesRaw
1var h = require('hyperscript')
2var pull = require('pull-stream')
3var sbot = require('./scuttlebot')
4var human = require('human-time')
5var id = require('./keys').id
6var mentions = require('ssb-mentions')
7
8var avatar = require('./avatar')
9var tools = require('./tools')
10
11var mime = require('simple-mime')('application/octect-stream')
12var split = require('split-buffer')
13
14var route = require('./views')
15
16function file_input (onAdded) {
17 return h('label.btn', 'Upload file',
18 h('input', { type: 'file', hidden: true,
19 onchange: function (ev) {
20 var file = ev.target.files[0]
21 if (!file) return
22 var reader = new FileReader()
23 reader.onload = function () {
24 pull(
25 pull.values(split(new Buffer(reader.result), 64*1024)),
26 sbot.addblob(function (err, blob) {
27 if(err) return console.error(err)
28 onAdded({
29 link: blob,
30 name: file.name,
31 size: reader.result.length || reader.result.byteLength,
32 type: mime(file.name)
33 })
34 })
35 )
36 }
37 reader.readAsArrayBuffer(file)
38 }
39 }))
40}
41
42module.exports = function (opts, fallback) {
43 var files = []
44 var filesById = {}
45
46 var composer = h('div.composer')
47 var container = h('div.container')
48 var boostName = avatar.cachedName(opts.boostAuthor)
49
50 if (opts.boostContent) {
51 var textarea = h('textarea.compose', 'Blah')
52 var str = opts.boostContent
53 var lines = str.split("\n")
54 for(var i=0; i<lines.length; i++) {
55 lines[i] = "> " + lines[i]
56 }
57 var newContent = lines.join("\n")
58 var content = 'Boosting: ' + opts.boostKey + ' by [' + boostName.textContent + ']('+ opts.boostAuthor + ')\n\n' + newContent
59 textarea.value = content
60 }
61
62 else if (opts.mentions) {
63 var textarea = h('textarea.compose', opts.mentions)
64 }
65
66 else if (opts.type == 'wiki')
67 var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a wiki (anyone can edit)'})
68 else if (opts.type == 'post')
69 var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a message (only you can edit)'})
70 else
71 var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a message (only you can edit)'}, fallback.messageText)
72
73 var cancelBtn = h('button.btn', 'Cancel', {
74 onclick: function () {
75 var cancel
76 console.log(opts)
77
78 if (opts.type == 'edit') {
79 cancel = document.getElementById('edit:' + opts.branch.substring(0,44))
80 var oldMessage = h('div.message__body', tools.markdown(fallback.messageText))
81 cancel.parentNode.replaceChild(oldMessage, cancel)
82 oldMessage.parentNode.appendChild(fallback.buttons)
83 } else if (opts.branch) {
84 //cancel reply composer
85 cancel = document.getElementById('re:' + opts.branch.substring(0,44))
86 cancel.parentNode.removeChild(cancel)
87 message = document.getElementById(opts.branch.substring(0,44))
88 message.appendChild(fallback.buttons)
89 } else {
90 // cancel generic composer
91 cancel = document.getElementById('composer')
92 cancel.parentNode.removeChild(cancel)
93 }
94 }
95
96 })
97
98 var initialButtons = h('span',
99 h('button.btn', 'Preview', {
100 onclick: function () {
101 if (textarea.value) {
102 var msg = {}
103
104 msg.value = {
105 "author": id,
106 "content": opts
107 }
108
109 msg.value.content.text = textarea.value
110 msg.value.content.mentions = mentions(textarea.value).map(
111 function (mention) {
112 var file = filesById[mention.link]
113 if (file) {
114 if (file.type) mention.type = file.type
115 if (file.size) mention.size = file.size
116 }
117 return mention
118 }
119 )
120
121 if (opts.recps)
122 msg.value.private = true
123
124 console.log(msg)
125 if (opts.type == 'post' || opts.type == 'wiki')
126 var header = tools.header(msg)
127 if (opts.type == 'update')
128 var header = tools.timestamp(msg, {edited: true})
129 var preview = h('div',
130 header,
131 h('div.message__content', tools.markdown(msg.value.content.text)),
132 h('button.btn', 'Publish', {
133 onclick: function () {
134 if (msg.value.content) {
135 sbot.publish(msg.value.content, function (err, msg) {
136 if(err) throw err
137 console.log('Published!', msg)
138 if (opts.type == 'edit') {
139 var message = document.getElementById(opts.branch.substring(0,44))
140 fallback.messageText = msg.value.content.text
141 var editBody = h('div.message__body',
142 tools.timestamp(msg, {edited: true}),
143 h('div', tools.markdown(msg.value.content.text))
144 )
145
146 message.replaceChild(editBody, message.childNodes[message.childNodes.length - 1])
147 editBody.parentNode.appendChild(fallback.buttons)
148 } else {
149 if (opts.branch)
150 cancel = document.getElementById('re:' + opts.branch.substring(0,44))
151 else
152 cancel = document.getElementById('composer')
153 cancel.parentNode.removeChild(cancel)
154 }
155 })
156 }
157 }
158 }),
159 h('button.btn', 'Cancel', {
160 onclick: function () {
161 composer.replaceChild(container, composer.firstChild)
162 container.appendChild(textarea)
163 container.appendChild(initialButtons)
164 }
165 })
166 )
167 composer.replaceChild(preview, composer.firstChild)
168 }
169 }
170 }),
171 file_input(function (file) {
172 files.push(file)
173 filesById[file.link] = file
174 var embed = file.type.indexOf('image/') === 0 ? '!' : ''
175 textarea.value += embed + '['+file.name+']('+file.link+')'
176 }),
177 cancelBtn
178 )
179
180 composer.appendChild(container)
181 container.appendChild(textarea)
182 container.appendChild(initialButtons)
183
184 return composer
185}
186
187

Built with git-ssb-web