Skip to content

Commit

Permalink
fix(editor): switch to normal mode on pressing Esc in vim keybindings (
Browse files Browse the repository at this point in the history
…#3123)

* editor(vim): switch to normal mode on pressing Esc

* chore(editor): remove redundant import
  • Loading branch information
maranix authored Jan 22, 2025
1 parent 808d407 commit 76ce77c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkgs/dartpad_ui/lib/editor/codemirror.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
_ => _,
};
}
8 changes: 8 additions & 0 deletions pkgs/dartpad_ui/lib/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ class _EditorWidgetState extends State<EditorWidget> implements EditorService {
return KeyEventResult.skipRemainingHandlers;
}

if (event.logicalKey == LogicalKeyboardKey.escape) {
if (codeMirror == null) {
return KeyEventResult.ignored;
}

CodeMirror.vim.handleEsc(codeMirror!);
}

return KeyEventResult.ignored;
},
);
Expand Down

0 comments on commit 76ce77c

Please sign in to comment.