git ssb

16+

Dominic / patchbay



Tree: f93ceefd06a4ef69e3af1d2fb39bc0fed84c2c7a

Files: f93ceefd06a4ef69e3af1d2fb39bc0fed84c2c7a / modules_basic / compose.js

4255 bytesRaw
1'use strict'
2const fs = require('fs')
3const h = require('../h')
4const u = require('../util')
5const suggest = require('suggest-box')
6const mentions = require('ssb-mentions')
7const cont = require('cont')
8
9//var plugs = require('../plugs')
10//var suggest_mentions= plugs.asyncConcat(exports.suggest_mentions = [])
11//var publish = plugs.first(exports.sbot_publish = [])
12//var message_content = plugs.first(exports.message_content = [])
13//var message_confirm = plugs.first(exports.message_confirm = [])
14//var file_input = plugs.first(exports.file_input = [])
15
16exports.needs = {
17 suggest_mentions: 'map', //<-- THIS MUST BE REWRITTEN
18 publish: 'first',
19 message_content: 'first',
20 message_confirm: 'first',
21 file_input: 'first'
22}
23
24exports.gives = {
25 'message_compose': true,
26 'mcss': true
27}
28
29exports.create = function (api) {
30 return {
31 message_compose,
32 mcss: () => fs.readFileSync(__filename.replace(/js$/, 'mcss'), 'utf8')
33 }
34
35 /*
36 opts can take
37
38 placeholder: string. placeholder text, defaults to "Write a message"
39 prepublish: function. called before publishing a message.
40 shrink: boolean. set to false, to make composer not shrink (or hide controls) when unfocused.
41 */
42
43 function message_compose (meta = {}, opts = {}, cb) {
44 if(!meta.type) throw new Error('message must have type')
45
46 if('function' === typeof cb) {
47 if('function' === typeof opts) {
48 opts = {prepublish: opts}
49 }
50 }
51 opts.prepublish = opts.prepublish || id
52
53 var actions
54
55 var textArea = h('textarea', {
56 placeholder: opts.placeholder || 'Write a message'
57 })
58
59 if(opts.shrink !== false) {
60 var blur
61 textArea.addEventListener('focus', () => {
62 clearTimeout(blur)
63 if(!textArea.value) {
64 composer.className = 'Compose -expanded'
65 }
66 })
67 textArea.addEventListener('blur', () => {
68 //don't shrink right away, so there is time
69 //to click the publish button.
70 clearTimeout(blur)
71 blur = setTimeout(() => {
72 if(textArea.value) return
73 composer.className = 'Compose -contracted'
74 }, 300)
75 })
76 }
77
78 textArea.addEventListener('keydown', ev => {
79 if(ev.keyCode === 13 && ev.ctrlKey) publish()
80 })
81
82 var files = []
83 var filesById = {}
84
85 function publish() {
86 publishBtn.disabled = true
87 var content
88 try {
89 content = JSON.parse(textArea.value)
90 } catch (err) {
91 meta.text = textArea.value
92 meta.mentions = mentions(textArea.value).map(mention => {
93 // merge markdown-detected mention with file info
94 var file = filesById[mention.link]
95 if (file) {
96 if (file.type) mention.type = file.type
97 if (file.size) mention.size = file.size
98 }
99 return mention
100 })
101 try {
102 meta = opts.prepublish(meta)
103 } catch (err) {
104 publishBtn.disabled = false
105 if (cb) cb(err)
106 else alert(err.message)
107 }
108 return api.message_confirm(meta, done)
109 }
110
111 api.message_confirm(content, done)
112
113 function done (err, msg) {
114 publishBtn.disabled = false
115 if(err) return alert(err.stack)
116 else if (msg) textArea.value = ''
117
118 if (cb) cb(err, msg)
119 }
120 }
121
122 var fileInput = api.file_input(file => {
123 files.push(file)
124 filesById[file.link] = file
125
126 var embed = file.type.indexOf('image/') === 0 ? '!' : ''
127
128 textArea.value += embed + '['+file.name+']('+file.link+')'
129 composer.className = 'Compose -expanded'
130 console.log('added:', file)
131 })
132 var publishBtn = h('button', {'ev-click': publish}, 'Publish' )
133 var actions = h('section.actions', [
134 fileInput, publishBtn
135 ])
136
137 var composer = h('Compose', {
138 className: opts.shrink === false ? '-expanded' : '-contracted'
139 }, [
140 textArea,
141 actions
142 ])
143
144 suggest(textArea, (name, cb) => {
145 cont.para(api.suggest_mentions(name))
146 ((err, ary) => {
147 cb(null, ary.reduce((a, b) => {
148 if(!b) return a
149 return a.concat(b)
150 }, []))
151 })
152 }, {})
153
154 return composer
155 }
156
157}
158
159function id (e) { return e }
160
161

Built with git-ssb-web