/** * The merges capability: workbook-owned merge rectangles per sheet, read by * every view and by export. Opt-in; `new Workbook()` includes it by default. * The kernel owns the backing map (and clears it on `removeSheet`); this * capability contributes the public read/write surface. */ import { colOf, pack, rectContains, rectsIntersect, rowOf } from '../addr' import type { WorkbookCapability, WorkbookInternal } from '../capability' import type { CellKey, CommitResult, Rect, SheetId } from '../types' declare module '../capability' { interface WorkbookPlugins { mergesCapability: WorkbookCapability } interface Workbook_FeatureMap { mergesCapability: Workbook_Merges } } /** The method surface the merges capability contributes to a `Workbook`. */ export interface Workbook_Merges { /** * Merge the cells of `rect`, replacing any existing merges that overlap it. * Merge state is workbook-owned, so every view and the exporter see the same * merges. Bumps the revision and notifies regions covering the merge. */ mergeCells: (sheet: SheetId, rect: Rect) => CommitResult /** Clear every merge that intersects `rect`. */ unmergeCells: (sheet: SheetId, rect: Rect) => CommitResult /** A copy of every merge rectangle on `sheet` (never a live internal array). */ getMerges: (sheet: SheetId) => Array /** The merge covering `(row, col)` on `sheet`, or `undefined`. */ getMergeAt: (sheet: SheetId, row: number, col: number) => Rect | undefined } export const mergesCapability: WorkbookCapability = { construct(internal: WorkbookInternal): void { const api = internal as unknown as WorkbookInternal & Workbook_Merges const notifyMergeChange = (sheet: SheetId, rect: Rect): CommitResult => { internal.baseAtoms.revision.set((r) => r + 1) const keys = new Set() for (let r = rect.r0; r <= rect.r1; r++) { for (let c = rect.c0; c <= rect.c1; c++) keys.add(pack(r, c)) } internal.notifyRegions(new Map([[sheet, keys]])) return { applied: [], recalculated: [...keys].map((k) => ({ sheet, row: rowOf(k), col: colOf(k), })), cycles: [], } } api.mergeCells = function mergeCells( sheet: SheetId, rect: Rect, ): CommitResult { internal.requireSheet(sheet) const next = (internal.merges.get(sheet) ?? []).filter( (m) => !rectsIntersect(m, rect), ) next.push({ ...rect }) internal.merges.set(sheet, next) return notifyMergeChange(sheet, rect) } api.unmergeCells = function unmergeCells( sheet: SheetId, rect: Rect, ): CommitResult { const list = internal.merges.get(sheet) if (!list) return { applied: [], recalculated: [], cycles: [] } const next = list.filter((m) => !rectsIntersect(m, rect)) internal.merges.set(sheet, next) return notifyMergeChange(sheet, rect) } api.getMerges = function getMerges(sheet: SheetId): Array { return (internal.merges.get(sheet) ?? []).map((m) => ({ ...m })) } api.getMergeAt = function getMergeAt( sheet: SheetId, row: number, col: number, ): Rect | undefined { for (const m of internal.merges.get(sheet) ?? []) { if (rectContains(m, row, col)) return { ...m } } return undefined } }, }