git ssb

16+

Dominic / patchbay



Tree: 9429dd07ea740e2d002c966affa5cdaccc0d73f3

Files: 9429dd07ea740e2d002c966affa5cdaccc0d73f3 / modules_basic / compose.js

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

Built with git-ssb-web