var h = require('hyperscript') var pull = require('pull-stream') var sbot = require('./scuttlebot') var human = require('human-time') var id = require('./keys').id var tools = require('./tools') var mime = require('simple-mime')('application/octect-stream') var split = require('split-buffer') var route = require('./views') function file_input (onAdded) { return h('label.btn', 'Upload file', h('input', { type: 'file', hidden: true, onchange: function (ev) { var file = ev.target.files[0] if (!file) return var reader = new FileReader() reader.onload = function () { pull( pull.values(split(new Buffer(reader.result), 64*1024)), sbot.addblob(function (err, blob) { if(err) return console.error(err) onAdded({ link: blob, name: file.name, size: reader.result.length || reader.result.byteLength, type: mime(file.name) }) }) ) } reader.readAsArrayBuffer(file) } })) } module.exports = function (opts, buttons) { var files = [] var filesById = {} var composer = h('div.composer') var container = h('div.container') if (opts.messageText) var textarea = h('textarea.compose', opts.messageText) else var textarea = h('textarea.compose', {placeholder: opts.placeholder || 'Write a message'}) if (window.location.hash.substring(1) != 'compose') var cancelBtn = h('button.btn', 'Cancel', { onclick: function () { var cancel if (opts.updated) { cancel = document.getElementById(opts.updated.substring(0,10)) var oldMessage = h('div.message__body', tools.markdown(opts.messageText)) cancel.parentNode.replaceChild(oldMessage, cancel) oldMessage.parentNode.appendChild(buttons) } else if (opts.branch) { cancel = document.getElementById(opts.branch.substring(0,10)) cancel.parentNode.removeChild(cancel) } else { cancel = document.getElementById('composer') cancel.parentNode.removeChild(cancel) } } }) else var cancel = h('span', '') var initialButtons = h('span', h('button.btn', 'Preview', { onclick: function () { var msg = {} msg.value = { "author": id, "content": { "type": opts.type } } if (opts.root) msg.value.content.root = opts.root if (opts.original) msg.value.content.original = opts.original if (opts.updated) msg.value.content.updated = opts.updated msg.value.content.text = textarea.value console.log(msg) if (opts.type == 'post') var header = tools.header(msg) if (opts.type == 'update') var header = h('div.timestamp', 'Edited: ', h('a', {href: msg.key}, human(new Date(msg.value.timestamp)))) var preview = h('div', header, h('div.message__content', tools.markdown(msg.value.content.text)), h('button.btn', 'Publish', { onclick: function () { sbot.publish(msg.value.content, function (err, msg) { if(err) throw err console.log('Published!', msg) if (opts.type == 'update') { var originalMessage = document.getElementById(opts.updated.substring(0,10)) console.log(originalMessage) opts.messageText = msg.value.content.text var newMessage = h('div.message__body', h('div.timestamp', 'Edited: ', h('a', {href: msg.key}, human(new Date(msg.value.timestamp)))), h('div', tools.markdown(msg.value.content.text)) ) originalMessage.parentNode.replaceChild(newMessage, originalMessage) newMessage.parentNode.appendChild(buttons) } }) } }), h('button.btn', 'Cancel', { onclick: function () { composer.replaceChild(container, composer.firstChild) container.appendChild(textarea) container.appendChild(initialButtons) } }) ) composer.replaceChild(preview, composer.firstChild) } }), file_input(function (file) { files.push(file) filesById[file.link] = file var embed = file.type.indexOf('image/') === 0 ? '!' : '' textarea.value += embed + '['+file.name+']('+file.link+')' }), cancelBtn ) composer.appendChild(container) container.appendChild(textarea) container.appendChild(initialButtons) return composer }