From 76ce77c5f995c59ceda16b320f32227b3f479e3a Mon Sep 17 00:00:00 2001 From: Raman Verma <59292838+maranix@users.noreply.github.com> Date: Thu, 23 Jan 2025 04:48:27 +0530 Subject: [PATCH] fix(editor): switch to normal mode on pressing Esc in vim keybindings (#3123) * editor(vim): switch to normal mode on pressing Esc * chore(editor): remove redundant import --- pkgs/dartpad_ui/lib/editor/codemirror.dart | 11 +++++++++++ pkgs/dartpad_ui/lib/editor/editor.dart | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/pkgs/dartpad_ui/lib/editor/codemirror.dart b/pkgs/dartpad_ui/lib/editor/codemirror.dart index b1587f93c..389b1c4fe 100644 --- a/pkgs/dartpad_ui/lib/editor/codemirror.dart +++ b/pkgs/dartpad_ui/lib/editor/codemirror.dart @@ -22,6 +22,8 @@ extension type CodeMirror._(JSObject _) implements JSObject { external static Commands commands; external static String get version; external static Hint hint; + @JS('Vim') + external static Vim vim; external static void registerHelper( String type, @@ -174,3 +176,12 @@ extension type HintOptions._(JSObject _) implements JSObject { extension type Hint._(JSObject _) implements JSObject { external JSAny dart; } + +extension type Vim._(JSObject _) implements JSObject { + external void exitInsertMode(CodeMirror cm); + + void handleEsc(CodeMirror cm) => switch (cm.getKeymap()) { + 'vim-insert' => exitInsertMode(cm), + _ => _, + }; +} diff --git a/pkgs/dartpad_ui/lib/editor/editor.dart b/pkgs/dartpad_ui/lib/editor/editor.dart index 8af6cfee3..fee9e9a9b 100644 --- a/pkgs/dartpad_ui/lib/editor/editor.dart +++ b/pkgs/dartpad_ui/lib/editor/editor.dart @@ -126,6 +126,14 @@ class _EditorWidgetState extends State implements EditorService { return KeyEventResult.skipRemainingHandlers; } + if (event.logicalKey == LogicalKeyboardKey.escape) { + if (codeMirror == null) { + return KeyEventResult.ignored; + } + + CodeMirror.vim.handleEsc(codeMirror!); + } + return KeyEventResult.ignored; }, );