-
Notifications
You must be signed in to change notification settings - Fork 0
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
Keyboard service ⌨️ #136
Merged
Merged
Keyboard service ⌨️ #136
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,53 @@ | ||
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] | ||
const recSequence = keySequence.slice(1) | ||
const recTree = keyTree.has(firstKey) ? keyTree.get(firstKey)! : new Map() | ||
return keyTree.set(firstKey, this.setLeaf(recSequence, recTree, 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 and ctrl + shift */ | ||
if (event.ctrlKey && event.key != 'Control') keySequence.push('Control') | ||
if (event.shiftKey && event.key != 'Shift') keySequence.push('Shift') | ||
keySequence.push(event.key) | ||
|
||
if (!this.selectedKeyspace) return | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
z
key is now, probably by mistake, in uppercase.