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