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