git ssb

2+

ev / mvd



Commit 865ad391590f0190881008c270959f5bd1f8c6da

move compose and render tools into their own files

Ev Bogue committed on 4/28/2018, 7:34:56 PM
Parent: 7ee2a3885b11ba612000e0ce307579bb5deccfe3

Files changed

render.jschanged
scuttlebot.jschanged
compose.jsadded
rendertools.jsadded
render.jsView
@@ -1,46 +1,20 @@
11 var h = require('hyperscript')
2-var human = require('human-time')
3-var ref = require('ssb-ref')
4-var avatar = require('./avatar')
5-
62 var markdown = require('ssb-markdown')
73 var config = require('./config')()
84
9-function rawJSON (obj) {
10- return JSON.stringify(obj, null, 2)
11- .split(/([%@&][a-zA-Z0-9\/\+]{43}=*\.[\w]+)/)
12- .map(function (e) {
13- if(ref.isMsg(e) || ref.isFeed(e) || ref.isBlob(e)) {
14- return h('a', {href: '#' + e}, e)
15- }
16- return e
17- })
18-}
5 +var sbot = require('./scuttlebot')
6 +var composer = require('./compose')
197
20-function header (msg) {
21- return h('div.header',
22- h('span.avatar',
23- h('a', {href: '#' + msg.value.author},
24- h('span.avatar--small', avatar.image(msg.value.author)),
25- avatar.name(msg.value.author)
26- )
27- ),
28- h('span.timestamp', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp)))),
29- )
30-}
8 +var tools = require('./rendertools')
319
32-function messageLink (msglink) {
33- var link = h('span', h('a', {href: '#' + msglink}, msglink.substring(0, 8) + '...'))
34- return link
35-}
10 +module.exports = function (msg) {
3611
37-module.exports = function (msg) {
3812 var message = h('div.message')
3913 if (msg.value.content.type == 'post') {
40- message.appendChild(header(msg))
14 + message.appendChild(tools.header(msg))
4115 if (msg.value.content.root) {
42- message.appendChild(h('span', 're: ', messageLink(msg.value.content.root)))
16 + message.appendChild(h('span', 're: ', tools.messageLink(msg.value.content.root)))
4317 }
4418 message.appendChild(h('div.message__body',
4519 {innerHTML: markdown.block(msg.value.content.text, {toUrl: function (url, image) {
4620 if(url[0] == '%' || url[0] == '@') return '#' + url
@@ -49,36 +23,42 @@
4923 return config.blobsUrl + url
5024 }})}
5125 )
5226 )
27 + message.appendChild(h('button.btn', 'Reply', {
28 + onclick: function () {
29 + var compose = composer()
30 + message.replaceChild(compose, message.lastElementChild)
31 + }
32 + }))
5333 return message
5434 } else if (msg.value.content.type == 'vote') {
55- message.appendChild(header(msg))
35 + message.appendChild(tools.header(msg))
5636 var embed = msg.value.content.vote.link
5737
5838 var embedded = h('div.embedded')
5939 sbot.get(embed, function (err, msg) {
6040 if (err) {console.log('could not find message locally, try ooo?') }
6141 msg.value = msg
6242 msg.key = embed
6343 if (msg.value.content.text) {
64- //message.appendChild(h('img.emoji', {src: config.emojiUrl + 'star.png'}))
6544 message.appendChild(embedded)
66- embedded.appendChild(header(msg))
45 + embedded.appendChild(tools.header(msg))
6746 embedded.appendChild(h('div.message__body',
6847 {innerHTML: markdown.block(msg.value.content.text.substring(0, 256) + '... ', {toUrl: function (url, image) {
6948 if(url[0] == '@') return '#' + url
7049 if(url[0] == '%') return '#' + url
7150 if(!image) return url
7251 if(url[0] !== '&') return url
7352 return config.blobsUrl + url
74- }})}, messageLink(msg.key)
53 + }})}, tools.messageLink(msg.key)
7554 ))
7655 }
7756 })
7857 return message
79- } else {
80- //message.appendChild(h('pre', rawJSON(msg.value.content)))
58 + } else {
59 + //message.appendChild(tools.header(msg))
60 + //message.appendChild(h('pre', tools.rawJSON(msg.value.content)))
8161 //return message
8262 return
8363 }
8464 }
scuttlebot.jsView
@@ -62,7 +62,30 @@
6262 })
6363 }),
6464 links: rec.source(function (query) {
6565 return sbot.links(query)
66 + }),
67 + addblob: rec.sink(function (cb) {
68 + return sbot.blobs.add(cb)
69 + }),
70 + publish: rec.async(function (content, cb) {
71 + if(content.recps)
72 + content = ssbKeys.box(content, content.recps.map(function (e) {
73 + return ref.isFeed(e) ? e : e.link
74 + }))
75 + else if(content.mentions)
76 + content.mentions.forEach(function (mention) {
77 + if(ref.isBlob(mention.link)) {
78 + sbot.blobs.push(mention.link, function (err) {
79 + if(err) console.error(err)
80 + })
81 + }
82 + })
83 + feed.add(content, function (err, msg) {
84 + if(err) console.error(err)
85 + else if(!cb) console.log(msg)
86 + cb && cb(err, msg)
87 + })
6688 })
89 +
6790 }
6891
compose.jsView
@@ -1,0 +1,52 @@
1 +var h = require('hyperscript')
2 +var pull = require('pull-stream')
3 +var sbot = require('./scuttlebot')
4 +
5 +var mime = require('simple-mime')('application/octect-stream')
6 +var split = require('split-buffer')
7 +
8 +function file_input (onAdded) {
9 + return h('label.btn', 'Upload file',
10 + h('input', { type: 'file', hidden: true,
11 + onchange: function (ev) {
12 + var file = ev.target.files[0]
13 + if (!file) return
14 + var reader = new FileReader()
15 + reader.onload = function () {
16 + pull(
17 + pull.values(split(new Buffer(reader.result), 64*1024)),
18 + sbot.addblob(function (err, blob) {
19 + if(err) return console.error(err)
20 + onAdded({
21 + link: blob,
22 + name: file.name,
23 + size: reader.result.length || reader.result.byteLength,
24 + type: mime(file.name)
25 + })
26 + })
27 + )
28 + }
29 + reader.readAsArrayBuffer(file)
30 + }
31 + }))
32 +}
33 +
34 +module.exports = function (opts) {
35 + var files = []
36 + var filesById = {}
37 +
38 + var textarea = h('textarea.compose', {placeholder: 'Reply to this post'})
39 +
40 + var composer = h('div',
41 + textarea,
42 + h('button.btn', 'Preview'),
43 + file_input(function (file) {
44 + files.push(file)
45 + filesById[file.link] = file
46 + var embed = file.type.indexOf('image/') === 0 ? '!' : ''
47 + textarea.value += embed + '['+file.name+']('+file.link+')'
48 + })
49 + )
50 + return composer
51 +}
52 +
rendertools.jsView
@@ -1,0 +1,33 @@
1 +var h = require('hyperscript')
2 +var human = require('human-time')
3 +var avatar = require('./avatar')
4 +var ref = require('ssb-ref')
5 +
6 +
7 +module.exports.header = function (msg) {
8 + return h('div.header',
9 + h('span.avatar',
10 + h('a', {href: '#' + msg.value.author},
11 + h('span.avatar--small', avatar.image(msg.value.author)),
12 + avatar.name(msg.value.author)
13 + )
14 + ),
15 + h('span.timestamp', h('a', {href: '#' + msg.key}, human(new Date(msg.value.timestamp)))),
16 + )
17 +}
18 +
19 +module.exports.messageLink = function (msglink) {
20 + var link = h('span', h('a', {href: '#' + msglink}, msglink.substring(0, 8) + '...'))
21 + return link
22 +}
23 +
24 +module.exports.rawJSON = function (obj) {
25 + return JSON.stringify(obj, null, 2)
26 + .split(/([%@&][a-zA-Z0-9\/\+]{43}=*\.[\w]+)/)
27 + .map(function (e) {
28 + if(ref.isMsg(e) || ref.isFeed(e) || ref.isBlob(e)) {
29 + return h('a', {href: '#' + e}, e)
30 + }
31 + return e
32 + })
33 +}

Built with git-ssb-web