feat(editor): execute custom sql template by shortcut on selection
This commit is contained in:
1 parent
60bfb41f13
commit
802019daa3
26 files changed
+878
-7
No files matched your search
@@ -0,0 +1,30 @@
|
||||
import { matchesShortcut } from "@/lib/editor/keyboardShortcuts";
|
||||
import { parseShortcutParts } from "@/lib/editor/shortcutDisplay";
|
||||
import { enabledSqlShortcutActions } from "@/lib/sql/sqlShortcutActions";
|
||||
import type { SqlShortcutAction } from "@/types/database";
|
||||
// pi-lens-ignore: typescript:2307
|
||||
import type { EditorView } from "@codemirror/view";
|
||||
|
||||
const LETTER_KEY_RE = /^[A-Za-z]$/;
|
||||
|
||||
export function isCharacterProducingShortcut(shortcut: string): boolean {
|
||||
const parts = parseShortcutParts(shortcut.trim());
|
||||
if (parts.length === 0) return false;
|
||||
const key = parts[parts.length - 1] ?? "";
|
||||
if (!LETTER_KEY_RE.test(key)) return false;
|
||||
const modifiers = new Set(parts.slice(0, -1));
|
||||
return !modifiers.has("Mod") && !modifiers.has("Meta") && !modifiers.has("Ctrl") && !modifiers.has("Alt");
|
||||
}
|
||||
|
||||
export function createQueryEditorSqlShortcutDomHandler(getActions: () => readonly SqlShortcutAction[], runAction: (action: SqlShortcutAction, view: EditorView, event: KeyboardEvent) => boolean, platform = globalThis.navigator?.platform || ""): (event: KeyboardEvent, view: EditorView) => boolean {
|
||||
return (event, view) => {
|
||||
for (const action of enabledSqlShortcutActions(getActions())) {
|
||||
if (!isCharacterProducingShortcut(action.shortcut)) continue;
|
||||
if (!matchesShortcut(event, action.shortcut, platform)) continue;
|
||||
if (!runAction(action, view, event)) return false;
|
||||
event.preventDefault();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
@@ -48,6 +48,17 @@ export function shortcutDisplayParts(shortcut?: string, platform = globalThis.na
|
||||
return shortcutDisplayOrder(parseShortcutParts(shortcut), platform);
|
||||
}
|
||||
|
||||
export function canonicalShortcutKey(shortcut: string): string {
|
||||
return shortcutDisplayParts(shortcut, "Win32")
|
||||
.map((part) => {
|
||||
if (part === "Mod") return "Ctrl";
|
||||
if (part === "Plus") return "+";
|
||||
return part;
|
||||
})
|
||||
.join("+")
|
||||
.toLowerCase();
|
||||
}
|
||||
|
||||
export function shortcutDisplayStrokes(shortcut?: string, platform = globalThis.navigator?.platform || ""): string[][] {
|
||||
return parseShortcutStrokes(shortcut).map((parts) => shortcutDisplayOrder(parts, platform));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user