git ssb

16+

Dominic / patchbay



Commit abc981ed003091aaeb68aab6d203e6356ea262a8

Merge pull request #68 from ssbc/compose_refactor

Compose refactor
mix irving authored on 1/11/2017, 2:56:12 AM
GitHub committed on 1/11/2017, 2:56:12 AM
Parent: 93d26ea7530a862ffda0f98070f6a1ce09d1d616
Parent: aec0d21aea07a4b1eeafa9c46b81f7a79beb6bbd

Files changed

modules_basic/compose.jschanged
modules_basic/compose.mcssadded
modules_core/style-mixins.jschanged
style.csschanged
modules_basic/compose.jsView
@@ -1,11 +1,11 @@
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 cont = require('cont')
88
99 //var plugs = require('../plugs')
1010 //var suggest_mentions= plugs.asyncConcat(exports.suggest_mentions = [])
1111 //var publish = plugs.first(exports.sbot_publish = [])
@@ -20,61 +20,63 @@
2020 message_confirm: 'first',
2121 file_input: 'first'
2222 }
2323
24-exports.gives = 'message_compose'
24 +exports.gives = {
25 + 'message_compose': true,
26 + 'mcss': true
27 +}
2528
26-function id (e) { return e }
29 +exports.create = function (api) {
30 + return {
31 + message_compose,
32 + mcss: () => fs.readFileSync(__filename.replace(/js$/, 'mcss'), 'utf8')
33 + }
2734
28-/*
29- opts can take
35 + /*
36 + opts can take
3037
31- placeholder: string. placeholder text, defaults to "Write a message"
32- prepublish: function. called before publishing a message.
33- shrink: boolean. set to false, to make composer not shrink (or hide controls) when unfocused.
34-*/
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 + */
3542
36-exports.create = function (api) {
43 + function message_compose (meta = {}, opts = {}, cb) {
44 + if(!meta.type) throw new Error('message must have type')
3745
38- return function (meta, opts, cb) {
3946 if('function' === typeof cb) {
40- if('function' === typeof opts)
47 + if('function' === typeof opts) {
4148 opts = {prepublish: opts}
4249 }
43-
44- if(!opts) opts = {}
50 + }
4551 opts.prepublish = opts.prepublish || id
4652
47- var accessories
48- meta = meta || {}
49- if(!meta.type) throw new Error('message must have type')
50- var ta = h('textarea', {
51- placeholder: opts.placeholder || 'Write a message',
52- style: {height: opts.shrink === false ? '200px' : ''}
53 + var actions
54 +
55 + var textArea = h('textarea', {
56 + placeholder: opts.placeholder || 'Write a message'
5357 })
5458
5559 if(opts.shrink !== false) {
5660 var blur
57- ta.addEventListener('focus', function () {
61 + textArea.addEventListener('focus', () => {
5862 clearTimeout(blur)
59- if(!ta.value) {
60- ta.style.height = '200px'
63 + if(!textArea.value) {
64 + composer.className = 'Compose -expanded'
6165 }
62- accessories.style.display = 'block'
6366 })
64- ta.addEventListener('blur', function () {
67 + textArea.addEventListener('blur', () => {
6568 //don't shrink right away, so there is time
6669 //to click the publish button.
6770 clearTimeout(blur)
68- blur = setTimeout(function () {
69- if(ta.value) return
70- ta.style.height = '50px'
71- accessories.style.display = 'none'
72- }, 200)
71 + blur = setTimeout(() => {
72 + if(textArea.value) return
73 + composer.className = 'Compose -contracted'
74 + }, 300)
7375 })
7476 }
7577
76- ta.addEventListener('keydown', function (ev) {
78 + textArea.addEventListener('keydown', ev => {
7779 if(ev.keyCode === 13 && ev.ctrlKey) publish()
7880 })
7981
8082 var files = []
@@ -83,12 +85,12 @@
8385 function publish() {
8486 publishBtn.disabled = true
8587 var content
8688 try {
87- content = JSON.parse(ta.value)
89 + content = JSON.parse(textArea.value)
8890 } catch (err) {
89- meta.text = ta.value
90- meta.mentions = mentions(ta.value).map(function (mention) {
91 + meta.text = textArea.value
92 + meta.mentions = mentions(textArea.value).map(mention => {
9193 // merge markdown-detected mention with file info
9294 var file = filesById[mention.link]
9395 if (file) {
9496 if (file.type) mention.type = file.type
@@ -110,46 +112,49 @@
110112
111113 function done (err, msg) {
112114 publishBtn.disabled = false
113115 if(err) return alert(err.stack)
114- else if (msg) ta.value = ''
116 + else if (msg) textArea.value = ''
115117
116118 if (cb) cb(err, msg)
117119 }
118120 }
119121
122 + var fileInput = api.file_input(file => {
123 + files.push(file)
124 + filesById[file.link] = file
120125
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
126 + var embed = file.type.indexOf('image/') === 0 ? '!' : ''
130127
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- )
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 + ])
138136
139- suggest(ta, function (name, cb) {
137 + var composer = h('Compose', {
138 + className: opts.shrink === false ? '-expanded' : '-contracted'
139 + }, [
140 + textArea,
141 + actions
142 + ])
143 +
144 + suggest(textArea, (name, cb) => {
140145 cont.para(api.suggest_mentions(name))
141- (function (err, ary) {
142- cb(null, ary.reduce(function (a, b) {
146 + ((err, ary) => {
147 + cb(null, ary.reduce((a, b) => {
143148 if(!b) return a
144149 return a.concat(b)
145150 }, []))
146151 })
147152 }, {})
148153
149154 return composer
150-
151155 }
152156
153157 }
154158
159 +function id (e) { return e }
155160
modules_basic/compose.mcssView
@@ -1,0 +1,90 @@
1 +Compose {
2 + display: flex
3 + flex-direction: column
4 +
5 + padding: 1rem .5rem 1rem 7.3rem
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 + margin-top: .4rem
17 +
18 + input[type="file"] {
19 + flex-grow: 1
20 +
21 + padding: .5rem 0
22 +
23 + width: 5.5rem
24 + color: transparent
25 +
26 + ::-webkit-file-upload-button {
27 + visibility: hidden
28 + }
29 +
30 + ::before {
31 + $composeButton
32 + padding-top: .3rem
33 +
34 + content: '📎'
35 + font-size: 1rem
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 + $composeButton
50 +
51 + text-transform: uppercase
52 + font-weight: bold
53 + font-size: .7rem
54 + }
55 + }
56 +
57 + -expanded {
58 + textarea {
59 + height: 200px
60 + transition: height .15s ease-out
61 + }
62 +
63 + section.actions {
64 + display: flex
65 + }
66 + }
67 +
68 + -contracted {
69 + textarea {
70 + height: 50px
71 + transition: height .15s ease-in
72 + }
73 +
74 + section.actions {
75 + display: none
76 + }
77 + }
78 +
79 +}
80 +
81 +$composeButton {
82 + background: #fff
83 + color: #666
84 + border: 1px solid #bbb
85 + border-radius: .5rem
86 + padding: .5rem
87 + margin: 0
88 + cursor: pointer
89 +}
90 +
modules_core/style-mixins.jsView
@@ -1,8 +1,8 @@
11
22 const mixins = `
33 $textPrimary {
4- color: black
4 + color: #222
55 }
66
77 $textSubtle {
88 color: gray
style.cssView
@@ -1,6 +1,7 @@
11 body {
22 font-family: sans-serif;
3 + color: #222;
34 }
45
56 h1, h2, h3, h4, h5, h6, p, ul, ol {
67 margin-top: .35em;
@@ -162,21 +163,8 @@
162163 margin-left: auto;
163164 margin-right: auto;
164165 }
165166
166-/* compose */
167-
168-.compose {
169- width: 100%;
170- margin-top: .5em;
171- margin-bottom: .5em;
172- border: 1px solid #f5f5f5;
173-}
174-
175-.compose button {
176- float: right;
177-}
178-
179167 /* messages */
180168 /* is .title used any more? */
181169
182170 .title {

Built with git-ssb-web