git ssb

16+

Dominic / patchbay



Tree: 3d3edbbfdd8ca30c4cc7695192b3883fb399ab26

Files: 3d3edbbfdd8ca30c4cc7695192b3883fb399ab26 / app / sync / catch-keyboard-shortcut.js

2940 bytesRaw
1const nest = require('depnest')
2
3exports.gives = nest('app.sync.catchKeyboardShortcut')
4
5exports.needs = nest({
6 'app.html.searchBar': 'first',
7 'app.html.tabs': 'first',
8 'app.sync.goTo': 'first',
9})
10
11var gPressed = false
12
13exports.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
29function isTextFieldEvent (ev) {
30 const tag = ev.target.nodeName
31 return (tag === 'INPUT' || tag === 'TEXTAREA')
32}
33
34function 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
46function 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
103function goToMessage (ev, { goTo }) {
104 const msg = ev.target
105 if (!msg.classList.contains('Message')) return
106
107 goTo(msg.dataset.id)
108 // TODO - rm the dataset.root decorator
109}
110
111function toggleRawMessage (ev) {
112 const msg = ev.target
113 if (!msg.classList.contains('Message')) return
114
115 // this uses a crudely exported nav api
116 msg.querySelector('.meta .toggle-raw-msg').click()
117}
118

Built with git-ssb-web