Skip to content

Commit

Permalink
add: magic key palette keyboard handle
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Nov 12, 2024
1 parent b6a3f3c commit 6590c94
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/extras/editor/magicKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ class PluginState {
</style>
<div class="popup-content">
<input type="text" class="popup-input" placeholder="Search commands" />
<div class="popup-list">
<div class="popup-list" tabindex="-1">
${Object.entries(this.commands)
.map(
([id, command]) => `
Expand Down Expand Up @@ -375,6 +375,38 @@ class PluginState {
} else if (event.key === "ArrowDown") {
this._selectCommand(this.selectedCommandIndex + 1, "down");
event.preventDefault();
} else if (event.key === "ArrowLeft") {
// Select the first command
this._selectCommand(this.commands.length, "up");
event.preventDefault();
} else if (event.key === "ArrowRight") {
// Select the last command
this._selectCommand(-1, "down");
event.preventDefault();
} else if (event.key === "Tab") {
// If has input, autocomplete the selected command to the first space
const command = this.commands[this.selectedCommandIndex];
if (!command) {
return;
}
if (!input.value) {
return;
}
const title = command.title!;
// Compute after the matched part
const matchedIndex = title
.toLowerCase()
.indexOf(input.value.toLowerCase());
const spaceIndex = title.indexOf(
" ",
matchedIndex + input.value.length,
);
if (spaceIndex >= 0) {
input.value = title.slice(0, spaceIndex);
} else {
input.value = title;
}
event.preventDefault();
} else if (event.key === "Enter") {
event.preventDefault();
const command = this.commands[this.selectedCommandIndex];
Expand All @@ -384,6 +416,7 @@ class PluginState {
}
this._executeCommand(this.selectedCommandIndex, state);
} else if (event.key === "Escape") {
event.preventDefault();
this._closePopup();
} else if (event.key === "z" && (event.ctrlKey || event.metaKey)) {
this._closePopup();
Expand Down

0 comments on commit 6590c94

Please sign in to comment.