git ssb

16+

Dominic / patchbay



Commit 2445f7f6c568d24593d20f150be2fa3a18c84c58

refactor compose to be modular

mix irving committed on 1/7/2017, 11:58:31 PM
Parent: 0472f8a3c5d13f1555a5d894311fe2bfe060e75a

Files changed

modules_basic/compose.jschanged
modules_basic/compose.mcssadded
modules_basic/compose.jsView
@@ -1,11 +1,12 @@
11 'use strict'
2-var h = require('hyperscript')
3-var u = require('../util')
4-var suggest = require('suggest-box')
5-var mentions = require('ssb-mentions')
6-var lightbox = require('hyperlightbox')
7-var cont = require('cont')
2 +const fs = require('fs')
3 +const h = require('../h')
4 +const u = require('../util')
5 +const suggest = require('suggest-box')
6 +const mentions = require('ssb-mentions')
7 +const lightbox = require('hyperlightbox')
8 +const cont = require('cont')
89
910 //var plugs = require('../plugs')
1011 //var suggest_mentions= plugs.asyncConcat(exports.suggest_mentions = [])
1112 //var publish = plugs.first(exports.sbot_publish = [])
@@ -20,12 +21,13 @@
2021 message_confirm: 'first',
2122 file_input: 'first'
2223 }
2324
24-exports.gives = 'message_compose'
25 +exports.gives = {
26 + 'message_compose': true,
27 + 'mcss': true
28 +}
2529
26-function id (e) { return e }
27-
2830 /*
2931 opts can take
3032
3133 placeholder: string. placeholder text, defaults to "Write a message"
@@ -33,48 +35,54 @@
3335 shrink: boolean. set to false, to make composer not shrink (or hide controls) when unfocused.
3436 */
3537
3638 exports.create = function (api) {
39 + return {
40 + message_compose,
41 + mcss: () => fs.readFileSync(__filename.replace(/js$/, 'mcss'), 'utf8')
42 + }
3743
38- return function (meta, opts, cb) {
44 + function message_compose (meta, opts, cb) {
3945 if('function' === typeof cb) {
40- if('function' === typeof opts)
46 + if('function' === typeof opts) {
4147 opts = {prepublish: opts}
4248 }
49 + }
4350
4451 if(!opts) opts = {}
4552 opts.prepublish = opts.prepublish || id
4653
47- var accessories
54 + var actions
4855 meta = meta || {}
4956 if(!meta.type) throw new Error('message must have type')
50- var ta = h('textarea', {
57 +
58 + var textArea = h('textarea', {
5159 placeholder: opts.placeholder || 'Write a message',
5260 style: {height: opts.shrink === false ? '200px' : ''}
5361 })
5462
5563 if(opts.shrink !== false) {
5664 var blur
57- ta.addEventListener('focus', function () {
65 + textArea.addEventListener('focus', () => {
5866 clearTimeout(blur)
59- if(!ta.value) {
60- ta.style.height = '200px'
67 + if(!textArea.value) {
68 + textArea.style.height = '200px'
6169 }
62- accessories.style.display = 'block'
70 + actions.style.display = 'flex'
6371 })
64- ta.addEventListener('blur', function () {
72 + textArea.addEventListener('blur', () => {
6573 //don't shrink right away, so there is time
6674 //to click the publish button.
6775 clearTimeout(blur)
68- blur = setTimeout(function () {
69- if(ta.value) return
70- ta.style.height = '50px'
71- accessories.style.display = 'none'
76 + blur = setTimeout(() => {
77 + if(textArea.value) return
78 + textArea.style.height = '50px'
79 + actions.style.display = 'none'
7280 }, 200)
7381 })
7482 }
7583
76- ta.addEventListener('keydown', function (ev) {
84 + textArea.addEventListener('keydown', ev => {
7785 if(ev.keyCode === 13 && ev.ctrlKey) publish()
7886 })
7987
8088 var files = []
@@ -83,12 +91,12 @@
8391 function publish() {
8492 publishBtn.disabled = true
8593 var content
8694 try {
87- content = JSON.parse(ta.value)
95 + content = JSON.parse(textArea.value)
8896 } catch (err) {
89- meta.text = ta.value
90- meta.mentions = mentions(ta.value).map(function (mention) {
97 + meta.text = textArea.value
98 + meta.mentions = mentions(textArea.value).map(mention => {
9199 // merge markdown-detected mention with file info
92100 var file = filesById[mention.link]
93101 if (file) {
94102 if (file.type) mention.type = file.type
@@ -110,46 +118,49 @@
110118
111119 function done (err, msg) {
112120 publishBtn.disabled = false
113121 if(err) return alert(err.stack)
114- else if (msg) ta.value = ''
122 + else if (msg) textArea.value = ''
115123
116124 if (cb) cb(err, msg)
117125 }
118126 }
119127
128 + var fileInput = api.file_input(file => {
129 + files.push(file)
130 + filesById[file.link] = file
120131
121- var publishBtn = h('button', 'Publish', {onclick: publish})
122- var composer =
123- h('div.compose', h('div.column', ta,
124- accessories = h('div.row.compose__controls',
125- //hidden until you focus the textarea
126- {style: {display: opts.shrink === false ? '' : 'none'}},
127- api.file_input(function (file) {
128- files.push(file)
129- filesById[file.link] = file
132 + var embed = file.type.indexOf('image/') === 0 ? '!' : ''
133 + textArea.value += embed + '['+file.name+']('+file.link+')'
134 + console.log('added:', file)
135 + })
136 + var publishBtn = h('button', {'ev-click': publish}, 'Publish' )
137 + var actions = h('section.actions',
138 + //hidden until you focus the textarea
139 + { style: {display: opts.shrink === false ? '' : ''} },
140 + // revert
141 + // { style: {display: opts.shrink === false ? '' : 'none'} },
142 + [ fileInput, publishBtn ]
143 + )
130144
131- var embed = file.type.indexOf('image/') === 0 ? '!' : ''
132- ta.value += embed + '['+file.name+']('+file.link+')'
133- console.log('added:', file)
134- }),
135- publishBtn)
136- )
137- )
145 + var composer = h('Compose', [
146 + textArea,
147 + actions
148 + ])
138149
139- suggest(ta, function (name, cb) {
150 + suggest(textArea, (name, cb) => {
140151 cont.para(api.suggest_mentions(name))
141- (function (err, ary) {
142- cb(null, ary.reduce(function (a, b) {
152 + ((err, ary) => {
153 + cb(null, ary.reduce((a, b) => {
143154 if(!b) return a
144155 return a.concat(b)
145156 }, []))
146157 })
147158 }, {})
148159
149160 return composer
150-
151161 }
152162
153163 }
154164
165 +function id (e) { return e }
155166
modules_basic/compose.mcssView
@@ -1,0 +1,63 @@
1 +Compose {
2 + display: flex
3 + flex-direction: column
4 +
5 + padding: 1rem .5rem
6 +
7 + textarea {
8 + border: 1px solid gainsboro
9 + }
10 +
11 + section.actions {
12 + display: flex
13 + flex-direction: row
14 + align-items: baseline
15 +
16 + input[type="file"] {
17 + flex-grow: 1
18 +
19 + width: 5.5rem
20 + color: transparent
21 +
22 + ::-webkit-file-upload-button {
23 + visibility: hidden
24 + }
25 +
26 + ::before {
27 + content: 'attach file'
28 +
29 + background: #fff
30 + color: #666
31 + padding: .5rem
32 + margin: .5rem 0
33 + cursor: pointer
34 + text-transform: uppercase
35 + font-size: .7rem
36 +
37 + outline: none
38 + white-space: nowrap
39 + -webkit-user-select: none
40 + }
41 +
42 + :active, :focus {
43 + outline: none
44 + box-shadow: none
45 + }
46 + }
47 +
48 + button {
49 + background: #fff
50 + color: #666
51 + border: 1px solid #bbb
52 + border-radius: .5rem
53 + padding: .5rem
54 + margin: .5rem 0
55 + cursor: pointer
56 + text-transform: uppercase
57 + font-weight: bold
58 + font-size: .7rem
59 + }
60 + }
61 +}
62 +
63 +

Built with git-ssb-web