Files: b01dbbea52f7a3b9f101ce6e29d8180184110ffb / app / sync / catch-keyboard-shortcut.js
2938 bytesRaw
1 | const nest = require('depnest') |
2 | |
3 | exports.gives = nest('app.sync.catchKeyboardShortcut') |
4 | |
5 | exports.needs = nest({ |
6 | 'app.html.searchBar': 'first', |
7 | 'app.html.tabs': 'first', |
8 | 'app.sync.goTo': 'first', |
9 | }) |
10 | |
11 | var gPressed = false |
12 | |
13 | exports.create = function (api) { |
14 | return nest('app.sync.catchKeyboardShortcut', catchKeyboardShortcut) |
15 | |
16 | function catchKeyboardShortcut (root) { |
17 | var tabs = api.app.html.tabs() |
18 | var search = api.app.html.searchBar() |
19 | var goTo = api.app.sync.goTo |
20 | |
21 | root.addEventListener('keydown', (ev) => { |
22 | isTextFieldEvent(ev) |
23 | ? textFieldShortcuts(ev) |
24 | : genericShortcuts(ev, { tabs, search, goTo }) |
25 | }) |
26 | } |
27 | } |
28 | |
29 | function isTextFieldEvent (ev) { |
30 | const tag = ev.target.nodeName |
31 | return (tag === 'INPUT' || tag === 'TEXTAREA') |
32 | } |
33 | |
34 | function textFieldShortcuts (ev) { |
35 | switch (ev.keyCode) { |
36 | case 13: // ctrl+enter |
37 | if (ev.ctrlKey) { |
38 | ev.target.publish() // expects the textField to have a publish method |
39 | } |
40 | return |
41 | case 27: // esc |
42 | return ev.target.blur() |
43 | } |
44 | } |
45 | |
46 | function genericShortcuts (ev, { tabs, search, goTo, back }) { |
47 | // Messages |
48 | if (ev.keyCode === 71) { // gg = scroll to top |
49 | if (!gPressed) { |
50 | gPressed = true |
51 | window.setTimeout(() => { |
52 | gPressed = false |
53 | }, 3000) |
54 | return |
55 | } |
56 | tabs.currentPage().scroll('first') |
57 | } |
58 | gPressed = false |
59 | |
60 | switch (ev.keyCode) { |
61 | // Messages (cont'd) |
62 | case 74: // j = older |
63 | return tabs.currentPage().scroll(1) |
64 | case 75: // k = newer |
65 | return tabs.currentPage().scroll(-1) |
66 | case 13: // enter = open |
67 | return goToMessage(ev, { goTo }) |
68 | case 79: // o = open |
69 | return goToMessage(ev, { goTo }) |
70 | case 192: // ` = toggle raw message view |
71 | return toggleRawMessage(ev) |
72 | |
73 | // Tabs |
74 | case 72: // h = left |
75 | tabs.selectRelative(-1) |
76 | return goTo(JSON.parse(tabs.currentPage().id)) |
77 | case 76: // l = right |
78 | tabs.selectRelative(1) |
79 | return goTo(JSON.parse(tabs.currentPage().id)) |
80 | case 88: // x = close |
81 | if (tabs.selected) { |
82 | var sel = tabs.selected |
83 | tabs.remove(sel) |
84 | } |
85 | return |
86 | |
87 | // Search |
88 | case 191: // / = routes search |
89 | if (ev.shiftKey) search.activate('?', ev) |
90 | else search.activate('/', ev) |
91 | return |
92 | case 50: // @ = mention search |
93 | if (ev.shiftKey) search.activate('@', ev) |
94 | return |
95 | case 51: // # = channel search |
96 | if (ev.shiftKey) search.activate('#', ev) |
97 | return |
98 | case 53: // % = message search |
99 | if (ev.shiftKey) search.activate('%', ev) |
100 | } |
101 | } |
102 | |
103 | function goToMessage (ev, { goTo }) { |
104 | const msg = ev.target |
105 | |
106 | if (msg.dataset && msg.dataset.id) { |
107 | goTo(msg.dataset.id) |
108 | // TODO - rm the dataset.root decorator |
109 | } |
110 | } |
111 | |
112 | function toggleRawMessage (ev) { |
113 | const msg = ev.target |
114 | if (!msg.classList.contains('Message')) return |
115 | |
116 | // this uses a crudely exported nav api |
117 | msg.querySelector('.meta .toggle-raw-msg').click() |
118 | } |
119 |
Built with git-ssb-web