Files: 9510d7867738dfa7a4aaee17c183c86bb081f55e / app / html / search-bar.js
1855 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 | var location = input.value.trim() |
30 | if (goTo(location, !ev.ctrlKey)) input.blur() |
31 | return |
32 | case 27: // escape |
33 | ev.preventDefault() |
34 | input.blur() |
35 | return |
36 | } |
37 | } |
38 | }) |
39 | input.addEventListener('suggestselect', ev => { |
40 | input.value = ev.detail.id // HACK : this over-rides the markdown value |
41 | |
42 | // if (goTo(input.value.trim(), !ev.ctrlKey)) |
43 | // input.blur() |
44 | }) |
45 | |
46 | _search = h('SearchBar', input) |
47 | _search.input = input |
48 | _search.activate = (sigil, ev) => { |
49 | input.focus() |
50 | ev.preventDefault() |
51 | if (input.value[0] === sigil) { |
52 | input.selectionStart = 1 |
53 | input.selectionEnd = input.value.length |
54 | } else { |
55 | input.value = sigil |
56 | } |
57 | } |
58 | |
59 | addSuggest(input, (inputText, cb) => { |
60 | if (inputText[0] === '@') { |
61 | cb(null, getProfileSuggestions(inputText.slice(1))) |
62 | } else if (inputText[0] === '#') { |
63 | cb(null, getChannelSuggestions(inputText.slice(1))) |
64 | } |
65 | }, {cls: 'SuggestBox'}) |
66 | |
67 | return _search |
68 | }) |
69 | } |
70 | |
71 |
Built with git-ssb-web