Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { ColumnDragHandle, ColumnDragHandleProps } from "./drag-handle";

type TableColumnDragHandlePluginState = {
decorations?: DecorationSet;
// track table structure to detect changes
tableWidth?: number;
tableNodePos?: number;
};

const TABLE_COLUMN_DRAG_HANDLE_PLUGIN_KEY = new PluginKey("tableColumnHandlerDecorationPlugin");
Expand All @@ -31,20 +34,33 @@ export const TableColumnDragHandlePlugin = (editor: Editor): Plugin<TableColumnD

const tableMap = TableMap.get(table.node);

let isStale = false;
const mapped = prev.decorations?.map(tr.mapping, tr.doc);
for (let col = 0; col < tableMap.width; col++) {
const pos = getTableCellWidgetDecorationPos(table, tableMap, col);
if (mapped?.find(pos, pos + 1)?.length !== 1) {
isStale = true;
break;
// Check if table structure changed (width or position)
const tableStructureChanged = prev.tableWidth !== tableMap.width || prev.tableNodePos !== table.pos;

let isStale = tableStructureChanged;

// Only do position-based stale check if structure hasn't changed
if (!isStale) {
const mapped = prev.decorations?.map(tr.mapping, tr.doc);
for (let col = 0; col < tableMap.width; col++) {
const pos = getTableCellWidgetDecorationPos(table, tableMap, col);
if (mapped?.find(pos, pos + 1)?.length !== 1) {
isStale = true;
break;
}
}
}

if (!isStale) {
return { decorations: mapped };
const mapped = prev.decorations?.map(tr.mapping, tr.doc);
return {
decorations: mapped,
tableWidth: tableMap.width,
tableNodePos: table.pos,
};
}

// recreate all decorations
const decorations: Decoration[] = [];

for (let col = 0; col < tableMap.width; col++) {
Expand All @@ -63,6 +79,8 @@ export const TableColumnDragHandlePlugin = (editor: Editor): Plugin<TableColumnD

return {
decorations: DecorationSet.create(newState.doc, decorations),
tableWidth: tableMap.width,
tableNodePos: table.pos,
};
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { RowDragHandle, RowDragHandleProps } from "./drag-handle";

type TableRowDragHandlePluginState = {
decorations?: DecorationSet;
// track table structure to detect changes
tableHeight?: number;
tableNodePos?: number;
};

const TABLE_ROW_DRAG_HANDLE_PLUGIN_KEY = new PluginKey("tableRowDragHandlePlugin");
Expand All @@ -31,20 +34,33 @@ export const TableRowDragHandlePlugin = (editor: Editor): Plugin<TableRowDragHan

const tableMap = TableMap.get(table.node);

let isStale = false;
const mapped = prev.decorations?.map(tr.mapping, tr.doc);
for (let row = 0; row < tableMap.height; row++) {
const pos = getTableCellWidgetDecorationPos(table, tableMap, row * tableMap.width);
if (mapped?.find(pos, pos + 1)?.length !== 1) {
isStale = true;
break;
// Check if table structure changed (height or position)
const tableStructureChanged = prev.tableHeight !== tableMap.height || prev.tableNodePos !== table.pos;

let isStale = tableStructureChanged;

// Only do position-based stale check if structure hasn't changed
if (!isStale) {
const mapped = prev.decorations?.map(tr.mapping, tr.doc);
for (let row = 0; row < tableMap.height; row++) {
const pos = getTableCellWidgetDecorationPos(table, tableMap, row * tableMap.width);
if (mapped?.find(pos, pos + 1)?.length !== 1) {
isStale = true;
break;
}
}
}

if (!isStale) {
return { decorations: mapped };
const mapped = prev.decorations?.map(tr.mapping, tr.doc);
return {
decorations: mapped,
tableHeight: tableMap.height,
tableNodePos: table.pos,
};
}

// recreate all decorations
const decorations: Decoration[] = [];

for (let row = 0; row < tableMap.height; row++) {
Expand All @@ -61,7 +77,11 @@ export const TableRowDragHandlePlugin = (editor: Editor): Plugin<TableRowDragHan
decorations.push(Decoration.widget(pos, () => dragHandleComponent.element));
}

return { decorations: DecorationSet.create(newState.doc, decorations) };
return {
decorations: DecorationSet.create(newState.doc, decorations),
tableHeight: tableMap.height,
tableNodePos: table.pos,
};
},
},
props: {
Expand Down
4 changes: 3 additions & 1 deletion packages/editor/src/core/plugins/drag-handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,9 @@ const handleNodeSelection = (
let draggedNodePos = nodePosAtDOM(node, view, options);
if (draggedNodePos == null || draggedNodePos < 0) return;

if (node.matches("blockquote")) {
if (node.matches("table")) {
draggedNodePos = draggedNodePos - 2;
} else if (node.matches("blockquote")) {
draggedNodePos = nodePosAtDOMForBlockQuotes(node, view);
if (draggedNodePos === null || draggedNodePos === undefined) return;
} else {
Expand Down
Loading