Skip to content

Commit

Permalink
use debug lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Elvin Dzhavadov committed Mar 6, 2020
1 parent 575b9c0 commit 92aefd1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"release": "np --contents dist"
},
"dependencies": {
"debug": "^4.1.1",
"invariant": "^2.2.4"
},
"devDependencies": {
Expand All @@ -34,6 +35,7 @@
"@babel/preset-env": "7.7.7",
"@babel/preset-typescript": "7.7.7",
"@cypress/webpack-preprocessor": "4.1.1",
"@types/debug": "^4.1.5",
"@types/invariant": "2.2.31",
"@types/jest": "24.0.25",
"@types/node": "13.1.2",
Expand Down
20 changes: 20 additions & 0 deletions src/creator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import invariant from 'invariant';
import debug from 'debug';
import {
DEFAULT_SCOPE,
MODIFIERS_KEYS,
Expand All @@ -21,6 +22,8 @@ interface Handler {
skipOther: boolean;
}

const log = debug('keybuddy');

const defaultFilter = (e: KeyboardEvent): boolean =>
e && !isEditable(e.target as HTMLElement);

Expand All @@ -43,6 +46,7 @@ export default (doc?: HTMLDocument, filterFn: FilterFn = defaultFilter) => {
modsKeys.forEach(key => {
modifiers[key] = e[MODIFIERS_MAP[key]];
});
log('Update modifiers', modifiers);
};

const bindKey = (
Expand Down Expand Up @@ -139,7 +143,9 @@ export default (doc?: HTMLDocument, filterFn: FilterFn = defaultFilter) => {
const { keyCode } = e;
const key = fixedKey(keyCode);

log(`Key ${key}`);
if (!filterFn(e)) {
log('Filtered', filterFn);
return;
}

Expand All @@ -153,13 +159,15 @@ export default (doc?: HTMLDocument, filterFn: FilterFn = defaultFilter) => {

if (!{}.hasOwnProperty.call(modifiers, key) && !downKeys.includes(key)) {
downKeys.push(key);
log('Push down keys', downKeys);
}
// See if we need to ignore the keypress (filter() can can be overridden)
// by default ignore key presses if a select, textarea, or input is focused
// if (!assignKey.filter.call(this, event)) return;

// abort if no potentially matching shortcuts found
if (!(key in handlers)) {
log('Key not in handler');
return;
}

Expand All @@ -175,6 +183,17 @@ export default (doc?: HTMLDocument, filterFn: FilterFn = defaultFilter) => {
}
);

log(
'Handlers for',
{
key,
downKeys,
modifiers
},
currentHandlers,
handlers
);

const primaryAction: Handler | undefined = currentHandlers.find(
action => action.skipOther
);
Expand All @@ -192,6 +211,7 @@ export default (doc?: HTMLDocument, filterFn: FilterFn = defaultFilter) => {
const key = fixedKey(keyCode);

downKeys = downKeys.filter(i => i !== key);
log(`Cleanup for ${key}`, downKeys);
};

const unbindScope = (deleteScope: string): void => {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1980,6 +1980,11 @@
dependencies:
"@babel/types" "^7.3.0"

"@types/debug@^4.1.5":
version "4.1.5"
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.5.tgz#b14efa8852b7768d898906613c23f688713e02cd"
integrity sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ==

"@types/eslint-visitor-keys@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
Expand Down

0 comments on commit 92aefd1

Please sign in to comment.