From a9ce68860e61a48d0df090b27b3f5392f172f595 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 24 Jan 2024 14:03:14 +0100 Subject: [PATCH] Fixes #638 --- web/boot.ts | 2 +- web/components/mini_editor.tsx | 2 ++ web/editor_state.ts | 20 +++++++------------- web/editor_ui.tsx | 4 ++-- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/web/boot.ts b/web/boot.ts index 024516ce..1e3694db 100644 --- a/web/boot.ts +++ b/web/boot.ts @@ -14,8 +14,8 @@ safeRun(async () => { document.getElementById("sb-root")!, syncMode, ); - await client.init(); window.client = client; + await client.init(); }); if (navigator.serviceWorker) { diff --git a/web/components/mini_editor.tsx b/web/components/mini_editor.tsx index 9f629eee..1075fe1d 100644 --- a/web/components/mini_editor.tsx +++ b/web/components/mini_editor.tsx @@ -20,6 +20,7 @@ import { vim, vimGetCm, } from "../deps.ts"; +import { createCommandKeyBindings } from "../editor_state.ts"; type MiniEditorEvents = { onEnter: (newText: string, shiftDown?: boolean) => void; @@ -197,6 +198,7 @@ export function MiniEditor( ...standardKeymap, ...historyKeymap, ...completionKeymap, + ...createCommandKeyBindings(window.client), ]), EditorView.domEventHandlers({ click: (e) => { diff --git a/web/editor_state.ts b/web/editor_state.ts index 5edf8b94..ff03dea9 100644 --- a/web/editor_state.ts +++ b/web/editor_state.ts @@ -261,7 +261,7 @@ export function createEditorState( }); } -export function createKeyBindings(client: Client): Extension { +export function createCommandKeyBindings(client: Client): KeyBinding[] { const commandKeyBindings: KeyBinding[] = []; // Track which keyboard shortcuts for which commands we've overridden, so we can skip them later @@ -336,8 +336,13 @@ export function createKeyBindings(client: Client): Extension { }); } } + + return commandKeyBindings; +} + +export function createKeyBindings(client: Client): Extension { return keymap.of([ - ...commandKeyBindings, + ...createCommandKeyBindings(client), ...smartQuoteKeymap, ...closeBracketsKeymap, ...standardKeymap, @@ -345,16 +350,5 @@ export function createKeyBindings(client: Client): Extension { ...historyKeymap, ...completionKeymap, indentWithTab, - { - key: "Ctrl-.", - mac: "Cmd-.", - run: (): boolean => { - client.ui.viewDispatch({ - type: "show-palette", - context: client.getContext(), - }); - return true; - }, - }, ]); } diff --git a/web/editor_ui.tsx b/web/editor_ui.tsx index c5258574..f0e444d7 100644 --- a/web/editor_ui.tsx +++ b/web/editor_ui.tsx @@ -12,7 +12,6 @@ import { preactRender, RefreshCwIcon, runScopeHandlers, - TemplateIcon, TerminalIcon, useEffect, useReducer, @@ -30,10 +29,11 @@ export class MainUI { // Make keyboard shortcuts work even when the editor is in read only mode or not focused globalThis.addEventListener("keydown", (ev) => { if (!client.editorView.hasFocus) { - if ((ev.target as any).closest(".cm-editor")) { + if ((ev.target as any).closest(".cm-content")) { // In some cm element, let's back out return; } + console.log("Delegated keydown", ev, "to editor"); if (runScopeHandlers(client.editorView, ev, "editor")) { ev.preventDefault(); }