git ssb

1+

gb / ssblist



Tree: 285367a3b350be6649ad98e8eaa32e96912ebc51

Files: 285367a3b350be6649ad98e8eaa32e96912ebc51 / compose.js

6751 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 if (opts.boostAuthor) {
49 var boostName = avatar.cachedName(opts.boostAuthor)
50 }
51 if (opts.boostContent) {
52 var textarea = h('textarea.compose')
53 var str = opts.boostContent
54 var lines = str.split("\n")
55 for(var i=0; i<lines.length; i++) {
56 lines[i] = "> " + lines[i]
57 }
58 var newContent = lines.join("\n")
59 var content = 'Boosting: ' + opts.boostKey + '\n\n' + newContent + '-[' + boostName.textContent + ']('+ opts.boostAuthor + ')'
60 textarea.value = content
61 }
62
63 else if (opts.mentions) {
64 var textarea = h('textarea.compose', opts.mentions)
65 }
66
67 else if (opts.type == 'wiki')
68 var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a wiki (anyone can edit)'})
69 else if (opts.type == 'post')
70 var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a message (only you can edit)'})
71 else
72 var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a message (only you can edit)'}, fallback.messageText)
73
74 var cancelBtn = h('button.btn', 'Cancel', {
75 onclick: function () {
76 var cancel
77 console.log(opts)
78
79 if (opts.type == 'edit') {
80 cancel = document.getElementById('edit:' + opts.branch.substring(0,44))
81 var oldMessage = h('div.message__body', tools.markdown(fallback.messageText))
82 cancel.parentNode.replaceChild(oldMessage, cancel)
83 oldMessage.parentNode.appendChild(fallback.buttons)
84 } else if (opts.branch) {
85 //cancel reply composer
86 cancel = document.getElementById('re:' + opts.branch.substring(0,44))
87 cancel.parentNode.removeChild(cancel)
88 message = document.getElementById(opts.branch.substring(0,44))
89 message.appendChild(fallback.buttons)
90 } else {
91 // cancel generic composer
92 cancel = document.getElementById('composer')
93 cancel.parentNode.removeChild(cancel)
94 }
95 }
96
97 })
98
99 var initialButtons = h('span',
100 h('button.btn', 'Preview', {
101 onclick: function () {
102 if (textarea.value) {
103 var msg = {}
104
105 msg.value = {
106 "author": id,
107 "content": opts
108 }
109
110 msg.value.content.text = textarea.value
111 msg.value.content.mentions = mentions(textarea.value).map(
112 function (mention) {
113 var file = filesById[mention.link]
114 if (file) {
115 if (file.type) mention.type = file.type
116 if (file.size) mention.size = file.size
117 }
118 return mention
119 }
120 )
121
122 if (opts.recps)
123 msg.value.private = true
124
125 console.log(msg)
126 if (opts.type == 'post' || opts.type == 'wiki')
127 var header = tools.header(msg)
128 if (opts.type == 'update')
129 var header = tools.timestamp(msg, {edited: true})
130
131 var preview = h('div',
132 header,
133 h('div.message__content', tools.markdown(msg.value.content.text)),
134 h('button.btn', 'Publish', {
135 onclick: function () {
136 if (msg.value.content) {
137 sbot.publish(msg.value.content, function (err, msg) {
138 if(err) throw err
139 console.log('Published!', msg)
140
141 if (opts.label) {
142 opts.label.link = msg.key
143 sbot.publish(opts.label, function (err, labelmsg) {
144
145 if(err) throw err
146 console.log('Published!', labelmsg)
147 })
148 }
149 if (opts.type == 'edit') {
150 var message = document.getElementById(opts.branch.substring(0,44))
151 fallback.messageText = msg.value.content.text
152 var editBody = h('div.message__body',
153 tools.timestamp(msg, {edited: true}),
154 h('div', tools.markdown(msg.value.content.text))
155 )
156
157 message.replaceChild(editBody, message.childNodes[message.childNodes.length - 1])
158 editBody.parentNode.appendChild(fallback.buttons)
159 } else {
160 if (opts.branch)
161 cancel = document.getElementById('re:' + opts.branch.substring(0,44))
162 else
163 cancel = document.getElementById('composer')
164 cancel.parentNode.removeChild(cancel)
165 }
166 })
167 }
168 }
169 }),
170 h('button.btn', 'Cancel', {
171 onclick: function () {
172 composer.replaceChild(container, composer.firstChild)
173 container.appendChild(textarea)
174 container.appendChild(initialButtons)
175 }
176 })
177 )
178 composer.replaceChild(preview, composer.firstChild)
179 }
180 }
181 }),
182 file_input(function (file) {
183 files.push(file)
184 filesById[file.link] = file
185 var embed = file.type.indexOf('image/') === 0 ? '!' : ''
186 textarea.value += embed + '['+file.name+']('+file.link+')'
187 }),
188 cancelBtn
189 )
190
191 composer.appendChild(container)
192 container.appendChild(textarea)
193 container.appendChild(initialButtons)
194
195 return composer
196}
197
198

Built with git-ssb-web