Files: 354bcc491180e33154ea042ff61d5912ae2d95ab / app / page / blogNew.js
2523 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, Struct, Value } = require('mutant') |
3 | const addSuggest = require('suggest-box') |
4 | |
5 | exports.gives = nest('app.page.blogNew') |
6 | |
7 | exports.needs = nest({ |
8 | 'app.html.context': 'first', |
9 | 'channel.async.suggest': 'first', |
10 | 'history.sync.push': 'first', |
11 | 'message.html.compose': 'first', |
12 | 'translations.sync.strings': 'first', |
13 | }) |
14 | |
15 | exports.create = (api) => { |
16 | var contentHtmlObs |
17 | |
18 | return nest('app.page.blogNew', blogNew) |
19 | |
20 | function blogNew (location) { |
21 | const strings = api.translations.sync.strings() |
22 | const getChannelSuggestions = api.channel.async.suggest() |
23 | |
24 | const meta = Struct({ |
25 | type: 'blog', |
26 | channel: Value(), |
27 | title: Value(), |
28 | }) |
29 | |
30 | const composer = api.message.html.compose( |
31 | { meta, placeholder: strings.blogNew.actions.writeBlog, shrink: false }, |
32 | (err, msg) => api.history.sync.push(err ? err : { page: 'blogIndex' }) |
33 | ) |
34 | |
35 | const channelInput = h('input', { |
36 | 'ev-input': e => meta.channel.set(e.target.value), |
37 | placeholder: strings.channel |
38 | }) |
39 | var page = h('Page -blogNew', [ |
40 | api.app.html.context(location), |
41 | h('div.content', [ |
42 | h('div.container', [ |
43 | h('div.field -channel', [ |
44 | h('div.label', strings.channel), |
45 | channelInput |
46 | ]), |
47 | h('div.field -title', [ |
48 | h('div.label', strings.blogNew.field.title), |
49 | h('input', { |
50 | 'ev-input': e => meta.title.set(e.target.value), |
51 | placeholder: strings.blogNew.field.title |
52 | }), |
53 | ]), |
54 | composer |
55 | ]) |
56 | ]) |
57 | ]) |
58 | |
59 | addSuggest(channelInput, (inputText, cb) => { |
60 | var suggestions = getChannelSuggestions(inputText) |
61 | .map(s => { |
62 | s.value = s.value.replace(/^#/, '') // strip the defualt # prefix here |
63 | return s |
64 | }) |
65 | .map(s => { |
66 | if (s.subtitle === 'subscribed') |
67 | s.subtitle = h('i.fa.fa-heart') // TODO - translation-friendly subscribed |
68 | return s |
69 | }) |
70 | |
71 | // HACK add the input text if it's not an option already |
72 | if (!suggestions.some(s => s.title === inputText)) { |
73 | suggestions.push({ |
74 | title: inputText, |
75 | subtitle: h('i.fa.fa-plus-circle'), |
76 | value: inputText |
77 | }) |
78 | } |
79 | |
80 | cb(null, suggestions) |
81 | }, {cls: 'PatchSuggest.-channelHorizontal'}) // WARNING hacking suggest-box cls |
82 | |
83 | channelInput.addEventListener('suggestselect', (e) => meta.channel.set(e.value)) |
84 | |
85 | return page |
86 | } |
87 | } |
88 |
Built with git-ssb-web