forked from jaywcjlove/hotkeys-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
141 lines (130 loc) · 4.72 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React, { useEffect, useState } from 'react';
import GithubCorner from '@uiw/react-github-corners';
import { Github } from '@uiw/react-shields';
import MarkdownPreview from '@uiw/react-markdown-preview';
import KeyBoard from '@uiw/react-mac-keyboard';
import '@wcj/dark-mode';
import Footer from './components/Footer';
import styles from './styles/index.module.less';
import DocumentStr from '../README.md';
import hotkeys from '..';
import pkg from '../package.json';
export default function AppRoot() {
const [keyCode, setKeyCode] = useState([]);
const [keyStr, setKeyStr] = useState([]);
useEffect(() => {
document.addEventListener('keyup', onKeyUpEvent);
function pkeys(keys, key) {
if (keys.indexOf(key) === -1) keys.push(key);
return keys;
}
function pkeysStr(keysStr, key) {
if (keysStr.indexOf(key) === -1) keysStr.push(key);
return keysStr;
}
hotkeys('*', (evn) => {
evn.preventDefault();
const keys = [];
const kStr = [];
if (hotkeys.shift) {
pkeys(keys, 16);
pkeysStr(kStr, 'shift');
}
if (hotkeys.ctrl) {
pkeys(keys, 17);
pkeysStr(kStr, 'ctrl');
}
if (hotkeys.alt) {
pkeys(keys, 18);
pkeysStr(kStr, 'alt');
}
if (hotkeys.control) {
pkeys(keys, 17);
pkeysStr(kStr, 'control');
}
if (hotkeys.command) {
pkeys(keys, 91);
pkeysStr(kStr, 'command');
}
kStr.push(evn.keyCode);
if (keys.indexOf(evn.keyCode) === -1) keys.push(evn.keyCode);
setKeyCode(keys);
setKeyStr(kStr);
});
return () => {
document.removeEventListener('keyup', onKeyUpEvent);
};
}, []);
let DocumentStrSource = DocumentStr;
if (DocumentStrSource) DocumentStrSource = DocumentStr.replace(/([\s\S]*)<!--dividing-->/, '');
const openVersionWebsite = (e) => {
if (e.target && e.target.value) {
window.location.href = e.target.value;
}
};
const onKeyUpEvent = () => {
setKeyCode([]);
setKeyStr([]);
};
const onKeyBoardMouseDown = (item) => {
if (item.keycode > -1) {
setKeyStr([item.keycode]);
}
};
const onKeyBoardMouseUp = () => setKeyStr([]);
return (
<div>
<div className={styles.tools}>
<select className={styles.version} onChange={openVersionWebsite}>
<option value="https://jaywcjlove.github.io/hotkeys-js">
v
{pkg.version}
</option>
<option value="https://unpkg.com/[email protected]/doc/index.html">v3.4.3</option>
<option value="https://unpkg.com/[email protected]/doc/index.html">v3.4.2</option>
<option value="https://unpkg.com/[email protected]/doc/index.html">v2.0.10</option>
</select>
<dark-mode permanent />
</div>
{keyStr.length > -1 && (
<div className={styles.keyCodeInfo}>
{keyStr.map((item) => <span key={`${item}`}>{item}</span>)}
</div>
)}
<GithubCorner href="https://github.com/jaywcjlove/hotkeys-js" target="__blank" />
<div className={styles.header}>
<div className={styles.title}>HotKeys.js</div>
<div className={styles.github}>
<a href="https://www.npmjs.com/package/hotkeys-js">
<button type="button">On NPM</button>
</a>
<a href="https://github.com/jaywcjlove/hotkeys-js/">
<button type="button">Fork on Github</button>
</a>
<a href="https://github.com/jaywcjlove/hotkeys-js/">
<button type="button">Doc on Github</button>
</a>
<a href="https://jaywcjlove.gitee.io/hotkeys/">
<button type="button">Doc on Gitee</button>
</a>
</div>
<div className={styles.info}>A robust Javascript library for capturing keyboard input and key combinations entered. It has no dependencies. Try to press your keyboard, The following button will highlight.</div>
</div>
<KeyBoard
style={{ top: -40 }}
onMouseDown={onKeyBoardMouseDown.bind(this)}
onMouseUp={onKeyBoardMouseUp}
keyCode={keyCode}
/>
<MarkdownPreview style={{ maxWidth: 995, margin: '0 auto' }} source={DocumentStrSource} />
<Footer name="Kenny Wong" href="http://jaywcjlove.github.io" year="2015-present">
<Github user="jaywcjlove" repo="hotkeys-js">
<Github.Social href="https://github.com/jaywcjlove/hotkeys-js" type="forks" />
<Github.Social href="https://github.com/jaywcjlove/hotkeys-js" type="stars" />
<Github.Social href="https://github.com/jaywcjlove/hotkeys-js" type="watchers" />
<Github.Social href="https://github.com/jaywcjlove/hotkeys-js" type="followers" />
</Github>
</Footer>
</div>
);
}