Something went wrong. Try again.
secure-scuttlebot classic
Something went wrong. Try again.
4.3 kB · 155 lines
JavaScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156'use strict'var h = require('hyperscript')var u = require('../util')var suggest = require('suggest-box')var mentions = require('ssb-mentions')var lightbox = require('hyperlightbox')var cont = require('cont')
//var plugs = require('../plugs')//var suggest_mentions= plugs.asyncConcat(exports.suggest_mentions = [])//var publish = plugs.first(exports.sbot_publish = [])//var message_content = plugs.first(exports.message_content = [])//var message_confirm = plugs.first(exports.message_confirm = [])//var file_input = plugs.first(exports.file_input = [])
exports.needs = { suggest_mentions: 'map', //<-- THIS MUST BE REWRITTEN publish: 'first', message_content: 'first', message_confirm: 'first', file_input: 'first'}
exports.gives = 'message_compose'
function id (e) { return e }
/* opts can take
placeholder: string. placeholder text, defaults to "Write a message" prepublish: function. called before publishing a message. shrink: boolean. set to false, to make composer not shrink (or hide controls) when unfocused.*/
exports.create = function (api) {
return function (meta, opts, cb) { if('function' === typeof cb) { if('function' === typeof opts) opts = {prepublish: opts} }
if(!opts) opts = {} opts.prepublish = opts.prepublish || id
var accessories meta = meta || {} if(!meta.type) throw new Error('message must have type') var ta = h('textarea', { placeholder: opts.placeholder || 'Write a message', style: {height: opts.shrink === false ? '200px' : ''} })
if(opts.shrink !== false) { var blur ta.addEventListener('focus', function () { clearTimeout(blur) if(!ta.value) { ta.style.height = '200px' } accessories.style.display = 'block' }) ta.addEventListener('blur', function () { //don't shrink right away, so there is time //to click the publish button. clearTimeout(blur) blur = setTimeout(function () { if(ta.value) return ta.style.height = '50px' accessories.style.display = 'none' }, 200) }) }
ta.addEventListener('keydown', function (ev) { if(ev.keyCode === 13 && ev.ctrlKey) publish() })
var files = [] var filesById = {}
function publish() { publishBtn.disabled = true var content try { content = JSON.parse(ta.value) } catch (err) { meta.text = ta.value meta.mentions = mentions(ta.value).map(function (mention) { // merge markdown-detected mention with file info var file = filesById[mention.link] if (file) { if (file.type) mention.type = file.type if (file.size) mention.size = file.size } return mention }) try { meta = opts.prepublish(meta) } catch (err) { publishBtn.disabled = false if (cb) cb(err) else alert(err.message) } return api.message_confirm(meta, done) }
api.message_confirm(content, done)
function done (err, msg) { publishBtn.disabled = false if(err) return alert(err.stack) else if (msg) ta.value = ''
if (cb) cb(err, msg) } }
var publishBtn = h('button', 'Publish', {onclick: publish}) var composer = h('div.compose', h('div.column', ta, accessories = h('div.row.compose__controls', //hidden until you focus the textarea {style: {display: opts.shrink === false ? '' : 'none'}}, api.file_input(function (file) { files.push(file) filesById[file.link] = file
var embed = file.type.indexOf('image/') === 0 ? '!' : '' ta.value += embed + '['+file.name+']('+file.link+')' console.log('added:', file) }), publishBtn) ) )
suggest(ta, function (name, cb) { cont.para(api.suggest_mentions(name)) (function (err, ary) { cb(null, ary.reduce(function (a, b) { if(!b) return a return a.concat(b) }, [])) }) }, {})
return composer
}
}