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