From 143da3ff87fcdbf73e75892fdad2f89c3c2db320 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 16 Sep 2026 21:45:20 -0400 Subject: [PATCH] fix: Address review follow-ups for mapped editor decorations (#13762) Treat checkbox list attribute changes as structural for id repair, keep remote transactions on the rebuild path for table decorations, and map column grip decorations through local inline edits. --- shared/editor/nodes/CheckboxList.ts | 4 +- shared/editor/nodes/TableCell.ts | 13 +++- shared/editor/nodes/TableHeader.ts | 47 +++++++++++---- shared/editor/queries/table.test.ts | 94 +++++++++++++++++++++++++++++ shared/editor/queries/table.ts | 38 +++++++++++- 5 files changed, 179 insertions(+), 17 deletions(-) create mode 100644 shared/editor/queries/table.test.ts diff --git a/shared/editor/nodes/CheckboxList.ts b/shared/editor/nodes/CheckboxList.ts index 36fafe9bbe..20c5f737bb 100644 --- a/shared/editor/nodes/CheckboxList.ts +++ b/shared/editor/nodes/CheckboxList.ts @@ -59,7 +59,9 @@ export default class CheckboxList extends Node { // only structural edits can introduce a list without an id. const isFirstChange = !assignIdsPluginKey.getState(oldSt); const hasStructuralChange = txs.some( - (t) => t.docChanged && !isInlineTransaction(t) + (t) => + t.docChanged && + !isInlineTransaction(t, (node) => node.type.name === this.name) ); if (!isFirstChange && !hasStructuralChange) { return null; diff --git a/shared/editor/nodes/TableCell.ts b/shared/editor/nodes/TableCell.ts index a73f7d829c..1036f52c12 100644 --- a/shared/editor/nodes/TableCell.ts +++ b/shared/editor/nodes/TableCell.ts @@ -8,6 +8,7 @@ import type { EditorState } from "prosemirror-state"; import { Plugin, PluginKey } from "prosemirror-state"; import { Decoration, DecorationSet } from "prosemirror-view"; import { TableMap } from "prosemirror-tables"; +import { isRemoteTransaction } from "../lib/multiplayer"; import { isInlineTransaction } from "../queries/isInlineTransaction"; import { getCellAttrs, @@ -20,6 +21,8 @@ import { presetColors, rgbaToHex } from "@shared/utils/color"; import { parseToRgb, transparentize } from "polished"; import type { RgbaColor } from "polished/lib/types/color"; +const isTableNode = (node: ProsemirrorNode) => !!node.type.spec.tableRole; + export default class TableCell extends Node { /** The default opacity of the table cell background */ static opacity = 0.7; @@ -151,9 +154,13 @@ export default class TableCell extends Node { return pluginState; } - // Inline edits cannot change the table layout, so mapping the - // existing decorations is enough. - if (isInlineTransaction(tr, (node) => !!node.type.spec.tableRole)) { + // Local inline edits cannot change the table layout, so mapping + // the existing decorations is enough. Remote transactions do not + // reflect the shape of the change, so they always rebuild. + if ( + !isRemoteTransaction(tr, newState) && + isInlineTransaction(tr, isTableNode) + ) { return pluginState.map(tr.mapping, tr.doc); } diff --git a/shared/editor/nodes/TableHeader.ts b/shared/editor/nodes/TableHeader.ts index bcf2b6d888..d09d56a210 100644 --- a/shared/editor/nodes/TableHeader.ts +++ b/shared/editor/nodes/TableHeader.ts @@ -1,5 +1,5 @@ import type Token from "markdown-it/lib/token.mjs"; -import type { NodeSpec } from "prosemirror-model"; +import type { Node as ProsemirrorNode, NodeSpec } from "prosemirror-model"; import type { EditorState } from "prosemirror-state"; import { Plugin, PluginKey } from "prosemirror-state"; import type { EditorView } from "prosemirror-view"; @@ -7,6 +7,7 @@ import { DecorationSet, Decoration } from "prosemirror-view"; import { isInTable, moveTableColumn, TableMap } from "prosemirror-tables"; import { addColumnBefore, selectColumn } from "../commands/table"; import { isMobile } from "../../utils/browser"; +import { isRemoteTransaction } from "../lib/multiplayer"; import { getCellAttrs, isValidCellAlignment, @@ -17,6 +18,7 @@ import { isInlineTransaction } from "../queries/isInlineTransaction"; import { getCellsInColumn, getCellsInRow, + hasTableSelectionChanged, isColumnSelected, isTableSelected, } from "../queries/table"; @@ -208,6 +210,8 @@ function createColumnDragDecorations(state: EditorState): DecorationSet { return DecorationSet.create(state.doc, decorations); } +const isTableNode = (node: ProsemirrorNode) => !!node.type.spec.tableRole; + export default class TableHeader extends Node { get name() { return "th"; @@ -408,9 +412,13 @@ export default class TableHeader extends Node { return pluginState; } - // Inline edits cannot change the table layout, so mapping the - // existing decorations is enough. - if (isInlineTransaction(tr, (node) => !!node.type.spec.tableRole)) { + // Local inline edits cannot change the table layout, so mapping + // the existing decorations is enough. Remote transactions do not + // reflect the shape of the change, so they always rebuild. + if ( + !isRemoteTransaction(tr, newState) && + isInlineTransaction(tr, isTableNode) + ) { return pluginState.map(tr.mapping, tr.doc); } @@ -428,17 +436,32 @@ export default class TableHeader extends Node { state: { init: (_, state) => createColumnDecorations(state), apply: (tr, pluginState, oldState, newState) => { - // Recompute if selection, document, or drag state changed - if ( - !tr.selectionSet && - !tr.docChanged && - !tr.getMeta(columnDragPluginKey) && - !tr.getMeta(rowDragPluginKey) - ) { + const hasDragChanged = + !!tr.getMeta(columnDragPluginKey) || + !!tr.getMeta(rowDragPluginKey); + if (!tr.selectionSet && !tr.docChanged && !hasDragChanged) { return pluginState; } - return createColumnDecorations(newState); + // Rebuild for drag state, selection changes that affect the grips, + // and any change that may alter the table layout. The set is also + // empty when built before the view existed, so build it once the + // selection is in a table. + if ( + hasDragChanged || + hasTableSelectionChanged(tr, oldState, newState) || + (pluginState === DecorationSet.empty && isInTable(newState)) || + (tr.docChanged && + (isRemoteTransaction(tr, newState) || + !isInlineTransaction(tr, isTableNode))) + ) { + return createColumnDecorations(newState); + } + + // Local inline edits only shift positions, so mapping is enough. + return tr.docChanged + ? pluginState.map(tr.mapping, tr.doc) + : pluginState; }, }, props: { diff --git a/shared/editor/queries/table.test.ts b/shared/editor/queries/table.test.ts new file mode 100644 index 0000000000..f6ce89956e --- /dev/null +++ b/shared/editor/queries/table.test.ts @@ -0,0 +1,94 @@ +import { TextSelection } from "prosemirror-state"; +import { CellSelection } from "prosemirror-tables"; +import { + createEditorStateWithSelection, + doc, + p, + table, + td, + tr, +} from "@shared/test/editor"; +import { hasTableSelectionChanged } from "./table"; + +/** + * Document layout: + * + * 0 table + * 1 tr + * 2 td (content starts at 3, paragraph text at 4) + * 9 td (content starts at 10, paragraph text at 11) + * 18 paragraph "between" (text at 19) + * 27 table + * 28 tr + * 29 td (paragraph text at 31) + */ +function state(pos: number) { + return createEditorStateWithSelection( + doc([ + table([tr([td("one"), td("two")])]), + p("between"), + table([tr([td("three")])]), + ]), + pos + ); +} + +describe("hasTableSelectionChanged", () => { + it("is false when the selection is not set", () => { + const s = state(4); + const t = s.tr.insertText("x", 5); + expect(hasTableSelectionChanged(t, s, s.apply(t))).toBe(false); + }); + + it("is false when the cursor moves within one table", () => { + const s = state(4); + const t = s.tr.setSelection(TextSelection.create(s.doc, 12)); + expect(hasTableSelectionChanged(t, s, s.apply(t))).toBe(false); + }); + + it("is false when typing keeps the cursor in the same table", () => { + const s = state(4); + const t = s.tr.insertText("x", 4); + t.setSelection(TextSelection.create(t.doc, 5)); + expect(hasTableSelectionChanged(t, s, s.apply(t))).toBe(false); + }); + + it("is false when the cursor moves outside of any table", () => { + const s = state(19); + const t = s.tr.setSelection(TextSelection.create(s.doc, 22)); + expect(hasTableSelectionChanged(t, s, s.apply(t))).toBe(false); + }); + + it("is true when the cursor moves out of a table", () => { + const s = state(4); + const t = s.tr.setSelection(TextSelection.create(s.doc, 19)); + expect(hasTableSelectionChanged(t, s, s.apply(t))).toBe(true); + }); + + it("is true when the cursor moves into a table", () => { + const s = state(19); + const t = s.tr.setSelection(TextSelection.create(s.doc, 4)); + expect(hasTableSelectionChanged(t, s, s.apply(t))).toBe(true); + }); + + it("is true when the cursor moves between tables", () => { + const s = state(4); + const t = s.tr.setSelection(TextSelection.create(s.doc, 31)); + expect(hasTableSelectionChanged(t, s, s.apply(t))).toBe(true); + }); + + it("is true when cells become selected", () => { + const s = state(4); + const t = s.tr.setSelection(CellSelection.create(s.doc, 2, 9)); + expect(hasTableSelectionChanged(t, s, s.apply(t))).toBe(true); + }); + + it("is true when a cell selection is dropped", () => { + const s = state(4); + const selected = s.apply( + s.tr.setSelection(CellSelection.create(s.doc, 2, 9)) + ); + const t = selected.tr.setSelection(TextSelection.create(selected.doc, 4)); + expect(hasTableSelectionChanged(t, selected, selected.apply(t))).toBe(true); + }); +}); diff --git a/shared/editor/queries/table.ts b/shared/editor/queries/table.ts index 00519ca2ae..5c912e7f23 100644 --- a/shared/editor/queries/table.ts +++ b/shared/editor/queries/table.ts @@ -1,6 +1,7 @@ -import type { EditorState } from "prosemirror-state"; +import type { EditorState, Transaction } from "prosemirror-state"; import type { TableRect } from "prosemirror-tables"; import { CellSelection, isInTable, selectedRect } from "prosemirror-tables"; +import { findParentNodeClosestToPos } from "./findParentNode"; import { ColumnSelection } from "../selection/ColumnSelection"; import { RowSelection } from "../selection/RowSelection"; import type { EditorView } from "prosemirror-view"; @@ -659,3 +660,38 @@ export const hasNodeAttrMarkWithAttrsCellSelection = ( return attrsMatch; }; + +/** + * Checks whether the selection change in a transaction can alter table + * decorations that depend on the selection: a cell selection on either side, + * or the selection moving into, out of, or between tables. Moving the cursor + * within one table does not. + * + * @param tr The transaction. + * @param oldState The editor state before the transaction. + * @param newState The editor state after the transaction. + * @returns true if the selected table or the cell selection changed. + */ +export function hasTableSelectionChanged( + tr: Transaction, + oldState: EditorState, + newState: EditorState +): boolean { + if (!tr.selectionSet) { + return false; + } + if ( + oldState.selection instanceof CellSelection || + newState.selection instanceof CellSelection + ) { + return true; + } + + const isTable = (node: Node) => node.type.spec.tableRole === "table"; + const before = findParentNodeClosestToPos(oldState.selection.$head, isTable); + const after = findParentNodeClosestToPos(newState.selection.$head, isTable); + if (!before || !after) { + return !!before !== !!after; + } + return tr.mapping.map(before.pos) !== after.pos; +}