Add shortcut to toggle line wrapping in code cells (#2974)

This commit is contained in:
Jonatan Kłosko 2025-04-01 11:10:43 +02:00 committed by GitHub
parent ba95bf7e57
commit 79259a1c35
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 0 deletions

View file

@ -57,6 +57,7 @@ import { ancestorNode, closestNode } from "./live_editor/codemirror/tree_utils";
import { selectingClass } from "./live_editor/codemirror/selecting_class";
import { globalPubsub } from "../../lib/pubsub";
import { hoverDetails } from "./live_editor/codemirror/hover_details";
import { toggleWith } from "./live_editor/codemirror/toggle_with";
/**
* Mounts cell source editor with real-time collaboration mechanism.
@ -385,6 +386,7 @@ export default class LiveEditor {
settings.editor_mode === "vim" ? [vim()] : [],
settings.editor_mode === "emacs" ? [emacs()] : [],
this.languageCompartment.of(this.languageExtensions()),
toggleWith("Alt-z", EditorView.lineWrapping),
EditorView.domEventHandlers({
click: this.handleEditorClick.bind(this),
keydown: this.handleEditorKeydown.bind(this),

View file

@ -0,0 +1,22 @@
import { EditorView, keymap } from "@codemirror/view";
import { Compartment } from "@codemirror/state";
/**
* Returns an extension that toggles the given extension with the given
* keyboard shortcut.
*/
export function toggleWith(key, extension) {
const compartment = new Compartment();
function toggle(view) {
const isEnabled = compartment.get(view.state) === extension;
view.dispatch({
effects: compartment.reconfigure(isEnabled ? [] : extension),
});
return true;
}
return [compartment.of([]), keymap.of({ key, run: toggle })];
}