Skip to content

Commit

Permalink
fix(code-blocks): remove buggy "exit code block" functionality (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwarren authored Feb 28, 2025
1 parent 8de12d2 commit 2ace7a9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/rich-text/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ export function exitInclusiveMarkCommand(
dispatch: (tr: Transaction) => void
) {
const $cursor = (<TextSelection>state.selection).$cursor;
const marks = state.storedMarks || $cursor.marks();
const marks = state.storedMarks || $cursor?.marks();

if (!marks?.length) {
return false;
Expand All @@ -546,7 +546,7 @@ export function exitInclusiveMarkCommand(
}

// check if we're at the end of the exitable mark
const nextNode = $cursor.nodeAfter;
const nextNode = $cursor?.nodeAfter;
let endExitables: Mark[];

let tr = state.tr;
Expand Down
6 changes: 2 additions & 4 deletions src/rich-text/key-bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
toggleMark,
wrapIn,
setBlockType,
exitCode,
baseKeymap,
} from "prosemirror-commands";
import { redo, undo } from "prosemirror-history";
Expand All @@ -26,12 +25,12 @@ import {
moveToPreviousCellCommand,
moveSelectionAfterTableCommand,
insertRichTextTableCommand,
exitInclusiveMarkCommand,
indentCodeBlockLinesCommand,
unindentCodeBlockLinesCommand,
toggleHeadingLevel,
toggleTagLinkCommand,
toggleList,
exitInclusiveMarkCommand,
} from "./commands";

export function allKeymaps(
Expand Down Expand Up @@ -86,9 +85,8 @@ export function allKeymaps(
"Mod-,": toggleMark(schema.marks.sub),
"Mod-.": toggleMark(schema.marks.sup),
"Mod-'": toggleMark(schema.marks.kbd),
// users expect to be able to leave certain blocks/marks using the arrow keys
// exit inline code block using the right arrow key
"ArrowRight": exitInclusiveMarkCommand,
"ArrowDown": exitCode,
});

const keymaps = [
Expand Down
21 changes: 19 additions & 2 deletions test/rich-text/commands/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EditorState, Transaction } from "prosemirror-state";
import { EditorState, TextSelection, Transaction } from "prosemirror-state";
import {
exitInclusiveMarkCommand,
insertRichTextHorizontalRuleCommand,
Expand Down Expand Up @@ -733,7 +733,7 @@ describe("commands", () => {
);
});

describe("exitMarkCommand", () => {
describe("exitInclusiveMarkCommand", () => {
it("all exitable marks should also be inclusive: true", () => {
Object.keys(testRichTextSchema.marks).forEach((markName) => {
const mark = testRichTextSchema.marks[markName];
Expand Down Expand Up @@ -783,6 +783,23 @@ describe("commands", () => {
expect(exitInclusiveMarkCommand(state, null)).toBe(true);
}
);

it("should handle the case when $cursor is null", () => {
// suppress console.warn
const consoleWarnSpy = jest
.spyOn(console, "warn")
.mockImplementation(() => {});

let state = createState("this is my state", []);
state = state.apply(
state.tr.setSelection(TextSelection.create(state.doc, 0, null))
);
expect((<TextSelection>state.selection).$cursor).toBeNull();
expect(() => exitInclusiveMarkCommand(state, null)).not.toThrow();

// restore console.warn
consoleWarnSpy.mockRestore();
});
});

describe("indentCodeBlockLinesCommand", () => {
Expand Down

0 comments on commit 2ace7a9

Please sign in to comment.