fix: Creates a gap cursor position between tables positioned next to each other (#8599)

This commit is contained in:
Tom Moor authored and GitHub committed 2025-03-01 13:11:42 -08:00
1 parent 3b1ce063bf
commit b34dd138cd
2 files changed
+47

No files matched your search

+44
View File
@@ -1,3 +1,4 @@
import { GapCursor } from "prosemirror-gapcursor";
import { Node, NodeType } from "prosemirror-model";
import { Command, EditorState, TextSelection } from "prosemirror-state";
import {
@@ -499,3 +500,46 @@ export function selectTable(): Command {
return false;
};
}
export function moveOutOfTable(direction: 1 | -1): Command {
return (state, dispatch): boolean => {
if (dispatch) {
if (state.selection instanceof GapCursor) {
return false;
}
if (!isInTable(state)) {
return false;
}
// check if current cursor position is at the top or bottom of the table
const rect = selectedRect(state);
const topOfTable =
rect.top === 0 && rect.bottom === 1 && direction === -1;
const bottomOfTable =
rect.top === rect.map.height - 1 &&
rect.bottom === rect.map.height &&
direction === 1;
if (!topOfTable && !bottomOfTable) {
return false;
}
const map = rect.map.map;
const $start = state.doc.resolve(rect.tableStart + map[0] - 1);
const $end = state.doc.resolve(rect.tableStart + map[map.length - 1] + 2);
// @ts-expect-error findGapCursorFrom is a ProseMirror internal method.
const $found = GapCursor.findGapCursorFrom(
direction > 0 ? $end : $start,
direction,
true
);
if ($found) {
dispatch(state.tr.setSelection(new GapCursor($found)));
return true;
}
}
return false;
};
}
+3
View File
@@ -22,6 +22,7 @@ import {
setTableAttr,
deleteColSelection,
deleteRowSelection,
moveOutOfTable,
} from "../commands/table";
import { MarkdownSerializerState } from "../lib/markdown/serializer";
import { FixTablesPlugin } from "../plugins/FixTables";
@@ -95,6 +96,8 @@ export default class Table extends Node {
deleteColSelection(),
deleteRowSelection()
),
ArrowDown: moveOutOfTable(1),
ArrowUp: moveOutOfTable(-1),
};
}