Skip to content

Commit 2ace7a9

Browse files
authored
fix(code-blocks): remove buggy "exit code block" functionality (#383)
1 parent 8de12d2 commit 2ace7a9

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/rich-text/commands/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ export function exitInclusiveMarkCommand(
532532
dispatch: (tr: Transaction) => void
533533
) {
534534
const $cursor = (<TextSelection>state.selection).$cursor;
535-
const marks = state.storedMarks || $cursor.marks();
535+
const marks = state.storedMarks || $cursor?.marks();
536536

537537
if (!marks?.length) {
538538
return false;
@@ -546,7 +546,7 @@ export function exitInclusiveMarkCommand(
546546
}
547547

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

552552
let tr = state.tr;

src/rich-text/key-bindings.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
toggleMark,
33
wrapIn,
44
setBlockType,
5-
exitCode,
65
baseKeymap,
76
} from "prosemirror-commands";
87
import { redo, undo } from "prosemirror-history";
@@ -26,12 +25,12 @@ import {
2625
moveToPreviousCellCommand,
2726
moveSelectionAfterTableCommand,
2827
insertRichTextTableCommand,
29-
exitInclusiveMarkCommand,
3028
indentCodeBlockLinesCommand,
3129
unindentCodeBlockLinesCommand,
3230
toggleHeadingLevel,
3331
toggleTagLinkCommand,
3432
toggleList,
33+
exitInclusiveMarkCommand,
3534
} from "./commands";
3635

3736
export function allKeymaps(
@@ -86,9 +85,8 @@ export function allKeymaps(
8685
"Mod-,": toggleMark(schema.marks.sub),
8786
"Mod-.": toggleMark(schema.marks.sup),
8887
"Mod-'": toggleMark(schema.marks.kbd),
89-
// users expect to be able to leave certain blocks/marks using the arrow keys
88+
// exit inline code block using the right arrow key
9089
"ArrowRight": exitInclusiveMarkCommand,
91-
"ArrowDown": exitCode,
9290
});
9391

9492
const keymaps = [

test/rich-text/commands/index.test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EditorState, Transaction } from "prosemirror-state";
1+
import { EditorState, TextSelection, Transaction } from "prosemirror-state";
22
import {
33
exitInclusiveMarkCommand,
44
insertRichTextHorizontalRuleCommand,
@@ -733,7 +733,7 @@ describe("commands", () => {
733733
);
734734
});
735735

736-
describe("exitMarkCommand", () => {
736+
describe("exitInclusiveMarkCommand", () => {
737737
it("all exitable marks should also be inclusive: true", () => {
738738
Object.keys(testRichTextSchema.marks).forEach((markName) => {
739739
const mark = testRichTextSchema.marks[markName];
@@ -783,6 +783,23 @@ describe("commands", () => {
783783
expect(exitInclusiveMarkCommand(state, null)).toBe(true);
784784
}
785785
);
786+
787+
it("should handle the case when $cursor is null", () => {
788+
// suppress console.warn
789+
const consoleWarnSpy = jest
790+
.spyOn(console, "warn")
791+
.mockImplementation(() => {});
792+
793+
let state = createState("this is my state", []);
794+
state = state.apply(
795+
state.tr.setSelection(TextSelection.create(state.doc, 0, null))
796+
);
797+
expect((<TextSelection>state.selection).$cursor).toBeNull();
798+
expect(() => exitInclusiveMarkCommand(state, null)).not.toThrow();
799+
800+
// restore console.warn
801+
consoleWarnSpy.mockRestore();
802+
});
786803
});
787804

788805
describe("indentCodeBlockLinesCommand", () => {

0 commit comments

Comments
 (0)