add term
This commit is contained in:
@ -1,23 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2015-present, Kenny Wong.
|
||||
|
||||
Copyright (c) 2011-2013 Thomas Fuchs (https://github.com/madrobby/keymaster)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -1,441 +0,0 @@
|
||||
# Hotkeys
|
||||
|
||||
<!--dividing-->
|
||||
|
||||
[](https://jaywcjlove.github.io/#/sponsor)
|
||||
[](https://www.npmjs.com/package/hotkeys-js)
|
||||
[](https://github.com/jaywcjlove/hotkeys/stargazers)
|
||||

|
||||
[](https://github.com/jaywcjlove/hotkeys-js/actions/workflows/ci.yml)
|
||||
[](https://coveralls.io/github/jaywcjlove/hotkeys?branch=master)
|
||||
[](https://github.com/jaywcjlove/hotkeys-js/blob/master/README-zh.md)
|
||||
[](https://gitee.com/jaywcjlove/hotkeys)
|
||||
|
||||
HotKeys.js is an input capture library with some very special features, it is easy to pick up and use, has a reasonable footprint ([~6kB](https://bundlephobia.com/result?p=hotkeys-js)) (gzipped: **`2.8kB`**), and has no dependencies. It should not interfere with any JavaScript libraries or frameworks. Official document [demo preview](https://jaywcjlove.github.io/hotkeys-js). [More examples](https://github.com/jaywcjlove/hotkeys-js/issues?q=label%3ADemo+).
|
||||
|
||||
```bash
|
||||
╭┈┈╮ ╭┈┈╮ ╭┈┈╮
|
||||
┆ ├┈┈..┈┈┈┈┈.┆ └┈╮┆ ├┈┈..┈┈┈┈┈..┈┈.┈┈..┈┈┈┈┈.
|
||||
┆ ┆┆ □ ┆┆ ┈┤┆ < ┆ -__┘┆ ┆ ┆┆__ ┈┈┤
|
||||
╰┈┈┴┈┈╯╰┈┈┈┈┈╯╰┈┈┈┈╯╰┈┈┴┈┈╯╰┈┈┈┈┈╯╰┈┈┈ ┆╰┈┈┈┈┈╯
|
||||
╰┈┈┈┈┈╯
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
You will need `Node.js` installed on your system.
|
||||
|
||||
```bash
|
||||
npm install hotkeys-js --save
|
||||
```
|
||||
|
||||
```js
|
||||
import hotkeys from 'hotkeys-js';
|
||||
|
||||
hotkeys('f5', function(event, handler){
|
||||
// Prevent the default refresh event under WINDOWS system
|
||||
event.preventDefault()
|
||||
alert('you pressed F5!')
|
||||
});
|
||||
```
|
||||
|
||||
Or manually download and link **hotkeys.js** in your HTML, It can also be downloaded via [UNPKG](https://unpkg.com/hotkeys-js/dist/):
|
||||
|
||||
CDN: [UNPKG](https://unpkg.com/hotkeys-js/dist/) | [jsDelivr](https://cdn.jsdelivr.net/npm/hotkeys-js@3.7.3/) | [Githack](https://raw.githack.com/jaywcjlove/hotkeys/master/dist/hotkeys.min.js) | [Statically](https://cdn.statically.io/gh/jaywcjlove/hotkeys/master/dist/hotkeys.min.js) | [bundle.run](https://bundle.run/hotkeys-js@3.7.3)
|
||||
|
||||
```html
|
||||
<script src="https://unpkg.com/hotkeys-js/dist/hotkeys.min.js"></script>
|
||||
<script type="text/javascript">
|
||||
hotkeys('ctrl+a,ctrl+b,r,f', function (event, handler){
|
||||
switch (handler.key) {
|
||||
case 'ctrl+a': alert('you pressed ctrl+a!');
|
||||
break;
|
||||
case 'ctrl+b': alert('you pressed ctrl+b!');
|
||||
break;
|
||||
case 'r': alert('you pressed r!');
|
||||
break;
|
||||
case 'f': alert('you pressed f!');
|
||||
break;
|
||||
default: alert(event);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
```
|
||||
|
||||
### Used in React
|
||||
|
||||
[react-hotkeys](https://github.com/jaywcjlove/react-hotkeys) is the React component that listen to keydown and keyup keyboard events, defining and dispatching keyboard shortcuts. Detailed use method please see its documentation [react-hotkeys](https://github.com/jaywcjlove/react-hotkeys).
|
||||
|
||||
[react-hotkeys-hook](https://github.com/JohannesKlauss/react-hotkeys-hook) - React hook for using keyboard shortcuts in components. Make sure that you have at least version 16.8 of react and react-dom installed, or otherwise hooks won't work for you.
|
||||
|
||||
## Browser Support
|
||||
|
||||
Hotkeys.js has been tested and should work in.
|
||||
|
||||
```shell
|
||||
Internet Explorer 6+
|
||||
Safari
|
||||
Firefox
|
||||
Chrome
|
||||
```
|
||||
|
||||
## Supported Keys
|
||||
|
||||
HotKeys understands the following modifiers: `⇧`, `shift`, `option`, `⌥`, `alt`, `ctrl`, `control`, `command`, and `⌘`.
|
||||
|
||||
The following special keys can be used for shortcuts: backspace, tab, clear, enter, return, esc, escape, space, up, down, left, right, home, end, pageup, pagedown, del, delete, f1 through f19, num_0 through num_9, num_multiply, num_add, num_enter, num_subtract, num_decimal, num_divide.
|
||||
|
||||
`⌘` Command()
|
||||
`⌃` Control
|
||||
`⌥` Option(alt)
|
||||
`⇧` Shift
|
||||
`⇪` Caps Lock(Capital)
|
||||
~~`fn` Does not support fn~~
|
||||
`↩︎` return/Enter space
|
||||
|
||||
## Defining Shortcuts
|
||||
|
||||
One global method is exposed, key which defines shortcuts when called directly.
|
||||
|
||||
```js
|
||||
hotkeys([keys:<String>], [option:[string|object|function]], [callback:<function>])
|
||||
```
|
||||
|
||||
|
||||
```js
|
||||
hotkeys('f5', function(event, handler) {
|
||||
// Prevent the default refresh event under WINDOWS system
|
||||
event.preventDefault();
|
||||
alert('you pressed F5!');
|
||||
});
|
||||
|
||||
// Returning false stops the event and prevents default browser events
|
||||
// Mac OS system defines `command + r` as a refresh shortcut
|
||||
hotkeys('ctrl+r, command+r', function() {
|
||||
alert('stopped reload!');
|
||||
return false;
|
||||
});
|
||||
|
||||
// Single key
|
||||
hotkeys('a', function(event,handler){
|
||||
//event.srcElement: input
|
||||
//event.target: input
|
||||
if(event.target === "input"){
|
||||
alert('you pressed a!')
|
||||
}
|
||||
alert('you pressed a!')
|
||||
});
|
||||
|
||||
// Key Combination
|
||||
hotkeys('ctrl+a,ctrl+b,r,f', function (event, handler){
|
||||
switch (handler.key) {
|
||||
case 'ctrl+a': alert('you pressed ctrl+a!');
|
||||
break;
|
||||
case 'ctrl+b': alert('you pressed ctrl+b!');
|
||||
break;
|
||||
case 'r': alert('you pressed r!');
|
||||
break;
|
||||
case 'f': alert('you pressed f!');
|
||||
break;
|
||||
default: alert(event);
|
||||
}
|
||||
});
|
||||
|
||||
hotkeys('ctrl+a+s', function() {
|
||||
alert('you pressed ctrl+a+s!');
|
||||
});
|
||||
|
||||
// Using a scope
|
||||
hotkeys('*','wcj', function(event){
|
||||
console.log('do something', event);
|
||||
});
|
||||
```
|
||||
|
||||
#### option
|
||||
|
||||
- `scope<String>`
|
||||
- `element<HTMLElement>`
|
||||
- `keyup<Boolean>`
|
||||
- `keydown<Boolean>`
|
||||
- `splitKey<string>` (default is `+`)
|
||||
- `capture<Boolean>`
|
||||
- `single<Boolean>`
|
||||
|
||||
```js
|
||||
hotkeys('o, enter', {
|
||||
scope: 'wcj',
|
||||
element: document.getElementById('wrapper'),
|
||||
}, function() {
|
||||
console.log('do something else');
|
||||
});
|
||||
|
||||
hotkeys('ctrl-+', { splitKey: '-' }, function(e) {
|
||||
console.log('you pressed ctrl and +');
|
||||
});
|
||||
|
||||
hotkeys('+', { splitKey: '-' }, function(e){
|
||||
console.log('you pressed +');
|
||||
})
|
||||
```
|
||||
|
||||
**keyup**
|
||||
|
||||
**key down** and **key up** both perform callback events.
|
||||
|
||||
```js
|
||||
hotkeys('ctrl+a,alt+a+s', {keyup: true}, function(event, handler) {
|
||||
if (event.type === 'keydown') {
|
||||
console.log('keydown:', event.type, handler, handler.key);
|
||||
}
|
||||
|
||||
if (event.type === 'keyup') {
|
||||
console.log('keyup:', event.type, handler, handler.key);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
## API REFERENCE
|
||||
|
||||
Asterisk "*"
|
||||
|
||||
Modifier key judgments
|
||||
|
||||
```js
|
||||
hotkeys('*', function() {
|
||||
if (hotkeys.shift) {
|
||||
console.log('shift is pressed!');
|
||||
}
|
||||
|
||||
if (hotkeys.ctrl) {
|
||||
console.log('ctrl is pressed!');
|
||||
}
|
||||
|
||||
if (hotkeys.alt) {
|
||||
console.log('alt is pressed!');
|
||||
}
|
||||
|
||||
if (hotkeys.option) {
|
||||
console.log('option is pressed!');
|
||||
}
|
||||
|
||||
if (hotkeys.control) {
|
||||
console.log('control is pressed!');
|
||||
}
|
||||
|
||||
if (hotkeys.cmd) {
|
||||
console.log('cmd is pressed!');
|
||||
}
|
||||
|
||||
if (hotkeys.command) {
|
||||
console.log('command is pressed!');
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### setScope
|
||||
|
||||
Use the `hotkeys.setScope` method to set scope. There can only be one active scope besides 'all'. By default 'all' is always active.
|
||||
|
||||
```js
|
||||
// Define shortcuts with a scope
|
||||
hotkeys('ctrl+o, ctrl+alt+enter', 'issues', function() {
|
||||
console.log('do something');
|
||||
});
|
||||
hotkeys('o, enter', 'files', function() {
|
||||
console.log('do something else');
|
||||
});
|
||||
|
||||
// Set the scope (only 'all' and 'issues' shortcuts will be honored)
|
||||
hotkeys.setScope('issues'); // default scope is 'all'
|
||||
```
|
||||
|
||||
### getScope
|
||||
|
||||
Use the `hotkeys.getScope` method to get scope.
|
||||
|
||||
```js
|
||||
hotkeys.getScope();
|
||||
```
|
||||
|
||||
### deleteScope
|
||||
|
||||
Use the `hotkeys.deleteScope` method to delete a scope. This will also remove all associated hotkeys with it.
|
||||
|
||||
```js
|
||||
hotkeys.deleteScope('issues');
|
||||
```
|
||||
You can use second argument, if need set new scope after deleting.
|
||||
|
||||
```js
|
||||
hotkeys.deleteScope('issues', 'newScopeName');
|
||||
```
|
||||
|
||||
### unbind
|
||||
|
||||
Similar to defining shortcuts, they can be unbound using `hotkeys.unbind`.
|
||||
|
||||
```js
|
||||
// unbind 'a' handler
|
||||
hotkeys.unbind('a');
|
||||
|
||||
// Unbind a hotkeys only for a single scope
|
||||
// If no scope is specified it defaults to the current scope (hotkeys.getScope())
|
||||
hotkeys.unbind('o, enter', 'issues');
|
||||
hotkeys.unbind('o, enter', 'files');
|
||||
```
|
||||
|
||||
Unbind events through functions.
|
||||
|
||||
```js
|
||||
function example() {
|
||||
hotkeys('a', example);
|
||||
hotkeys.unbind('a', example);
|
||||
|
||||
hotkeys('a', 'issues', example);
|
||||
hotkeys.unbind('a', 'issues', example);
|
||||
}
|
||||
```
|
||||
|
||||
To unbind everything.
|
||||
|
||||
```js
|
||||
hotkeys.unbind();
|
||||
```
|
||||
|
||||
### isPressed
|
||||
|
||||
For example, `hotkeys.isPressed(77)` is true if the `M` key is currently pressed.
|
||||
|
||||
```js
|
||||
hotkeys('a', function() {
|
||||
console.log(hotkeys.isPressed('a')); //=> true
|
||||
console.log(hotkeys.isPressed('A')); //=> true
|
||||
console.log(hotkeys.isPressed(65)); //=> true
|
||||
});
|
||||
```
|
||||
|
||||
### trigger
|
||||
|
||||
trigger shortcut key event
|
||||
|
||||
```js
|
||||
hotkeys.trigger('ctrl+o');
|
||||
hotkeys.trigger('ctrl+o', 'scope2');
|
||||
```
|
||||
|
||||
### getPressedKeyCodes
|
||||
|
||||
Returns an array of key codes currently pressed.
|
||||
|
||||
```js
|
||||
hotkeys('command+ctrl+shift+a,f', function() {
|
||||
console.log(hotkeys.getPressedKeyCodes()); //=> [17, 65] or [70]
|
||||
})
|
||||
```
|
||||
|
||||
### getPressedKeyString
|
||||
|
||||
Returns an array of key codes currently pressed.
|
||||
|
||||
```js
|
||||
hotkeys('command+ctrl+shift+a,f', function() {
|
||||
console.log(hotkeys.getPressedKeyString()); //=> ['⌘', '⌃', '⇧', 'A', 'F']
|
||||
})
|
||||
```
|
||||
|
||||
### getAllKeyCodes
|
||||
|
||||
Get a list of all registration codes.
|
||||
|
||||
```js
|
||||
hotkeys('command+ctrl+shift+a,f', function() {
|
||||
console.log(hotkeys.getAllKeyCodes());
|
||||
// [
|
||||
// { scope: 'all', shortcut: 'command+ctrl+shift+a', mods: [91, 17, 16], keys: [91, 17, 16, 65] },
|
||||
// { scope: 'all', shortcut: 'f', mods: [], keys: [42] }
|
||||
// ]
|
||||
})
|
||||
```
|
||||
|
||||
### filter
|
||||
|
||||
By default hotkeys are not enabled for `INPUT` `SELECT` `TEXTAREA` elements. `Hotkeys.filter` to return to the `true` shortcut keys set to play a role, `false` shortcut keys set up failure.
|
||||
|
||||
```js
|
||||
hotkeys.filter = function(event){
|
||||
return true;
|
||||
}
|
||||
//How to add the filter to edit labels. <div contentEditable="true"></div>
|
||||
//"contentEditable" Older browsers that do not support drops
|
||||
hotkeys.filter = function(event) {
|
||||
var target = event.target || event.srcElement;
|
||||
var tagName = target.tagName;
|
||||
return !(target.isContentEditable || tagName == 'INPUT' || tagName == 'SELECT' || tagName == 'TEXTAREA');
|
||||
}
|
||||
|
||||
hotkeys.filter = function(event){
|
||||
var tagName = (event.target || event.srcElement).tagName;
|
||||
hotkeys.setScope(/^(INPUT|TEXTAREA|SELECT)$/.test(tagName) ? 'input' : 'other');
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
### noConflict
|
||||
|
||||
Relinquish HotKeys’s control of the `hotkeys` variable.
|
||||
|
||||
```js
|
||||
var k = hotkeys.noConflict();
|
||||
k('a', function() {
|
||||
console.log("do something")
|
||||
});
|
||||
|
||||
hotkeys()
|
||||
// -->Uncaught TypeError: hotkeys is not a function(anonymous function)
|
||||
// @ VM2170:2InjectedScript._evaluateOn
|
||||
// @ VM2165:883InjectedScript._evaluateAndWrap
|
||||
// @ VM2165:816InjectedScript.evaluate @ VM2165:682
|
||||
```
|
||||
|
||||
## Development
|
||||
|
||||
To develop, Install dependencies, Get the code:
|
||||
|
||||
```shell
|
||||
$ git https://github.com/jaywcjlove/hotkeys.git
|
||||
$ cd hotkeys # Into the directory
|
||||
$ npm install # or yarn install
|
||||
```
|
||||
|
||||
To develop, run the self-reloading build:
|
||||
|
||||
```shell
|
||||
$ npm run watch
|
||||
```
|
||||
|
||||
Run Document Website Environment.
|
||||
|
||||
```shell
|
||||
$ npm run doc
|
||||
```
|
||||
|
||||
To contribute, please fork Hotkeys.js, add your patch and tests for it (in the `test/` folder) and submit a pull request.
|
||||
|
||||
```shell
|
||||
$ npm run test
|
||||
$ npm run test:watch # Development model
|
||||
```
|
||||
|
||||
## Contributors
|
||||
|
||||
As always, thanks to our amazing contributors!
|
||||
|
||||
<a href="https://github.com/jaywcjlove/hotkeys-js/graphs/contributors">
|
||||
<img src="https://jaywcjlove.github.io/hotkeys-js/CONTRIBUTORS.svg" />
|
||||
</a>
|
||||
|
||||
Made with [github-action-contributors](https://github.com/jaywcjlove/github-action-contributors).
|
||||
|
||||
## License
|
||||
|
||||
[MIT © Kenny Wong](./LICENSE)
|
13
static/pkg/hotkeys-js/dist/README.md
vendored
13
static/pkg/hotkeys-js/dist/README.md
vendored
@ -1,13 +0,0 @@
|
||||
|
||||
|
||||
Explanation of Build Files
|
||||
---
|
||||
|
||||
| UMD | Size | CommonJS | Size | ES Module | Size |
|
||||
| ---- | ---- | ---- | ---- | ---- | ---- |
|
||||
| hotkeys.js | 8.37kb | hotkeys.common.js | 8.13kb | hotkeys.esm.js | 8.12kb |
|
||||
| hotkeys.min.js | 3.62kb (gzipped: 1.73kb) | hotkeys.common.min.js | (gzipped: 1.84kb) | - | - |
|
||||
|
||||
- [UMD](https://github.com/umdjs/umd): UMD builds can be used directly in the browser via a `<script>` tag.
|
||||
- [CommonJS](http://wiki.commonjs.org/wiki/Modules/1.1): CommonJS builds are intended for use with older bundlers like [browserify](http://browserify.org/) or [webpack 1](https://webpack.github.io/).
|
||||
- [ES Module](http://exploringjs.com/es6/ch_modules.html): ES module builds are intended for use with modern bundlers like [webpack 2](https://webpack.js.org/) or [rollup](http://rollupjs.org/).
|
677
static/pkg/hotkeys-js/dist/hotkeys.common.js
vendored
677
static/pkg/hotkeys-js/dist/hotkeys.common.js
vendored
@ -1,677 +0,0 @@
|
||||
/**!
|
||||
* hotkeys-js v3.13.7
|
||||
* A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
|
||||
*
|
||||
* Copyright (c) 2024 kenny wong <wowohoo@qq.com>
|
||||
* https://github.com/jaywcjlove/hotkeys-js.git
|
||||
*
|
||||
* @website: https://jaywcjlove.github.io/hotkeys-js
|
||||
|
||||
* Licensed under the MIT license
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false;
|
||||
|
||||
// 绑定事件
|
||||
function addEvent(object, event, method, useCapture) {
|
||||
if (object.addEventListener) {
|
||||
object.addEventListener(event, method, useCapture);
|
||||
} else if (object.attachEvent) {
|
||||
object.attachEvent("on".concat(event), method);
|
||||
}
|
||||
}
|
||||
function removeEvent(object, event, method, useCapture) {
|
||||
if (object.removeEventListener) {
|
||||
object.removeEventListener(event, method, useCapture);
|
||||
} else if (object.detachEvent) {
|
||||
object.detachEvent("on".concat(event), method);
|
||||
}
|
||||
}
|
||||
|
||||
// 修饰键转换成对应的键码
|
||||
function getMods(modifier, key) {
|
||||
const mods = key.slice(0, key.length - 1);
|
||||
for (let i = 0; i < mods.length; i++) mods[i] = modifier[mods[i].toLowerCase()];
|
||||
return mods;
|
||||
}
|
||||
|
||||
// 处理传的key字符串转换成数组
|
||||
function getKeys(key) {
|
||||
if (typeof key !== 'string') key = '';
|
||||
key = key.replace(/\s/g, ''); // 匹配任何空白字符,包括空格、制表符、换页符等等
|
||||
const keys = key.split(','); // 同时设置多个快捷键,以','分割
|
||||
let index = keys.lastIndexOf('');
|
||||
|
||||
// 快捷键可能包含',',需特殊处理
|
||||
for (; index >= 0;) {
|
||||
keys[index - 1] += ',';
|
||||
keys.splice(index, 1);
|
||||
index = keys.lastIndexOf('');
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
// 比较修饰键的数组
|
||||
function compareArray(a1, a2) {
|
||||
const arr1 = a1.length >= a2.length ? a1 : a2;
|
||||
const arr2 = a1.length >= a2.length ? a2 : a1;
|
||||
let isIndex = true;
|
||||
for (let i = 0; i < arr1.length; i++) {
|
||||
if (arr2.indexOf(arr1[i]) === -1) isIndex = false;
|
||||
}
|
||||
return isIndex;
|
||||
}
|
||||
|
||||
// Special Keys
|
||||
const _keyMap = {
|
||||
backspace: 8,
|
||||
'⌫': 8,
|
||||
tab: 9,
|
||||
clear: 12,
|
||||
enter: 13,
|
||||
'↩': 13,
|
||||
return: 13,
|
||||
esc: 27,
|
||||
escape: 27,
|
||||
space: 32,
|
||||
left: 37,
|
||||
up: 38,
|
||||
right: 39,
|
||||
down: 40,
|
||||
del: 46,
|
||||
delete: 46,
|
||||
ins: 45,
|
||||
insert: 45,
|
||||
home: 36,
|
||||
end: 35,
|
||||
pageup: 33,
|
||||
pagedown: 34,
|
||||
capslock: 20,
|
||||
num_0: 96,
|
||||
num_1: 97,
|
||||
num_2: 98,
|
||||
num_3: 99,
|
||||
num_4: 100,
|
||||
num_5: 101,
|
||||
num_6: 102,
|
||||
num_7: 103,
|
||||
num_8: 104,
|
||||
num_9: 105,
|
||||
num_multiply: 106,
|
||||
num_add: 107,
|
||||
num_enter: 108,
|
||||
num_subtract: 109,
|
||||
num_decimal: 110,
|
||||
num_divide: 111,
|
||||
'⇪': 20,
|
||||
',': 188,
|
||||
'.': 190,
|
||||
'/': 191,
|
||||
'`': 192,
|
||||
'-': isff ? 173 : 189,
|
||||
'=': isff ? 61 : 187,
|
||||
';': isff ? 59 : 186,
|
||||
'\'': 222,
|
||||
'[': 219,
|
||||
']': 221,
|
||||
'\\': 220
|
||||
};
|
||||
|
||||
// Modifier Keys
|
||||
const _modifier = {
|
||||
// shiftKey
|
||||
'⇧': 16,
|
||||
shift: 16,
|
||||
// altKey
|
||||
'⌥': 18,
|
||||
alt: 18,
|
||||
option: 18,
|
||||
// ctrlKey
|
||||
'⌃': 17,
|
||||
ctrl: 17,
|
||||
control: 17,
|
||||
// metaKey
|
||||
'⌘': 91,
|
||||
cmd: 91,
|
||||
command: 91
|
||||
};
|
||||
const modifierMap = {
|
||||
16: 'shiftKey',
|
||||
18: 'altKey',
|
||||
17: 'ctrlKey',
|
||||
91: 'metaKey',
|
||||
shiftKey: 16,
|
||||
ctrlKey: 17,
|
||||
altKey: 18,
|
||||
metaKey: 91
|
||||
};
|
||||
const _mods = {
|
||||
16: false,
|
||||
18: false,
|
||||
17: false,
|
||||
91: false
|
||||
};
|
||||
const _handlers = {};
|
||||
|
||||
// F1~F12 special key
|
||||
for (let k = 1; k < 20; k++) {
|
||||
_keyMap["f".concat(k)] = 111 + k;
|
||||
}
|
||||
|
||||
let _downKeys = []; // 记录摁下的绑定键
|
||||
let winListendFocus = null; // window是否已经监听了focus事件
|
||||
let _scope = 'all'; // 默认热键范围
|
||||
const elementEventMap = new Map(); // 已绑定事件的节点记录
|
||||
|
||||
// 返回键码
|
||||
const code = x => _keyMap[x.toLowerCase()] || _modifier[x.toLowerCase()] || x.toUpperCase().charCodeAt(0);
|
||||
const getKey = x => Object.keys(_keyMap).find(k => _keyMap[k] === x);
|
||||
const getModifier = x => Object.keys(_modifier).find(k => _modifier[k] === x);
|
||||
|
||||
// 设置获取当前范围(默认为'所有')
|
||||
function setScope(scope) {
|
||||
_scope = scope || 'all';
|
||||
}
|
||||
// 获取当前范围
|
||||
function getScope() {
|
||||
return _scope || 'all';
|
||||
}
|
||||
// 获取摁下绑定键的键值
|
||||
function getPressedKeyCodes() {
|
||||
return _downKeys.slice(0);
|
||||
}
|
||||
function getPressedKeyString() {
|
||||
return _downKeys.map(c => getKey(c) || getModifier(c) || String.fromCharCode(c));
|
||||
}
|
||||
function getAllKeyCodes() {
|
||||
const result = [];
|
||||
Object.keys(_handlers).forEach(k => {
|
||||
_handlers[k].forEach(_ref => {
|
||||
let {
|
||||
key,
|
||||
scope,
|
||||
mods,
|
||||
shortcut
|
||||
} = _ref;
|
||||
result.push({
|
||||
scope,
|
||||
shortcut,
|
||||
mods,
|
||||
keys: key.split('+').map(v => code(v))
|
||||
});
|
||||
});
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
// 表单控件控件判断 返回 Boolean
|
||||
// hotkey is effective only when filter return true
|
||||
function filter(event) {
|
||||
const target = event.target || event.srcElement;
|
||||
const {
|
||||
tagName
|
||||
} = target;
|
||||
let flag = true;
|
||||
const isInput = tagName === 'INPUT' && !['checkbox', 'radio', 'range', 'button', 'file', 'reset', 'submit', 'color'].includes(target.type);
|
||||
// ignore: isContentEditable === 'true', <input> and <textarea> when readOnly state is false, <select>
|
||||
if (target.isContentEditable || (isInput || tagName === 'TEXTAREA' || tagName === 'SELECT') && !target.readOnly) {
|
||||
flag = false;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
// 判断摁下的键是否为某个键,返回true或者false
|
||||
function isPressed(keyCode) {
|
||||
if (typeof keyCode === 'string') {
|
||||
keyCode = code(keyCode); // 转换成键码
|
||||
}
|
||||
return _downKeys.indexOf(keyCode) !== -1;
|
||||
}
|
||||
|
||||
// 循环删除handlers中的所有 scope(范围)
|
||||
function deleteScope(scope, newScope) {
|
||||
let handlers;
|
||||
let i;
|
||||
|
||||
// 没有指定scope,获取scope
|
||||
if (!scope) scope = getScope();
|
||||
for (const key in _handlers) {
|
||||
if (Object.prototype.hasOwnProperty.call(_handlers, key)) {
|
||||
handlers = _handlers[key];
|
||||
for (i = 0; i < handlers.length;) {
|
||||
if (handlers[i].scope === scope) {
|
||||
const deleteItems = handlers.splice(i, 1);
|
||||
deleteItems.forEach(_ref2 => {
|
||||
let {
|
||||
element
|
||||
} = _ref2;
|
||||
return removeKeyEvent(element);
|
||||
});
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果scope被删除,将scope重置为all
|
||||
if (getScope() === scope) setScope(newScope || 'all');
|
||||
}
|
||||
|
||||
// 清除修饰键
|
||||
function clearModifier(event) {
|
||||
let key = event.keyCode || event.which || event.charCode;
|
||||
const i = _downKeys.indexOf(key);
|
||||
|
||||
// 从列表中清除按压过的键
|
||||
if (i >= 0) {
|
||||
_downKeys.splice(i, 1);
|
||||
}
|
||||
// 特殊处理 cmmand 键,在 cmmand 组合快捷键 keyup 只执行一次的问题
|
||||
if (event.key && event.key.toLowerCase() === 'meta') {
|
||||
_downKeys.splice(0, _downKeys.length);
|
||||
}
|
||||
|
||||
// 修饰键 shiftKey altKey ctrlKey (command||metaKey) 清除
|
||||
if (key === 93 || key === 224) key = 91;
|
||||
if (key in _mods) {
|
||||
_mods[key] = false;
|
||||
|
||||
// 将修饰键重置为false
|
||||
for (const k in _modifier) if (_modifier[k] === key) hotkeys[k] = false;
|
||||
}
|
||||
}
|
||||
function unbind(keysInfo) {
|
||||
// unbind(), unbind all keys
|
||||
if (typeof keysInfo === 'undefined') {
|
||||
Object.keys(_handlers).forEach(key => {
|
||||
Array.isArray(_handlers[key]) && _handlers[key].forEach(info => eachUnbind(info));
|
||||
delete _handlers[key];
|
||||
});
|
||||
removeKeyEvent(null);
|
||||
} else if (Array.isArray(keysInfo)) {
|
||||
// support like : unbind([{key: 'ctrl+a', scope: 's1'}, {key: 'ctrl-a', scope: 's2', splitKey: '-'}])
|
||||
keysInfo.forEach(info => {
|
||||
if (info.key) eachUnbind(info);
|
||||
});
|
||||
} else if (typeof keysInfo === 'object') {
|
||||
// support like unbind({key: 'ctrl+a, ctrl+b', scope:'abc'})
|
||||
if (keysInfo.key) eachUnbind(keysInfo);
|
||||
} else if (typeof keysInfo === 'string') {
|
||||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
||||
args[_key - 1] = arguments[_key];
|
||||
}
|
||||
// support old method
|
||||
// eslint-disable-line
|
||||
let [scope, method] = args;
|
||||
if (typeof scope === 'function') {
|
||||
method = scope;
|
||||
scope = '';
|
||||
}
|
||||
eachUnbind({
|
||||
key: keysInfo,
|
||||
scope,
|
||||
method,
|
||||
splitKey: '+'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 解除绑定某个范围的快捷键
|
||||
const eachUnbind = _ref3 => {
|
||||
let {
|
||||
key,
|
||||
scope,
|
||||
method,
|
||||
splitKey = '+'
|
||||
} = _ref3;
|
||||
const multipleKeys = getKeys(key);
|
||||
multipleKeys.forEach(originKey => {
|
||||
const unbindKeys = originKey.split(splitKey);
|
||||
const len = unbindKeys.length;
|
||||
const lastKey = unbindKeys[len - 1];
|
||||
const keyCode = lastKey === '*' ? '*' : code(lastKey);
|
||||
if (!_handlers[keyCode]) return;
|
||||
// 判断是否传入范围,没有就获取范围
|
||||
if (!scope) scope = getScope();
|
||||
const mods = len > 1 ? getMods(_modifier, unbindKeys) : [];
|
||||
const unbindElements = [];
|
||||
_handlers[keyCode] = _handlers[keyCode].filter(record => {
|
||||
// 通过函数判断,是否解除绑定,函数相等直接返回
|
||||
const isMatchingMethod = method ? record.method === method : true;
|
||||
const isUnbind = isMatchingMethod && record.scope === scope && compareArray(record.mods, mods);
|
||||
if (isUnbind) unbindElements.push(record.element);
|
||||
return !isUnbind;
|
||||
});
|
||||
unbindElements.forEach(element => removeKeyEvent(element));
|
||||
});
|
||||
};
|
||||
|
||||
// 对监听对应快捷键的回调函数进行处理
|
||||
function eventHandler(event, handler, scope, element) {
|
||||
if (handler.element !== element) {
|
||||
return;
|
||||
}
|
||||
let modifiersMatch;
|
||||
|
||||
// 看它是否在当前范围
|
||||
if (handler.scope === scope || handler.scope === 'all') {
|
||||
// 检查是否匹配修饰符(如果有返回true)
|
||||
modifiersMatch = handler.mods.length > 0;
|
||||
for (const y in _mods) {
|
||||
if (Object.prototype.hasOwnProperty.call(_mods, y)) {
|
||||
if (!_mods[y] && handler.mods.indexOf(+y) > -1 || _mods[y] && handler.mods.indexOf(+y) === -1) {
|
||||
modifiersMatch = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 调用处理程序,如果是修饰键不做处理
|
||||
if (handler.mods.length === 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91] || modifiersMatch || handler.shortcut === '*') {
|
||||
handler.keys = [];
|
||||
handler.keys = handler.keys.concat(_downKeys);
|
||||
if (handler.method(event, handler) === false) {
|
||||
if (event.preventDefault) event.preventDefault();else event.returnValue = false;
|
||||
if (event.stopPropagation) event.stopPropagation();
|
||||
if (event.cancelBubble) event.cancelBubble = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理keydown事件
|
||||
function dispatch(event, element) {
|
||||
const asterisk = _handlers['*'];
|
||||
let key = event.keyCode || event.which || event.charCode;
|
||||
|
||||
// 表单控件过滤 默认表单控件不触发快捷键
|
||||
if (!hotkeys.filter.call(this, event)) return;
|
||||
|
||||
// Gecko(Firefox)的command键值224,在Webkit(Chrome)中保持一致
|
||||
// Webkit左右 command 键值不一样
|
||||
if (key === 93 || key === 224) key = 91;
|
||||
|
||||
/**
|
||||
* Collect bound keys
|
||||
* If an Input Method Editor is processing key input and the event is keydown, return 229.
|
||||
* https://stackoverflow.com/questions/25043934/is-it-ok-to-ignore-keydown-events-with-keycode-229
|
||||
* http://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html
|
||||
*/
|
||||
if (_downKeys.indexOf(key) === -1 && key !== 229) _downKeys.push(key);
|
||||
/**
|
||||
* Jest test cases are required.
|
||||
* ===============================
|
||||
*/
|
||||
['ctrlKey', 'altKey', 'shiftKey', 'metaKey'].forEach(keyName => {
|
||||
const keyNum = modifierMap[keyName];
|
||||
if (event[keyName] && _downKeys.indexOf(keyNum) === -1) {
|
||||
_downKeys.push(keyNum);
|
||||
} else if (!event[keyName] && _downKeys.indexOf(keyNum) > -1) {
|
||||
_downKeys.splice(_downKeys.indexOf(keyNum), 1);
|
||||
} else if (keyName === 'metaKey' && event[keyName] && _downKeys.length === 3) {
|
||||
/**
|
||||
* Fix if Command is pressed:
|
||||
* ===============================
|
||||
*/
|
||||
if (!(event.ctrlKey || event.shiftKey || event.altKey)) {
|
||||
_downKeys = _downKeys.slice(_downKeys.indexOf(keyNum));
|
||||
}
|
||||
}
|
||||
});
|
||||
/**
|
||||
* -------------------------------
|
||||
*/
|
||||
|
||||
if (key in _mods) {
|
||||
_mods[key] = true;
|
||||
|
||||
// 将特殊字符的key注册到 hotkeys 上
|
||||
for (const k in _modifier) {
|
||||
if (_modifier[k] === key) hotkeys[k] = true;
|
||||
}
|
||||
if (!asterisk) return;
|
||||
}
|
||||
|
||||
// 将 modifierMap 里面的修饰键绑定到 event 中
|
||||
for (const e in _mods) {
|
||||
if (Object.prototype.hasOwnProperty.call(_mods, e)) {
|
||||
_mods[e] = event[modifierMap[e]];
|
||||
}
|
||||
}
|
||||
/**
|
||||
* https://github.com/jaywcjlove/hotkeys/pull/129
|
||||
* This solves the issue in Firefox on Windows where hotkeys corresponding to special characters would not trigger.
|
||||
* An example of this is ctrl+alt+m on a Swedish keyboard which is used to type μ.
|
||||
* Browser support: https://caniuse.com/#feat=keyboardevent-getmodifierstate
|
||||
*/
|
||||
if (event.getModifierState && !(event.altKey && !event.ctrlKey) && event.getModifierState('AltGraph')) {
|
||||
if (_downKeys.indexOf(17) === -1) {
|
||||
_downKeys.push(17);
|
||||
}
|
||||
if (_downKeys.indexOf(18) === -1) {
|
||||
_downKeys.push(18);
|
||||
}
|
||||
_mods[17] = true;
|
||||
_mods[18] = true;
|
||||
}
|
||||
|
||||
// 获取范围 默认为 `all`
|
||||
const scope = getScope();
|
||||
// 对任何快捷键都需要做的处理
|
||||
if (asterisk) {
|
||||
for (let i = 0; i < asterisk.length; i++) {
|
||||
if (asterisk[i].scope === scope && (event.type === 'keydown' && asterisk[i].keydown || event.type === 'keyup' && asterisk[i].keyup)) {
|
||||
eventHandler(event, asterisk[i], scope, element);
|
||||
}
|
||||
}
|
||||
}
|
||||
// key 不在 _handlers 中返回
|
||||
if (!(key in _handlers)) return;
|
||||
const handlerKey = _handlers[key];
|
||||
const keyLen = handlerKey.length;
|
||||
for (let i = 0; i < keyLen; i++) {
|
||||
if (event.type === 'keydown' && handlerKey[i].keydown || event.type === 'keyup' && handlerKey[i].keyup) {
|
||||
if (handlerKey[i].key) {
|
||||
const record = handlerKey[i];
|
||||
const {
|
||||
splitKey
|
||||
} = record;
|
||||
const keyShortcut = record.key.split(splitKey);
|
||||
const _downKeysCurrent = []; // 记录当前按键键值
|
||||
for (let a = 0; a < keyShortcut.length; a++) {
|
||||
_downKeysCurrent.push(code(keyShortcut[a]));
|
||||
}
|
||||
if (_downKeysCurrent.sort().join('') === _downKeys.sort().join('')) {
|
||||
// 找到处理内容
|
||||
eventHandler(event, record, scope, element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
function hotkeys(key, option, method) {
|
||||
_downKeys = [];
|
||||
const keys = getKeys(key); // 需要处理的快捷键列表
|
||||
let mods = [];
|
||||
let scope = 'all'; // scope默认为all,所有范围都有效
|
||||
let element = document; // 快捷键事件绑定节点
|
||||
let i = 0;
|
||||
let keyup = false;
|
||||
let keydown = true;
|
||||
let splitKey = '+';
|
||||
let capture = false;
|
||||
let single = false; // 单个callback
|
||||
|
||||
// 对为设定范围的判断
|
||||
if (method === undefined && typeof option === 'function') {
|
||||
method = option;
|
||||
}
|
||||
if (Object.prototype.toString.call(option) === '[object Object]') {
|
||||
if (option.scope) scope = option.scope; // eslint-disable-line
|
||||
if (option.element) element = option.element; // eslint-disable-line
|
||||
if (option.keyup) keyup = option.keyup; // eslint-disable-line
|
||||
if (option.keydown !== undefined) keydown = option.keydown; // eslint-disable-line
|
||||
if (option.capture !== undefined) capture = option.capture; // eslint-disable-line
|
||||
if (typeof option.splitKey === 'string') splitKey = option.splitKey; // eslint-disable-line
|
||||
if (option.single === true) single = true; // eslint-disable-line
|
||||
}
|
||||
if (typeof option === 'string') scope = option;
|
||||
|
||||
// 如果只允许单个callback,先unbind
|
||||
if (single) unbind(key, scope);
|
||||
|
||||
// 对于每个快捷键进行处理
|
||||
for (; i < keys.length; i++) {
|
||||
key = keys[i].split(splitKey); // 按键列表
|
||||
mods = [];
|
||||
|
||||
// 如果是组合快捷键取得组合快捷键
|
||||
if (key.length > 1) mods = getMods(_modifier, key);
|
||||
|
||||
// 将非修饰键转化为键码
|
||||
key = key[key.length - 1];
|
||||
key = key === '*' ? '*' : code(key); // *表示匹配所有快捷键
|
||||
|
||||
// 判断key是否在_handlers中,不在就赋一个空数组
|
||||
if (!(key in _handlers)) _handlers[key] = [];
|
||||
_handlers[key].push({
|
||||
keyup,
|
||||
keydown,
|
||||
scope,
|
||||
mods,
|
||||
shortcut: keys[i],
|
||||
method,
|
||||
key: keys[i],
|
||||
splitKey,
|
||||
element
|
||||
});
|
||||
}
|
||||
// 在全局document上设置快捷键
|
||||
if (typeof element !== 'undefined' && window) {
|
||||
if (!elementEventMap.has(element)) {
|
||||
const keydownListener = function () {
|
||||
let event = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.event;
|
||||
return dispatch(event, element);
|
||||
};
|
||||
const keyupListenr = function () {
|
||||
let event = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.event;
|
||||
dispatch(event, element);
|
||||
clearModifier(event);
|
||||
};
|
||||
elementEventMap.set(element, {
|
||||
keydownListener,
|
||||
keyupListenr,
|
||||
capture
|
||||
});
|
||||
addEvent(element, 'keydown', keydownListener, capture);
|
||||
addEvent(element, 'keyup', keyupListenr, capture);
|
||||
}
|
||||
if (!winListendFocus) {
|
||||
const listener = () => {
|
||||
_downKeys = [];
|
||||
};
|
||||
winListendFocus = {
|
||||
listener,
|
||||
capture
|
||||
};
|
||||
addEvent(window, 'focus', listener, capture);
|
||||
}
|
||||
}
|
||||
}
|
||||
function trigger(shortcut) {
|
||||
let scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'all';
|
||||
Object.keys(_handlers).forEach(key => {
|
||||
const dataList = _handlers[key].filter(item => item.scope === scope && item.shortcut === shortcut);
|
||||
dataList.forEach(data => {
|
||||
if (data && data.method) {
|
||||
data.method();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 销毁事件,unbind之后判断element上是否还有键盘快捷键,如果没有移除监听
|
||||
function removeKeyEvent(element) {
|
||||
const values = Object.values(_handlers).flat();
|
||||
const findindex = values.findIndex(_ref4 => {
|
||||
let {
|
||||
element: el
|
||||
} = _ref4;
|
||||
return el === element;
|
||||
});
|
||||
if (findindex < 0) {
|
||||
const {
|
||||
keydownListener,
|
||||
keyupListenr,
|
||||
capture
|
||||
} = elementEventMap.get(element) || {};
|
||||
if (keydownListener && keyupListenr) {
|
||||
removeEvent(element, 'keyup', keyupListenr, capture);
|
||||
removeEvent(element, 'keydown', keydownListener, capture);
|
||||
elementEventMap.delete(element);
|
||||
}
|
||||
}
|
||||
if (values.length <= 0 || elementEventMap.size <= 0) {
|
||||
// 移除所有的元素上的监听
|
||||
const eventKeys = Object.keys(elementEventMap);
|
||||
eventKeys.forEach(el => {
|
||||
const {
|
||||
keydownListener,
|
||||
keyupListenr,
|
||||
capture
|
||||
} = elementEventMap.get(el) || {};
|
||||
if (keydownListener && keyupListenr) {
|
||||
removeEvent(el, 'keyup', keyupListenr, capture);
|
||||
removeEvent(el, 'keydown', keydownListener, capture);
|
||||
elementEventMap.delete(el);
|
||||
}
|
||||
});
|
||||
// 清空 elementEventMap
|
||||
elementEventMap.clear();
|
||||
// 清空 _handlers
|
||||
Object.keys(_handlers).forEach(key => delete _handlers[key]);
|
||||
// 移除window上的focus监听
|
||||
if (winListendFocus) {
|
||||
const {
|
||||
listener,
|
||||
capture
|
||||
} = winListendFocus;
|
||||
removeEvent(window, 'focus', listener, capture);
|
||||
winListendFocus = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
const _api = {
|
||||
getPressedKeyString,
|
||||
setScope,
|
||||
getScope,
|
||||
deleteScope,
|
||||
getPressedKeyCodes,
|
||||
getAllKeyCodes,
|
||||
isPressed,
|
||||
filter,
|
||||
trigger,
|
||||
unbind,
|
||||
keyMap: _keyMap,
|
||||
modifier: _modifier,
|
||||
modifierMap
|
||||
};
|
||||
for (const a in _api) {
|
||||
if (Object.prototype.hasOwnProperty.call(_api, a)) {
|
||||
hotkeys[a] = _api[a];
|
||||
}
|
||||
}
|
||||
if (typeof window !== 'undefined') {
|
||||
const _hotkeys = window.hotkeys;
|
||||
hotkeys.noConflict = deep => {
|
||||
if (deep && window.hotkeys === hotkeys) {
|
||||
window.hotkeys = _hotkeys;
|
||||
}
|
||||
return hotkeys;
|
||||
};
|
||||
window.hotkeys = hotkeys;
|
||||
}
|
||||
|
||||
module.exports = hotkeys;
|
File diff suppressed because one or more lines are too long
675
static/pkg/hotkeys-js/dist/hotkeys.esm.js
vendored
675
static/pkg/hotkeys-js/dist/hotkeys.esm.js
vendored
@ -1,675 +0,0 @@
|
||||
/**!
|
||||
* hotkeys-js v3.13.7
|
||||
* A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
|
||||
*
|
||||
* Copyright (c) 2024 kenny wong <wowohoo@qq.com>
|
||||
* https://github.com/jaywcjlove/hotkeys-js.git
|
||||
*
|
||||
* @website: https://jaywcjlove.github.io/hotkeys-js
|
||||
|
||||
* Licensed under the MIT license
|
||||
*/
|
||||
|
||||
const isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false;
|
||||
|
||||
// 绑定事件
|
||||
function addEvent(object, event, method, useCapture) {
|
||||
if (object.addEventListener) {
|
||||
object.addEventListener(event, method, useCapture);
|
||||
} else if (object.attachEvent) {
|
||||
object.attachEvent("on".concat(event), method);
|
||||
}
|
||||
}
|
||||
function removeEvent(object, event, method, useCapture) {
|
||||
if (object.removeEventListener) {
|
||||
object.removeEventListener(event, method, useCapture);
|
||||
} else if (object.detachEvent) {
|
||||
object.detachEvent("on".concat(event), method);
|
||||
}
|
||||
}
|
||||
|
||||
// 修饰键转换成对应的键码
|
||||
function getMods(modifier, key) {
|
||||
const mods = key.slice(0, key.length - 1);
|
||||
for (let i = 0; i < mods.length; i++) mods[i] = modifier[mods[i].toLowerCase()];
|
||||
return mods;
|
||||
}
|
||||
|
||||
// 处理传的key字符串转换成数组
|
||||
function getKeys(key) {
|
||||
if (typeof key !== 'string') key = '';
|
||||
key = key.replace(/\s/g, ''); // 匹配任何空白字符,包括空格、制表符、换页符等等
|
||||
const keys = key.split(','); // 同时设置多个快捷键,以','分割
|
||||
let index = keys.lastIndexOf('');
|
||||
|
||||
// 快捷键可能包含',',需特殊处理
|
||||
for (; index >= 0;) {
|
||||
keys[index - 1] += ',';
|
||||
keys.splice(index, 1);
|
||||
index = keys.lastIndexOf('');
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
// 比较修饰键的数组
|
||||
function compareArray(a1, a2) {
|
||||
const arr1 = a1.length >= a2.length ? a1 : a2;
|
||||
const arr2 = a1.length >= a2.length ? a2 : a1;
|
||||
let isIndex = true;
|
||||
for (let i = 0; i < arr1.length; i++) {
|
||||
if (arr2.indexOf(arr1[i]) === -1) isIndex = false;
|
||||
}
|
||||
return isIndex;
|
||||
}
|
||||
|
||||
// Special Keys
|
||||
const _keyMap = {
|
||||
backspace: 8,
|
||||
'⌫': 8,
|
||||
tab: 9,
|
||||
clear: 12,
|
||||
enter: 13,
|
||||
'↩': 13,
|
||||
return: 13,
|
||||
esc: 27,
|
||||
escape: 27,
|
||||
space: 32,
|
||||
left: 37,
|
||||
up: 38,
|
||||
right: 39,
|
||||
down: 40,
|
||||
del: 46,
|
||||
delete: 46,
|
||||
ins: 45,
|
||||
insert: 45,
|
||||
home: 36,
|
||||
end: 35,
|
||||
pageup: 33,
|
||||
pagedown: 34,
|
||||
capslock: 20,
|
||||
num_0: 96,
|
||||
num_1: 97,
|
||||
num_2: 98,
|
||||
num_3: 99,
|
||||
num_4: 100,
|
||||
num_5: 101,
|
||||
num_6: 102,
|
||||
num_7: 103,
|
||||
num_8: 104,
|
||||
num_9: 105,
|
||||
num_multiply: 106,
|
||||
num_add: 107,
|
||||
num_enter: 108,
|
||||
num_subtract: 109,
|
||||
num_decimal: 110,
|
||||
num_divide: 111,
|
||||
'⇪': 20,
|
||||
',': 188,
|
||||
'.': 190,
|
||||
'/': 191,
|
||||
'`': 192,
|
||||
'-': isff ? 173 : 189,
|
||||
'=': isff ? 61 : 187,
|
||||
';': isff ? 59 : 186,
|
||||
'\'': 222,
|
||||
'[': 219,
|
||||
']': 221,
|
||||
'\\': 220
|
||||
};
|
||||
|
||||
// Modifier Keys
|
||||
const _modifier = {
|
||||
// shiftKey
|
||||
'⇧': 16,
|
||||
shift: 16,
|
||||
// altKey
|
||||
'⌥': 18,
|
||||
alt: 18,
|
||||
option: 18,
|
||||
// ctrlKey
|
||||
'⌃': 17,
|
||||
ctrl: 17,
|
||||
control: 17,
|
||||
// metaKey
|
||||
'⌘': 91,
|
||||
cmd: 91,
|
||||
command: 91
|
||||
};
|
||||
const modifierMap = {
|
||||
16: 'shiftKey',
|
||||
18: 'altKey',
|
||||
17: 'ctrlKey',
|
||||
91: 'metaKey',
|
||||
shiftKey: 16,
|
||||
ctrlKey: 17,
|
||||
altKey: 18,
|
||||
metaKey: 91
|
||||
};
|
||||
const _mods = {
|
||||
16: false,
|
||||
18: false,
|
||||
17: false,
|
||||
91: false
|
||||
};
|
||||
const _handlers = {};
|
||||
|
||||
// F1~F12 special key
|
||||
for (let k = 1; k < 20; k++) {
|
||||
_keyMap["f".concat(k)] = 111 + k;
|
||||
}
|
||||
|
||||
let _downKeys = []; // 记录摁下的绑定键
|
||||
let winListendFocus = null; // window是否已经监听了focus事件
|
||||
let _scope = 'all'; // 默认热键范围
|
||||
const elementEventMap = new Map(); // 已绑定事件的节点记录
|
||||
|
||||
// 返回键码
|
||||
const code = x => _keyMap[x.toLowerCase()] || _modifier[x.toLowerCase()] || x.toUpperCase().charCodeAt(0);
|
||||
const getKey = x => Object.keys(_keyMap).find(k => _keyMap[k] === x);
|
||||
const getModifier = x => Object.keys(_modifier).find(k => _modifier[k] === x);
|
||||
|
||||
// 设置获取当前范围(默认为'所有')
|
||||
function setScope(scope) {
|
||||
_scope = scope || 'all';
|
||||
}
|
||||
// 获取当前范围
|
||||
function getScope() {
|
||||
return _scope || 'all';
|
||||
}
|
||||
// 获取摁下绑定键的键值
|
||||
function getPressedKeyCodes() {
|
||||
return _downKeys.slice(0);
|
||||
}
|
||||
function getPressedKeyString() {
|
||||
return _downKeys.map(c => getKey(c) || getModifier(c) || String.fromCharCode(c));
|
||||
}
|
||||
function getAllKeyCodes() {
|
||||
const result = [];
|
||||
Object.keys(_handlers).forEach(k => {
|
||||
_handlers[k].forEach(_ref => {
|
||||
let {
|
||||
key,
|
||||
scope,
|
||||
mods,
|
||||
shortcut
|
||||
} = _ref;
|
||||
result.push({
|
||||
scope,
|
||||
shortcut,
|
||||
mods,
|
||||
keys: key.split('+').map(v => code(v))
|
||||
});
|
||||
});
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
// 表单控件控件判断 返回 Boolean
|
||||
// hotkey is effective only when filter return true
|
||||
function filter(event) {
|
||||
const target = event.target || event.srcElement;
|
||||
const {
|
||||
tagName
|
||||
} = target;
|
||||
let flag = true;
|
||||
const isInput = tagName === 'INPUT' && !['checkbox', 'radio', 'range', 'button', 'file', 'reset', 'submit', 'color'].includes(target.type);
|
||||
// ignore: isContentEditable === 'true', <input> and <textarea> when readOnly state is false, <select>
|
||||
if (target.isContentEditable || (isInput || tagName === 'TEXTAREA' || tagName === 'SELECT') && !target.readOnly) {
|
||||
flag = false;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
// 判断摁下的键是否为某个键,返回true或者false
|
||||
function isPressed(keyCode) {
|
||||
if (typeof keyCode === 'string') {
|
||||
keyCode = code(keyCode); // 转换成键码
|
||||
}
|
||||
return _downKeys.indexOf(keyCode) !== -1;
|
||||
}
|
||||
|
||||
// 循环删除handlers中的所有 scope(范围)
|
||||
function deleteScope(scope, newScope) {
|
||||
let handlers;
|
||||
let i;
|
||||
|
||||
// 没有指定scope,获取scope
|
||||
if (!scope) scope = getScope();
|
||||
for (const key in _handlers) {
|
||||
if (Object.prototype.hasOwnProperty.call(_handlers, key)) {
|
||||
handlers = _handlers[key];
|
||||
for (i = 0; i < handlers.length;) {
|
||||
if (handlers[i].scope === scope) {
|
||||
const deleteItems = handlers.splice(i, 1);
|
||||
deleteItems.forEach(_ref2 => {
|
||||
let {
|
||||
element
|
||||
} = _ref2;
|
||||
return removeKeyEvent(element);
|
||||
});
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果scope被删除,将scope重置为all
|
||||
if (getScope() === scope) setScope(newScope || 'all');
|
||||
}
|
||||
|
||||
// 清除修饰键
|
||||
function clearModifier(event) {
|
||||
let key = event.keyCode || event.which || event.charCode;
|
||||
const i = _downKeys.indexOf(key);
|
||||
|
||||
// 从列表中清除按压过的键
|
||||
if (i >= 0) {
|
||||
_downKeys.splice(i, 1);
|
||||
}
|
||||
// 特殊处理 cmmand 键,在 cmmand 组合快捷键 keyup 只执行一次的问题
|
||||
if (event.key && event.key.toLowerCase() === 'meta') {
|
||||
_downKeys.splice(0, _downKeys.length);
|
||||
}
|
||||
|
||||
// 修饰键 shiftKey altKey ctrlKey (command||metaKey) 清除
|
||||
if (key === 93 || key === 224) key = 91;
|
||||
if (key in _mods) {
|
||||
_mods[key] = false;
|
||||
|
||||
// 将修饰键重置为false
|
||||
for (const k in _modifier) if (_modifier[k] === key) hotkeys[k] = false;
|
||||
}
|
||||
}
|
||||
function unbind(keysInfo) {
|
||||
// unbind(), unbind all keys
|
||||
if (typeof keysInfo === 'undefined') {
|
||||
Object.keys(_handlers).forEach(key => {
|
||||
Array.isArray(_handlers[key]) && _handlers[key].forEach(info => eachUnbind(info));
|
||||
delete _handlers[key];
|
||||
});
|
||||
removeKeyEvent(null);
|
||||
} else if (Array.isArray(keysInfo)) {
|
||||
// support like : unbind([{key: 'ctrl+a', scope: 's1'}, {key: 'ctrl-a', scope: 's2', splitKey: '-'}])
|
||||
keysInfo.forEach(info => {
|
||||
if (info.key) eachUnbind(info);
|
||||
});
|
||||
} else if (typeof keysInfo === 'object') {
|
||||
// support like unbind({key: 'ctrl+a, ctrl+b', scope:'abc'})
|
||||
if (keysInfo.key) eachUnbind(keysInfo);
|
||||
} else if (typeof keysInfo === 'string') {
|
||||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
||||
args[_key - 1] = arguments[_key];
|
||||
}
|
||||
// support old method
|
||||
// eslint-disable-line
|
||||
let [scope, method] = args;
|
||||
if (typeof scope === 'function') {
|
||||
method = scope;
|
||||
scope = '';
|
||||
}
|
||||
eachUnbind({
|
||||
key: keysInfo,
|
||||
scope,
|
||||
method,
|
||||
splitKey: '+'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 解除绑定某个范围的快捷键
|
||||
const eachUnbind = _ref3 => {
|
||||
let {
|
||||
key,
|
||||
scope,
|
||||
method,
|
||||
splitKey = '+'
|
||||
} = _ref3;
|
||||
const multipleKeys = getKeys(key);
|
||||
multipleKeys.forEach(originKey => {
|
||||
const unbindKeys = originKey.split(splitKey);
|
||||
const len = unbindKeys.length;
|
||||
const lastKey = unbindKeys[len - 1];
|
||||
const keyCode = lastKey === '*' ? '*' : code(lastKey);
|
||||
if (!_handlers[keyCode]) return;
|
||||
// 判断是否传入范围,没有就获取范围
|
||||
if (!scope) scope = getScope();
|
||||
const mods = len > 1 ? getMods(_modifier, unbindKeys) : [];
|
||||
const unbindElements = [];
|
||||
_handlers[keyCode] = _handlers[keyCode].filter(record => {
|
||||
// 通过函数判断,是否解除绑定,函数相等直接返回
|
||||
const isMatchingMethod = method ? record.method === method : true;
|
||||
const isUnbind = isMatchingMethod && record.scope === scope && compareArray(record.mods, mods);
|
||||
if (isUnbind) unbindElements.push(record.element);
|
||||
return !isUnbind;
|
||||
});
|
||||
unbindElements.forEach(element => removeKeyEvent(element));
|
||||
});
|
||||
};
|
||||
|
||||
// 对监听对应快捷键的回调函数进行处理
|
||||
function eventHandler(event, handler, scope, element) {
|
||||
if (handler.element !== element) {
|
||||
return;
|
||||
}
|
||||
let modifiersMatch;
|
||||
|
||||
// 看它是否在当前范围
|
||||
if (handler.scope === scope || handler.scope === 'all') {
|
||||
// 检查是否匹配修饰符(如果有返回true)
|
||||
modifiersMatch = handler.mods.length > 0;
|
||||
for (const y in _mods) {
|
||||
if (Object.prototype.hasOwnProperty.call(_mods, y)) {
|
||||
if (!_mods[y] && handler.mods.indexOf(+y) > -1 || _mods[y] && handler.mods.indexOf(+y) === -1) {
|
||||
modifiersMatch = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 调用处理程序,如果是修饰键不做处理
|
||||
if (handler.mods.length === 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91] || modifiersMatch || handler.shortcut === '*') {
|
||||
handler.keys = [];
|
||||
handler.keys = handler.keys.concat(_downKeys);
|
||||
if (handler.method(event, handler) === false) {
|
||||
if (event.preventDefault) event.preventDefault();else event.returnValue = false;
|
||||
if (event.stopPropagation) event.stopPropagation();
|
||||
if (event.cancelBubble) event.cancelBubble = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理keydown事件
|
||||
function dispatch(event, element) {
|
||||
const asterisk = _handlers['*'];
|
||||
let key = event.keyCode || event.which || event.charCode;
|
||||
|
||||
// 表单控件过滤 默认表单控件不触发快捷键
|
||||
if (!hotkeys.filter.call(this, event)) return;
|
||||
|
||||
// Gecko(Firefox)的command键值224,在Webkit(Chrome)中保持一致
|
||||
// Webkit左右 command 键值不一样
|
||||
if (key === 93 || key === 224) key = 91;
|
||||
|
||||
/**
|
||||
* Collect bound keys
|
||||
* If an Input Method Editor is processing key input and the event is keydown, return 229.
|
||||
* https://stackoverflow.com/questions/25043934/is-it-ok-to-ignore-keydown-events-with-keycode-229
|
||||
* http://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html
|
||||
*/
|
||||
if (_downKeys.indexOf(key) === -1 && key !== 229) _downKeys.push(key);
|
||||
/**
|
||||
* Jest test cases are required.
|
||||
* ===============================
|
||||
*/
|
||||
['ctrlKey', 'altKey', 'shiftKey', 'metaKey'].forEach(keyName => {
|
||||
const keyNum = modifierMap[keyName];
|
||||
if (event[keyName] && _downKeys.indexOf(keyNum) === -1) {
|
||||
_downKeys.push(keyNum);
|
||||
} else if (!event[keyName] && _downKeys.indexOf(keyNum) > -1) {
|
||||
_downKeys.splice(_downKeys.indexOf(keyNum), 1);
|
||||
} else if (keyName === 'metaKey' && event[keyName] && _downKeys.length === 3) {
|
||||
/**
|
||||
* Fix if Command is pressed:
|
||||
* ===============================
|
||||
*/
|
||||
if (!(event.ctrlKey || event.shiftKey || event.altKey)) {
|
||||
_downKeys = _downKeys.slice(_downKeys.indexOf(keyNum));
|
||||
}
|
||||
}
|
||||
});
|
||||
/**
|
||||
* -------------------------------
|
||||
*/
|
||||
|
||||
if (key in _mods) {
|
||||
_mods[key] = true;
|
||||
|
||||
// 将特殊字符的key注册到 hotkeys 上
|
||||
for (const k in _modifier) {
|
||||
if (_modifier[k] === key) hotkeys[k] = true;
|
||||
}
|
||||
if (!asterisk) return;
|
||||
}
|
||||
|
||||
// 将 modifierMap 里面的修饰键绑定到 event 中
|
||||
for (const e in _mods) {
|
||||
if (Object.prototype.hasOwnProperty.call(_mods, e)) {
|
||||
_mods[e] = event[modifierMap[e]];
|
||||
}
|
||||
}
|
||||
/**
|
||||
* https://github.com/jaywcjlove/hotkeys/pull/129
|
||||
* This solves the issue in Firefox on Windows where hotkeys corresponding to special characters would not trigger.
|
||||
* An example of this is ctrl+alt+m on a Swedish keyboard which is used to type μ.
|
||||
* Browser support: https://caniuse.com/#feat=keyboardevent-getmodifierstate
|
||||
*/
|
||||
if (event.getModifierState && !(event.altKey && !event.ctrlKey) && event.getModifierState('AltGraph')) {
|
||||
if (_downKeys.indexOf(17) === -1) {
|
||||
_downKeys.push(17);
|
||||
}
|
||||
if (_downKeys.indexOf(18) === -1) {
|
||||
_downKeys.push(18);
|
||||
}
|
||||
_mods[17] = true;
|
||||
_mods[18] = true;
|
||||
}
|
||||
|
||||
// 获取范围 默认为 `all`
|
||||
const scope = getScope();
|
||||
// 对任何快捷键都需要做的处理
|
||||
if (asterisk) {
|
||||
for (let i = 0; i < asterisk.length; i++) {
|
||||
if (asterisk[i].scope === scope && (event.type === 'keydown' && asterisk[i].keydown || event.type === 'keyup' && asterisk[i].keyup)) {
|
||||
eventHandler(event, asterisk[i], scope, element);
|
||||
}
|
||||
}
|
||||
}
|
||||
// key 不在 _handlers 中返回
|
||||
if (!(key in _handlers)) return;
|
||||
const handlerKey = _handlers[key];
|
||||
const keyLen = handlerKey.length;
|
||||
for (let i = 0; i < keyLen; i++) {
|
||||
if (event.type === 'keydown' && handlerKey[i].keydown || event.type === 'keyup' && handlerKey[i].keyup) {
|
||||
if (handlerKey[i].key) {
|
||||
const record = handlerKey[i];
|
||||
const {
|
||||
splitKey
|
||||
} = record;
|
||||
const keyShortcut = record.key.split(splitKey);
|
||||
const _downKeysCurrent = []; // 记录当前按键键值
|
||||
for (let a = 0; a < keyShortcut.length; a++) {
|
||||
_downKeysCurrent.push(code(keyShortcut[a]));
|
||||
}
|
||||
if (_downKeysCurrent.sort().join('') === _downKeys.sort().join('')) {
|
||||
// 找到处理内容
|
||||
eventHandler(event, record, scope, element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
function hotkeys(key, option, method) {
|
||||
_downKeys = [];
|
||||
const keys = getKeys(key); // 需要处理的快捷键列表
|
||||
let mods = [];
|
||||
let scope = 'all'; // scope默认为all,所有范围都有效
|
||||
let element = document; // 快捷键事件绑定节点
|
||||
let i = 0;
|
||||
let keyup = false;
|
||||
let keydown = true;
|
||||
let splitKey = '+';
|
||||
let capture = false;
|
||||
let single = false; // 单个callback
|
||||
|
||||
// 对为设定范围的判断
|
||||
if (method === undefined && typeof option === 'function') {
|
||||
method = option;
|
||||
}
|
||||
if (Object.prototype.toString.call(option) === '[object Object]') {
|
||||
if (option.scope) scope = option.scope; // eslint-disable-line
|
||||
if (option.element) element = option.element; // eslint-disable-line
|
||||
if (option.keyup) keyup = option.keyup; // eslint-disable-line
|
||||
if (option.keydown !== undefined) keydown = option.keydown; // eslint-disable-line
|
||||
if (option.capture !== undefined) capture = option.capture; // eslint-disable-line
|
||||
if (typeof option.splitKey === 'string') splitKey = option.splitKey; // eslint-disable-line
|
||||
if (option.single === true) single = true; // eslint-disable-line
|
||||
}
|
||||
if (typeof option === 'string') scope = option;
|
||||
|
||||
// 如果只允许单个callback,先unbind
|
||||
if (single) unbind(key, scope);
|
||||
|
||||
// 对于每个快捷键进行处理
|
||||
for (; i < keys.length; i++) {
|
||||
key = keys[i].split(splitKey); // 按键列表
|
||||
mods = [];
|
||||
|
||||
// 如果是组合快捷键取得组合快捷键
|
||||
if (key.length > 1) mods = getMods(_modifier, key);
|
||||
|
||||
// 将非修饰键转化为键码
|
||||
key = key[key.length - 1];
|
||||
key = key === '*' ? '*' : code(key); // *表示匹配所有快捷键
|
||||
|
||||
// 判断key是否在_handlers中,不在就赋一个空数组
|
||||
if (!(key in _handlers)) _handlers[key] = [];
|
||||
_handlers[key].push({
|
||||
keyup,
|
||||
keydown,
|
||||
scope,
|
||||
mods,
|
||||
shortcut: keys[i],
|
||||
method,
|
||||
key: keys[i],
|
||||
splitKey,
|
||||
element
|
||||
});
|
||||
}
|
||||
// 在全局document上设置快捷键
|
||||
if (typeof element !== 'undefined' && window) {
|
||||
if (!elementEventMap.has(element)) {
|
||||
const keydownListener = function () {
|
||||
let event = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.event;
|
||||
return dispatch(event, element);
|
||||
};
|
||||
const keyupListenr = function () {
|
||||
let event = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.event;
|
||||
dispatch(event, element);
|
||||
clearModifier(event);
|
||||
};
|
||||
elementEventMap.set(element, {
|
||||
keydownListener,
|
||||
keyupListenr,
|
||||
capture
|
||||
});
|
||||
addEvent(element, 'keydown', keydownListener, capture);
|
||||
addEvent(element, 'keyup', keyupListenr, capture);
|
||||
}
|
||||
if (!winListendFocus) {
|
||||
const listener = () => {
|
||||
_downKeys = [];
|
||||
};
|
||||
winListendFocus = {
|
||||
listener,
|
||||
capture
|
||||
};
|
||||
addEvent(window, 'focus', listener, capture);
|
||||
}
|
||||
}
|
||||
}
|
||||
function trigger(shortcut) {
|
||||
let scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'all';
|
||||
Object.keys(_handlers).forEach(key => {
|
||||
const dataList = _handlers[key].filter(item => item.scope === scope && item.shortcut === shortcut);
|
||||
dataList.forEach(data => {
|
||||
if (data && data.method) {
|
||||
data.method();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 销毁事件,unbind之后判断element上是否还有键盘快捷键,如果没有移除监听
|
||||
function removeKeyEvent(element) {
|
||||
const values = Object.values(_handlers).flat();
|
||||
const findindex = values.findIndex(_ref4 => {
|
||||
let {
|
||||
element: el
|
||||
} = _ref4;
|
||||
return el === element;
|
||||
});
|
||||
if (findindex < 0) {
|
||||
const {
|
||||
keydownListener,
|
||||
keyupListenr,
|
||||
capture
|
||||
} = elementEventMap.get(element) || {};
|
||||
if (keydownListener && keyupListenr) {
|
||||
removeEvent(element, 'keyup', keyupListenr, capture);
|
||||
removeEvent(element, 'keydown', keydownListener, capture);
|
||||
elementEventMap.delete(element);
|
||||
}
|
||||
}
|
||||
if (values.length <= 0 || elementEventMap.size <= 0) {
|
||||
// 移除所有的元素上的监听
|
||||
const eventKeys = Object.keys(elementEventMap);
|
||||
eventKeys.forEach(el => {
|
||||
const {
|
||||
keydownListener,
|
||||
keyupListenr,
|
||||
capture
|
||||
} = elementEventMap.get(el) || {};
|
||||
if (keydownListener && keyupListenr) {
|
||||
removeEvent(el, 'keyup', keyupListenr, capture);
|
||||
removeEvent(el, 'keydown', keydownListener, capture);
|
||||
elementEventMap.delete(el);
|
||||
}
|
||||
});
|
||||
// 清空 elementEventMap
|
||||
elementEventMap.clear();
|
||||
// 清空 _handlers
|
||||
Object.keys(_handlers).forEach(key => delete _handlers[key]);
|
||||
// 移除window上的focus监听
|
||||
if (winListendFocus) {
|
||||
const {
|
||||
listener,
|
||||
capture
|
||||
} = winListendFocus;
|
||||
removeEvent(window, 'focus', listener, capture);
|
||||
winListendFocus = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
const _api = {
|
||||
getPressedKeyString,
|
||||
setScope,
|
||||
getScope,
|
||||
deleteScope,
|
||||
getPressedKeyCodes,
|
||||
getAllKeyCodes,
|
||||
isPressed,
|
||||
filter,
|
||||
trigger,
|
||||
unbind,
|
||||
keyMap: _keyMap,
|
||||
modifier: _modifier,
|
||||
modifierMap
|
||||
};
|
||||
for (const a in _api) {
|
||||
if (Object.prototype.hasOwnProperty.call(_api, a)) {
|
||||
hotkeys[a] = _api[a];
|
||||
}
|
||||
}
|
||||
if (typeof window !== 'undefined') {
|
||||
const _hotkeys = window.hotkeys;
|
||||
hotkeys.noConflict = deep => {
|
||||
if (deep && window.hotkeys === hotkeys) {
|
||||
window.hotkeys = _hotkeys;
|
||||
}
|
||||
return hotkeys;
|
||||
};
|
||||
window.hotkeys = hotkeys;
|
||||
}
|
||||
|
||||
export { hotkeys as default };
|
683
static/pkg/hotkeys-js/dist/hotkeys.js
vendored
683
static/pkg/hotkeys-js/dist/hotkeys.js
vendored
@ -1,683 +0,0 @@
|
||||
/**!
|
||||
* hotkeys-js v3.13.7
|
||||
* A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
|
||||
*
|
||||
* Copyright (c) 2024 kenny wong <wowohoo@qq.com>
|
||||
* https://github.com/jaywcjlove/hotkeys-js.git
|
||||
*
|
||||
* @website: https://jaywcjlove.github.io/hotkeys-js
|
||||
|
||||
* Licensed under the MIT license
|
||||
*/
|
||||
|
||||
(function (global, factory) {
|
||||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
||||
typeof define === 'function' && define.amd ? define(factory) :
|
||||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.hotkeys = factory());
|
||||
})(this, (function () { 'use strict';
|
||||
|
||||
const isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false;
|
||||
|
||||
// 绑定事件
|
||||
function addEvent(object, event, method, useCapture) {
|
||||
if (object.addEventListener) {
|
||||
object.addEventListener(event, method, useCapture);
|
||||
} else if (object.attachEvent) {
|
||||
object.attachEvent("on".concat(event), method);
|
||||
}
|
||||
}
|
||||
function removeEvent(object, event, method, useCapture) {
|
||||
if (object.removeEventListener) {
|
||||
object.removeEventListener(event, method, useCapture);
|
||||
} else if (object.detachEvent) {
|
||||
object.detachEvent("on".concat(event), method);
|
||||
}
|
||||
}
|
||||
|
||||
// 修饰键转换成对应的键码
|
||||
function getMods(modifier, key) {
|
||||
const mods = key.slice(0, key.length - 1);
|
||||
for (let i = 0; i < mods.length; i++) mods[i] = modifier[mods[i].toLowerCase()];
|
||||
return mods;
|
||||
}
|
||||
|
||||
// 处理传的key字符串转换成数组
|
||||
function getKeys(key) {
|
||||
if (typeof key !== 'string') key = '';
|
||||
key = key.replace(/\s/g, ''); // 匹配任何空白字符,包括空格、制表符、换页符等等
|
||||
const keys = key.split(','); // 同时设置多个快捷键,以','分割
|
||||
let index = keys.lastIndexOf('');
|
||||
|
||||
// 快捷键可能包含',',需特殊处理
|
||||
for (; index >= 0;) {
|
||||
keys[index - 1] += ',';
|
||||
keys.splice(index, 1);
|
||||
index = keys.lastIndexOf('');
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
// 比较修饰键的数组
|
||||
function compareArray(a1, a2) {
|
||||
const arr1 = a1.length >= a2.length ? a1 : a2;
|
||||
const arr2 = a1.length >= a2.length ? a2 : a1;
|
||||
let isIndex = true;
|
||||
for (let i = 0; i < arr1.length; i++) {
|
||||
if (arr2.indexOf(arr1[i]) === -1) isIndex = false;
|
||||
}
|
||||
return isIndex;
|
||||
}
|
||||
|
||||
// Special Keys
|
||||
const _keyMap = {
|
||||
backspace: 8,
|
||||
'⌫': 8,
|
||||
tab: 9,
|
||||
clear: 12,
|
||||
enter: 13,
|
||||
'↩': 13,
|
||||
return: 13,
|
||||
esc: 27,
|
||||
escape: 27,
|
||||
space: 32,
|
||||
left: 37,
|
||||
up: 38,
|
||||
right: 39,
|
||||
down: 40,
|
||||
del: 46,
|
||||
delete: 46,
|
||||
ins: 45,
|
||||
insert: 45,
|
||||
home: 36,
|
||||
end: 35,
|
||||
pageup: 33,
|
||||
pagedown: 34,
|
||||
capslock: 20,
|
||||
num_0: 96,
|
||||
num_1: 97,
|
||||
num_2: 98,
|
||||
num_3: 99,
|
||||
num_4: 100,
|
||||
num_5: 101,
|
||||
num_6: 102,
|
||||
num_7: 103,
|
||||
num_8: 104,
|
||||
num_9: 105,
|
||||
num_multiply: 106,
|
||||
num_add: 107,
|
||||
num_enter: 108,
|
||||
num_subtract: 109,
|
||||
num_decimal: 110,
|
||||
num_divide: 111,
|
||||
'⇪': 20,
|
||||
',': 188,
|
||||
'.': 190,
|
||||
'/': 191,
|
||||
'`': 192,
|
||||
'-': isff ? 173 : 189,
|
||||
'=': isff ? 61 : 187,
|
||||
';': isff ? 59 : 186,
|
||||
'\'': 222,
|
||||
'[': 219,
|
||||
']': 221,
|
||||
'\\': 220
|
||||
};
|
||||
|
||||
// Modifier Keys
|
||||
const _modifier = {
|
||||
// shiftKey
|
||||
'⇧': 16,
|
||||
shift: 16,
|
||||
// altKey
|
||||
'⌥': 18,
|
||||
alt: 18,
|
||||
option: 18,
|
||||
// ctrlKey
|
||||
'⌃': 17,
|
||||
ctrl: 17,
|
||||
control: 17,
|
||||
// metaKey
|
||||
'⌘': 91,
|
||||
cmd: 91,
|
||||
command: 91
|
||||
};
|
||||
const modifierMap = {
|
||||
16: 'shiftKey',
|
||||
18: 'altKey',
|
||||
17: 'ctrlKey',
|
||||
91: 'metaKey',
|
||||
shiftKey: 16,
|
||||
ctrlKey: 17,
|
||||
altKey: 18,
|
||||
metaKey: 91
|
||||
};
|
||||
const _mods = {
|
||||
16: false,
|
||||
18: false,
|
||||
17: false,
|
||||
91: false
|
||||
};
|
||||
const _handlers = {};
|
||||
|
||||
// F1~F12 special key
|
||||
for (let k = 1; k < 20; k++) {
|
||||
_keyMap["f".concat(k)] = 111 + k;
|
||||
}
|
||||
|
||||
let _downKeys = []; // 记录摁下的绑定键
|
||||
let winListendFocus = null; // window是否已经监听了focus事件
|
||||
let _scope = 'all'; // 默认热键范围
|
||||
const elementEventMap = new Map(); // 已绑定事件的节点记录
|
||||
|
||||
// 返回键码
|
||||
const code = x => _keyMap[x.toLowerCase()] || _modifier[x.toLowerCase()] || x.toUpperCase().charCodeAt(0);
|
||||
const getKey = x => Object.keys(_keyMap).find(k => _keyMap[k] === x);
|
||||
const getModifier = x => Object.keys(_modifier).find(k => _modifier[k] === x);
|
||||
|
||||
// 设置获取当前范围(默认为'所有')
|
||||
function setScope(scope) {
|
||||
_scope = scope || 'all';
|
||||
}
|
||||
// 获取当前范围
|
||||
function getScope() {
|
||||
return _scope || 'all';
|
||||
}
|
||||
// 获取摁下绑定键的键值
|
||||
function getPressedKeyCodes() {
|
||||
return _downKeys.slice(0);
|
||||
}
|
||||
function getPressedKeyString() {
|
||||
return _downKeys.map(c => getKey(c) || getModifier(c) || String.fromCharCode(c));
|
||||
}
|
||||
function getAllKeyCodes() {
|
||||
const result = [];
|
||||
Object.keys(_handlers).forEach(k => {
|
||||
_handlers[k].forEach(_ref => {
|
||||
let {
|
||||
key,
|
||||
scope,
|
||||
mods,
|
||||
shortcut
|
||||
} = _ref;
|
||||
result.push({
|
||||
scope,
|
||||
shortcut,
|
||||
mods,
|
||||
keys: key.split('+').map(v => code(v))
|
||||
});
|
||||
});
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
// 表单控件控件判断 返回 Boolean
|
||||
// hotkey is effective only when filter return true
|
||||
function filter(event) {
|
||||
const target = event.target || event.srcElement;
|
||||
const {
|
||||
tagName
|
||||
} = target;
|
||||
let flag = true;
|
||||
const isInput = tagName === 'INPUT' && !['checkbox', 'radio', 'range', 'button', 'file', 'reset', 'submit', 'color'].includes(target.type);
|
||||
// ignore: isContentEditable === 'true', <input> and <textarea> when readOnly state is false, <select>
|
||||
if (target.isContentEditable || (isInput || tagName === 'TEXTAREA' || tagName === 'SELECT') && !target.readOnly) {
|
||||
flag = false;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
// 判断摁下的键是否为某个键,返回true或者false
|
||||
function isPressed(keyCode) {
|
||||
if (typeof keyCode === 'string') {
|
||||
keyCode = code(keyCode); // 转换成键码
|
||||
}
|
||||
return _downKeys.indexOf(keyCode) !== -1;
|
||||
}
|
||||
|
||||
// 循环删除handlers中的所有 scope(范围)
|
||||
function deleteScope(scope, newScope) {
|
||||
let handlers;
|
||||
let i;
|
||||
|
||||
// 没有指定scope,获取scope
|
||||
if (!scope) scope = getScope();
|
||||
for (const key in _handlers) {
|
||||
if (Object.prototype.hasOwnProperty.call(_handlers, key)) {
|
||||
handlers = _handlers[key];
|
||||
for (i = 0; i < handlers.length;) {
|
||||
if (handlers[i].scope === scope) {
|
||||
const deleteItems = handlers.splice(i, 1);
|
||||
deleteItems.forEach(_ref2 => {
|
||||
let {
|
||||
element
|
||||
} = _ref2;
|
||||
return removeKeyEvent(element);
|
||||
});
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果scope被删除,将scope重置为all
|
||||
if (getScope() === scope) setScope(newScope || 'all');
|
||||
}
|
||||
|
||||
// 清除修饰键
|
||||
function clearModifier(event) {
|
||||
let key = event.keyCode || event.which || event.charCode;
|
||||
const i = _downKeys.indexOf(key);
|
||||
|
||||
// 从列表中清除按压过的键
|
||||
if (i >= 0) {
|
||||
_downKeys.splice(i, 1);
|
||||
}
|
||||
// 特殊处理 cmmand 键,在 cmmand 组合快捷键 keyup 只执行一次的问题
|
||||
if (event.key && event.key.toLowerCase() === 'meta') {
|
||||
_downKeys.splice(0, _downKeys.length);
|
||||
}
|
||||
|
||||
// 修饰键 shiftKey altKey ctrlKey (command||metaKey) 清除
|
||||
if (key === 93 || key === 224) key = 91;
|
||||
if (key in _mods) {
|
||||
_mods[key] = false;
|
||||
|
||||
// 将修饰键重置为false
|
||||
for (const k in _modifier) if (_modifier[k] === key) hotkeys[k] = false;
|
||||
}
|
||||
}
|
||||
function unbind(keysInfo) {
|
||||
// unbind(), unbind all keys
|
||||
if (typeof keysInfo === 'undefined') {
|
||||
Object.keys(_handlers).forEach(key => {
|
||||
Array.isArray(_handlers[key]) && _handlers[key].forEach(info => eachUnbind(info));
|
||||
delete _handlers[key];
|
||||
});
|
||||
removeKeyEvent(null);
|
||||
} else if (Array.isArray(keysInfo)) {
|
||||
// support like : unbind([{key: 'ctrl+a', scope: 's1'}, {key: 'ctrl-a', scope: 's2', splitKey: '-'}])
|
||||
keysInfo.forEach(info => {
|
||||
if (info.key) eachUnbind(info);
|
||||
});
|
||||
} else if (typeof keysInfo === 'object') {
|
||||
// support like unbind({key: 'ctrl+a, ctrl+b', scope:'abc'})
|
||||
if (keysInfo.key) eachUnbind(keysInfo);
|
||||
} else if (typeof keysInfo === 'string') {
|
||||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
||||
args[_key - 1] = arguments[_key];
|
||||
}
|
||||
// support old method
|
||||
// eslint-disable-line
|
||||
let [scope, method] = args;
|
||||
if (typeof scope === 'function') {
|
||||
method = scope;
|
||||
scope = '';
|
||||
}
|
||||
eachUnbind({
|
||||
key: keysInfo,
|
||||
scope,
|
||||
method,
|
||||
splitKey: '+'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 解除绑定某个范围的快捷键
|
||||
const eachUnbind = _ref3 => {
|
||||
let {
|
||||
key,
|
||||
scope,
|
||||
method,
|
||||
splitKey = '+'
|
||||
} = _ref3;
|
||||
const multipleKeys = getKeys(key);
|
||||
multipleKeys.forEach(originKey => {
|
||||
const unbindKeys = originKey.split(splitKey);
|
||||
const len = unbindKeys.length;
|
||||
const lastKey = unbindKeys[len - 1];
|
||||
const keyCode = lastKey === '*' ? '*' : code(lastKey);
|
||||
if (!_handlers[keyCode]) return;
|
||||
// 判断是否传入范围,没有就获取范围
|
||||
if (!scope) scope = getScope();
|
||||
const mods = len > 1 ? getMods(_modifier, unbindKeys) : [];
|
||||
const unbindElements = [];
|
||||
_handlers[keyCode] = _handlers[keyCode].filter(record => {
|
||||
// 通过函数判断,是否解除绑定,函数相等直接返回
|
||||
const isMatchingMethod = method ? record.method === method : true;
|
||||
const isUnbind = isMatchingMethod && record.scope === scope && compareArray(record.mods, mods);
|
||||
if (isUnbind) unbindElements.push(record.element);
|
||||
return !isUnbind;
|
||||
});
|
||||
unbindElements.forEach(element => removeKeyEvent(element));
|
||||
});
|
||||
};
|
||||
|
||||
// 对监听对应快捷键的回调函数进行处理
|
||||
function eventHandler(event, handler, scope, element) {
|
||||
if (handler.element !== element) {
|
||||
return;
|
||||
}
|
||||
let modifiersMatch;
|
||||
|
||||
// 看它是否在当前范围
|
||||
if (handler.scope === scope || handler.scope === 'all') {
|
||||
// 检查是否匹配修饰符(如果有返回true)
|
||||
modifiersMatch = handler.mods.length > 0;
|
||||
for (const y in _mods) {
|
||||
if (Object.prototype.hasOwnProperty.call(_mods, y)) {
|
||||
if (!_mods[y] && handler.mods.indexOf(+y) > -1 || _mods[y] && handler.mods.indexOf(+y) === -1) {
|
||||
modifiersMatch = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 调用处理程序,如果是修饰键不做处理
|
||||
if (handler.mods.length === 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91] || modifiersMatch || handler.shortcut === '*') {
|
||||
handler.keys = [];
|
||||
handler.keys = handler.keys.concat(_downKeys);
|
||||
if (handler.method(event, handler) === false) {
|
||||
if (event.preventDefault) event.preventDefault();else event.returnValue = false;
|
||||
if (event.stopPropagation) event.stopPropagation();
|
||||
if (event.cancelBubble) event.cancelBubble = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理keydown事件
|
||||
function dispatch(event, element) {
|
||||
const asterisk = _handlers['*'];
|
||||
let key = event.keyCode || event.which || event.charCode;
|
||||
|
||||
// 表单控件过滤 默认表单控件不触发快捷键
|
||||
if (!hotkeys.filter.call(this, event)) return;
|
||||
|
||||
// Gecko(Firefox)的command键值224,在Webkit(Chrome)中保持一致
|
||||
// Webkit左右 command 键值不一样
|
||||
if (key === 93 || key === 224) key = 91;
|
||||
|
||||
/**
|
||||
* Collect bound keys
|
||||
* If an Input Method Editor is processing key input and the event is keydown, return 229.
|
||||
* https://stackoverflow.com/questions/25043934/is-it-ok-to-ignore-keydown-events-with-keycode-229
|
||||
* http://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html
|
||||
*/
|
||||
if (_downKeys.indexOf(key) === -1 && key !== 229) _downKeys.push(key);
|
||||
/**
|
||||
* Jest test cases are required.
|
||||
* ===============================
|
||||
*/
|
||||
['ctrlKey', 'altKey', 'shiftKey', 'metaKey'].forEach(keyName => {
|
||||
const keyNum = modifierMap[keyName];
|
||||
if (event[keyName] && _downKeys.indexOf(keyNum) === -1) {
|
||||
_downKeys.push(keyNum);
|
||||
} else if (!event[keyName] && _downKeys.indexOf(keyNum) > -1) {
|
||||
_downKeys.splice(_downKeys.indexOf(keyNum), 1);
|
||||
} else if (keyName === 'metaKey' && event[keyName] && _downKeys.length === 3) {
|
||||
/**
|
||||
* Fix if Command is pressed:
|
||||
* ===============================
|
||||
*/
|
||||
if (!(event.ctrlKey || event.shiftKey || event.altKey)) {
|
||||
_downKeys = _downKeys.slice(_downKeys.indexOf(keyNum));
|
||||
}
|
||||
}
|
||||
});
|
||||
/**
|
||||
* -------------------------------
|
||||
*/
|
||||
|
||||
if (key in _mods) {
|
||||
_mods[key] = true;
|
||||
|
||||
// 将特殊字符的key注册到 hotkeys 上
|
||||
for (const k in _modifier) {
|
||||
if (_modifier[k] === key) hotkeys[k] = true;
|
||||
}
|
||||
if (!asterisk) return;
|
||||
}
|
||||
|
||||
// 将 modifierMap 里面的修饰键绑定到 event 中
|
||||
for (const e in _mods) {
|
||||
if (Object.prototype.hasOwnProperty.call(_mods, e)) {
|
||||
_mods[e] = event[modifierMap[e]];
|
||||
}
|
||||
}
|
||||
/**
|
||||
* https://github.com/jaywcjlove/hotkeys/pull/129
|
||||
* This solves the issue in Firefox on Windows where hotkeys corresponding to special characters would not trigger.
|
||||
* An example of this is ctrl+alt+m on a Swedish keyboard which is used to type μ.
|
||||
* Browser support: https://caniuse.com/#feat=keyboardevent-getmodifierstate
|
||||
*/
|
||||
if (event.getModifierState && !(event.altKey && !event.ctrlKey) && event.getModifierState('AltGraph')) {
|
||||
if (_downKeys.indexOf(17) === -1) {
|
||||
_downKeys.push(17);
|
||||
}
|
||||
if (_downKeys.indexOf(18) === -1) {
|
||||
_downKeys.push(18);
|
||||
}
|
||||
_mods[17] = true;
|
||||
_mods[18] = true;
|
||||
}
|
||||
|
||||
// 获取范围 默认为 `all`
|
||||
const scope = getScope();
|
||||
// 对任何快捷键都需要做的处理
|
||||
if (asterisk) {
|
||||
for (let i = 0; i < asterisk.length; i++) {
|
||||
if (asterisk[i].scope === scope && (event.type === 'keydown' && asterisk[i].keydown || event.type === 'keyup' && asterisk[i].keyup)) {
|
||||
eventHandler(event, asterisk[i], scope, element);
|
||||
}
|
||||
}
|
||||
}
|
||||
// key 不在 _handlers 中返回
|
||||
if (!(key in _handlers)) return;
|
||||
const handlerKey = _handlers[key];
|
||||
const keyLen = handlerKey.length;
|
||||
for (let i = 0; i < keyLen; i++) {
|
||||
if (event.type === 'keydown' && handlerKey[i].keydown || event.type === 'keyup' && handlerKey[i].keyup) {
|
||||
if (handlerKey[i].key) {
|
||||
const record = handlerKey[i];
|
||||
const {
|
||||
splitKey
|
||||
} = record;
|
||||
const keyShortcut = record.key.split(splitKey);
|
||||
const _downKeysCurrent = []; // 记录当前按键键值
|
||||
for (let a = 0; a < keyShortcut.length; a++) {
|
||||
_downKeysCurrent.push(code(keyShortcut[a]));
|
||||
}
|
||||
if (_downKeysCurrent.sort().join('') === _downKeys.sort().join('')) {
|
||||
// 找到处理内容
|
||||
eventHandler(event, record, scope, element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
function hotkeys(key, option, method) {
|
||||
_downKeys = [];
|
||||
const keys = getKeys(key); // 需要处理的快捷键列表
|
||||
let mods = [];
|
||||
let scope = 'all'; // scope默认为all,所有范围都有效
|
||||
let element = document; // 快捷键事件绑定节点
|
||||
let i = 0;
|
||||
let keyup = false;
|
||||
let keydown = true;
|
||||
let splitKey = '+';
|
||||
let capture = false;
|
||||
let single = false; // 单个callback
|
||||
|
||||
// 对为设定范围的判断
|
||||
if (method === undefined && typeof option === 'function') {
|
||||
method = option;
|
||||
}
|
||||
if (Object.prototype.toString.call(option) === '[object Object]') {
|
||||
if (option.scope) scope = option.scope; // eslint-disable-line
|
||||
if (option.element) element = option.element; // eslint-disable-line
|
||||
if (option.keyup) keyup = option.keyup; // eslint-disable-line
|
||||
if (option.keydown !== undefined) keydown = option.keydown; // eslint-disable-line
|
||||
if (option.capture !== undefined) capture = option.capture; // eslint-disable-line
|
||||
if (typeof option.splitKey === 'string') splitKey = option.splitKey; // eslint-disable-line
|
||||
if (option.single === true) single = true; // eslint-disable-line
|
||||
}
|
||||
if (typeof option === 'string') scope = option;
|
||||
|
||||
// 如果只允许单个callback,先unbind
|
||||
if (single) unbind(key, scope);
|
||||
|
||||
// 对于每个快捷键进行处理
|
||||
for (; i < keys.length; i++) {
|
||||
key = keys[i].split(splitKey); // 按键列表
|
||||
mods = [];
|
||||
|
||||
// 如果是组合快捷键取得组合快捷键
|
||||
if (key.length > 1) mods = getMods(_modifier, key);
|
||||
|
||||
// 将非修饰键转化为键码
|
||||
key = key[key.length - 1];
|
||||
key = key === '*' ? '*' : code(key); // *表示匹配所有快捷键
|
||||
|
||||
// 判断key是否在_handlers中,不在就赋一个空数组
|
||||
if (!(key in _handlers)) _handlers[key] = [];
|
||||
_handlers[key].push({
|
||||
keyup,
|
||||
keydown,
|
||||
scope,
|
||||
mods,
|
||||
shortcut: keys[i],
|
||||
method,
|
||||
key: keys[i],
|
||||
splitKey,
|
||||
element
|
||||
});
|
||||
}
|
||||
// 在全局document上设置快捷键
|
||||
if (typeof element !== 'undefined' && window) {
|
||||
if (!elementEventMap.has(element)) {
|
||||
const keydownListener = function () {
|
||||
let event = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.event;
|
||||
return dispatch(event, element);
|
||||
};
|
||||
const keyupListenr = function () {
|
||||
let event = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.event;
|
||||
dispatch(event, element);
|
||||
clearModifier(event);
|
||||
};
|
||||
elementEventMap.set(element, {
|
||||
keydownListener,
|
||||
keyupListenr,
|
||||
capture
|
||||
});
|
||||
addEvent(element, 'keydown', keydownListener, capture);
|
||||
addEvent(element, 'keyup', keyupListenr, capture);
|
||||
}
|
||||
if (!winListendFocus) {
|
||||
const listener = () => {
|
||||
_downKeys = [];
|
||||
};
|
||||
winListendFocus = {
|
||||
listener,
|
||||
capture
|
||||
};
|
||||
addEvent(window, 'focus', listener, capture);
|
||||
}
|
||||
}
|
||||
}
|
||||
function trigger(shortcut) {
|
||||
let scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'all';
|
||||
Object.keys(_handlers).forEach(key => {
|
||||
const dataList = _handlers[key].filter(item => item.scope === scope && item.shortcut === shortcut);
|
||||
dataList.forEach(data => {
|
||||
if (data && data.method) {
|
||||
data.method();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 销毁事件,unbind之后判断element上是否还有键盘快捷键,如果没有移除监听
|
||||
function removeKeyEvent(element) {
|
||||
const values = Object.values(_handlers).flat();
|
||||
const findindex = values.findIndex(_ref4 => {
|
||||
let {
|
||||
element: el
|
||||
} = _ref4;
|
||||
return el === element;
|
||||
});
|
||||
if (findindex < 0) {
|
||||
const {
|
||||
keydownListener,
|
||||
keyupListenr,
|
||||
capture
|
||||
} = elementEventMap.get(element) || {};
|
||||
if (keydownListener && keyupListenr) {
|
||||
removeEvent(element, 'keyup', keyupListenr, capture);
|
||||
removeEvent(element, 'keydown', keydownListener, capture);
|
||||
elementEventMap.delete(element);
|
||||
}
|
||||
}
|
||||
if (values.length <= 0 || elementEventMap.size <= 0) {
|
||||
// 移除所有的元素上的监听
|
||||
const eventKeys = Object.keys(elementEventMap);
|
||||
eventKeys.forEach(el => {
|
||||
const {
|
||||
keydownListener,
|
||||
keyupListenr,
|
||||
capture
|
||||
} = elementEventMap.get(el) || {};
|
||||
if (keydownListener && keyupListenr) {
|
||||
removeEvent(el, 'keyup', keyupListenr, capture);
|
||||
removeEvent(el, 'keydown', keydownListener, capture);
|
||||
elementEventMap.delete(el);
|
||||
}
|
||||
});
|
||||
// 清空 elementEventMap
|
||||
elementEventMap.clear();
|
||||
// 清空 _handlers
|
||||
Object.keys(_handlers).forEach(key => delete _handlers[key]);
|
||||
// 移除window上的focus监听
|
||||
if (winListendFocus) {
|
||||
const {
|
||||
listener,
|
||||
capture
|
||||
} = winListendFocus;
|
||||
removeEvent(window, 'focus', listener, capture);
|
||||
winListendFocus = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
const _api = {
|
||||
getPressedKeyString,
|
||||
setScope,
|
||||
getScope,
|
||||
deleteScope,
|
||||
getPressedKeyCodes,
|
||||
getAllKeyCodes,
|
||||
isPressed,
|
||||
filter,
|
||||
trigger,
|
||||
unbind,
|
||||
keyMap: _keyMap,
|
||||
modifier: _modifier,
|
||||
modifierMap
|
||||
};
|
||||
for (const a in _api) {
|
||||
if (Object.prototype.hasOwnProperty.call(_api, a)) {
|
||||
hotkeys[a] = _api[a];
|
||||
}
|
||||
}
|
||||
if (typeof window !== 'undefined') {
|
||||
const _hotkeys = window.hotkeys;
|
||||
hotkeys.noConflict = deep => {
|
||||
if (deep && window.hotkeys === hotkeys) {
|
||||
window.hotkeys = _hotkeys;
|
||||
}
|
||||
return hotkeys;
|
||||
};
|
||||
window.hotkeys = hotkeys;
|
||||
}
|
||||
|
||||
return hotkeys;
|
||||
|
||||
}));
|
2
static/pkg/hotkeys-js/dist/hotkeys.min.js
vendored
2
static/pkg/hotkeys-js/dist/hotkeys.min.js
vendored
File diff suppressed because one or more lines are too long
14
static/pkg/hotkeys-js/dist/terminal.js
vendored
14
static/pkg/hotkeys-js/dist/terminal.js
vendored
@ -1,14 +0,0 @@
|
||||
hotkeys('ctrl+enter', function() {
|
||||
var ele = document.getElementById('aiterm');
|
||||
var svg = document.getElementById('aisvg');
|
||||
if (ele.style.display == 'none') {
|
||||
ele.style.display = 'block';
|
||||
svg.style.display = 'none';
|
||||
ele.focus();
|
||||
} else {
|
||||
ele.style.display = 'none';
|
||||
svg.style.display = 'block';
|
||||
svg.focus();
|
||||
}
|
||||
});
|
||||
|
181
static/pkg/hotkeys-js/index.d.ts
vendored
181
static/pkg/hotkeys-js/index.d.ts
vendored
@ -1,181 +0,0 @@
|
||||
export interface HotkeysEvent {
|
||||
key: string;
|
||||
keys: number[];
|
||||
method: KeyHandler;
|
||||
mods: number[];
|
||||
scope: string;
|
||||
shortcut: string;
|
||||
}
|
||||
|
||||
export interface KeyHandler {
|
||||
(keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent): void | boolean;
|
||||
}
|
||||
|
||||
type Options = {
|
||||
scope?: string;
|
||||
element?: HTMLElement | null;
|
||||
keyup?: boolean | null;
|
||||
keydown?: boolean | null;
|
||||
capture?: boolean
|
||||
splitKey?: string;
|
||||
single?: boolean;
|
||||
}
|
||||
|
||||
export interface Hotkeys {
|
||||
(key: string, method: KeyHandler): void;
|
||||
(key: string, scope: string, method: KeyHandler): void;
|
||||
(key: string, options: Options, method: KeyHandler): void;
|
||||
|
||||
shift: boolean;
|
||||
ctrl: boolean;
|
||||
alt: boolean;
|
||||
option: boolean;
|
||||
control: boolean;
|
||||
cmd: boolean;
|
||||
command: boolean;
|
||||
|
||||
keyMap: Record<string, number>;
|
||||
modifier: Record<string, number>;
|
||||
modifierMap: Record<string, number | string>;
|
||||
|
||||
/**
|
||||
* Use the `hotkeys.setScope` method to set scope. There can only be one active scope besides 'all'. By default 'all' is always active.
|
||||
*
|
||||
* ```js
|
||||
* // Define shortcuts with a scope
|
||||
* hotkeys('ctrl+o, ctrl+alt+enter', 'issues', function() {
|
||||
* console.log('do something');
|
||||
* });
|
||||
* hotkeys('o, enter', 'files', function() {
|
||||
* console.log('do something else');
|
||||
* });
|
||||
*
|
||||
* // Set the scope (only 'all' and 'issues' shortcuts will be honored)
|
||||
* hotkeys.setScope('issues'); // default scope is 'all'
|
||||
* ```
|
||||
*/
|
||||
setScope(scopeName: string): void;
|
||||
/**
|
||||
* Use the `hotkeys.getScope` method to get scope.
|
||||
*
|
||||
* ```js
|
||||
* hotkeys.getScope();
|
||||
* ```
|
||||
*/
|
||||
getScope(): string;
|
||||
/**
|
||||
* Use the `hotkeys.deleteScope` method to delete a scope. This will also remove all associated hotkeys with it.
|
||||
*
|
||||
* ```js
|
||||
* hotkeys.deleteScope('issues');
|
||||
* ```
|
||||
* You can use second argument, if need set new scope after deleting.
|
||||
*
|
||||
* ```js
|
||||
* hotkeys.deleteScope('issues', 'newScopeName');
|
||||
* ```
|
||||
*/
|
||||
deleteScope(scopeName: string, newScopeName?: string): void;
|
||||
|
||||
/**
|
||||
* Relinquish HotKeys’s control of the `hotkeys` variable.
|
||||
*
|
||||
* ```js
|
||||
* var k = hotkeys.noConflict();
|
||||
* k('a', function() {
|
||||
* console.log("do something")
|
||||
* });
|
||||
*
|
||||
* hotkeys()
|
||||
* // -->Uncaught TypeError: hotkeys is not a function(anonymous function)
|
||||
* // @ VM2170:2InjectedScript._evaluateOn
|
||||
* // @ VM2165:883InjectedScript._evaluateAndWrap
|
||||
* // @ VM2165:816InjectedScript.evaluate @ VM2165:682
|
||||
* ```
|
||||
*/
|
||||
noConflict(): Hotkeys;
|
||||
|
||||
/**
|
||||
* trigger shortcut key event
|
||||
*
|
||||
* ```js
|
||||
* hotkeys.trigger('ctrl+o');
|
||||
* hotkeys.trigger('ctrl+o', 'scope2');
|
||||
* ```
|
||||
*/
|
||||
trigger(shortcut: string, scope?: string): void;
|
||||
|
||||
unbind(key?: string): void;
|
||||
unbind(key: string, scopeName: string): void;
|
||||
unbind(key: string, scopeName: string, method: KeyHandler): void;
|
||||
unbind(key: string, method: KeyHandler): void;
|
||||
|
||||
/** For example, `hotkeys.isPressed(77)` is true if the `M` key is currently pressed. */
|
||||
isPressed(keyCode: number): boolean;
|
||||
/** For example, `hotkeys.isPressed('m')` is true if the `M` key is currently pressed. */
|
||||
isPressed(keyCode: string): boolean;
|
||||
/**
|
||||
* Returns an array of key codes currently pressed.
|
||||
*
|
||||
* ```js
|
||||
* hotkeys('command+ctrl+shift+a,f', function() {
|
||||
* console.log(hotkeys.getPressedKeyCodes()); //=> [17, 65] or [70]
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
getPressedKeyCodes(): number[];
|
||||
/**
|
||||
* Returns an array of key codes currently pressed.
|
||||
*
|
||||
* ```js
|
||||
* hotkeys('command+ctrl+shift+a,f', function() {
|
||||
* console.log(hotkeys.getPressedKeyString()); //=> ['⌘', '⌃', '⇧', 'A', 'F']
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
getPressedKeyString(): string[];
|
||||
/**
|
||||
* Get a list of all registration codes.
|
||||
*
|
||||
* ```js
|
||||
* hotkeys('command+ctrl+shift+a,f', function() {
|
||||
* console.log(hotkeys.getAllKeyCodes());
|
||||
* // [
|
||||
* // { scope: 'all', shortcut: 'command+ctrl+shift+a', mods: [91, 17, 16], keys: [91, 17, 16, 65] },
|
||||
* // { scope: 'all', shortcut: 'f', mods: [], keys: [42] }
|
||||
* // ]
|
||||
* })
|
||||
* ```
|
||||
*
|
||||
*/
|
||||
getAllKeyCodes(): Omit<HotkeysEvent, 'method' | 'key'>[];
|
||||
|
||||
/**
|
||||
* By default hotkeys are not enabled for `INPUT` `SELECT` `TEXTAREA` elements.
|
||||
* `Hotkeys.filter` to return to the `true` shortcut keys set to play a role,
|
||||
* `false` shortcut keys set up failure.
|
||||
*
|
||||
* ```js
|
||||
* hotkeys.filter = function(event){
|
||||
* return true;
|
||||
* }
|
||||
* //How to add the filter to edit labels. <div contentEditable="true"></div>
|
||||
* //"contentEditable" Older browsers that do not support drops
|
||||
* hotkeys.filter = function(event) {
|
||||
* var target = event.target || event.srcElement;
|
||||
* var tagName = target.tagName;
|
||||
* return !(target.isContentEditable || tagName == 'INPUT' || tagName == 'SELECT' || tagName == 'TEXTAREA');
|
||||
* }
|
||||
*
|
||||
* hotkeys.filter = function(event){
|
||||
* var tagName = (event.target || event.srcElement).tagName;
|
||||
* hotkeys.setScope(/^(INPUT|TEXTAREA|SELECT)$/.test(tagName) ? 'input' : 'other');
|
||||
* return true;
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
filter(event: KeyboardEvent): boolean;
|
||||
}
|
||||
// https://github.com/eiriklv/react-masonry-component/issues/57
|
||||
declare var hotkeys: Hotkeys;
|
||||
export default hotkeys;
|
@ -1,7 +0,0 @@
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
// eslint-disable-next-line global-require
|
||||
module.exports = require('./dist/hotkeys.common.min.js');
|
||||
} else {
|
||||
// eslint-disable-next-line global-require
|
||||
module.exports = require('./dist/hotkeys.common.js');
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
{
|
||||
"name": "hotkeys-js",
|
||||
"description": "A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.",
|
||||
"version": "3.13.7",
|
||||
"main": "index.js",
|
||||
"types": "index.d.ts",
|
||||
"module": "dist/hotkeys.esm.js",
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"dist",
|
||||
"doc"
|
||||
],
|
||||
"keywords": [
|
||||
"hotkey",
|
||||
"hotkeys",
|
||||
"hotkeys-js",
|
||||
"hotkeysjs",
|
||||
"key",
|
||||
"keys",
|
||||
"keyboard",
|
||||
"shortcuts",
|
||||
"keypress"
|
||||
],
|
||||
"author": "kenny wong <wowohoo@qq.com>",
|
||||
"license": "MIT",
|
||||
"homepage": "https://jaywcjlove.github.io/hotkeys-js",
|
||||
"funding": "https://jaywcjlove.github.io/#/sponsor",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/jaywcjlove/hotkeys-js.git"
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 171 KiB After Width: | Height: | Size: 57 KiB |
Binary file not shown.
Binary file not shown.
99
static/pkg/icomoon/icomoon.css
Normal file
99
static/pkg/icomoon/icomoon.css
Normal file
@ -0,0 +1,99 @@
|
||||
@font-face {
|
||||
font-family: 'icomoon';
|
||||
src: url('fonts/icomoon.eot?3louk1');
|
||||
src: url('fonts/icomoon.eot?3louk1#iefix') format('embedded-opentype'),
|
||||
url('fonts/icomoon.ttf?3louk1') format('truetype'),
|
||||
url('fonts/icomoon.woff?3louk1') format('woff'),
|
||||
url('fonts/icomoon.svg?3louk1#icomoon') format('svg');
|
||||
font-weight: normal;
|
||||
font-style: normal;
|
||||
font-display: block;
|
||||
}
|
||||
|
||||
[class^="icon-"], [class*=" icon-"] {
|
||||
/* use !important to prevent issues with browser extensions that change fonts */
|
||||
font-family: 'icomoon' !important;
|
||||
speak: never;
|
||||
font-style: normal;
|
||||
font-weight: normal;
|
||||
font-variant: normal;
|
||||
text-transform: none;
|
||||
line-height: 1;
|
||||
|
||||
/* Better Font Rendering =========== */
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
|
||||
.icon-cube:before {
|
||||
content: "\e900";
|
||||
}
|
||||
.icon-game:before {
|
||||
content: "\e9d5";
|
||||
}
|
||||
.icon-card:before {
|
||||
content: "\e9d6";
|
||||
}
|
||||
.icon-book:before {
|
||||
content: "\e9d7";
|
||||
}
|
||||
.icon-git_bg:before {
|
||||
content: "\e9d4";
|
||||
}
|
||||
.icon-git:before {
|
||||
content: "\e9d3";
|
||||
}
|
||||
.icon-moji_a:before {
|
||||
content: "\e9c3";
|
||||
}
|
||||
.icon-archlinux:before {
|
||||
content: "\e9c4";
|
||||
}
|
||||
.icon-archlinuxjp:before {
|
||||
content: "\e9c5";
|
||||
}
|
||||
.icon-syui:before {
|
||||
content: "\e9c6";
|
||||
}
|
||||
.icon-phoenix-power:before {
|
||||
content: "\e9c7";
|
||||
}
|
||||
.icon-phoenix-world:before {
|
||||
content: "\e9c8";
|
||||
}
|
||||
.icon-power:before {
|
||||
content: "\e9c9";
|
||||
}
|
||||
.icon-phoenix:before {
|
||||
content: "\e9ca";
|
||||
}
|
||||
.icon-honeycomb:before {
|
||||
content: "\e9cb";
|
||||
}
|
||||
.icon-ai:before {
|
||||
content: "\e9cc";
|
||||
}
|
||||
.icon-robot:before {
|
||||
content: "\e9cd";
|
||||
}
|
||||
.icon-sandar:before {
|
||||
content: "\e9ce";
|
||||
}
|
||||
.icon-moon:before {
|
||||
content: "\e9cf";
|
||||
}
|
||||
.icon-home:before {
|
||||
content: "\e9d0";
|
||||
}
|
||||
.icon-cloud:before {
|
||||
content: "\e9d1";
|
||||
}
|
||||
.icon-api:before {
|
||||
content: "\e9d2";
|
||||
}
|
||||
.icon-aibadge:before {
|
||||
content: "\ebf8";
|
||||
}
|
||||
.icon-aiterm:before {
|
||||
content: "\ebf7";
|
||||
}
|
Reference in New Issue
Block a user