Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feature): user can add custom key map #222

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface KeyHandler {
(keyboardEvent: KeyboardEvent, hotkeysEvent: HotkeysEvent): void | boolean
}

type Options = {
export type Options = {
scope?: string,
element?: HTMLElement | null,
keyup?: boolean | null
Expand Down Expand Up @@ -47,6 +47,8 @@ interface Hotkeys {
getPressedKeyCodes(): number[]

filter(event: KeyboardEvent): boolean

addCustomKeyMap(customKeyMap: Record<string, number>): void
}
// https://github.com/eiriklv/react-masonry-component/issues/57
declare var hotkeys: Hotkeys
Expand Down
28 changes: 26 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { addEvent, getMods, getKeys, compareArray } from './utils';
import { addEvent, getMods, getKeys, compareArray, isInteger } from './utils';
import { _keyMap, _modifier, modifierMap, _mods, _handlers } from './var';

let _downKeys = []; // 记录摁下的绑定键
const _customKeyMap = {};

let _scope = 'all'; // 默认热键范围
const elementHasBindEvent = []; // 已绑定事件的节点记录

// 返回键码
const code = (x) => _keyMap[x.toLowerCase()]
const code = (x) => _customKeyMap[x] || _keyMap[x.toLowerCase()]
|| _modifier[x.toLowerCase()]
|| x.toUpperCase().charCodeAt(0);

Expand All @@ -24,6 +25,28 @@ function getPressedKeyCodes() {
return _downKeys.slice(0);
}

function addCustomKeyMap(keyMapPatch) {
if (typeof _keyMapPatch !== 'object') {
return;
}

const _keyMapPatch = {};

// check that all values for each key is integer
Object.keys(_keyMapPatch).forEach((key) => {
const keyCode = keyMapPatch[key];

if (isInteger(keyCode)) {
_keyMapPatch[key] = keyCode;
} else if (module.hot) {
window.console.warning(`[hotkeys-js] (addCustomKeyMap) key "${key}" has wrong value type, ignored`);
}
});

// merge only valid keyCodes
Object.assign(_customKeyMap, _keyMapPatch);
}

// 表单控件控件判断 返回 Boolean
// hotkey is effective only when filter return true
function filter(event) {
Expand Down Expand Up @@ -390,6 +413,7 @@ const _api = {
isPressed,
filter,
unbind,
addCustomKeyMap,
};
for (const a in _api) {
if (Object.prototype.hasOwnProperty.call(_api, a)) {
Expand Down
5 changes: 5 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ function compareArray(a1, a2) {
return isIndex;
}

function isInteger(value) {
return typeof value === 'number' && Number.isFinite(value) && Math.floor(value) === value;
}

export {
isff,
getMods,
getKeys,
addEvent,
compareArray,
isInteger,
};