Files: 5e5bef4d7a2202ff89f9fd54978ed73376abe898 / app / html / search-bar.js
2062 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 getProfileSuggestions = api.about.async.suggest() |
20 | const getChannelSuggestions = api.channel.async.suggest() |
21 | |
22 | const input = h('input', { |
23 | type: 'search', |
24 | placeholder: '?search, @name, #channel', |
25 | 'ev-keyup': ev => { |
26 | switch (ev.keyCode) { |
27 | case 13: // enter |
28 | var location = input.value.trim() |
29 | if (location[0] == '?') |
30 | location = { page: 'search', query: location.substring(1) } |
31 | else if (!['@', '#', '%', '&'].includes(location[0])) |
32 | location = { page: 'search', query: location } |
33 | |
34 | api.app.sync.goTo(location) |
35 | if (!ev.ctrlKey) input.blur() |
36 | return |
37 | case 27: // escape |
38 | ev.preventDefault() |
39 | input.blur() |
40 | } |
41 | } |
42 | }) |
43 | input.addEventListener('suggestselect', ev => { |
44 | input.value = ev.detail.id // HACK : this over-rides the markdown value |
45 | |
46 | // if (goTo(input.value.trim(), !ev.ctrlKey)) |
47 | // input.blur() |
48 | }) |
49 | |
50 | _search = h('SearchBar', input) |
51 | _search.input = input |
52 | _search.activate = (sigil, ev) => { |
53 | input.focus() |
54 | ev.preventDefault() |
55 | if (input.value[0] === sigil) { |
56 | input.selectionStart = 1 |
57 | input.selectionEnd = input.value.length |
58 | } else { |
59 | input.value = sigil |
60 | } |
61 | } |
62 | |
63 | addSuggest(input, (inputText, cb) => { |
64 | if (inputText[0] === '@') { |
65 | cb(null, getProfileSuggestions(inputText.slice(1))) |
66 | } else if (inputText[0] === '#') { |
67 | cb(null, getChannelSuggestions(inputText.slice(1))) |
68 | } |
69 | }, {cls: 'SuggestBox'}) |
70 | |
71 | return _search |
72 | }) |
73 | } |
74 |
Built with git-ssb-web