Files: 09c0a2e49ccc68a4cdc3a98da00657c1aff6158b / app / html / search-bar.js
1849 bytesRaw
1 | const nest = require('depnest') |
2 | const { h } = require('mutant') |
3 | const addSuggest = require('suggest-box') |
4 | |
5 | exports.gives = nest('app.html.searchBar') |
6 | |
7 | exports.needs = nest({ |
8 | 'app.sync.goTo': 'first', |
9 | 'about.async.suggest': 'first', |
10 | 'channel.async.suggest': 'first' |
11 | }) |
12 | |
13 | exports.create = function (api) { |
14 | var _search |
15 | |
16 | return nest('app.html.searchBar', function searchBar () { |
17 | if (_search) return _search |
18 | |
19 | const goTo = api.app.sync.goTo |
20 | const getProfileSuggestions = api.about.async.suggest() |
21 | const getChannelSuggestions = api.channel.async.suggest() |
22 | |
23 | const input = h('input', { |
24 | type: 'search', |
25 | placeholder: '?search, @name, #channel', |
26 | 'ev-keyup': ev => { |
27 | switch (ev.keyCode) { |
28 | case 13: // enter |
29 | if (goTo(input.value.trim(), !ev.ctrlKey)) { |
30 | input.blur() |
31 | } |
32 | return |
33 | case 27: // escape |
34 | ev.preventDefault() |
35 | input.blur() |
36 | return |
37 | } |
38 | } |
39 | }) |
40 | input.addEventListener('suggestselect', ev => { |
41 | input.value = ev.detail.id // HACK : this over-rides the markdown value |
42 | |
43 | // if (goTo(input.value.trim(), !ev.ctrlKey)) |
44 | // input.blur() |
45 | }) |
46 | |
47 | _search = h('SearchBar', input) |
48 | _search.input = input |
49 | _search.activate = (sigil, ev) => { |
50 | input.focus() |
51 | ev.preventDefault() |
52 | if (input.value[0] === sigil) { |
53 | input.selectionStart = 1 |
54 | input.selectionEnd = input.value.length |
55 | } else { |
56 | input.value = sigil |
57 | } |
58 | } |
59 | |
60 | addSuggest(input, (inputText, cb) => { |
61 | if (inputText[0] === '@') { |
62 | cb(null, getProfileSuggestions(inputText.slice(1))) |
63 | } else if (inputText[0] === '#') { |
64 | cb(null, getChannelSuggestions(inputText.slice(1))) |
65 | } |
66 | }, {cls: 'SuggestBox'}) |
67 | |
68 | return _search |
69 | }) |
70 | } |
71 | |
72 |
Built with git-ssb-web