Files: 155cb7d3bab180ae158641a9c5d8413332bacdfc / app / sync / catch-keyboard-shortcut.js
2943 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 | const scroll = tabs.currentPage().scroll || function () {} |
48 | |
49 | // Messages |
50 | if (ev.keyCode === 71) { // gg = scroll to top |
51 | if (!gPressed) { |
52 | gPressed = true |
53 | window.setTimeout(() => { |
54 | gPressed = false |
55 | }, 3000) |
56 | return |
57 | } |
58 | scroll('first') |
59 | } |
60 | gPressed = false |
61 | |
62 | switch (ev.keyCode) { |
63 | // Messages (cont'd) |
64 | case 74: // j = older |
65 | return scroll(1) |
66 | case 75: // k = newer |
67 | return scroll(-1) |
68 | case 13: // enter = open |
69 | return goToMessage(ev, { goTo }) |
70 | case 79: // o = open |
71 | return goToMessage(ev, { goTo }) |
72 | case 192: // ` = toggle raw message view |
73 | return toggleRawMessage(ev) |
74 | |
75 | // Tabs |
76 | case 72: // h = left |
77 | tabs.selectRelative(-1) |
78 | return goTo(JSON.parse(tabs.currentPage().id)) |
79 | case 76: // l = right |
80 | tabs.selectRelative(1) |
81 | return goTo(JSON.parse(tabs.currentPage().id)) |
82 | case 88: // x = close |
83 | if (tabs.selected) { |
84 | var sel = tabs.selected |
85 | tabs.remove(sel) |
86 | } |
87 | return |
88 | |
89 | // Search |
90 | case 191: // / = routes search |
91 | if (ev.shiftKey) search.activate('?', ev) |
92 | else search.activate('/', ev) |
93 | return |
94 | case 50: // @ = mention search |
95 | if (ev.shiftKey) search.activate('@', ev) |
96 | return |
97 | case 51: // # = channel search |
98 | if (ev.shiftKey) search.activate('#', ev) |
99 | return |
100 | case 53: // % = message search |
101 | if (ev.shiftKey) search.activate('%', ev) |
102 | } |
103 | } |
104 | |
105 | function goToMessage (ev, { goTo }) { |
106 | const msg = ev.target |
107 | |
108 | if (msg.dataset && msg.dataset.id) { |
109 | goTo(msg.dataset.id) |
110 | // TODO - rm the dataset.root decorator |
111 | } |
112 | } |
113 | |
114 | function toggleRawMessage (ev) { |
115 | const msg = ev.target |
116 | if (!msg.classList.contains('Message')) return |
117 | |
118 | // this uses a crudely exported nav api |
119 | msg.querySelector('.meta .toggle-raw-msg').click() |
120 | } |
121 |
Built with git-ssb-web