From b98e9c7d005d2073535b9a51067b6a85a9854077 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Tue, 4 Jun 2024 16:28:08 +0200 Subject: [PATCH] Add toggleTabFocusMode and temporarilySetTabFocusMode commands FEATURE: The new `toggleTabFocusMode` and `temporarilySetTabFocusMode` commands provide control over the view's tab-focus mode. FEATURE: The default keymap now binds Ctrl-m (Shift-Alt-m on macOS) to `toggleTabFocusMode`. --- package.json | 2 +- src/README.md | 6 ++++++ src/commands.ts | 23 ++++++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index d38a700..0a65513 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "dependencies": { "@codemirror/language": "^6.0.0", "@codemirror/state": "^6.4.0", - "@codemirror/view": "^6.0.0", + "@codemirror/view": "^6.27.0", "@lezer/common": "^1.1.0" }, "devDependencies": { diff --git a/src/README.md b/src/README.md index 8164f52..32f54e0 100644 --- a/src/README.md +++ b/src/README.md @@ -234,3 +234,9 @@ with key bindings for a lot of them. @blockUncomment @toggleBlockCommentByLine + +### Tab Focus Mode + +@toggleTabFocusMode + +@temporarilySetTabFocusMode diff --git a/src/commands.ts b/src/commands.ts index 0d138db..c14c1dd 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -825,6 +825,24 @@ export const indentLess: StateCommand = ({state, dispatch}) => { return true } +/// Enables or disables +/// [tab-focus mode](#view.EditorView.setTabFocusMode). While on, this +/// prevents the editor's key bindings from capturing Tab or +/// Shift-Tab, making it possible for the user to move focus out of +/// the editor with the keyboard. +export const toggleTabFocusMode: Command = view => { + view.setTabFocusMode() + return true +} + +/// Temporarily enables [tab-focus +/// mode](#view.EditorView.setTabFocusMode) for two seconds or until +/// another key is pressed. +export const temporarilySetTabFocusMode: Command = view => { + view.setTabFocusMode(2000) + return true +} + /// Insert a tab character at the cursor or, if something is selected, /// use [`indentMore`](#commands.indentMore) to indent the entire /// selection. @@ -961,6 +979,7 @@ export const standardKeymap: readonly KeyBinding[] = ([ /// - Shift-Ctrl-\\ (Shift-Cmd-\\ on macOS): [`cursorMatchingBracket`](#commands.cursorMatchingBracket) /// - Ctrl-/ (Cmd-/ on macOS): [`toggleComment`](#commands.toggleComment). /// - Shift-Alt-a: [`toggleBlockComment`](#commands.toggleBlockComment). +/// - Ctrl-m (Alt-Shift-m on macOS): [`toggleTabFocusMode`](#commands.toggleTabFocusMode). export const defaultKeymap: readonly KeyBinding[] = ([ {key: "Alt-ArrowLeft", mac: "Ctrl-ArrowLeft", run: cursorSyntaxLeft, shift: selectSyntaxLeft}, {key: "Alt-ArrowRight", mac: "Ctrl-ArrowRight", run: cursorSyntaxRight, shift: selectSyntaxRight}, @@ -986,7 +1005,9 @@ export const defaultKeymap: readonly KeyBinding[] = ([ {key: "Shift-Mod-\\", run: cursorMatchingBracket}, {key: "Mod-/", run: toggleComment}, - {key: "Alt-A", run: toggleBlockComment} + {key: "Alt-A", run: toggleBlockComment}, + + {key: "Ctrl-m", mac: "Shift-Alt-m", run: toggleTabFocusMode}, ] as readonly KeyBinding[]).concat(standardKeymap) /// A binding that binds Tab to [`indentMore`](#commands.indentMore) and