Skip to content

Commit 456f60e

Browse files
authored
fix(document-api): fix cell shading in document api (#2215)
* fix(document-api): fix cell shading in document api * fix(document-api): fix auto color in setShading for tables
1 parent ec31699 commit 456f60e

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

packages/super-editor/src/document-api-adapters/tables-adapter.regressions.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,56 @@ describe('tables-adapter regressions', () => {
570570
});
571571
});
572572

573+
it('applies table shading to all cells when target is a table', () => {
574+
const editor = makeTableEditor();
575+
const tr = editor.state.tr as unknown as { setNodeMarkup: ReturnType<typeof vi.fn> };
576+
577+
const result = tablesSetShadingAdapter(editor, {
578+
nodeId: 'table-1',
579+
color: 'FFFF00',
580+
});
581+
582+
expect(result.success).toBe(true);
583+
584+
const cellUpdates = tr.setNodeMarkup.mock.calls.filter(
585+
(call) =>
586+
typeof call[2] === 'object' &&
587+
call[2] != null &&
588+
(call[2] as { tableCellProperties?: { shading?: { fill?: string } } }).tableCellProperties?.shading?.fill ===
589+
'FFFF00',
590+
);
591+
592+
expect(cellUpdates).toHaveLength(4);
593+
for (const call of cellUpdates) {
594+
expect((call[2] as { background?: { color?: string } }).background).toEqual({ color: 'FFFF00' });
595+
}
596+
});
597+
598+
it('does not write cell background when table shading color is auto', () => {
599+
const editor = makeTableEditor();
600+
const tr = editor.state.tr as unknown as { setNodeMarkup: ReturnType<typeof vi.fn> };
601+
602+
const result = tablesSetShadingAdapter(editor, {
603+
nodeId: 'table-1',
604+
color: 'auto',
605+
});
606+
607+
expect(result.success).toBe(true);
608+
609+
const cellUpdates = tr.setNodeMarkup.mock.calls.filter(
610+
(call) =>
611+
typeof call[2] === 'object' &&
612+
call[2] != null &&
613+
(call[2] as { tableCellProperties?: { shading?: { fill?: string } } }).tableCellProperties?.shading?.fill ===
614+
'auto',
615+
);
616+
617+
expect(cellUpdates).toHaveLength(4);
618+
for (const call of cellUpdates) {
619+
expect((call[2] as { background?: unknown }).background).toBeUndefined();
620+
}
621+
});
622+
573623
it.each([
574624
{
575625
name: 'tables.setBorder',

packages/super-editor/src/document-api-adapters/tables-adapter.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,6 +2669,38 @@ export function tablesSetShadingAdapter(
26692669
currentProps.shading = { fill: input.color, val: 'clear', color: 'auto' };
26702670
const syncAttrs = resolved.scope === 'table' ? syncExtractedTableAttrs(currentProps) : {};
26712671
tr.setNodeMarkup(resolved.pos, null, { ...currentAttrs, [propsKey]: currentProps, ...syncAttrs });
2672+
2673+
if (resolved.scope === 'table') {
2674+
const tableNode = resolved.node;
2675+
const tableStart = resolved.pos + 1;
2676+
const map = TableMap.get(tableNode);
2677+
const seen = new Set<number>();
2678+
const mapFrom = tr.mapping.maps.length;
2679+
2680+
for (let i = 0; i < map.map.length; i++) {
2681+
const relPos = map.map[i]!;
2682+
if (seen.has(relPos)) continue;
2683+
seen.add(relPos);
2684+
2685+
const cellNode = tableNode.nodeAt(relPos);
2686+
if (!cellNode) continue;
2687+
2688+
const cellAttrs = cellNode.attrs as Record<string, unknown>;
2689+
const cellProps = { ...((cellAttrs.tableCellProperties ?? {}) as Record<string, unknown>) };
2690+
cellProps.shading = { fill: input.color, val: 'clear', color: 'auto' };
2691+
2692+
const nextCellAttrs: Record<string, unknown> = {
2693+
...cellAttrs,
2694+
tableCellProperties: cellProps,
2695+
};
2696+
2697+
if (input.color === 'auto') delete nextCellAttrs.background;
2698+
else nextCellAttrs.background = { color: input.color };
2699+
2700+
tr.setNodeMarkup(tr.mapping.slice(mapFrom).map(tableStart + relPos), null, nextCellAttrs);
2701+
}
2702+
}
2703+
26722704
applyDirectMutationMeta(tr);
26732705
editor.dispatch(tr);
26742706
clearIndexCache(editor);

0 commit comments

Comments
 (0)