mirror of
https://github.com/outline/outline.git
synced 2026-11-21 13:34:02 +03:00
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.
This commit is contained in:
1 parent
30b8e681cf
commit
143da3ff87
5 files changed
+179
-17
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user