-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add keyboard service, handle undo/redo as a pilot
- Loading branch information
1 parent
c43b388
commit 9a6eb84
Showing
4 changed files
with
132 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { InvalidArguments } from '@/utils/exceptions/InvalidArguments' | ||
|
||
export type KeyCallback = () => void | ||
type KeyTree = Map<string, KeyTree> | KeyCallback | ||
|
||
export default class KeyMap { | ||
private keyTreeRoot: KeyTree = new Map<string, KeyTree>() | ||
|
||
public get(keySequence: string[]) { | ||
return this.getLeaf(keySequence, this.keyTreeRoot) | ||
} | ||
|
||
public set(keySequence: string[], callback: KeyCallback) { | ||
if (keySequence.length < 1) return | ||
this.keyTreeRoot = this.setLeaf(keySequence, this.keyTreeRoot, callback) | ||
} | ||
|
||
private getLeaf(keySequence: string[], keyTree: KeyTree): KeyCallback | null { | ||
if (keySequence.length < 1) { | ||
/* Reached bottom of sequence */ | ||
if (keyTree instanceof Map) return null | ||
return keyTree | ||
} | ||
|
||
if (!(keyTree instanceof Map)) { | ||
/* Reached bottom of the tree */ | ||
return null | ||
} | ||
|
||
const firstKey = keySequence[0] | ||
if (!keyTree.has(firstKey)) return null | ||
return this.getLeaf(keySequence.slice(1), keyTree.get(firstKey)!) | ||
} | ||
|
||
private setLeaf( | ||
keySequence: string[], | ||
keyTree: KeyTree, | ||
leaf: KeyCallback | ||
): KeyTree { | ||
if (keySequence.length < 1) { | ||
/* Reached bottom of the sequence */ | ||
return leaf | ||
} | ||
|
||
if (!(keyTree instanceof Map)) { | ||
/* Reached bottom of the tree */ | ||
throw new InvalidArguments('KeyMap.set - conflicting shortcuts') | ||
} | ||
|
||
const firstKey = keySequence[0] | ||
if (keyTree.has(firstKey)) | ||
return this.setLeaf(keySequence.slice(1), keyTree.get(firstKey)!, leaf) | ||
return keyTree.set( | ||
firstKey, | ||
this.setLeaf(keySequence.slice(1), new Map(), leaf) | ||
) | ||
} | ||
} |
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,65 @@ | ||
import Logger, { type ILogger } from 'js-logger' | ||
import KeyMap, { type KeyCallback } from './KeyMap' | ||
|
||
type KeyboardServiceMap = Map<string, KeyboardService> | ||
|
||
class KeyboardService { | ||
private logger: ILogger | ||
private keyTree: KeyMap = new KeyMap() | ||
|
||
constructor(keyspace: string) { | ||
this.logger = Logger.get(`KeyboardService[${keyspace}]`) | ||
} | ||
|
||
public registerCallback(keySequence: string[], callback: KeyCallback) { | ||
this.keyTree.set(keySequence, callback) | ||
} | ||
|
||
public handleClick(keySequence: string[]) { | ||
const callback = this.keyTree.get(keySequence) | ||
if (callback) callback() | ||
} | ||
} | ||
|
||
class GlobalKeyboardService { | ||
private logger = Logger.get('KeyboardService') | ||
private keyspaces: KeyboardServiceMap = new Map<string, KeyboardService>() | ||
private selectedKeyspace: string | null = null | ||
|
||
constructor() { | ||
const clickHandler = (event: KeyboardEvent) => this.handleClick(event) | ||
window.removeEventListener('keydown', clickHandler) | ||
window.addEventListener('keydown', clickHandler) | ||
} | ||
|
||
public get(keyspace: string) { | ||
if (this.keyspaces.has(keyspace)) return this.keyspaces.get(keyspace)! | ||
|
||
const newKeyboardService = new KeyboardService(keyspace) | ||
this.keyspaces.set(keyspace, newKeyboardService) | ||
this.logger.debug(`New keyspace added: ${keyspace}`) | ||
|
||
return newKeyboardService | ||
} | ||
|
||
public activateKeyspace(keyspace: string | null) { | ||
const previousKeyspace = this.selectedKeyspace | ||
this.selectedKeyspace = keyspace | ||
this.logger.debug(`Activated keyspace ${keyspace}`) | ||
return previousKeyspace | ||
} | ||
|
||
public handleClick(event: KeyboardEvent) { | ||
const keySequence: Array<string> = [] | ||
/* For now support only combinations with ctrl */ | ||
if (event.ctrlKey && event.key != 'Control') keySequence.push('Control') | ||
keySequence.push(event.key) | ||
|
||
if (this.selectedKeyspace) { | ||
const service = this.get(this.selectedKeyspace) | ||
service.handleClick(keySequence) | ||
} | ||
} | ||
} | ||
|
||
export default new GlobalKeyboardService() |
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