From b34dd138cdac26c3a8dec072228fdde14ef718db Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 1 Mar 2025 16:11:42 -0500 Subject: [PATCH] fix: Creates a gap cursor position between tables positioned next to each other (#8599) --- shared/editor/commands/table.ts | 44 +++++++++++++++++++++++++++++++++ shared/editor/nodes/Table.ts | 3 +++ 2 files changed, 47 insertions(+) diff --git a/shared/editor/commands/table.ts b/shared/editor/commands/table.ts index 3ed236a11c..d0b4d7e302 100644 --- a/shared/editor/commands/table.ts +++ b/shared/editor/commands/table.ts @@ -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; + }; +} diff --git a/shared/editor/nodes/Table.ts b/shared/editor/nodes/Table.ts index 94b463f9a8..03ff223d50 100644 --- a/shared/editor/nodes/Table.ts +++ b/shared/editor/nodes/Table.ts @@ -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), }; }