Files: c27d9c765e55088d4fd855d29313c20839bb982c / app / page / blogNew.js
3624 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Struct, Value } = require('mutant') |
3 | const addSuggest = require('suggest-box') |
4 | const pull = require('pull-stream') |
5 | const marksum = require('markdown-summary') |
6 | |
7 | exports.gives = nest('app.page.blogNew') |
8 | |
9 | exports.needs = nest({ |
10 | 'app.html.sideNav': 'first', |
11 | 'channel.async.suggest': 'first', |
12 | 'history.sync.push': 'first', |
13 | 'message.html.compose': 'first', |
14 | 'translations.sync.strings': 'first', |
15 | 'sbot.async.addBlob': 'first' |
16 | }) |
17 | |
18 | exports.create = (api) => { |
19 | var contentHtmlObs |
20 | |
21 | return nest('app.page.blogNew', blogNew) |
22 | |
23 | function blogNew (location) { |
24 | const strings = api.translations.sync.strings() |
25 | const getChannelSuggestions = api.channel.async.suggest() |
26 | |
27 | const meta = Struct({ |
28 | type: 'blog', |
29 | channel: Value(), |
30 | title: Value(), |
31 | summary: Value(), |
32 | }) |
33 | |
34 | const composer = api.message.html.compose( |
35 | { |
36 | meta, |
37 | placeholder: strings.blogNew.actions.writeBlog, |
38 | shrink: false, |
39 | prepublish: function (content, cb) { |
40 | var m = /\!\[[^]+\]\(([^\)]+)\)/.exec(marksum.image(content.text)) |
41 | content.thumbnail = m && m[1] |
42 | // content.summary = marksum.summary(content.text) // Need a summary whihch doesn't trim the start |
43 | |
44 | var stream = pull.values([content.text]) |
45 | delete content.text |
46 | api.sbot.async.addBlob(stream, function (err, hash) { |
47 | if(err) return cb(err) |
48 | if(!hash) throw new Error('missing hash') |
49 | content.blog = hash |
50 | cb(null, content) |
51 | }) |
52 | } |
53 | }, |
54 | (err, msg) => api.history.sync.push(err ? err : { page: 'blogIndex' }) |
55 | ) |
56 | |
57 | const channelInput = h('input', { |
58 | 'ev-input': e => meta.channel.set(e.target.value), |
59 | placeholder: strings.channel |
60 | }) |
61 | |
62 | var page = h('Page -blogNew', [ |
63 | api.app.html.sideNav(location), |
64 | h('div.content', [ |
65 | h('div.container', [ |
66 | h('div.field -channel', [ |
67 | h('div.label', strings.channel), |
68 | channelInput |
69 | ]), |
70 | h('div.field -title', [ |
71 | h('div.label', strings.blogNew.field.title), |
72 | h('input', { |
73 | 'ev-input': e => meta.title.set(e.target.value), |
74 | placeholder: strings.blogNew.field.title |
75 | }), |
76 | ]), |
77 | h('div.field -summary', [ |
78 | h('div.label', strings.blogNew.field.summary), |
79 | h('input', { |
80 | 'ev-input': e => meta.summary.set(e.target.value), |
81 | placeholder: strings.blogNew.field.summary |
82 | }), |
83 | ]), |
84 | composer |
85 | ]) |
86 | ]) |
87 | ]) |
88 | |
89 | addSuggest(channelInput, (inputText, cb) => { |
90 | inputText = inputText.replace(/^#/, '') |
91 | var suggestions = getChannelSuggestions(inputText) |
92 | .map(s => { |
93 | s.value = s.value.replace(/^#/, '') // strip the defualt # prefix here |
94 | return s |
95 | }) |
96 | .map(s => { |
97 | if (s.subtitle === 'subscribed') |
98 | s.subtitle = h('i.fa.fa-heart') // TODO - translation-friendly subscribed |
99 | return s |
100 | }) |
101 | |
102 | // HACK add the input text if it's not an option already |
103 | if (!suggestions.some(s => s.title === inputText)) { |
104 | suggestions.push({ |
105 | title: inputText, |
106 | subtitle: h('i.fa.fa-plus-circle'), |
107 | value: inputText |
108 | }) |
109 | } |
110 | |
111 | cb(null, suggestions) |
112 | }, {cls: 'PatchSuggest.-channelHorizontal'}) // WARNING hacking suggest-box cls |
113 | |
114 | channelInput.addEventListener('suggestselect', (e) => { |
115 | meta.channel.set(e.detail.value) |
116 | }) |
117 | |
118 | return page |
119 | } |
120 | } |
121 | |
122 | |
123 | |
124 | |
125 | |
126 |
Built with git-ssb-web