Files: f14ea4b803726285144ed09e5e390000b8baf41a / modules_core / search-box.js
1327 bytesRaw
1 | |
2 | const h = require('../h') |
3 | const fs = require('fs') |
4 | |
5 | |
6 | exports.needs = { |
7 | suggest_search: 'map', //REWRITE |
8 | build_suggest_box: 'first' |
9 | } |
10 | |
11 | exports.gives = { |
12 | search_box: true, |
13 | mcss: true |
14 | } |
15 | |
16 | exports.create = function (api) { |
17 | |
18 | return { |
19 | search_box, |
20 | mcss: () => fs.readFileSync(__filename.replace(/js$/, 'mcss'), 'utf8') |
21 | } |
22 | |
23 | function search_box (go) { |
24 | const input = h('input', { |
25 | type: 'search', |
26 | placeholder: 'Commands', |
27 | 'ev-keyup': ev => { |
28 | switch (ev.keyCode) { |
29 | case 13: // enter |
30 | if (go(input.value.trim(), !ev.ctrlKey)) |
31 | input.blur() |
32 | return |
33 | case 27: // escape |
34 | ev.preventDefault() |
35 | input.blur() |
36 | return |
37 | } |
38 | } |
39 | }) |
40 | input.addEventListener('suggestselect', ev => { |
41 | if (go(input.value.trim(), !ev.ctrlKey)) |
42 | input.blur() |
43 | }) |
44 | const search = h('Search', input) |
45 | |
46 | search.input = input |
47 | search.activate = (sigil, ev) => { |
48 | input.focus() |
49 | ev.preventDefault() |
50 | if (input.value[0] === sigil) { |
51 | input.selectionStart = 1 |
52 | input.selectionEnd = input.value.length |
53 | } else { |
54 | input.value = sigil |
55 | } |
56 | } |
57 | |
58 | const suggestBox = api.build_suggest_box(input, api.suggest_search) |
59 | |
60 | return search |
61 | } |
62 | |
63 | } |
64 | |
65 |
Built with git-ssb-web