-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
69 changed files
with
2,942 additions
and
942 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
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
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 |
---|---|---|
@@ -1,39 +0,0 @@ | ||
/** | ||
* A keybinding is a sequence of chords. | ||
*/ | ||
export class Keybinding { | ||
public readonly chords: Chord[]; | ||
|
||
constructor(chords: Chord[]) { | ||
if (chords.length === 0) { | ||
throw illegalArgument(`chords`); | ||
} | ||
this.chords = chords; | ||
} | ||
|
||
public getHashCode(): string { | ||
let result = ''; | ||
for (let i = 0, len = this.chords.length; i < len; i++) { | ||
if (i !== 0) { | ||
result += ';'; | ||
} | ||
result += this.chords[i].getHashCode(); | ||
} | ||
return result; | ||
} | ||
|
||
public equals(other: Keybinding | null): boolean { | ||
if (other === null) { | ||
return false; | ||
} | ||
if (this.chords.length !== other.chords.length) { | ||
return false; | ||
} | ||
for (let i = 0; i < this.chords.length; i++) { | ||
if (!this.chords[i].equals(other.chords[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
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,99 @@ | ||
import { KeyCodeUtils, ScanCodeUtils } from '@alilc/lowcode-shared'; | ||
import { KeyCodeChord, ScanCodeChord, Keybinding, Chord } from './keybindings'; | ||
|
||
export class KeybindingParser { | ||
private static _readModifiers(input: string) { | ||
input = input.toLowerCase().trim(); | ||
|
||
let ctrl = false; | ||
let shift = false; | ||
let alt = false; | ||
let meta = false; | ||
|
||
let matchedModifier: boolean; | ||
|
||
do { | ||
matchedModifier = false; | ||
if (/^ctrl(\+|-)/.test(input)) { | ||
ctrl = true; | ||
input = input.slice('ctrl-'.length); | ||
matchedModifier = true; | ||
} | ||
if (/^shift(\+|-)/.test(input)) { | ||
shift = true; | ||
input = input.slice('shift-'.length); | ||
matchedModifier = true; | ||
} | ||
if (/^alt(\+|-)/.test(input)) { | ||
alt = true; | ||
input = input.slice('alt-'.length); | ||
matchedModifier = true; | ||
} | ||
if (/^meta(\+|-)/.test(input)) { | ||
meta = true; | ||
input = input.slice('meta-'.length); | ||
matchedModifier = true; | ||
} | ||
if (/^win(\+|-)/.test(input)) { | ||
meta = true; | ||
input = input.slice('win-'.length); | ||
matchedModifier = true; | ||
} | ||
if (/^cmd(\+|-)/.test(input)) { | ||
meta = true; | ||
input = input.slice('cmd-'.length); | ||
matchedModifier = true; | ||
} | ||
} while (matchedModifier); | ||
|
||
let key: string; | ||
|
||
const firstSpaceIdx = input.indexOf(' '); | ||
if (firstSpaceIdx > 0) { | ||
key = input.substring(0, firstSpaceIdx); | ||
input = input.substring(firstSpaceIdx); | ||
} else { | ||
key = input; | ||
input = ''; | ||
} | ||
|
||
return { | ||
remains: input, | ||
ctrl, | ||
shift, | ||
alt, | ||
meta, | ||
key, | ||
}; | ||
} | ||
|
||
private static parseChord(input: string): [Chord, string] { | ||
const mods = this._readModifiers(input); | ||
const scanCodeMatch = mods.key.match(/^\[([^\]]+)\]$/); | ||
if (scanCodeMatch) { | ||
const strScanCode = scanCodeMatch[1]; | ||
const scanCode = ScanCodeUtils.lowerCaseToEnum(strScanCode); | ||
return [ | ||
new ScanCodeChord(mods.ctrl, mods.shift, mods.alt, mods.meta, scanCode), | ||
mods.remains, | ||
]; | ||
} | ||
const keyCode = KeyCodeUtils.fromUserSettings(mods.key); | ||
return [new KeyCodeChord(mods.ctrl, mods.shift, mods.alt, mods.meta, keyCode), mods.remains]; | ||
} | ||
|
||
static parseKeybinding(input: string): Keybinding | null { | ||
if (!input) { | ||
return null; | ||
} | ||
|
||
const chords: Chord[] = []; | ||
let chord: Chord; | ||
|
||
while (input.length > 0) { | ||
[chord, input] = this.parseChord(input); | ||
chords.push(chord); | ||
} | ||
return chords.length > 0 ? new Keybinding(chords) : null; | ||
} | ||
} |
Oops, something went wrong.