-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from alexjoverm/master
Several improvements
- Loading branch information
Showing
23 changed files
with
821 additions
and
414 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Action } from './action' | ||
import { KeyCombo } from './key-combo' | ||
import { logger, compareSets } from './utils' | ||
|
||
export class EventProcessor { | ||
public currentCombo: KeyCombo | ||
public lastEvent: KeyboardEvent | ||
|
||
constructor () { | ||
this.currentCombo = new KeyCombo() | ||
} | ||
|
||
public reset () { | ||
this.currentCombo = new KeyCombo() | ||
} | ||
|
||
public processEvent (ev: KeyboardEvent, actions: Map<string, Action>, debug: boolean) { | ||
const wasAppended = this.currentCombo.addEvent(ev) | ||
|
||
// Avoid repeated events | ||
if (!wasAppended) { | ||
return false | ||
} | ||
|
||
if (debug) { | ||
this.printDebugKeyPressed(ev) | ||
} | ||
|
||
if (wasAppended) { | ||
this.processActionCombos(ev, actions, debug) | ||
} | ||
} | ||
|
||
public cleanCombo (debug) { | ||
this.reset() | ||
|
||
if (debug) { | ||
logger.log('ShortcutJS: Cleaned keyMap') | ||
} | ||
} | ||
|
||
/** | ||
* Search for the right action, given a keyCombo, and execute its callbacks | ||
*/ | ||
public processActionCombos (ev: KeyboardEvent, actions: Map<string, Action>, debug: boolean) { | ||
for (let action of actions.values()) { | ||
if (this.matchesComboAction(action)) { | ||
if (debug) { | ||
this.printDebugActionFound(action) | ||
} | ||
|
||
for (let cb of action.callbacks) { | ||
cb(ev) | ||
} | ||
// Don't continue after finding it | ||
return false | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Checks whether the que matches a particular action | ||
*/ | ||
private matchesComboAction (action: Action) { | ||
return KeyCombo.isEqual(this.currentCombo, action.keyCombo) | ||
} | ||
|
||
private printDebugKeyPressed (ev: KeyboardEvent) { | ||
logger.group('ShortcutJS: KeyPressed') | ||
logger.log('Key: ', ev.keyCode) | ||
logger.group('Current combo:') | ||
logger.log('Keys: ', [...this.currentCombo.keys]) | ||
logger.log('State keys: ', this.currentCombo.stateKeys) | ||
logger.groupEnd() | ||
logger.groupEnd() | ||
} | ||
|
||
private printDebugActionFound (action: Action) { | ||
logger.group('%cShortcutJS: Action Matched', 'color: green') | ||
logger.log('Action: ', action.name) | ||
logger.group('Current combo:') | ||
logger.log('Keys: ', [...this.currentCombo.keys]) | ||
logger.log('State keys: ', this.currentCombo.stateKeys) | ||
logger.groupEnd() | ||
logger.log(`${action.callbacks.size} callbacks found`) | ||
logger.groupEnd() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.