Files: f10d4f05f93f10bbe4bfbb914a054e6332171721 / modules_core / search-box.js
1268 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 | ev.stopPropagation() |
31 | suggestBox.complete() |
32 | |
33 | if (go(input.value.trim(), !ev.ctrlKey)) |
34 | input.blur() |
35 | return |
36 | case 27: // escape |
37 | ev.preventDefault() |
38 | input.blur() |
39 | return |
40 | } |
41 | } |
42 | }) |
43 | const search = h('Search', 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 | const suggestBox = api.build_suggest_box(input, api.suggest_search) |
58 | |
59 | return search |
60 | } |
61 | |
62 | } |
63 | |
64 |
Built with git-ssb-web