git ssb

2+

mixmix / ticktack



Tree: 1f4b2f787babd3652136a929c1880a5c92064ce9

Files: 1f4b2f787babd3652136a929c1880a5c92064ce9 / app / html / thread-card.js

4189 bytesRaw
1var nest = require('depnest')
2var h = require('mutant/h')
3var isString= require('lodash/isString')
4var maxBy= require('lodash/maxBy')
5var humanTime = require('human-time')
6var marksum = require('markdown-summary')
7var markdown = require('ssb-markdown')
8var ref = require('ssb-ref')
9var htmlEscape = require('html-escape')
10
11function renderEmoji (emoji, url) {
12 if (!url) return ':' + emoji + ':'
13 return `
14 <img
15 src="${htmlEscape(url)}"
16 alt=":${htmlEscape(emoji)}:"
17 title=":${htmlEscape(emoji)}:"
18 class="emoji"
19 >
20 `
21}
22
23exports.gives = nest('app.html.threadCard', true)
24
25exports.needs = nest({
26 'keys.sync.id': 'first',
27 'history.sync.push': 'first',
28 'about.obs.name': 'first',
29 'about.html.avatar': 'first',
30 'translations.sync.strings': 'first',
31 'unread.sync.isUnread': 'first',
32 'message.html.markdown': 'first',
33 'blob.sync.url': 'first',
34 'emoji.sync.url': 'first'
35})
36
37exports.create = function (api) {
38
39 //render markdown, but don't support patchwork@2 style mentions or custom emoji right now.
40 function render (source) {
41 return markdown.block(source, {
42 emoji: (emoji) => {
43 return renderEmoji(emoji, api.emoji.sync.url(emoji))
44 },
45 toUrl: (id) => {
46 if (ref.isBlob(id)) return api.blob.sync.url(id)
47 return id
48 },
49 imageLink: (id) => id
50 })
51 }
52
53
54 //render the icon for a thread.
55 //it would be more depjecty to split this
56 //into two methods, one in a private plugin
57 //one in a channel plugin
58 function threadIcon (msg) {
59 if(msg.value.private) {
60 const myId = api.keys.sync.id()
61
62 return msg.value.content.recps
63 .map(link => isString(link) ? link : link.link)
64 .filter(link => link !== myId)
65 .map(api.about.html.avatar)
66 }
67 else if(msg.value.content.channel)
68 return '#'+msg.value.content.channel
69 }
70
71
72 // REFACTOR: move this to a template?
73 function buildRecipientNames (thread) {
74 const myId = api.keys.sync.id()
75
76 return thread.value.content.recps
77 .map(link => isString(link) ? link : link.link)
78 .filter(link => link !== myId)
79 .map(api.about.obs.name)
80 }
81
82 return nest('app.html.threadCard', (thread, opts = {}) => {
83 var strings = api.translations.sync.strings()
84 const { subject } = api.message.html
85
86 if(!thread.value) return
87 if(!thread.value.content.text) return
88
89// const subjectEl = h('div.subject', [
90// opts.nameRecipients
91// ? h('div.recps', buildRecipientNames(thread).map(recp => h('div.recp', recp)))
92// : null,
93// subject(thread)
94// ])
95
96 const lastReply = thread.replies && maxBy(thread.replies, r => r.timestamp)
97// const replySample = lastReply ? subject(lastReply) : null
98
99 const onClick = opts.onClick || function () { api.history.sync.push(thread) }
100 const id = `${thread.key.replace(/[^a-z0-9]/gi, '')}` //-${JSON.stringify(opts)}`
101 // id is only here to help morphdom morph accurately
102
103 var img = marksum.image(thread.value.content.text)
104 var m = /\!\[[^]+\]\(([^\)]+)\)/.exec(img)
105 if(m) {
106 //hey this works! fit an image into a specific size (see thread-card.mcss)
107 //centered, and scaled to fit the square (works with both landscape and portrait!)
108 img = h('Thumbnail')
109 img.style = 'background-image: url("'+api.blob.sync.url(m[1])+'"); background-position:center; background-size: cover;'
110 }
111 else img = ''
112 var title = render(marksum.title(thread.value.content.text))
113 var summary = render(marksum.summary(thread.value.content.text))
114
115 var className = thread.unread ? '-unread': ''
116 return h('ThreadCard', { id, className }, [
117 h('div.context', [
118 api.about.html.avatar(thread.value.author),
119 ' ',
120 api.about.obs.name(thread.value.author),
121 ' ',
122 humanTime(new Date(thread.value.timestamp)),
123 ' ',
124 thread.value.content.channel ? '#'+thread.value.content.channel : null
125 ]),
126 h('div.content', {'ev-click': onClick}, [
127 img,
128 h('Text', [
129 h('h2', {innerHTML: title}),
130 h('Summary', {innerHTML: summary})
131 ])
132 ])
133 ])
134 })
135}
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

Built with git-ssb-web