@ai-matrx/office 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/chunk-5T2M6LSI.js +529 -0
  3. package/dist/chunk-5T2M6LSI.js.map +1 -0
  4. package/dist/chunk-QZE7HBZB.cjs +529 -0
  5. package/dist/chunk-QZE7HBZB.cjs.map +1 -0
  6. package/dist/chunk-WKBWRG7N.js +209 -0
  7. package/dist/chunk-WKBWRG7N.js.map +1 -0
  8. package/dist/chunk-ZDZCCOUB.cjs +209 -0
  9. package/dist/chunk-ZDZCCOUB.cjs.map +1 -0
  10. package/dist/core.d.cts +5 -1
  11. package/dist/core.d.ts +5 -1
  12. package/dist/matrxImagePorts-JDFYE4QI.cjs +59 -0
  13. package/dist/matrxImagePorts-JDFYE4QI.cjs.map +1 -0
  14. package/dist/matrxImagePorts-VPBBUADM.js +59 -0
  15. package/dist/matrxImagePorts-VPBBUADM.js.map +1 -0
  16. package/dist/sheets-xlsx.cjs +525 -0
  17. package/dist/sheets-xlsx.cjs.map +1 -0
  18. package/dist/sheets-xlsx.d.cts +153 -0
  19. package/dist/sheets-xlsx.d.ts +153 -0
  20. package/dist/sheets-xlsx.js +525 -0
  21. package/dist/sheets-xlsx.js.map +1 -0
  22. package/dist/sheets.cjs +921 -0
  23. package/dist/sheets.cjs.map +1 -0
  24. package/dist/sheets.d.cts +605 -0
  25. package/dist/sheets.d.ts +605 -0
  26. package/dist/sheets.js +921 -0
  27. package/dist/sheets.js.map +1 -0
  28. package/dist/univer-collab.cjs +2 -78
  29. package/dist/univer-collab.cjs.map +1 -1
  30. package/dist/univer-collab.d.cts +7 -15
  31. package/dist/univer-collab.d.ts +7 -15
  32. package/dist/univer-collab.js +2 -78
  33. package/dist/univer-collab.js.map +1 -1
  34. package/dist/univer-realtime.cjs +102 -0
  35. package/dist/univer-realtime.cjs.map +1 -0
  36. package/dist/univer-realtime.d.cts +15 -0
  37. package/dist/univer-realtime.d.ts +15 -0
  38. package/dist/univer-realtime.js +80 -0
  39. package/dist/univer-realtime.js.map +1 -0
  40. package/dist/xlsx-to-univer-AQAXU3TC.cjs +7 -0
  41. package/dist/xlsx-to-univer-AQAXU3TC.cjs.map +1 -0
  42. package/dist/xlsx-to-univer-OPMMBWEW.js +7 -0
  43. package/dist/xlsx-to-univer-OPMMBWEW.js.map +1 -0
  44. package/package.json +41 -4
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/sheets/univer-snapshot-rows.ts","../src/sheets/snapshot-preview-svg.ts","../src/sheets/snapshot-grid.ts","../src/core/document-page-style.ts","../src/sheets/sheet-page-setup.ts","../src/sheets/sheet-page-setup-service.ts","../src/sheets/sheet-print-pages.ts","../src/sheets/sheet-output.ts","../src/sheets/workbook-scope-source.ts"],"sourcesContent":["/**\n * features/workbooks/univer-snapshot-rows.ts\n *\n * Snapshot → rows: read a Univer `IWorkbookData` snapshot back out as a plain\n * string grid. The missing half of the workbook round trip — `export-targets`\n * pushes rows INTO a workbook; this reads the (possibly user-edited) values\n * back OUT so features can offer \"edit in the workbook, then import your\n * changes\". Values only (formulas come back as their cached value); first\n * sheet by `sheetOrder` unless a sheet id is named. A number shown through a date format reads as\n * its ISO date (a spreadsheet keeps dates as day serials). Reusable — do not fork a per-feature\n * snapshot walker. Callers: the workbook's \"Make this sheet a table\" (features/workbooks/table-link).\n */\n\ninterface CellShape {\n v?: unknown;\n s?: unknown;\n}\n\ninterface SheetShape {\n name?: string;\n /** Frozen panes: `xSplit` columns and `ySplit` rows are pinned. */\n freeze?: { xSplit?: number; ySplit?: number } | null;\n /** Univer marks a hidden column/row `hd: 1` (a table link's Record ID column is one). */\n columnData?: Record<string, { hd?: number } | null | undefined>;\n rowData?: Record<string, { hd?: number } | null | undefined>;\n cellData?: Record<string, Record<string, CellShape | null | undefined>>;\n}\n\ninterface SnapshotShape {\n sheetOrder?: string[];\n sheets?: Record<string, SheetShape>;\n styles?: Record<string, { n?: { pattern?: string } | null } | null>;\n}\n\nexport interface SnapshotRows {\n sheetName: string;\n /** Dense rectangular grid, trailing empty rows/columns trimmed. */\n rows: string[][];\n /** Rows pinned at the top of the sheet (Freeze rows), counted in `rows`. */\n frozenRows: number;\n /** Columns pinned at the left of the sheet (Freeze columns), counted in each row. */\n frozenCols: number;\n}\n\n/** A zero-based, inclusive cell range (a selection). */\nexport interface CellRange {\n startRow: number;\n endRow: number;\n startColumn: number;\n endColumn: number;\n}\n\n/** Sheet ids in tab order. */\nexport function univerSheetIds(snapshot: unknown): string[] {\n if (typeof snapshot !== \"object\" || snapshot === null) return [];\n const shape = snapshot as SnapshotShape;\n const sheets = shape.sheets ?? {};\n const ordered = (shape.sheetOrder ?? []).filter((id) => sheets[id]);\n return ordered.length ? ordered : Object.keys(sheets);\n}\n\ntype Styles = SnapshotShape[\"styles\"];\n\n/** The number-format pattern a cell is shown through, if any. */\nfunction patternOf(cell: CellShape | null | undefined, styles: Styles): string | null {\n const s = cell?.s;\n const style = typeof s === \"string\" ? styles?.[s] : s && typeof s === \"object\" ? (s as { n?: { pattern?: string } }) : null;\n const pattern = style?.n?.pattern;\n return typeof pattern === \"string\" ? pattern : null;\n}\n\nconst DAY_MS = 86_400_000;\nconst SERIAL_EPOCH_MS = Date.UTC(1899, 11, 30);\n\nfunction cellText(cell: CellShape | null | undefined, styles?: Styles): string {\n const value = cell?.v;\n if (value === null || value === undefined) return \"\";\n if (typeof value === \"number\") {\n const pattern = patternOf(cell, styles)?.replace(/\"[^\"]*\"/g, \"\").toLowerCase() ?? \"\";\n if (/[dy]/.test(pattern)) {\n const iso = new Date(Math.round(SERIAL_EPOCH_MS + value * DAY_MS)).toISOString();\n return /h/.test(pattern) ? `${iso.slice(0, 10)} ${iso.slice(11, 16)}` : iso.slice(0, 10);\n }\n }\n return String(value);\n}\n\n/**\n * Extract the first worksheet of a Univer workbook snapshot as a string grid.\n * Returns null when the value is not snapshot-shaped or has no sheets.\n * `skipHidden` leaves out hidden columns and rows: every read-only view (a share page, an agent's\n * read) passes it, so the table link's hidden Record ID column never reaches a reader.\n */\nexport function univerSnapshotToRows(snapshot: unknown, options: { sheetId?: string; skipHidden?: boolean; range?: CellRange } = {}): SnapshotRows | null {\n if (typeof snapshot !== \"object\" || snapshot === null) return null;\n const shape = snapshot as SnapshotShape;\n const sheets = shape.sheets ?? {};\n const styles = shape.styles;\n const firstId = options.sheetId\n ? sheets[options.sheetId]\n ? options.sheetId\n : undefined\n : (shape.sheetOrder?.find((id) => sheets[id]) ?? Object.keys(sheets)[0]);\n if (!firstId) return null;\n const sheet = sheets[firstId];\n const cellData = sheet?.cellData ?? {};\n\n let maxRow = -1;\n let maxCol = -1;\n for (const [rowKey, row] of Object.entries(cellData)) {\n const rowIndex = Number(rowKey);\n if (!Number.isInteger(rowIndex) || !row) continue;\n for (const [colKey, cell] of Object.entries(row)) {\n const colIndex = Number(colKey);\n if (!Number.isInteger(colIndex)) continue;\n if (cellText(cell) === \"\") continue;\n if (rowIndex > maxRow) maxRow = rowIndex;\n if (colIndex > maxCol) maxCol = colIndex;\n }\n }\n const range = options.range;\n if (range) {\n // A selection prints exactly the cells chosen, empty ones included.\n maxRow = range.endRow;\n maxCol = range.endColumn;\n }\n if (maxRow < 0 || maxCol < 0) {\n return { sheetName: sheet?.name ?? \"Sheet1\", rows: [], frozenRows: 0, frozenCols: 0 };\n }\n const firstRow = range?.startRow ?? 0;\n const firstCol = range?.startColumn ?? 0;\n\n const hiddenCol = (c: number) => options.skipHidden === true && sheet?.columnData?.[String(c)]?.hd === 1;\n const hiddenRow = (r: number) => options.skipHidden === true && sheet?.rowData?.[String(r)]?.hd === 1;\n const rows: string[][] = [];\n const ySplit = Math.max(0, sheet?.freeze?.ySplit ?? 0);\n const xSplit = Math.max(0, sheet?.freeze?.xSplit ?? 0);\n let frozenRows = 0;\n let frozenCols = 0;\n for (let r = firstRow; r <= maxRow; r += 1) {\n if (hiddenRow(r)) continue;\n if (r < ySplit) frozenRows += 1;\n const row: string[] = [];\n const source = cellData[String(r)] ?? {};\n let colsFrozenHere = 0;\n for (let c = firstCol; c <= maxCol; c += 1) {\n if (hiddenCol(c)) continue;\n if (c < xSplit) colsFrozenHere += 1;\n row.push(cellText(source[String(c)], styles));\n }\n frozenCols = colsFrozenHere;\n rows.push(row);\n }\n return { sheetName: sheet?.name ?? \"Sheet1\", rows, frozenRows, frozenCols };\n}\n","/**\n * snapshot → SVG: a faithful miniature of a Univer workbook's first sheet or a\n * Univer document's first page — the gallery tile Google draws. Pure (no DOM,\n * no Univer runtime), so it runs in the browser (thumbnail capture), in tests\n * and in node. Reads only what a snapshot stores: cell values + styles +\n * column widths + merges + conditional formats + checkboxes for sheets; the\n * dataStream + text runs + paragraph styles + tables for documents.\n */\n\nexport const THUMB_WIDTH = 320;\nexport const THUMB_HEIGHT = 414;\n\ntype Rgb = { rgb?: string | null } | null | undefined;\ntype CellStyle = {\n bg?: Rgb;\n cl?: Rgb;\n bl?: number;\n it?: number;\n fs?: number;\n ht?: number;\n n?: { pattern?: string } | null;\n bd?: Record<string, unknown> | null;\n};\ntype Cell = { v?: unknown; s?: string | CellStyle | null; t?: number; f?: string };\ntype Sheet = {\n id?: string;\n name?: string;\n cellData?: Record<string, Record<string, Cell>>;\n columnData?: Record<string, { w?: number; hd?: number }>;\n rowData?: Record<string, { h?: number; hd?: number }>;\n defaultColumnWidth?: number;\n defaultRowHeight?: number;\n mergeData?: Array<{ startRow: number; endRow: number; startColumn: number; endColumn: number }>;\n showGridlines?: number;\n freeze?: { ySplit?: number; xSplit?: number };\n};\nexport type Workbook = {\n sheetOrder?: string[];\n sheets?: Record<string, Sheet>;\n styles?: Record<string, CellStyle | null>;\n resources?: Array<{ name?: string; data?: string }>;\n};\n\nconst esc = (s: string) =>\n s.replace(/&/g, \"&amp;\").replace(/</g, \"&lt;\").replace(/>/g, \"&gt;\").replace(/\"/g, \"&quot;\");\n\nfunction color(c: Rgb, fallback: string): string {\n const v = c?.rgb;\n return typeof v === \"string\" && v.length > 0 ? v : fallback;\n}\n\n// ─── number formats ──────────────────────────────────────────────────────────\n\nconst EXCEL_EPOCH = Date.UTC(1899, 11, 30);\nconst MONTHS = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"];\n\nexport function formatCellValue(v: unknown, pattern?: string | null): string {\n if (v === null || v === undefined) return \"\";\n if (typeof v === \"boolean\") return v ? \"TRUE\" : \"FALSE\";\n if (typeof v !== \"number\" || !pattern || pattern === \"General\") return String(v);\n const p = pattern.toLowerCase();\n if (/[dy]/.test(p) && !p.includes(\"$\")) {\n const d = new Date(EXCEL_EPOCH + Math.round(v) * 86400000);\n const mm = d.getUTCMonth();\n const dd = d.getUTCDate();\n const yy = d.getUTCFullYear();\n if (p.includes(\"mmm\")) return `${MONTHS[mm]} ${dd}${p.includes(\"y\") ? `, ${yy}` : \"\"}`;\n return p.includes(\"y\") ? `${mm + 1}/${dd}/${yy}` : `${mm + 1}/${dd}`;\n }\n const decimals = (pattern.split(\".\")[1]?.match(/0/g) ?? []).length;\n if (p.includes(\"%\")) return `${(v * 100).toFixed(decimals)}%`;\n const grouped = Math.abs(v).toLocaleString(\"en-US\", {\n minimumFractionDigits: decimals,\n maximumFractionDigits: decimals,\n useGrouping: pattern.includes(\",\"),\n });\n const sign = v < 0 ? \"-\" : \"\";\n return p.includes(\"$\") ? `${sign}$${grouped}` : `${sign}${grouped}`;\n}\n\n// ─── conditional formatting (the rules a preview can evaluate) ───────────────\n\nexport type CfRule = {\n rule?: {\n type?: string;\n subType?: string;\n operator?: string;\n value?: unknown;\n style?: CellStyle;\n };\n ranges?: Array<{ startRow: number; endRow: number; startColumn: number; endColumn: number }>;\n};\n\nexport function parseResource<T>(wb: Workbook, name: string): T | null {\n const res = wb.resources?.find((r) => r.name === name);\n if (!res?.data) return null;\n try {\n return JSON.parse(res.data) as T;\n } catch {\n return null;\n }\n}\n\nexport function cfMatch(rule: NonNullable<CfRule[\"rule\"]>, v: unknown): boolean {\n if (rule.type !== \"highlightCell\") return false;\n if (rule.subType === \"number\" && typeof v === \"number\") {\n const x = rule.value;\n const [a = 0, b = 0] = Array.isArray(x) ? (x as number[]) : [Number(x), Number(x)];\n switch (rule.operator) {\n case \"greaterThan\": return v > a;\n case \"greaterThanOrEqual\": return v >= a;\n case \"lessThan\": return v < a;\n case \"lessThanOrEqual\": return v <= a;\n case \"equal\": return v === a;\n case \"notEqual\": return v !== a;\n case \"between\": return v >= a && v <= b;\n default: return false;\n }\n }\n if (rule.subType === \"text\" && v !== undefined && v !== null) {\n const s = String(v).toLowerCase();\n const want = String(rule.value ?? \"\").toLowerCase();\n switch (rule.operator) {\n case \"containsText\": return s.includes(want);\n case \"equal\": return s === want;\n case \"beginsWith\": return s.startsWith(want);\n default: return false;\n }\n }\n return false;\n}\n\n// ─── workbook ────────────────────────────────────────────────────────────────\n\nexport function workbookPreviewSvg(snapshot: unknown): string {\n const wb = (snapshot ?? {}) as Workbook;\n const firstId = wb.sheetOrder?.[0] ?? Object.keys(wb.sheets ?? {})[0];\n const sheet: Sheet = (firstId && wb.sheets?.[firstId]) || {};\n const sheetId = sheet.id ?? firstId ?? \"\";\n const styles = wb.styles ?? {};\n const cfAll = parseResource<Record<string, CfRule[]>>(wb, \"SHEET_CONDITIONAL_FORMATTING_PLUGIN\");\n const cf = (cfAll?.[sheetId] ?? []).filter((r) => r.rule && r.ranges);\n const dvAll = parseResource<Record<string, Array<{ type?: string; ranges?: CfRule[\"ranges\"] }>>>(\n wb,\n \"SHEET_DATA_VALIDATION_PLUGIN\",\n );\n const checkboxRanges = (dvAll?.[sheetId] ?? []).filter((d) => d.type === \"checkbox\").flatMap((d) => d.ranges ?? []);\n const inRanges = (rs: CfRule[\"ranges\"], r: number, c: number) =>\n (rs ?? []).some((x) => r >= x.startRow && r <= x.endRow && c >= x.startColumn && c <= x.endColumn);\n\n const dcw = sheet.defaultColumnWidth ?? 88;\n const drh = sheet.defaultRowHeight ?? 24;\n const colW = (c: number) => (sheet.columnData?.[c]?.hd ? 0 : sheet.columnData?.[c]?.w ?? dcw);\n const rowH = (r: number) => (sheet.rowData?.[r]?.hd ? 0 : sheet.rowData?.[r]?.h ?? drh);\n\n // Show the used width (at least ~5 default columns), scaled to the tile.\n let maxCol = 0;\n for (const row of Object.values(sheet.cellData ?? {})) {\n for (const c of Object.keys(row ?? {})) maxCol = Math.max(maxCol, Number(c));\n }\n let used = 0;\n for (let c = 0; c <= maxCol; c++) used += colW(c);\n // Google's tiles show the top-left of the sheet at a readable size, not the whole width.\n const span = Math.min(Math.max(used + 12, dcw * 5), 640);\n const scale = THUMB_WIDTH / span;\n\n const out: string[] = [];\n out.push(`<rect width=\"${THUMB_WIDTH}\" height=\"${THUMB_HEIGHT}\" fill=\"#ffffff\"/>`);\n const gridOn = sheet.showGridlines !== 0;\n const merges = sheet.mergeData ?? [];\n const covered = new Set<string>();\n for (const m of merges) {\n for (let r = m.startRow; r <= m.endRow; r++)\n for (let c = m.startColumn; c <= m.endColumn; c++)\n if (r !== m.startRow || c !== m.startColumn) covered.add(`${r}:${c}`);\n }\n const texts: string[] = [];\n let y = 0;\n for (let r = 0; y * scale < THUMB_HEIGHT && r < 400; r++) {\n const h = rowH(r);\n let x = 0;\n for (let c = 0; x * scale < THUMB_WIDTH && c < 60; c++) {\n const w = colW(c);\n if (w === 0 || h === 0 || covered.has(`${r}:${c}`)) {\n x += w;\n continue;\n }\n const m = merges.find((mm) => mm.startRow === r && mm.startColumn === c);\n let cw = w;\n let ch = h;\n if (m) {\n cw = 0;\n for (let k = m.startColumn; k <= m.endColumn; k++) cw += colW(k);\n ch = 0;\n for (let k = m.startRow; k <= m.endRow; k++) ch += rowH(k);\n }\n const cell = sheet.cellData?.[r]?.[c];\n const base: CellStyle =\n (typeof cell?.s === \"string\" ? styles[cell.s] : (cell?.s as CellStyle | null)) ?? {};\n let st: CellStyle = base;\n for (const rule of cf) {\n if (inRanges(rule.ranges, r, c) && rule.rule && cfMatch(rule.rule, cell?.v)) {\n st = { ...st, ...rule.rule.style };\n break;\n }\n }\n const X = x * scale;\n const Y = y * scale;\n const W = cw * scale;\n const H = ch * scale;\n const bg = st.bg?.rgb;\n if (bg) out.push(`<rect x=\"${X.toFixed(1)}\" y=\"${Y.toFixed(1)}\" width=\"${W.toFixed(1)}\" height=\"${H.toFixed(1)}\" fill=\"${esc(bg)}\"/>`);\n if (gridOn) out.push(`<rect x=\"${X.toFixed(1)}\" y=\"${Y.toFixed(1)}\" width=\"${W.toFixed(1)}\" height=\"${H.toFixed(1)}\" fill=\"none\" stroke=\"#e2e3e3\" stroke-width=\"0.5\"/>`);\n const fs = Math.max((st.fs ?? 10) * 1.333 * scale, 3);\n const midY = Y + H / 2 + fs * 0.35;\n if (inRanges(checkboxRanges, r, c) && r > 0 && (cell?.v === true || cell?.v === false || cell?.v === 1 || cell?.v === 0 || cell?.v === \"TRUE\" || cell?.v === \"FALSE\")) {\n const box = Math.min(H * 0.55, 9);\n const bx = X + W / 2 - box / 2;\n const by = Y + H / 2 - box / 2;\n const on = cell?.v === true || cell?.v === 1 || cell?.v === \"TRUE\";\n out.push(`<rect x=\"${bx.toFixed(1)}\" y=\"${by.toFixed(1)}\" width=\"${box.toFixed(1)}\" height=\"${box.toFixed(1)}\" rx=\"1\" fill=\"${on ? \"#1a73e8\" : \"#ffffff\"}\" stroke=\"${on ? \"#1a73e8\" : \"#5f6368\"}\" stroke-width=\"0.7\"/>`);\n if (on) out.push(`<path d=\"M${(bx + box * 0.2).toFixed(1)} ${(by + box * 0.5).toFixed(1)} l${(box * 0.25).toFixed(1)} ${(box * 0.25).toFixed(1)} l${(box * 0.4).toFixed(1)} ${(-box * 0.45).toFixed(1)}\" stroke=\"#fff\" stroke-width=\"0.9\" fill=\"none\"/>`);\n } else if (cell && cell.v !== undefined && cell.v !== null && cell.v !== \"\") {\n const text = formatCellValue(cell.v, st.n?.pattern);\n const numeric = typeof cell.v === \"number\";\n const ht = st.ht ?? (numeric ? 3 : 1);\n const anchor = ht === 2 ? \"middle\" : ht === 3 ? \"end\" : \"start\";\n const tx = ht === 2 ? X + W / 2 : ht === 3 ? X + W - 2 * scale * 2 : X + 3 * scale;\n const maxChars = Math.max(Math.floor((m ? W : W * (ht === 1 && !numeric ? 3 : 1)) / (fs * 0.55)), 1);\n const shown = text.length > maxChars ? text.slice(0, maxChars) : text;\n texts.push(\n `<text x=\"${tx.toFixed(1)}\" y=\"${midY.toFixed(1)}\" font-size=\"${fs.toFixed(1)}\" text-anchor=\"${anchor}\" fill=\"${esc(color(st.cl, \"#202124\"))}\"${st.bl ? ' font-weight=\"700\"' : \"\"}${st.it ? ' font-style=\"italic\"' : \"\"}>${esc(shown)}</text>`,\n );\n }\n x += w;\n }\n y += h;\n }\n return wrapSvg([...out, ...texts].join(\"\"));\n}\n\n// ─── document ────────────────────────────────────────────────────────────────\n\ntype TextStyle = { fs?: number; bl?: number; it?: number; cl?: Rgb; ff?: string; bg?: Rgb };\ntype DocData = {\n body?: {\n dataStream?: string;\n paragraphs?: Array<{ startIndex: number; paragraphStyle?: { horizontalAlign?: number; spaceAbove?: { v?: number }; spaceBelow?: { v?: number } } }>;\n textRuns?: Array<{ st: number; ed: number; ts?: TextStyle }>;\n tables?: Array<{ startIndex: number; endIndex: number; tableId: string }>;\n };\n documentStyle?: {\n pageSize?: { width?: number; height?: number };\n marginTop?: number;\n marginLeft?: number;\n marginRight?: number;\n };\n};\n\nconst CONTROL = /[\\x1A\\x1B\\x1C\\x1D\\x0E\\x0F\\x1F\\x1E\\b]/g;\n\nexport function documentPreviewSvg(snapshot: unknown): string {\n const doc = (snapshot ?? {}) as DocData;\n const stream = doc.body?.dataStream ?? \"\";\n const pageW = doc.documentStyle?.pageSize?.width ?? 595;\n const scale = THUMB_WIDTH / pageW;\n const ml = (doc.documentStyle?.marginLeft ?? 72) * scale;\n const mr = (doc.documentStyle?.marginRight ?? 72) * scale;\n const mt = (doc.documentStyle?.marginTop ?? 72) * scale;\n const contentW = THUMB_WIDTH - ml - mr;\n const runs = [...(doc.body?.textRuns ?? [])].sort((a, b) => a.st - b.st);\n const tables = doc.body?.tables ?? [];\n const styleAt = (i: number): TextStyle => runs.find((r) => i >= r.st && i < r.ed)?.ts ?? {};\n /** The style most of a paragraph's characters carry (a bold lead-in does not make the line bold). */\n const dominantStyle = (from: number, to: number): TextStyle => {\n const counts = new Map<string, { n: number; ts: TextStyle }>();\n for (let i = from; i < to; i++) {\n if (/\\s/.test(stream[i] ?? \"\")) continue;\n const ts = styleAt(i);\n const k = JSON.stringify(ts);\n const e = counts.get(k) ?? { n: 0, ts };\n e.n++;\n counts.set(k, e);\n }\n let best: { n: number; ts: TextStyle } | null = null;\n for (const e of counts.values()) if (!best || e.n > best.n) best = e;\n return best?.ts ?? {};\n };\n const out: string[] = [`<rect width=\"${THUMB_WIDTH}\" height=\"${THUMB_HEIGHT}\" fill=\"#ffffff\"/>`];\n\n let y = mt;\n let start = 0;\n const paragraphs = doc.body?.paragraphs ?? [];\n for (let p = 0; p < paragraphs.length && y < THUMB_HEIGHT; p++) {\n const para = paragraphs[p];\n if (!para) break;\n const end = para.startIndex;\n const table = tables.find((t) => start >= t.startIndex && start < t.endIndex);\n if (table) {\n // Draw the whole table once: rows split on the row token, cells on the cell token.\n const raw = stream.slice(table.startIndex, table.endIndex);\n const rows = raw.split(\"\\x1B\").slice(1).map((row) =>\n row.split(\"\\x1C\").slice(1).map((cell) => cell.replace(/[\\r\\n]/g, \" \").replace(CONTROL, \"\").trim()),\n );\n const cols = Math.max(1, ...rows.map((r) => r.length));\n const cw = contentW / cols;\n // Bold header row when the first cell's text is bold.\n const headerBold = styleAt(table.startIndex + 4).bl === 1;\n for (let r = 0; r < rows.length && y < THUMB_HEIGHT; r++) {\n const rh = 16 * scale * 1.2 + 6 * scale;\n for (let c = 0; c < cols; c++) {\n const cx = ml + c * cw;\n out.push(`<rect x=\"${cx.toFixed(1)}\" y=\"${y.toFixed(1)}\" width=\"${cw.toFixed(1)}\" height=\"${rh.toFixed(1)}\" fill=\"${r === 0 && headerBold ? \"#f1f3f4\" : \"none\"}\" stroke=\"#9aa0a6\" stroke-width=\"0.4\"/>`);\n const fs = 11 * 1.333 * scale;\n const maxChars = Math.max(Math.floor((cw - 6 * scale) / (fs * 0.5)), 1);\n const t = rows[r]?.[c] ?? \"\";\n out.push(`<text x=\"${(cx + 4 * scale).toFixed(1)}\" y=\"${(y + rh / 2 + fs * 0.35).toFixed(1)}\" font-size=\"${fs.toFixed(1)}\" fill=\"#202124\"${r === 0 && headerBold ? ' font-weight=\"700\"' : \"\"}>${esc(t.slice(0, maxChars))}</text>`);\n }\n y += rh;\n }\n // Skip every paragraph inside the table.\n while (p + 1 < paragraphs.length && (paragraphs[p + 1]?.startIndex ?? Infinity) < table.endIndex) p++;\n start = table.endIndex;\n y += 6 * scale;\n continue;\n }\n const text = stream.slice(start, end).replace(CONTROL, \"\");\n const st = dominantStyle(start, end);\n const ps = para.paragraphStyle ?? {};\n const fsPt = st.fs ?? 11;\n const fs = fsPt * 1.333 * scale;\n const lineH = fs * 1.3;\n y += (ps.spaceAbove?.v ?? (fsPt > 13 ? 8 : 0)) * scale;\n const align = ps.horizontalAlign ?? 1;\n const anchor = align === 2 ? \"middle\" : align === 3 ? \"end\" : \"start\";\n const tx = align === 2 ? ml + contentW / 2 : align === 3 ? ml + contentW : ml;\n // Paragraph shading (a filled band) when the run carries a highlight.\n if (!text.trim()) {\n y += lineH * 0.8;\n } else {\n const maxChars = Math.max(Math.floor(contentW / (fs * (st.bl ? 0.56 : 0.5))), 4);\n const words = text.split(/\\s+/);\n const lines: string[] = [];\n let line = \"\";\n for (const w of words) {\n if ((line + \" \" + w).trim().length > maxChars && line) {\n lines.push(line);\n line = w;\n } else line = (line + \" \" + w).trim();\n }\n if (line) lines.push(line);\n for (const l of lines) {\n if (y > THUMB_HEIGHT) break;\n y += lineH;\n if (st.bg?.rgb) out.push(`<rect x=\"${ml.toFixed(1)}\" y=\"${(y - fs).toFixed(1)}\" width=\"${contentW.toFixed(1)}\" height=\"${lineH.toFixed(1)}\" fill=\"${esc(st.bg.rgb)}\"/>`);\n out.push(`<text x=\"${tx.toFixed(1)}\" y=\"${y.toFixed(1)}\" font-size=\"${fs.toFixed(1)}\" text-anchor=\"${anchor}\" fill=\"${esc(color(st.cl, \"#202124\"))}\"${st.bl ? ' font-weight=\"700\"' : \"\"}${st.it ? ' font-style=\"italic\"' : \"\"}${st.ff && /serif|georgia|times|garamond/i.test(st.ff) && !/sans/i.test(st.ff) ? ' font-family=\"Georgia, serif\"' : \"\"}>${esc(l)}</text>`);\n }\n }\n y += (ps.spaceBelow?.v ?? 4) * scale;\n start = end + 1;\n }\n return wrapSvg(out.join(\"\"));\n}\n\nfunction wrapSvg(body: string): string {\n return `<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"${THUMB_WIDTH}\" height=\"${THUMB_HEIGHT}\" viewBox=\"0 0 ${THUMB_WIDTH} ${THUMB_HEIGHT}\" font-family=\"Arial, Helvetica, sans-serif\">${body}</svg>`;\n}\n\nexport function snapshotPreviewSvg(kind: \"workbook\" | \"document\", snapshot: unknown): string {\n return kind === \"workbook\" ? workbookPreviewSvg(snapshot) : documentPreviewSvg(snapshot);\n}\n","/**\n * features/workbooks/snapshot-grid.ts\n *\n * Snapshot → a formatted, read-only grid model: what a spreadsheet VIEW needs and the plain-string\n * `univer-snapshot-rows` does not carry — number/currency/date formats applied, checkbox cells\n * (data-validation ranges of type checkbox), alignment, bold/italic, fill and text colour, column\n * widths, merges, frozen rows/columns, hidden rows/columns and sheets. Pure (no DOM, no Univer\n * runtime): the signed-out share page draws it. The number formatter is the thumbnail's\n * (`formatCellValue`) so a tile, a share link and the editor read the same.\n */\n\nimport {\n cfMatch,\n formatCellValue,\n parseResource,\n type CfRule,\n type Workbook,\n} from \"./snapshot-preview-svg\";\n\ntype Rgb = { rgb?: string | null } | null | undefined;\ninterface Style {\n bg?: Rgb;\n cl?: Rgb;\n bl?: number;\n it?: number;\n fs?: number;\n ht?: number;\n tb?: number;\n n?: { pattern?: string } | null;\n}\ninterface Cell {\n v?: unknown;\n s?: string | Style | null;\n}\ninterface Sheet {\n id?: string;\n name?: string;\n hidden?: number;\n cellData?: Record<string, Record<string, Cell | null | undefined>>;\n columnData?: Record<string, { w?: number; hd?: number } | null | undefined>;\n rowData?: Record<string, { h?: number; hd?: number } | null | undefined>;\n defaultColumnWidth?: number;\n defaultRowHeight?: number;\n mergeData?: Array<{ startRow: number; endRow: number; startColumn: number; endColumn: number }>;\n showGridlines?: number;\n freeze?: { xSplit?: number; ySplit?: number };\n}\n\nexport interface GridCell {\n text: string;\n /** A checkbox cell (a data-validation checkbox range): `checked` says its state. */\n checkbox?: { checked: boolean } | undefined;\n align: \"left\" | \"center\" | \"right\";\n bold: boolean;\n italic: boolean;\n /** Font size in px (the snapshot stores points). */\n fontPx: number;\n bg?: string | undefined;\n color?: string | undefined;\n wrap: boolean;\n rowSpan: number;\n colSpan: number;\n /** Covered by a merge that starts elsewhere: draw nothing. */\n covered: boolean;\n}\n\nexport interface GridSheet {\n id: string;\n name: string;\n /** Visible column indexes (hidden columns left out) with their pixel widths. */\n columns: Array<{ index: number; width: number }>;\n rows: Array<{ index: number; height: number; cells: GridCell[] }>;\n /** How many leading VISIBLE rows / columns stay put while scrolling. */\n frozenRows: number;\n frozenColumns: number;\n gridlines: boolean;\n /** Rows beyond the cap were left out. */\n truncatedFrom: number | null;\n}\n\nexport const GRID_MAX_ROWS = 500;\nexport const GRID_MAX_COLUMNS = 60;\n\nconst PT_TO_PX = 1.333;\nconst isCheckboxValue = (v: unknown) => v === true || v === false || v === 1 || v === 0 || v === \"TRUE\" || v === \"FALSE\";\nconst isOn = (v: unknown) => v === true || v === 1 || v === \"TRUE\";\n\nfunction hasValue(cell: Cell | null | undefined): boolean {\n const v = cell?.v;\n return v !== undefined && v !== null && v !== \"\";\n}\n\n/** Every visible sheet that has something in it, in tab order. Empty and hidden sheets are left out. */\nexport function snapshotToGridSheets(snapshot: unknown): GridSheet[] {\n if (typeof snapshot !== \"object\" || snapshot === null) return [];\n const wb = snapshot as Workbook;\n const sheets = wb.sheets ?? {};\n const order = (wb.sheetOrder ?? []).filter((id) => sheets[id]);\n const ids = order.length > 0 ? order : Object.keys(sheets);\n const out: GridSheet[] = [];\n for (const id of ids) {\n const grid = sheetToGrid(wb, id, sheets[id] as Sheet);\n if (grid) out.push(grid);\n }\n return out;\n}\n\nfunction sheetToGrid(wb: Workbook, id: string, sheet: Sheet): GridSheet | null {\n if (sheet.hidden === 1) return null;\n const cellData = sheet.cellData ?? {};\n let maxRow = -1;\n let maxCol = -1;\n for (const [rk, row] of Object.entries(cellData)) {\n const r = Number(rk);\n if (!Number.isInteger(r) || !row) continue;\n for (const [ck, cell] of Object.entries(row)) {\n const c = Number(ck);\n if (!Number.isInteger(c) || !hasValue(cell)) continue;\n if (r > maxRow) maxRow = r;\n if (c > maxCol) maxCol = c;\n }\n }\n if (maxRow < 0 || maxCol < 0) return null;\n\n const styles = wb.styles ?? {};\n const sheetId = sheet.id ?? id;\n const cfAll = parseResource<Record<string, CfRule[]>>(wb, \"SHEET_CONDITIONAL_FORMATTING_PLUGIN\");\n const cf = (cfAll?.[sheetId] ?? []).filter((r) => r.rule && r.ranges);\n const dvAll = parseResource<Record<string, Array<{ type?: string; ranges?: CfRule[\"ranges\"] }>>>(\n wb,\n \"SHEET_DATA_VALIDATION_PLUGIN\",\n );\n const checkboxRanges = (dvAll?.[sheetId] ?? []).filter((d) => d.type === \"checkbox\").flatMap((d) => d.ranges ?? []);\n const inRanges = (rs: CfRule[\"ranges\"], r: number, c: number) =>\n (rs ?? []).some((x) => r >= x.startRow && r <= x.endRow && c >= x.startColumn && c <= x.endColumn);\n\n const dcw = sheet.defaultColumnWidth ?? 88;\n const drh = sheet.defaultRowHeight ?? 24;\n const lastCol = Math.min(maxCol, GRID_MAX_COLUMNS - 1);\n const columns: GridSheet[\"columns\"] = [];\n for (let c = 0; c <= lastCol; c += 1) {\n const col = sheet.columnData?.[String(c)];\n if (col?.hd === 1) continue;\n columns.push({ index: c, width: col?.w ?? dcw });\n }\n\n const merges = sheet.mergeData ?? [];\n const covered = new Set<string>();\n for (const m of merges) {\n for (let r = m.startRow; r <= m.endRow; r += 1)\n for (let c = m.startColumn; c <= m.endColumn; c += 1) if (r !== m.startRow || c !== m.startColumn) covered.add(`${r}:${c}`);\n }\n\n const rows: GridSheet[\"rows\"] = [];\n const lastRow = Math.min(maxRow, GRID_MAX_ROWS - 1);\n for (let r = 0; r <= lastRow; r += 1) {\n const rd = sheet.rowData?.[String(r)];\n if (rd?.hd === 1) continue;\n const source = cellData[String(r)] ?? {};\n const cells: GridCell[] = columns.map(({ index: c }) => {\n const cell = source[String(c)] ?? undefined;\n const base: Style = (typeof cell?.s === \"string\" ? (styles[cell.s] as Style | null | undefined) : (cell?.s as Style | null | undefined)) ?? {};\n let st: Style = base;\n for (const rule of cf) {\n if (inRanges(rule.ranges, r, c) && rule.rule && cfMatch(rule.rule, cell?.v)) {\n st = { ...st, ...(rule.rule.style as Style | undefined) };\n break;\n }\n }\n const m = merges.find((mm) => mm.startRow === r && mm.startColumn === c);\n const v = cell?.v;\n const box = hasValue(cell) && inRanges(checkboxRanges, r, c) && isCheckboxValue(v);\n const numeric = typeof v === \"number\";\n const ht = st.ht ?? 0;\n return {\n text: box ? \"\" : hasValue(cell) ? formatCellValue(v, st.n?.pattern) : \"\",\n checkbox: box ? { checked: isOn(v) } : undefined,\n align: box || ht === 2 ? \"center\" : ht === 3 || (ht === 0 && numeric) ? \"right\" : \"left\",\n bold: st.bl === 1,\n italic: st.it === 1,\n fontPx: Math.round((st.fs ?? 10) * PT_TO_PX),\n bg: st.bg?.rgb ?? undefined,\n color: st.cl?.rgb ?? undefined,\n wrap: st.tb === 3,\n rowSpan: m ? m.endRow - m.startRow + 1 : 1,\n colSpan: m ? m.endColumn - m.startColumn + 1 : 1,\n covered: covered.has(`${r}:${c}`),\n };\n });\n rows.push({ index: r, height: rd?.h ?? drh, cells });\n }\n\n const ySplit = sheet.freeze?.ySplit ?? 0;\n const xSplit = sheet.freeze?.xSplit ?? 0;\n return {\n id: sheetId,\n name: sheet.name ?? \"Sheet1\",\n columns,\n rows,\n frozenRows: Math.min(rows.filter((r) => r.index < ySplit).length, rows.length),\n frozenColumns: Math.min(columns.filter((c) => c.index < xSplit).length, columns.length),\n gridlines: sheet.showGridlines !== 0,\n truncatedFrom: maxRow > lastRow ? maxRow + 1 : null,\n };\n}\n","/**\n * The ONE page geometry every Matrx cloud document is laid out with.\n *\n * Univer lays a document out inside `documentStyle.pageSize`. With no pageSize\n * the docs renderer has no page box at all: text never wraps (it runs off the\n * right edge) and the vertical extent is unbounded, so the docs viewport\n * reports nothing to scroll — the wheel is swallowed and the page sits frozen\n * at the top. Every writer of a Univer snapshot MUST stamp this style.\n *\n * Consumers: `DocumentEditor` (new empty doc), `markdown-to-univer-doc`\n * (markdown import), and `sanitizeUniverDocSnapshot` (recovery for snapshots\n * that reached storage without it — notably `origin='agent'` writes).\n */\nimport type { DocumentFlavor, IDocumentData, PageOrientType } from \"@univerjs/core\";\n\n// Univer's enum values, written out: this file is read by tests and the markdown importer, which must not\n// pull Univer's runtime in just for four numbers (guarded by the page-style test).\nconst TRADITIONAL = 1 as DocumentFlavor;\nconst MODERN = 2 as DocumentFlavor;\nconst PORTRAIT = 0 as PageOrientType;\nconst LANDSCAPE = 1 as PageOrientType;\n\ntype DocumentStyle = NonNullable<IDocumentData[\"documentStyle\"]>;\n\n/** Univer lays documents out in 96-dpi CSS pixels: one inch is 96 units. */\nexport const PX_PER_INCH = 96;\n\n/** Paper sizes the Page setup dialog offers, in 96-dpi pixels, portrait (width x height). */\nexport const PAPER_SIZES = {\n Letter: { width: 816, height: 1056, label: \"Letter (8.5 x 11 in)\" },\n Legal: { width: 816, height: 1344, label: \"Legal (8.5 x 14 in)\" },\n Tabloid: { width: 1056, height: 1632, label: \"Tabloid (11 x 17 in)\" },\n Executive: { width: 696, height: 1008, label: \"Executive (7.25 x 10.5 in)\" },\n Statement: { width: 528, height: 816, label: \"Statement (5.5 x 8.5 in)\" },\n A3: { width: 1123, height: 1587, label: \"A3 (11.69 x 16.54 in)\" },\n A4: { width: 794, height: 1124, label: \"A4 (8.27 x 11.69 in)\" },\n A5: { width: 559, height: 794, label: \"A5 (5.83 x 8.27 in)\" },\n} as const;\nexport type PaperSizeName = keyof typeof PAPER_SIZES;\n\n/** A real page, like Google Docs: Letter, one-inch margins (624px of text), paginated. */\nexport const DEFAULT_DOCUMENT_PAGE_STYLE: DocumentStyle = {\n pageSize: { width: 816, height: 1056 },\n documentFlavor: TRADITIONAL,\n pageOrient: PORTRAIT,\n marginTop: PX_PER_INCH,\n marginBottom: PX_PER_INCH,\n marginLeft: PX_PER_INCH,\n marginRight: PX_PER_INCH,\n // Univer applies no paragraph spacing to a paginated page by itself; without this every paragraph\n // runs into the next (Google Docs' own default: 1.15 lines, 10px after).\n defaultParagraphStyle: { spaceAbove: { v: 0 }, spaceBelow: { v: 10 }, lineSpacing: 1.15 },\n};\n\n/** Fresh copy — Univer mutates the style it is handed. */\nexport function defaultDocumentPageStyle(): DocumentStyle {\n return {\n ...DEFAULT_DOCUMENT_PAGE_STYLE,\n pageSize: { ...DEFAULT_DOCUMENT_PAGE_STYLE.pageSize! },\n defaultParagraphStyle: { ...DEFAULT_DOCUMENT_PAGE_STYLE.defaultParagraphStyle! },\n };\n}\n\n/**\n * The squeezed geometry every document was stamped with until 2026-10-15: A4 at 72 dpi (595 x 842)\n * with 90/72 margins, in Univer's pageless flavour, so the text column was ~415px with no paper.\n * Pageless was never offered then, so this exact shape is always the old default, never a choice.\n */\nexport function isLegacySqueezedPageStyle(style: DocumentStyle | undefined): boolean {\n const size = style?.pageSize;\n return (\n !!size &&\n size.width === 595 &&\n size.height === 842 &&\n style?.marginLeft === 90 &&\n style?.marginRight === 90 &&\n style?.marginTop === 72 &&\n style?.marginBottom === 72\n );\n}\n\n/** What the Page setup dialog edits. Margins are in inches, like Google's. */\nexport interface PageSetupValues {\n pageless: boolean;\n orientation: \"portrait\" | \"landscape\";\n paper: PaperSizeName;\n marginTop: number;\n marginBottom: number;\n marginLeft: number;\n marginRight: number;\n /** Paper colour, hex. Always a light colour so the black ink stays readable. */\n pageColor: string;\n}\n\nexport const PAGE_COLORS = [\"#ffffff\", \"#fff8e7\", \"#f3f3f3\", \"#e8f0fe\", \"#e6f4ea\", \"#fef7e0\"] as const;\nexport const DEFAULT_PAGE_COLOR = PAGE_COLORS[0];\n/** Key on `documentStyle` carrying the paper colour (Univer paints it from the host's surface theme). */\nexport const PAGE_COLOR_KEY = \"matrxPageColor\";\n\nconst round2 = (n: number) => Math.round(n * 100) / 100;\n\n/** The dialog's values, read from a document's style (missing parts fall back to the default page). */\nexport function pageSetupFromStyle(style: DocumentStyle | undefined): PageSetupValues {\n const landscape = style?.pageOrient === LANDSCAPE;\n const w = style?.pageSize?.width ?? DEFAULT_DOCUMENT_PAGE_STYLE.pageSize!.width!;\n const h = style?.pageSize?.height ?? DEFAULT_DOCUMENT_PAGE_STYLE.pageSize!.height!;\n const [pw, ph] = landscape ? [h, w] : [w, h];\n const paper =\n (Object.keys(PAPER_SIZES) as PaperSizeName[]).find(\n (k) => Math.abs(PAPER_SIZES[k].width - pw) <= 1 && Math.abs(PAPER_SIZES[k].height - ph) <= 1,\n ) ?? \"Letter\";\n const inch = (v: number | undefined) => round2((v ?? PX_PER_INCH) / PX_PER_INCH);\n const color = (style as Record<string, unknown> | undefined)?.[PAGE_COLOR_KEY];\n return {\n pageless: style?.documentFlavor === MODERN,\n orientation: landscape ? \"landscape\" : \"portrait\",\n paper,\n marginTop: inch(style?.marginTop),\n marginBottom: inch(style?.marginBottom),\n marginLeft: inch(style?.marginLeft),\n marginRight: inch(style?.marginRight),\n pageColor: typeof color === \"string\" && color ? color : DEFAULT_PAGE_COLOR,\n };\n}\n\n/** Params for Univer's `docs.command.page-setup` (pageSize is the as-printed size: swapped when landscape). */\nexport function pageSetupCommandParams(v: PageSetupValues) {\n const base = PAPER_SIZES[v.paper];\n const landscape = v.orientation === \"landscape\";\n return {\n documentFlavor: v.pageless ? MODERN : TRADITIONAL,\n pageOrient: landscape ? LANDSCAPE : PORTRAIT,\n pageSize: landscape ? { width: base.height, height: base.width } : { width: base.width, height: base.height },\n marginTop: Math.round(v.marginTop * PX_PER_INCH),\n marginBottom: Math.round(v.marginBottom * PX_PER_INCH),\n marginLeft: Math.round(v.marginLeft * PX_PER_INCH),\n marginRight: Math.round(v.marginRight * PX_PER_INCH),\n };\n}\n","/**\n * A workbook's print settings — Google Sheets' File → Page setup / Print, as pure data and pure\n * pagination (no React, no IO). They live on the workbook (`udt_workbooks.metadata.page_setup`),\n * so Print, Download as PDF and the Page setup dialog all read the same values.\n *\n * Geometry is in 96-dpi CSS pixels, like the rest of the document stack (`PX_PER_INCH`).\n */\n\nimport { PAPER_SIZES, PX_PER_INCH, type PaperSizeName } from \"../core\";\n\nexport const SHEET_SCALES = [\"normal\", \"width\", \"height\", \"page\"] as const;\nexport type SheetScale = (typeof SHEET_SCALES)[number];\nexport const SHEET_MARGIN_PRESETS = [\"normal\", \"narrow\", \"wide\", \"custom\"] as const;\nexport type SheetMarginPreset = (typeof SHEET_MARGIN_PRESETS)[number];\nexport const SHEET_PRINT_SCOPES = [\"sheet\", \"workbook\", \"selection\"] as const;\nexport type SheetPrintScope = (typeof SHEET_PRINT_SCOPES)[number];\nexport type SheetPageOrder = \"down-over\" | \"over-down\";\nexport type SheetAlignH = \"left\" | \"center\" | \"right\";\nexport type SheetAlignV = \"top\" | \"middle\" | \"bottom\";\n\nexport interface SheetPageSetup {\n paper: PaperSizeName;\n orientation: \"portrait\" | \"landscape\";\n scale: SheetScale;\n margins: SheetMarginPreset;\n /** Inches; read only when `margins` is \"custom\". */\n customMargins: { top: number; bottom: number; left: number; right: number };\n scope: SheetPrintScope;\n gridlines: boolean;\n repeatFrozenRows: boolean;\n repeatFrozenColumns: boolean;\n pageOrder: SheetPageOrder;\n alignH: SheetAlignH;\n alignV: SheetAlignV;\n}\n\n/** Google Sheets' defaults: Letter, portrait, 100%, normal margins, current sheet, gridlines on. */\nexport const DEFAULT_SHEET_PAGE_SETUP: SheetPageSetup = {\n paper: \"Letter\",\n orientation: \"portrait\",\n scale: \"normal\",\n margins: \"normal\",\n customMargins: { top: 0.75, bottom: 0.75, left: 0.7, right: 0.7 },\n scope: \"sheet\",\n gridlines: true,\n repeatFrozenRows: false,\n repeatFrozenColumns: false,\n pageOrder: \"down-over\",\n alignH: \"left\",\n alignV: \"top\",\n};\n\n/** Google's three preset margins, in inches. */\nexport const MARGIN_PRESET_INCHES: Record<Exclude<SheetMarginPreset, \"custom\">, { top: number; bottom: number; left: number; right: number }> = {\n normal: { top: 0.75, bottom: 0.75, left: 0.7, right: 0.7 },\n narrow: { top: 0.25, bottom: 0.25, left: 0.25, right: 0.25 },\n wide: { top: 1, bottom: 1, left: 1, right: 1 },\n};\n\nconst isPaper = (v: unknown): v is PaperSizeName => typeof v === \"string\" && v in PAPER_SIZES;\nconst oneOf = <T extends string>(list: readonly T[], v: unknown, fallback: T): T => (typeof v === \"string\" && (list as readonly string[]).includes(v) ? (v as T) : fallback);\nconst inches = (v: unknown, fallback: number) => (typeof v === \"number\" && Number.isFinite(v) && v >= 0 && v <= 5 ? v : fallback);\n\n/** Any stored value → a complete, valid setup. Unknown or broken parts fall back to the default. */\nexport function normalizeSheetPageSetup(raw: unknown): SheetPageSetup {\n const r = (raw && typeof raw === \"object\" ? raw : {}) as Record<string, unknown>;\n const m = (r.customMargins && typeof r.customMargins === \"object\" ? r.customMargins : {}) as Record<string, unknown>;\n const d = DEFAULT_SHEET_PAGE_SETUP;\n return {\n paper: isPaper(r.paper) ? r.paper : d.paper,\n orientation: r.orientation === \"landscape\" ? \"landscape\" : \"portrait\",\n scale: oneOf(SHEET_SCALES, r.scale, d.scale),\n margins: oneOf(SHEET_MARGIN_PRESETS, r.margins, d.margins),\n customMargins: {\n top: inches(m.top, d.customMargins.top),\n bottom: inches(m.bottom, d.customMargins.bottom),\n left: inches(m.left, d.customMargins.left),\n right: inches(m.right, d.customMargins.right),\n },\n scope: oneOf(SHEET_PRINT_SCOPES, r.scope, d.scope),\n gridlines: typeof r.gridlines === \"boolean\" ? r.gridlines : d.gridlines,\n repeatFrozenRows: r.repeatFrozenRows === true,\n repeatFrozenColumns: r.repeatFrozenColumns === true,\n pageOrder: r.pageOrder === \"over-down\" ? \"over-down\" : \"down-over\",\n alignH: oneOf([\"left\", \"center\", \"right\"] as const, r.alignH, d.alignH),\n alignV: oneOf([\"top\", \"middle\", \"bottom\"] as const, r.alignV, d.alignV),\n };\n}\n\n/** The setup stored on a workbook's metadata (`metadata.page_setup`), complete and valid. */\nexport function pageSetupFromMetadata(metadata: unknown): SheetPageSetup {\n return normalizeSheetPageSetup((metadata as { page_setup?: unknown } | null)?.page_setup);\n}\n\nexport interface PrintGeometry {\n /** Paper as printed (orientation applied), px. */\n paperWidth: number;\n paperHeight: number;\n marginTop: number;\n marginBottom: number;\n marginLeft: number;\n marginRight: number;\n /** The printable box inside the margins, px. */\n areaWidth: number;\n areaHeight: number;\n}\n\nexport function printGeometry(setup: SheetPageSetup): PrintGeometry {\n const base = PAPER_SIZES[setup.paper];\n const landscape = setup.orientation === \"landscape\";\n const paperWidth = landscape ? base.height : base.width;\n const paperHeight = landscape ? base.width : base.height;\n const m = setup.margins === \"custom\" ? setup.customMargins : MARGIN_PRESET_INCHES[setup.margins];\n const px = (n: number) => Math.round(n * PX_PER_INCH);\n const geometry = {\n paperWidth,\n paperHeight,\n marginTop: px(m.top),\n marginBottom: px(m.bottom),\n marginLeft: px(m.left),\n marginRight: px(m.right),\n };\n return {\n ...geometry,\n areaWidth: Math.max(48, paperWidth - geometry.marginLeft - geometry.marginRight),\n areaHeight: Math.max(48, paperHeight - geometry.marginTop - geometry.marginBottom),\n };\n}\n\nexport const PRINT_ROW_HEIGHT = 22;\nconst MIN_COL = 48;\nconst MAX_COL = 320;\nconst CHAR_PX = 7;\n\n/** Column widths (px) fitted to the text the column holds. */\nexport function printColumnWidths(rows: readonly (readonly string[])[]): number[] {\n const width = rows.reduce((n, r) => Math.max(n, r.length), 0);\n const out: number[] = [];\n for (let c = 0; c < width; c += 1) {\n let longest = 0;\n for (const r of rows) longest = Math.max(longest, (r[c] ?? \"\").length);\n out.push(Math.min(MAX_COL, Math.max(MIN_COL, longest * CHAR_PX + 16)));\n }\n return out;\n}\n\nexport interface PrintPage {\n /** Grid row indexes drawn on this page, in order (repeated frozen rows first). */\n rows: number[];\n /** Grid column indexes drawn on this page, in order (repeated frozen columns first). */\n cols: number[];\n}\n\nexport interface PrintPlan {\n scale: number;\n geometry: PrintGeometry;\n colWidths: number[];\n pages: PrintPage[];\n}\n\n/** Splits `count` items into bands that fit `capacity(px)`, repeating `frozen` leading items on every band after the first. */\nfunction bands(sizes: readonly number[], capacityPx: number, frozen: number, repeat: boolean): number[][] {\n const total = sizes.length;\n if (total === 0) return [];\n const result: number[][] = [];\n const lead = repeat ? Math.min(frozen, total) : 0;\n const leadPx = sizes.slice(0, lead).reduce((a, b) => a + b, 0);\n let at = 0;\n while (at < total) {\n const first = result.length === 0;\n const carry = !first && lead > 0 ? Array.from({ length: lead }, (_, i) => i) : [];\n let used = first ? 0 : leadPx;\n const band = [...carry];\n // A repeated header never leaves a page with no room for a body item.\n if (!first && leadPx >= capacityPx) used = 0;\n let added = 0;\n while (at < total) {\n const size = sizes[at]!;\n if (used + size > capacityPx && added > 0) break;\n band.push(at);\n used += size;\n added += 1;\n at += 1;\n }\n result.push(band);\n }\n return result;\n}\n\n/** The pages one grid prints on under `setup`: scale, repeated frozen panes, and page order. */\nexport function planSheetPrint(\n rows: readonly (readonly string[])[],\n setup: SheetPageSetup,\n frozen: { rows: number; cols: number } = { rows: 0, cols: 0 },\n): PrintPlan {\n const geometry = printGeometry(setup);\n const colWidths = printColumnWidths(rows);\n const totalW = colWidths.reduce((a, b) => a + b, 0) || 1;\n const totalH = rows.length * PRINT_ROW_HEIGHT || 1;\n const fitW = geometry.areaWidth / totalW;\n const fitH = geometry.areaHeight / totalH;\n const scale =\n setup.scale === \"width\" ? Math.min(1, fitW) : setup.scale === \"height\" ? Math.min(1, fitH) : setup.scale === \"page\" ? Math.min(1, fitW, fitH) : 1;\n const rowBands = bands(\n rows.map(() => PRINT_ROW_HEIGHT),\n geometry.areaHeight / scale,\n frozen.rows,\n setup.repeatFrozenRows,\n );\n const colBands = bands(colWidths, geometry.areaWidth / scale, frozen.cols, setup.repeatFrozenColumns);\n const pages: PrintPage[] = [];\n if (setup.pageOrder === \"down-over\") {\n for (const cols of colBands) for (const r of rowBands) pages.push({ rows: r, cols });\n } else {\n for (const r of rowBands) for (const cols of colBands) pages.push({ rows: r, cols });\n }\n return { scale, geometry, colWidths, pages };\n}\n","/**\n * A workbook's print settings, read and written on the workbook itself:\n * `udt_workbooks.metadata.page_setup` (merged with `mergeJsonColumn`, like the table links beside it).\n */\nimport { mergeJsonColumn } from \"@ai-matrx/data/db\";\n\nimport { getSheetsHost } from \"./ports\";\nimport { normalizeSheetPageSetup, pageSetupFromMetadata, type SheetPageSetup } from \"./sheet-page-setup\";\n\nconst db = () => getSheetsHost().getSupabase();\n\ntype WorkbookMetaRow = { id: string; version: number; metadata: unknown };\n\nexport type PageSetupResult = { ok: true; data: SheetPageSetup } | { ok: false; error: string };\n\nexport async function readSheetPageSetup(workbookId: string): Promise<PageSetupResult> {\n const { data, error } = await db().schema(\"workbench\").from(\"udt_workbooks\").select(\"metadata\").eq(\"id\", workbookId).maybeSingle();\n if (error) return { ok: false, error: error.message };\n return { ok: true, data: pageSetupFromMetadata(data?.metadata) };\n}\n\nexport async function writeSheetPageSetup(workbookId: string, setup: SheetPageSetup): Promise<PageSetupResult> {\n const clean = normalizeSheetPageSetup(setup);\n const result = await mergeJsonColumn<WorkbookMetaRow>({\n fetchCurrent: () => db().schema(\"workbench\").from(\"udt_workbooks\").select(\"id, version, metadata\").eq(\"id\", workbookId).maybeSingle(),\n readColumn: (row) => row.metadata,\n merge: (current) => ({ ...current, page_setup: clean }),\n applyUpdate: ({ value, expectedVersion, nextVersion }) =>\n db()\n .schema(\"workbench\")\n .from(\"udt_workbooks\")\n .update({ metadata: value as never, version: nextVersion })\n .eq(\"id\", workbookId)\n .eq(\"version\", expectedVersion)\n .select(\"id, version, metadata\")\n .maybeSingle(),\n });\n if (result.status === \"saved\") return { ok: true, data: pageSetupFromMetadata(result.row.metadata) };\n if (result.status === \"not_found\") return { ok: false, error: \"This workbook could not be found\" };\n if (result.status === \"conflict\") return { ok: false, error: \"The workbook changed while saving; try again\" };\n const reason: unknown = result.error;\n return { ok: false, error: reason instanceof Error ? reason.message : typeof reason === \"string\" ? reason : \"Something went wrong\" };\n}\n","/**\n * The pages a workbook prints on: the snapshot's cells → paper-sized HTML pages that follow the\n * workbook's print settings (`sheet-page-setup.ts`). Print and Download as PDF both draw these\n * same pages, so what the browser prints is what the PDF holds.\n */\n\nimport { PX_PER_INCH } from \"../core\";\nimport {\n PRINT_ROW_HEIGHT,\n planSheetPrint,\n type PrintGeometry,\n type SheetPageSetup,\n} from \"./sheet-page-setup\";\nimport { univerSheetIds, univerSnapshotToRows, type CellRange } from \"./univer-snapshot-rows\";\n\nexport interface PrintSheet {\n name: string;\n rows: string[][];\n frozenRows: number;\n frozenCols: number;\n}\n\n/** The sheets the setup's \"what to print\" asks for (empty sheets left out). */\nexport function collectPrintSheets(\n snapshot: unknown,\n setup: SheetPageSetup,\n options: { sheetId?: string; selection?: CellRange | null },\n): PrintSheet[] {\n const read = (sheetId: string | undefined, range?: CellRange): PrintSheet | null => {\n const grid = univerSnapshotToRows(snapshot, { skipHidden: true, ...(sheetId ? { sheetId } : {}), ...(range ? { range } : {}) });\n if (!grid || grid.rows.length === 0) return null;\n return { name: grid.sheetName, rows: grid.rows, frozenRows: grid.frozenRows, frozenCols: grid.frozenCols };\n };\n if (setup.scope === \"workbook\") {\n return univerSheetIds(snapshot)\n .map((id) => read(id))\n .filter((s): s is PrintSheet => s !== null);\n }\n if (setup.scope === \"selection\") {\n const sel = options.selection;\n if (!sel || (sel.startRow === sel.endRow && sel.startColumn === sel.endColumn)) return [];\n const one = read(options.sheetId, sel);\n return one ? [one] : [];\n }\n const one = read(options.sheetId);\n return one ? [one] : [];\n}\n\nconst esc = (s: string) => s.replace(/&/g, \"&amp;\").replace(/</g, \"&lt;\").replace(/>/g, \"&gt;\").replace(/\"/g, \"&quot;\");\n\nconst JUSTIFY = { left: \"flex-start\", center: \"center\", right: \"flex-end\" } as const;\nconst ALIGN = { top: \"flex-start\", middle: \"center\", bottom: \"flex-end\" } as const;\n\nfunction pageHtml(\n sheet: PrintSheet,\n page: { rows: number[]; cols: number[] },\n scale: number,\n colWidths: number[],\n g: PrintGeometry,\n setup: SheetPageSetup,\n numbering: { sheet: string; at: number; of: number },\n): string {\n const widths = page.cols.map((c) => colWidths[c] ?? 0);\n const tableW = widths.reduce((a, b) => a + b, 0);\n const tableH = page.rows.length * PRINT_ROW_HEIGHT;\n const border = setup.gridlines ? \"1px solid #b7b7b7\" : \"none\";\n const body = page.rows\n .map(\n (r) =>\n `<tr style=\"height:${PRINT_ROW_HEIGHT}px\">${page.cols\n .map((c) => `<td style=\"border:${border}\">${esc(sheet.rows[r]?.[c] ?? \"\")}</td>`)\n .join(\"\")}</tr>`,\n )\n .join(\"\");\n const cols = widths.map((w) => `<col style=\"width:${w}px\">`).join(\"\");\n return `<section data-print-page data-sheet=\"${esc(numbering.sheet)}\" data-page-number=\"${numbering.at}\" data-page-count=\"${numbering.of}\" data-page-rows=\"${page.rows.length}\" data-page-cols=\"${page.cols.length}\" style=\"width:${g.paperWidth}px;height:${g.paperHeight}px;box-sizing:border-box;padding:${g.marginTop}px ${g.marginRight}px ${g.marginBottom}px ${g.marginLeft}px;background:#fff;overflow:hidden;break-after:page;page-break-after:always\">\n<div style=\"width:${g.areaWidth}px;height:${g.areaHeight}px;display:flex;justify-content:${JUSTIFY[setup.alignH]};align-items:${ALIGN[setup.alignV]};overflow:hidden\">\n<div data-print-content style=\"width:${tableW * scale}px;height:${tableH * scale}px;flex:none;overflow:hidden\">\n<table style=\"width:${tableW}px;table-layout:fixed;border-collapse:collapse;transform:scale(${scale});transform-origin:0 0;font:12px/${PRINT_ROW_HEIGHT - 2}px system-ui,sans-serif;color:#111\"><colgroup>${cols}</colgroup><tbody>${body}</tbody></table>\n</div></div></section>`;\n}\n\nexport interface PrintDocument {\n /** Every page, in print order. */\n pagesHtml: string[];\n pageWidth: number;\n pageHeight: number;\n /** `@page` size in inches, for the browser's print engine. */\n pageSizeInches: { width: number; height: number };\n}\n\n/** All the printed pages of the chosen sheets under the setup (empty when there is nothing to print). */\nexport function buildPrintDocument(sheets: readonly PrintSheet[], setup: SheetPageSetup): PrintDocument | null {\n const out: string[] = [];\n let geometry: PrintGeometry | null = null;\n for (const sheet of sheets) {\n const plan = planSheetPrint(sheet.rows, setup, { rows: sheet.frozenRows, cols: sheet.frozenCols });\n geometry = plan.geometry;\n plan.pages.forEach((page, i) => {\n out.push(pageHtml(sheet, page, plan.scale, plan.colWidths, plan.geometry, setup, { sheet: sheet.name, at: i + 1, of: plan.pages.length }));\n });\n }\n if (!geometry || out.length === 0) return null;\n return {\n pagesHtml: out,\n pageWidth: geometry.paperWidth,\n pageHeight: geometry.paperHeight,\n pageSizeInches: { width: geometry.paperWidth / PX_PER_INCH, height: geometry.paperHeight / PX_PER_INCH },\n };\n}\n\n/** The whole printable web page: `@page` set to the paper, one section per page. */\nexport function printDocumentHtml(doc: PrintDocument, title: string): string {\n const { width, height } = doc.pageSizeInches;\n return `<!doctype html><html><head><meta charset=\"utf-8\"><title>${esc(title)}</title>\n<style>@page{size:${width.toFixed(3)}in ${height.toFixed(3)}in;margin:0}html,body{margin:0;padding:0;background:#fff}td{padding:0 6px;overflow:hidden;white-space:nowrap;text-overflow:clip;vertical-align:middle}section:last-child{break-after:auto;page-break-after:auto}</style></head>\n<body data-print-paper=\"${width.toFixed(3)}x${height.toFixed(3)}in\">${doc.pagesHtml.join(\"\\n\")}</body></html>`;\n}\n","/**\n * Outputs of a workbook: CSV/TSV/HTML downloads of the current sheet, and Print / PDF of what its page setup asks for.\n * Both read the same string grid the rest of the workbook code reads\n * (`univerSnapshotToRows`) — one source of what \"the sheet\" contains.\n */\n\nimport { downloadFile } from \"@ai-matrx/kit/download\";\nimport { toDelimitedText } from \"@ai-matrx/alchemy/operate/read\";\nimport { PX_PER_INCH } from \"../core\";\nimport type { SheetPageSetup } from \"./sheet-page-setup\";\nimport { buildPrintDocument, collectPrintSheets, printDocumentHtml } from \"./sheet-print-pages\";\nimport { univerSnapshotToRows, type CellRange } from \"./univer-snapshot-rows\";\n\nexport function rowsToCsv(rows: readonly (readonly string[])[]): string {\n const [header, ...body] = rows;\n if (!header) return \"\";\n return toDelimitedText(header, body, { spreadsheetSafe: false });\n}\n\nexport function downloadSheetAsCsv(\n snapshot: unknown,\n options: { sheetId?: string; filename: string },\n): boolean {\n const grid = univerSnapshotToRows(snapshot, { skipHidden: true, ...(options.sheetId ? { sheetId: options.sheetId } : {}) });\n if (!grid || grid.rows.length === 0) return false;\n const name = `${options.filename.replace(/\\.csv$/i, \"\")}.csv`;\n // BOM so Excel reads UTF-8.\n const blob = new Blob([\"\", rowsToCsv(grid.rows)], { type: \"text/csv;charset=utf-8\" });\n downloadFile(name, blob, blob.type);\n return true;\n}\n\n/** Tab-separated values of the current sheet (Google's .tsv download). */\nexport function downloadSheetAsTsv(\n snapshot: unknown,\n options: { sheetId?: string; filename: string },\n): boolean {\n const grid = univerSnapshotToRows(snapshot, { skipHidden: true, ...(options.sheetId ? { sheetId: options.sheetId } : {}) });\n if (!grid || grid.rows.length === 0) return false;\n const [header, ...body] = grid.rows;\n const text = toDelimitedText(header!, body, { format: \"tsv\", spreadsheetSafe: false });\n const blob = new Blob([\"\\uFEFF\", text], { type: \"text/tab-separated-values;charset=utf-8\" });\n downloadFile(`${options.filename.replace(/\\.tsv$/i, \"\")}.tsv`, blob, blob.type);\n return true;\n}\n\n/** The current sheet as a standalone web page (Google's .html download). */\nexport function downloadSheetAsHtml(\n snapshot: unknown,\n options: { sheetId?: string; filename: string },\n): boolean {\n const grid = univerSnapshotToRows(snapshot, { skipHidden: true, ...(options.sheetId ? { sheetId: options.sheetId } : {}) });\n if (!grid || grid.rows.length === 0) return false;\n const body = grid.rows.map((r) => `<tr>${r.map((c) => `<td>${esc(c)}</td>`).join(\"\")}</tr>`).join(\"\\n\");\n const html = `<!doctype html><html><head><meta charset=\"utf-8\"><title>${esc(options.filename)} — ${esc(grid.sheetName)}</title>\n<style>body{font:13px system-ui,sans-serif;margin:16px}table{border-collapse:collapse}td{border:1px solid #c0c0c0;padding:2px 6px;vertical-align:top}</style></head>\n<body><table>\n${body}\n</table></body></html>`;\n const blob = new Blob([html], { type: \"text/html;charset=utf-8\" });\n downloadFile(`${options.filename.replace(/\\.html?$/i, \"\")}.html`, blob, blob.type);\n return true;\n}\n\nfunction esc(s: string): string {\n return s.replace(/&/g, \"&amp;\").replace(/</g, \"&lt;\").replace(/>/g, \"&gt;\");\n}\n\n/** What a print or PDF needs besides the snapshot: the workbook's page setup and the live selection. */\nexport interface SheetPrintOptions {\n sheetId?: string;\n title: string;\n setup: SheetPageSetup;\n /** The cells selected right now (used when the setup prints \"selected cells\"). */\n selection?: CellRange | null;\n}\n\nfunction printDocumentFor(snapshot: unknown, options: SheetPrintOptions) {\n const sheets = collectPrintSheets(snapshot, options.setup, {\n ...(options.sheetId ? { sheetId: options.sheetId } : {}),\n selection: options.selection ?? null,\n });\n return buildPrintDocument(sheets, options.setup);\n}\n\n/** Opens the browser print sheet for what the workbook's page setup asks to print (choose \"Save as PDF\" there). */\nexport function printSheet(snapshot: unknown, options: SheetPrintOptions): boolean {\n const doc = printDocumentFor(snapshot, options);\n if (!doc) return false;\n const html = printDocumentHtml(doc, options.title);\n const frame = document.createElement(\"iframe\");\n frame.style.cssText = \"position:fixed;right:0;bottom:0;width:0;height:0;border:0\";\n document.body.appendChild(frame);\n const fdoc = frame.contentDocument;\n const win = frame.contentWindow;\n if (!fdoc || !win) {\n frame.remove();\n return false;\n }\n fdoc.open();\n fdoc.write(html);\n fdoc.close();\n win.addEventListener(\"afterprint\", () => frame.remove());\n win.focus();\n win.print();\n return true;\n}\n\n/** The sheet's grid as a markdown table (first row is the header row). */\nexport function rowsToMarkdownTable(rows: readonly (readonly string[])[]): string {\n if (rows.length === 0) return \"\";\n const width = Math.max(...rows.map((r) => r.length));\n const cell = (v: string | undefined) => (v ?? \"\").replace(/\\|/g, \"\\\\|\").replace(/\\r?\\n/g, \" \");\n const line = (r: readonly string[]) =>\n `| ${Array.from({ length: width }, (_, i) => cell(r[i])).join(\" | \")} |`;\n const [head, ...rest] = rows;\n return [line(head!), `| ${Array.from({ length: width }, () => \"---\").join(\" | \")} |`, ...rest.map(line)].join(\"\\n\");\n}\n\n/**\n * Real PDF download of what the workbook's page setup prints: the same pages Print draws, each one\n * captured and laid on a PDF page of the chosen paper size and orientation. Returns false when empty.\n */\nexport async function downloadSheetAsPdf(snapshot: unknown, options: SheetPrintOptions): Promise<boolean> {\n const doc = printDocumentFor(snapshot, options);\n if (!doc) return false;\n const [{ renderElement, pagesToPdf }, { downloadFile: save }] = await Promise.all([\n import(\"@ai-matrx/alchemy/operate/capture\"),\n import(\"@ai-matrx/kit/download\"),\n ]);\n const host = document.createElement(\"div\");\n host.setAttribute(\"aria-hidden\", \"true\");\n host.style.cssText = \"position:fixed;left:-20000px;top:0\";\n host.innerHTML = `<style>td{padding:0 6px;overflow:hidden;white-space:nowrap;vertical-align:middle}</style>${doc.pagesHtml.join(\"\")}`;\n document.body.appendChild(host);\n try {\n const pages = [];\n for (const el of Array.from(host.querySelectorAll<HTMLElement>(\"[data-print-page]\"))) {\n const image = await renderElement(el, { pixelRatio: 2, backgroundColor: \"#ffffff\", cacheBust: true });\n pages.push({ image, imageType: \"JPEG\" as const, quality: 0.92, placement: \"fill\" as const });\n }\n const mm = (px: number) => Math.round(((px / PX_PER_INCH) * 25.4) * 100) / 100;\n const pdf = await pagesToPdf(pages, {\n orientation: doc.pageWidth > doc.pageHeight ? \"landscape\" : \"portrait\",\n unit: \"mm\",\n format: [mm(Math.min(doc.pageWidth, doc.pageHeight)), mm(Math.max(doc.pageWidth, doc.pageHeight))],\n });\n save(`${options.title.replace(/[\\\\/:*?\"<>|]+/g, \" \").trim() || \"workbook\"}.pdf`, pdf, \"application/pdf\");\n } finally {\n host.remove();\n }\n return true;\n}\n","// features/workbooks/workbook-scope-source.ts\n//\n// Live bridge from the mounted `WorkbookEditor` to the `matrx-user/workbooks`\n// surface emitter on `/workbooks/[id]`.\n//\n// Why a module slot rather than props: the page owns the workbook row and\n// mounts `<SurfaceRuntimeProvider>`, but the sheets / snapshot / save status\n// live inside the editor (Univer refs + local state), one level DOWN. Pushing\n// them up as state would re-render the editor on every keystroke; a\n// registered getter is read once, at Run time, when the user actually\n// launches an agent.\n//\n// Exactly one editor is mounted at a time (one workbook per route), so a\n// single slot keyed by workbookId is enough. The getter returns null when the\n// editor has not booted — callers omit the values, they never fake them.\n\nexport interface WorkbookLiveScope {\n sheets: Array<{ id: string; name: string; index: number }>;\n activeSheetId: string | null;\n activeSheetName: string | null;\n snapshot: Record<string, unknown> | null;\n bootState: \"booting\" | \"ready\" | \"load_error\";\n loadError: string | null;\n saveStatus: string;\n collab: {\n enabled: boolean;\n is_host: boolean;\n self_uid: string;\n remote_peers: number;\n };\n}\n\ninterface Registration {\n workbookId: string;\n read: () => WorkbookLiveScope | null;\n}\n\nlet current: Registration | null = null;\n\n/** Called by `WorkbookEditor` while mounted. Returns an unregister. */\nexport function registerWorkbookScopeSource(\n registration: Registration,\n): () => void {\n current = registration;\n return () => {\n if (current === registration) current = null;\n };\n}\n\n/**\n * Read the live editor state for `workbookId`. Returns null when no editor is\n * mounted, when a different workbook is mounted, or when Univer has not\n * booted yet — never a fabricated shape.\n */\nexport function readWorkbookScopeSource(\n workbookId: string,\n): WorkbookLiveScope | null {\n if (!current || current.workbookId !== workbookId) return null;\n return current.read();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAqDO,SAAS,eAAe,UAA6B;AAC1D,MAAI,OAAO,aAAa,YAAY,aAAa,KAAM,QAAO,CAAC;AAC/D,QAAM,QAAQ;AACd,QAAM,SAAS,MAAM,UAAU,CAAC;AAChC,QAAM,WAAW,MAAM,cAAc,CAAC,GAAG,OAAO,CAAC,OAAO,OAAO,EAAE,CAAC;AAClE,SAAO,QAAQ,SAAS,UAAU,OAAO,KAAK,MAAM;AACtD;AAKA,SAAS,UAAU,MAAoC,QAA+B;AACpF,QAAM,IAAI,MAAM;AAChB,QAAM,QAAQ,OAAO,MAAM,WAAW,SAAS,CAAC,IAAI,KAAK,OAAO,MAAM,WAAY,IAAqC;AACvH,QAAM,UAAU,OAAO,GAAG;AAC1B,SAAO,OAAO,YAAY,WAAW,UAAU;AACjD;AAEA,IAAM,SAAS;AACf,IAAM,kBAAkB,KAAK,IAAI,MAAM,IAAI,EAAE;AAE7C,SAAS,SAAS,MAAoC,QAAyB;AAC7E,QAAM,QAAQ,MAAM;AACpB,MAAI,UAAU,QAAQ,UAAU,OAAW,QAAO;AAClD,MAAI,OAAO,UAAU,UAAU;AAC7B,UAAM,UAAU,UAAU,MAAM,MAAM,GAAG,QAAQ,YAAY,EAAE,EAAE,YAAY,KAAK;AAClF,QAAI,OAAO,KAAK,OAAO,GAAG;AACxB,YAAM,MAAM,IAAI,KAAK,KAAK,MAAM,kBAAkB,QAAQ,MAAM,CAAC,EAAE,YAAY;AAC/E,aAAO,IAAI,KAAK,OAAO,IAAI,GAAG,IAAI,MAAM,GAAG,EAAE,CAAC,IAAI,IAAI,MAAM,IAAI,EAAE,CAAC,KAAK,IAAI,MAAM,GAAG,EAAE;AAAA,IACzF;AAAA,EACF;AACA,SAAO,OAAO,KAAK;AACrB;AAQO,SAAS,qBAAqB,UAAmB,U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c","esc"]}
@@ -32,8 +32,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
32
32
  var collab_exports = {};
33
33
  __export(collab_exports, {
34
34
  WorkbookCollabSession: () => WorkbookCollabSession,
35
- isCellEditorScratch: () => isCellEditorScratch,
36
- useWorkbookRealtime: () => useWorkbookRealtime
35
+ isCellEditorScratch: () => isCellEditorScratch
37
36
  });
38
37
  module.exports = __toCommonJS(collab_exports);
39
38
 
@@ -235,7 +234,7 @@ var WorkbookCollabSession = class {
235
234
  // ─── internals ────────────────────────────────────────────────────────
236
235
  handleLocalMutation(info) {
237
236
  if (this.muted > 0) return;
238
- if (isCellEditorScratch(info)) return;
237
+ if (!this.options.forwardDocumentMutations && isCellEditorScratch(info)) return;
239
238
  if (info.params && typeof info.params === "object" && !Array.isArray(info.params) && (0, import_collab.remoteOriginOf)(info.params) !== null) {
240
239
  return;
241
240
  }
@@ -300,79 +299,4 @@ function pickColor(uid) {
300
299
  }
301
300
  return CURSOR_PALETTE[Math.abs(h) % CURSOR_PALETTE.length] ?? "#ec4899";
302
301
  }
303
-
304
- // src/univer/collab/useWorkbookRealtime.ts
305
- var import_react = require("react");
306
- var import_realtime = require("@ai-matrx/data/realtime");
307
- var import_react2 = require("@ai-matrx/data/realtime/react");
308
-
309
- // src/univer/ports.ts
310
- var SLOT = /* @__PURE__ */ Symbol.for("ai-matrx.office.univer-host");
311
- var slot = () => {
312
- const g = globalThis;
313
- return g[SLOT] ??= {};
314
- };
315
- function getUniverHost() {
316
- const ports = slot().ports;
317
- if (!ports) throw new Error("@ai-matrx/office/univer: configureUniverHost(ports) was not called before an editor mounted.");
318
- return ports;
319
- }
320
-
321
- // src/univer/collab/useWorkbookRealtime.ts
322
- var workbookSnapshotsChannel = (0, import_realtime.defineChannelNamespace)({
323
- namespace: "udt-workbook-snapshots",
324
- parts: ["workbookId"],
325
- description: "workbench.udt_workbook_snapshots inserts for one workbook"
326
- });
327
- function useWorkbookRealtime(workbookId, onSnapshot, options) {
328
- const enabled = options?.enabled ?? true;
329
- const emit = (0, import_react.useCallback)(
330
- (row) => {
331
- if (!row?.id) return;
332
- onSnapshot({
333
- snapshotId: row.id,
334
- // component-created-by-ok: append-only snapshot with NO updated_by column and NO parent-rewrite trigger — created_by is the author the client wrote at insert (the only person field this table has)
335
- createdBy: row.created_by ?? null,
336
- createdAt: row.created_at ?? (/* @__PURE__ */ new Date()).toISOString()
337
- });
338
- },
339
- [onSnapshot]
340
- );
341
- (0, import_react2.useChannel)(
342
- enabled && workbookId ? {
343
- topic: workbookSnapshotsChannel.topic({ workbookId }),
344
- postgresChanges: [
345
- {
346
- event: "INSERT",
347
- schema: "workbench",
348
- table: "udt_workbook_snapshots",
349
- filter: `workbook_id=eq.${workbookId}`,
350
- rowId: (row) => typeof row.id === "string" ? row.id : void 0,
351
- onChange: ({ row }) => emit(row)
352
- }
353
- ],
354
- // Realtime has no replay. Re-read the newest snapshot so a reconnect,
355
- // tab wake or network restore cannot leave this client checkpointing
356
- // on top of a base that moved while it was away.
357
- onBackfill: async () => {
358
- let data;
359
- let error;
360
- try {
361
- ({ data, error } = await getUniverHost().readNewestWorkbookSnapshot(workbookId));
362
- } catch (e) {
363
- data = null;
364
- error = { message: e instanceof Error ? e.message : String(e) };
365
- }
366
- if (error) {
367
- console.warn(
368
- "[udt-workbook RT] catch-up read failed \u2014 this workbook may be working from a stale snapshot base; reload before saving.",
369
- error.message
370
- );
371
- return;
372
- }
373
- emit(data);
374
- }
375
- } : null
376
- );
377
- }
378
302
  //# sourceMappingURL=univer-collab.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/univer/collab/index.ts","../src/univer/collab/WorkbookCollabSession.ts","../src/univer/collab/useWorkbookRealtime.ts","../src/univer/ports.ts"],"sourcesContent":["/**\n * The collab layer shared by `/sheets` and `/docs` (was matrx-frontend `features/workbooks/collab` and\n * `hooks/useWorkbookRealtime`): the Univer ↔ Yjs session and the snapshot-insert subscription. A\n * transitional subpath, like `./univer`: it goes internal when the editors move into their sections.\n */\nexport * from \"./WorkbookCollabSession\";\nexport * from \"./useWorkbookRealtime\";\n","/**\n * WorkbookCollabSession — the Univer ↔ Yjs bridge.\n *\n * Subscribes to Univer's public `commandService.onMutationExecutedForCollab`\n * hook for local mutations and pushes them onto a `Y.Array<MutationOp>` shared\n * via `SupabaseYjsProvider`. Inbound Yjs updates produce `IMutationInfo` ops\n * that are applied through `commandService.syncExecuteCommand` with the\n * `fromCollab` execution option flagged so the outbound listener short-\n * circuits and we don't echo our own apply.\n *\n * See `FEATURE.md` in this directory for the architecture, the \"do NOT use\n * the Pro preset\" verdict, and the host-election rule. This class is the\n * runtime side of that plan.\n *\n * Lifecycle:\n * - constructor: wire references, no side effects\n * - start(): create Y.Doc + Awareness, instantiate provider, subscribe to\n * command stream, subscribe to Yjs Array observer\n * - stop(): detach all listeners, dispose provider\n *\n * The session is one-shot per workbook open. Reload = stop() + new instance.\n */\n\"use client\";\n\nimport * as Y from \"yjs\";\nimport { Awareness } from \"y-protocols/awareness\";\n\nimport { electCollabHost, remoteOriginOf, REMOTE_PARAM_TAG } from \"@ai-matrx/data/realtime/collab\";\n\n/**\n * Structural transport contract. `SupabaseYjsProvider` satisfies it in\n * production; the verification harness injects an in-memory loopback\n * transport through the same seam. Keeping this structural (not the concrete\n * class) is what makes the session testable without network.\n */\nexport type CollabProviderLike = {\n connect(): Promise<void>;\n disconnect(): void;\n ready(): Promise<void>;\n /**\n * True when this is the only holder of its room in this tab allowed to act as\n * host (autosave). Same-tab holders share one client id and one uid, so\n * awareness cannot tell them apart. Absent = a lone holder = leader.\n */\n isTabLeader?(): boolean;\n};\n\n// Univer types are loose at the public hook boundary; we narrow with a\n// structural interface so we don't have to import the full Univer surface.\ntype MutationParams = Record<string, unknown> | unknown[] | null;\n\nexport type CollabMutationInfo = {\n id: string;\n type: number; // CommandType.MUTATION = 2 in Univer's enum\n params: MutationParams;\n /** Who made it (the person's uid), so a peer can tell its own other tab's edit from a stranger's. */\n uid?: string;\n};\n\ntype ExecuteOptions = {\n /** Per-Univer convention: when true, the command runs locally only and is\n * NOT broadcast to other plugins. We set this on inbound-apply so the\n * subsequent onMutationExecutedForCollab does NOT re-fire on our peer. */\n onlyLocal?: boolean;\n fromCollab?: boolean;\n};\n\nexport type CommandServiceLike = {\n onMutationExecutedForCollab(\n listener: (info: CollabMutationInfo, options?: ExecuteOptions) => void,\n ): {\n dispose: () => void;\n };\n syncExecuteCommand(\n id: string,\n params: MutationParams,\n options?: ExecuteOptions,\n ): unknown;\n};\n\nexport type WorkbookCollabSessionOptions = {\n workbookId: string;\n uid: string;\n clientId: string;\n commandService: CommandServiceLike;\n /** Factory so this file does not statically depend on the provider. */\n makeProvider: (args: {\n workbookId: string;\n clientId: string;\n doc: Y.Doc;\n awareness: Awareness;\n /** Wire to the provider so a host handover re-runs the election. */\n onLeaderChange: () => void;\n }) => CollabProviderLike;\n /** Called when the awareness set changes (peers join / leave / move cursor). */\n onAwarenessChange?: (awareness: Awareness) => void;\n /**\n * A peer replaced the whole content (history restore, Google update): the\n * newest saved snapshot is the content now. Reload it in place; never\n * re-announce. See `announceReplaced`.\n */\n onReplaced?: (snapshotId: string) => void;\n};\n\n/** What `announceReplaced` writes into the room's control map. */\ntype ReplacedSignal = { snapshotId: string; by: string; nonce: string };\n\nconst MUTATIONS_ARRAY_KEY = \"mutations\";\nconst CONTROL_MAP_KEY = \"control\";\nconst REPLACED_KEY = \"replaced\";\n\nexport class WorkbookCollabSession {\n private readonly options: WorkbookCollabSessionOptions;\n private doc: Y.Doc | null = null;\n private awareness: Awareness | null = null;\n private provider: CollabProviderLike | null = null;\n private commandDisposer: { dispose: () => void } | null = null;\n private yArrayObserver:\n ((event: Y.YArrayEvent<CollabMutationInfo>) => void) | null = null;\n private awarenessObserver: (() => void) | null = null;\n private controlObserver: ((event: Y.YMapEvent<unknown>) => void) | null = null;\n /** >0 while this client swaps its own content: its mutations are not edits. */\n private muted = 0;\n private started = false;\n private disposed = false;\n\n constructor(options: WorkbookCollabSessionOptions) {\n this.options = options;\n }\n\n async start(): Promise<void> {\n if (this.started || this.disposed) return;\n this.started = true;\n\n this.doc = new Y.Doc();\n this.awareness = new Awareness(this.doc);\n\n // Seed local awareness with the user identity. Cursor (sheetId/row/col)\n // is set later via setCursor().\n this.awareness.setLocalState({\n uid: this.options.uid,\n clientId: this.options.clientId,\n name: this.options.uid.slice(0, 8),\n color: pickColor(this.options.uid),\n sheetId: null,\n row: null,\n col: null,\n ts: Date.now(),\n });\n\n this.provider = this.options.makeProvider({\n workbookId: this.options.workbookId,\n clientId: this.options.clientId,\n doc: this.doc,\n awareness: this.awareness,\n onLeaderChange: () => {\n if (this.awareness) this.options.onAwarenessChange?.(this.awareness);\n },\n });\n\n // Subscribe to inbound Yjs array changes BEFORE connecting the provider,\n // so the initial-state replay (if any) hits our observer.\n const yArray = this.doc.getArray<CollabMutationInfo>(MUTATIONS_ARRAY_KEY);\n this.yArrayObserver = (event) => {\n // Local transactions are OUR OWN pushes from handleLocalMutation —\n // Univer already executed those mutations. Re-applying them here would\n // double-apply every local edit on the originator. Only act on\n // transactions that arrived via Y.applyUpdate (origin 'remote').\n if (event.transaction.local) return;\n for (const change of event.changes.delta) {\n if (!change.insert || !Array.isArray(change.insert)) continue;\n for (const op of change.insert as CollabMutationInfo[]) {\n this.applyRemoteMutation(op);\n }\n }\n };\n yArray.observe(this.yArrayObserver as Parameters<typeof yArray.observe>[0]);\n\n // Awareness change passthrough.\n if (this.options.onAwarenessChange) {\n const cb = this.options.onAwarenessChange;\n this.awarenessObserver = () => {\n if (this.awareness) cb(this.awareness);\n };\n this.awareness.on(\"change\", this.awarenessObserver);\n }\n\n // Subscribe to Univer's collab-grain mutation stream.\n this.commandDisposer =\n this.options.commandService.onMutationExecutedForCollab((info) =>\n this.handleLocalMutation(info),\n );\n\n await this.provider.connect();\n await this.provider.ready();\n\n // Whole-content replacement from a peer. Bound AFTER the initial sync: a\n // joiner already loaded the newest snapshot, so the signal it finds in\n // the room's state is history, not news (the editor also skips a\n // snapshot it already shows).\n if (this.disposed || !this.doc) return;\n const control = this.doc.getMap<unknown>(CONTROL_MAP_KEY);\n const seen = (control.get(REPLACED_KEY) as ReplacedSignal | undefined)?.nonce ?? null;\n this.controlObserver = (event) => {\n if (event.transaction.local || !event.keysChanged.has(REPLACED_KEY)) return;\n const signal = control.get(REPLACED_KEY) as ReplacedSignal | undefined;\n if (!signal?.snapshotId || signal.nonce === seen) return;\n this.options.onReplaced?.(signal.snapshotId);\n };\n control.observe(this.controlObserver);\n }\n\n stop(): void {\n if (this.disposed) return;\n this.disposed = true;\n\n this.commandDisposer?.dispose();\n this.commandDisposer = null;\n\n if (this.doc && this.yArrayObserver) {\n const yArray = this.doc.getArray<CollabMutationInfo>(MUTATIONS_ARRAY_KEY);\n yArray.unobserve(\n this.yArrayObserver as Parameters<typeof yArray.unobserve>[0],\n );\n this.yArrayObserver = null;\n }\n\n if (this.doc && this.controlObserver) {\n this.doc.getMap<unknown>(CONTROL_MAP_KEY).unobserve(this.controlObserver);\n this.controlObserver = null;\n }\n\n if (this.awareness && this.awarenessObserver) {\n this.awareness.off(\"change\", this.awarenessObserver);\n this.awarenessObserver = null;\n }\n\n this.provider?.disconnect();\n this.provider = null;\n\n if (this.awareness) {\n this.awareness.destroy();\n this.awareness = null;\n }\n\n if (this.doc) {\n this.doc.destroy();\n this.doc = null;\n }\n }\n\n /**\n * How many mutations the shared log holds right now. Read it BEFORE\n * `workbook.save()`: that many entries are guaranteed to be inside the\n * snapshot that save produces.\n */\n logLength(): number {\n return this.doc?.getArray<CollabMutationInfo>(MUTATIONS_ARRAY_KEY).length ?? 0;\n }\n\n /**\n * Drop the first `upTo` mutations from the shared log once a snapshot that\n * contains them has been SAVED. Without this a joiner loads the saved\n * workbook (which already has every mutation baked in) and then catches up\n * the whole log on top of it, replaying edits into a workbook that already\n * holds them — Univer refuses with \"Trying to overwrite value at key\"\n * (feedback 88184e53). Only the host calls this; peers receive it as a\n * delete, which the inbound observer ignores (it acts on inserts only).\n */\n compactLog(upTo: number): void {\n if (!this.doc || upTo <= 0) return;\n const yArray = this.doc.getArray<CollabMutationInfo>(MUTATIONS_ARRAY_KEY);\n const n = Math.min(upTo, yArray.length);\n if (n > 0) this.doc.transact(() => yArray.delete(0, n));\n }\n\n /**\n * This client replaced the whole content and SAVED it as `snapshotId`.\n * One transaction empties the shared mutation log (every entry targets the\n * content that is gone — a joiner must never replay them onto the new\n * snapshot) and tells every peer to reload the newest snapshot.\n */\n announceReplaced(snapshotId: string): void {\n if (!this.doc) return;\n const yArray = this.doc.getArray<CollabMutationInfo>(MUTATIONS_ARRAY_KEY);\n const control = this.doc.getMap<unknown>(CONTROL_MAP_KEY);\n const signal: ReplacedSignal = {\n snapshotId,\n by: this.options.clientId,\n nonce: `${this.options.clientId}:${Date.now()}:${Math.random().toString(36).slice(2, 8)}`,\n };\n this.doc.transact(() => {\n if (yArray.length > 0) yArray.delete(0, yArray.length);\n control.set(REPLACED_KEY, signal);\n });\n }\n\n /**\n * Run `swap` (this client tearing its content down and mounting the new\n * one) without broadcasting what Univer emits while it does: those are not\n * edits, and peers reload the same snapshot themselves.\n */\n muteWhile<T>(swap: () => T): T {\n this.muted += 1;\n try {\n return swap();\n } finally {\n this.muted -= 1;\n }\n }\n\n /**\n * Update the local awareness cursor. Call this from Univer's selection\n * listener (`worksheet.onSelectionChange(...)`). Throttle at the call site\n * (~50ms is plenty); the provider broadcasts on its own debounced cycle.\n */\n setCursor(input: {\n sheetId: string | null;\n row: number | null;\n col: number | null;\n }): void {\n if (!this.awareness) return;\n const current = this.awareness.getLocalState() ?? {};\n this.awareness.setLocalState({\n ...current,\n sheetId: input.sheetId,\n row: input.row,\n col: input.col,\n ts: Date.now(),\n });\n }\n\n /**\n * Returns the deterministic host: the connected peer with the lowest uid\n * (lexicographic). Host is the only client that writes canonical snapshots.\n * No election protocol; recompute whenever awareness changes.\n */\n electHost(opts: { tabTieBreak?: boolean } = {}): { isHost: boolean; hostUid: string | null } {\n if (!this.awareness) return { isHost: false, hostUid: null };\n const states = Array.from(this.awareness.getStates().values()) as Array<\n Record<string, unknown>\n >;\n // Lowest uid wins ACROSS tabs; inside this tab only the room's tab leader\n // may be host, because every same-tab holder carries the same uid.\n const leader = this.provider?.isTabLeader?.() ?? true;\n return electCollabHost(states, this.options.uid, this.options.clientId, leader, opts.tabTieBreak ?? false);\n }\n\n // ─── internals ────────────────────────────────────────────────────────\n\n private handleLocalMutation(info: CollabMutationInfo): void {\n if (this.muted > 0) return;\n if (isCellEditorScratch(info)) return;\n // Inbound-applied mutations re-fire this listener because Univer's\n // syncExecuteCommand legitimately processes them through the full\n // pipeline. Skip by sniffing the sentinel we stamped on apply.\n if (\n info.params &&\n typeof info.params === \"object\" &&\n !Array.isArray(info.params) &&\n remoteOriginOf(info.params) !== null\n ) {\n return;\n }\n if (!this.doc) return;\n\n // Normalize params through a JSON round-trip BEFORE pushing into Yjs.\n // Yjs (and the Broadcast transport) only carry JSON-encodable values;\n // Univer types its mutation params as \"serializable\" but JS objects can\n // smuggle Date / Map / class instances through that promise. The\n // round-trip makes the failure mode deterministic: a non-encodable\n // mutation is skipped with a warning instead of corrupting the shared\n // doc or desyncing peers mid-session. What we push here is byte-for-byte\n // what remote peers will apply, so local-apply == remote-apply.\n let safeParams: MutationParams;\n try {\n safeParams =\n info.params === undefined || info.params === null\n ? null\n : (JSON.parse(JSON.stringify(info.params)) as MutationParams);\n } catch (err) {\n console.warn(\n `[collab] skipping non-serializable mutation \"${info.id}\" — peers will not receive it`,\n err,\n );\n return;\n }\n\n const yArray = this.doc.getArray<CollabMutationInfo>(MUTATIONS_ARRAY_KEY);\n yArray.push([{ id: info.id, type: info.type, params: safeParams, uid: this.options.uid }]);\n }\n\n private applyRemoteMutation(op: CollabMutationInfo): void {\n // Wrap params with the sentinel so handleLocalMutation knows not to\n // echo the apply back into Yjs.\n const taggedParams: MutationParams =\n op.params && typeof op.params === \"object\" && !Array.isArray(op.params)\n ? {\n ...(op.params as Record<string, unknown>),\n [REMOTE_PARAM_TAG]: op.uid ?? true,\n }\n : op.params;\n\n try {\n this.options.commandService.syncExecuteCommand(op.id, taggedParams, {\n onlyLocal: true,\n fromCollab: true,\n });\n } catch (err) {\n // One unappliable op (already baked into the loaded snapshot, or a\n // stale target) must not throw into the realtime channel handler and\n // abort the rest of the catch-up. Said, never swallowed.\n console.warn(\n `[collab] could not apply remote mutation \"${op.id}\" — skipped`,\n err,\n );\n }\n }\n}\n\n// ─── helpers ────────────────────────────────────────────────────────────\n\n/**\n * A workbook holds no Univer DOCUMENT units of its own: the `doc.mutation.*` stream a sheet emits is\n * the in-cell editor's scratch document (typing into a cell). The finished text reaches the sheet as\n * `sheet.mutation.set-range-values`, which does travel. Sending the scratch edits made a joiner replay\n * them against an editor unit it does not have (\"Trying to overwrite value at key\").\n */\nexport function isCellEditorScratch(info: Pick<CollabMutationInfo, \"id\">): boolean {\n return typeof info.id === \"string\" && info.id.startsWith(\"doc.mutation.\");\n}\n\nconst CURSOR_PALETTE = [\n \"#3b82f6\", // blue\n \"#10b981\", // emerald\n \"#f59e0b\", // amber\n \"#ef4444\", // red\n \"#8b5cf6\", // violet\n \"#06b6d4\", // cyan\n \"#ec4899\", // pink\n \"#84cc16\", // lime\n];\n\nfunction pickColor(uid: string): string {\n // Deterministic FNV-1a-ish hash so the same user is always the same color\n // across sessions and across other users' screens.\n let h = 2166136261;\n for (let i = 0; i < uid.length; i++) {\n h ^= uid.charCodeAt(i);\n h = Math.imul(h, 16777619);\n }\n return CURSOR_PALETTE[Math.abs(h) % CURSOR_PALETTE.length] ?? \"#ec4899\";\n}\n","/**\n * useWorkbookRealtime — subscribe to udt_workbook_snapshots inserts for one\n * workbook. Fires whenever a NEW snapshot is committed (any client, including\n * us). The caller passes a callback that receives the new snapshot id and\n * created_by, so it can decide whether to reload (e.g. ignore our own writes\n * via the created_by comparison).\n *\n * NOT a CRDT layer. This is \"last write wins on the snapshot row,\" matching\n * the v1 storage model in `udt_v2_workbook_snapshots.sql`. Real concurrent\n * collab (per-keystroke CRDT updates) is a follow-up phase. *\n * REALTIME: `@ai-matrx/data/realtime` owns the channel (`useChannel`) — dedup, the\n * decoupled ordered handler queue, jittered reconnect, tab sleep, diagnostics.\n * What was here was a raw `.channel(...).subscribe()` with manual teardown and\n * NO catch-up read, which on this surface is the worst version of the bug: a\n * snapshot committed by a collaborator while the tab slept was never seen, so\n * the next local save checkpointed on top of a base that had already moved.\n * `onBackfill` re-reads the newest snapshot and hands it to the same callback,\n * so a recovery is indistinguishable from having received the event.\n *\n * The catch-up read is the host port `readNewestWorkbookSnapshot` (the package owns no database client).\n *\n * Echo suppression is deliberately NOT relied on here: these are INSERTs of\n * new rows this client never registers on the write ledger, and the caller's\n * `created_by` comparison is the contract — every insert is delivered.\n */\n\"use client\";\n\nimport { useCallback } from \"react\";\n\nimport { defineChannelNamespace } from \"@ai-matrx/data/realtime\";\nimport { useChannel } from \"@ai-matrx/data/realtime/react\";\nimport { getUniverHost } from \"../ports\";\n\nexport type WorkbookRealtimeEvent = {\n snapshotId: string;\n createdBy: string | null;\n createdAt: string;\n};\n\n/** One place names this channel. A second, different declaration throws. */\nconst workbookSnapshotsChannel = defineChannelNamespace({\n namespace: \"udt-workbook-snapshots\",\n parts: [\"workbookId\"],\n description: \"workbench.udt_workbook_snapshots inserts for one workbook\",\n});\n\nexport type SnapshotRow = {\n id?: string;\n created_by?: string | null;\n created_at?: string;\n};\n\nexport function useWorkbookRealtime(\n workbookId: string | null | undefined,\n onSnapshot: (event: WorkbookRealtimeEvent) => void,\n options?: { enabled?: boolean },\n) {\n const enabled = options?.enabled ?? true;\n\n const emit = useCallback(\n (row: SnapshotRow | null) => {\n if (!row?.id) return;\n onSnapshot({\n snapshotId: row.id,\n // component-created-by-ok: append-only snapshot with NO updated_by column and NO parent-rewrite trigger — created_by is the author the client wrote at insert (the only person field this table has)\n createdBy: row.created_by ?? null,\n createdAt: row.created_at ?? new Date().toISOString(),\n });\n },\n [onSnapshot],\n );\n\n useChannel(\n enabled && workbookId\n ? {\n topic: workbookSnapshotsChannel.topic({ workbookId }),\n postgresChanges: [\n {\n event: \"INSERT\",\n schema: \"workbench\",\n table: \"udt_workbook_snapshots\",\n filter: `workbook_id=eq.${workbookId}`,\n rowId: (row) => (typeof row.id === \"string\" ? row.id : undefined),\n onChange: ({ row }) => emit(row as SnapshotRow | null),\n },\n ],\n // Realtime has no replay. Re-read the newest snapshot so a reconnect,\n // tab wake or network restore cannot leave this client checkpointing\n // on top of a base that moved while it was away.\n onBackfill: async () => {\n let data: SnapshotRow | null;\n let error: { message: string } | null;\n try {\n ({ data, error } = await getUniverHost().readNewestWorkbookSnapshot(workbookId));\n } catch (e) {\n data = null;\n error = { message: e instanceof Error ? e.message : String(e) };\n }\n if (error) {\n // Not silent: without this read the caller is working from a\n // base it cannot know is stale.\n console.warn(\n \"[udt-workbook RT] catch-up read failed — this workbook may be \" +\n \"working from a stale snapshot base; reload before saving.\",\n error.message,\n );\n return;\n }\n emit(data);\n },\n }\n : null,\n );\n}\n","/**\n * The host ports of the Univer glue.\n *\n * The glue belongs to the package, but four things belong to the app that mounts an editor: the\n * toast path, the signed-in person, the file store, the snapshot catch-up read and the painted theme. The host states them\n * once at start-up with `configureUniverHost`; the glue reads them here. State sits on a\n * `globalThis` slot so a dual-bundled copy of the package still sees one host (dual-bundle rule).\n * An unconfigured port THROWS naming the port (nothing fails silently).\n */\n\nexport type UniverToastId = string | number;\ntype ToastShow = (title: string, options?: { description?: string; duration?: number }) => UniverToastId;\n\nexport interface UniverHostPorts {\n /** The app's toast path (one toaster per page). */\n toast: {\n success: ToastShow;\n info: ToastShow;\n warning: ToastShow;\n error: ToastShow;\n message: ToastShow;\n dismiss(id: UniverToastId): void;\n };\n /** Who is typing, or null when signed out. `metadata` is the auth user's metadata bag. */\n getSignedInUser(): Promise<{ id: string; email?: string | null; metadata?: Record<string, unknown> | null } | null>;\n /** The file store behind pictures on a sheet or in a document. */\n files: {\n upload(file: File): Promise<{ fileId: string }>;\n getBlob(fileId: string): Promise<Blob>;\n };\n /** The newest snapshot row of a workbook (the realtime catch-up read), or `data: null` when it has none. */\n readNewestWorkbookSnapshot(\n workbookId: string,\n ): Promise<{ data: { id?: string; created_by?: string | null; created_at?: string } | null; error: { message: string } | null }>;\n /** Tell the person a picture did not upload. */\n notifyUploadError(error: unknown): void;\n /** The painted colour mode, as a React hook (re-renders on a switch). */\n useThemeMode(): \"light\" | \"dark\";\n}\n\nconst SLOT = Symbol.for(\"ai-matrx.office.univer-host\");\ntype Slot = { ports?: UniverHostPorts };\nconst slot = (): Slot => {\n const g = globalThis as Record<symbol, Slot | undefined>;\n return (g[SLOT] ??= {});\n};\n\nexport function configureUniverHost(ports: UniverHostPorts): void {\n slot().ports = ports;\n}\n\nexport function getUniverHost(): UniverHostPorts {\n const ports = slot().ports;\n if (!ports) throw new Error(\"@ai-matrx/office/univer: configureUniverHost(ports) was not called before an editor mounted.\");\n return ports;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACwBA,QAAmB;AACnB,uBAA0B;AAE1B,oBAAkE;AAgFlE,IAAM,sBAAsB;AAC5B,IAAM,kBAAkB;AACxB,IAAM,eAAe;AAEd,IAAM,wBAAN,MAA4B;AAAA,EAChB;AAAA,EACT,MAAoB;AAAA,EACpB,YAA8B;AAAA,EAC9B,WAAsC;AAAA,EACtC,kBAAkD;AAAA,EAClD,iBACwD;AAAA,EACxD,oBAAyC;AAAA,EACzC,kBAAkE;AAAA;AAAA,EAElE,QAAQ;AAAA,EACR,UAAU;AAAA,EACV,WAAW;AAAA,EAEnB,YAAY,SAAuC;AACjD,SAAK,UAAU;AAAA,EACjB;AAAA,EAEA,MAAM,QAAuB;AAC3B,QAAI,KAAK,WAAW,KAAK,SAAU;AACnC,SAAK,UAAU;AAEf,SAAK,MAAM,IAAM,MAAI;AACrB,SAAK,YAAY,IAAI,2B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AAA,EACpB,aAAa;AACf,CAAC;AAQM,SAAS,oBACd,YACA,YACA,SACA;AACA,QAAM,UAAU,SAAS,WAAW;AAEpC,QAAM,WAAO;AAAA,IACX,CAAC,QAA4B;AAC3B,UAAI,CAAC,KAAK,GAAI;AACd,iBAAW;AAAA,QACT,YAAY,IAAI;AAAA;AAAA,QAEhB,WAAW,IAAI,cAAc;AAAA,QAC7B,WAAW,IAAI,eAAc,oBAAI,KAAK,GAAE,YAAY;AAAA,MACtD,CAAC;AAAA,IACH;AAAA,IACA,CAAC,UAAU;AAAA,EACb;AAEA;AAAA,IACE,WAAW,aACP;AAAA,MACE,OAAO,yBAAyB,MAAM,EAAE,WAAW,CAAC;AAAA,MACpD,iBAAiB;AAAA,QACf;AAAA,UACE,OAAO;AAAA,UACP,QAAQ;AAAA,UACR,OAAO;AAAA,UACP,QAAQ,kBAAkB,UAAU;AAAA,UACpC,OAAO,CAAC,QAAS,OAAO,IAAI,OAAO,WAAW,IAAI,KAAK;AAAA,UACvD,UAAU,CAAC,EAAE,IAAI,MAAM,KAAK,GAAyB;AAAA,QACvD;AAAA,MACF;AAAA;AAAA;AAAA;AAAA,MAIA,YAAY,YAAY;AACtB,YAAI;AACJ,YAAI;AACJ,YAAI;AACF,WAAC,EAAE,MAAM,MAAM,IAAI,MAAM,cAAc,EAAE,2BAA2B,UAAU;AAAA,QAChF,SAAS,GAAG;AACV,iBAAO;AACP,kBAAQ,EAAE,SAAS,aAAa,QAAQ,EAAE,UAAU,OAAO,CAAC,EAAE;AAAA,QAChE;AACA,YAAI,OAAO;AAGT,kBAAQ;AAAA,YACN;AAAA,YAEA,MAAM;AAAA,UACR;AACA;AAAA,QACF;AACA,aAAK,IAAI;AAAA,MACX;AAAA,IACF,IACA;AAAA,EACN;AACF;","names":["import_react"]}
1
+ {"version":3,"sources":["../src/univer/collab/index.ts","../src/univer/collab/WorkbookCollabSession.ts"],"sourcesContent":["/**\n * The collab session shared by `/sheets` and `/docs` (was matrx-frontend `features/workbooks/collab`): the\n * Univer <-> Yjs bridge. It owns the Yjs import, so it is loaded on demand (`import()` when a workbook\n * or document goes live). The snapshot-insert subscription hook is its own entry,\n * `@ai-matrx/office/univer/realtime`, so a static import of the hook never carries Yjs into the editor\n * chunk. A transitional subpath, like `./univer`: it goes internal when the editors move.\n */\nexport * from \"./WorkbookCollabSession\";\n","/**\n * WorkbookCollabSession — the Univer ↔ Yjs bridge.\n *\n * Subscribes to Univer's public `commandService.onMutationExecutedForCollab`\n * hook for local mutations and pushes them onto a `Y.Array<MutationOp>` shared\n * via `SupabaseYjsProvider`. Inbound Yjs updates produce `IMutationInfo` ops\n * that are applied through `commandService.syncExecuteCommand` with the\n * `fromCollab` execution option flagged so the outbound listener short-\n * circuits and we don't echo our own apply.\n *\n * See `FEATURE.md` in this directory for the architecture, the \"do NOT use\n * the Pro preset\" verdict, and the host-election rule. This class is the\n * runtime side of that plan.\n *\n * Lifecycle:\n * - constructor: wire references, no side effects\n * - start(): create Y.Doc + Awareness, instantiate provider, subscribe to\n * command stream, subscribe to Yjs Array observer\n * - stop(): detach all listeners, dispose provider\n *\n * The session is one-shot per workbook open. Reload = stop() + new instance.\n */\n\"use client\";\n\nimport * as Y from \"yjs\";\nimport { Awareness } from \"y-protocols/awareness\";\n\nimport { electCollabHost, remoteOriginOf, REMOTE_PARAM_TAG } from \"@ai-matrx/data/realtime/collab\";\n\n/**\n * Structural transport contract. `SupabaseYjsProvider` satisfies it in\n * production; the verification harness injects an in-memory loopback\n * transport through the same seam. Keeping this structural (not the concrete\n * class) is what makes the session testable without network.\n */\nexport type CollabProviderLike = {\n connect(): Promise<void>;\n disconnect(): void;\n ready(): Promise<void>;\n /**\n * True when this is the only holder of its room in this tab allowed to act as\n * host (autosave). Same-tab holders share one client id and one uid, so\n * awareness cannot tell them apart. Absent = a lone holder = leader.\n */\n isTabLeader?(): boolean;\n};\n\n// Univer types are loose at the public hook boundary; we narrow with a\n// structural interface so we don't have to import the full Univer surface.\ntype MutationParams = Record<string, unknown> | unknown[] | null;\n\nexport type CollabMutationInfo = {\n id: string;\n type: number; // CommandType.MUTATION = 2 in Univer's enum\n params: MutationParams;\n /** Who made it (the person's uid), so a peer can tell its own other tab's edit from a stranger's. */\n uid?: string;\n};\n\ntype ExecuteOptions = {\n /** Per-Univer convention: when true, the command runs locally only and is\n * NOT broadcast to other plugins. We set this on inbound-apply so the\n * subsequent onMutationExecutedForCollab does NOT re-fire on our peer. */\n onlyLocal?: boolean;\n fromCollab?: boolean;\n};\n\nexport type CommandServiceLike = {\n onMutationExecutedForCollab(\n listener: (info: CollabMutationInfo, options?: ExecuteOptions) => void,\n ): {\n dispose: () => void;\n };\n syncExecuteCommand(\n id: string,\n params: MutationParams,\n options?: ExecuteOptions,\n ): unknown;\n};\n\nexport type WorkbookCollabSessionOptions = {\n workbookId: string;\n uid: string;\n clientId: string;\n commandService: CommandServiceLike;\n /** Factory so this file does not statically depend on the provider. */\n makeProvider: (args: {\n workbookId: string;\n clientId: string;\n doc: Y.Doc;\n awareness: Awareness;\n /** Wire to the provider so a host handover re-runs the election. */\n onLeaderChange: () => void;\n }) => CollabProviderLike;\n /** Called when the awareness set changes (peers join / leave / move cursor). */\n onAwarenessChange?: (awareness: Awareness) => void;\n /**\n * A peer replaced the whole content (history restore, Google update): the\n * newest saved snapshot is the content now. Reload it in place; never\n * re-announce. See `announceReplaced`.\n */\n onReplaced?: (snapshotId: string) => void;\n /**\n * TRUE when the room is bound to a Univer DOCUMENT unit (the Docs editor): there the `doc.mutation.*`\n * stream IS the content, so it must travel. Leave unset for a workbook, where `doc.mutation.*` is the\n * in-cell editor's scratch document and never travels (see `isCellEditorScratch`).\n */\n forwardDocumentMutations?: boolean;\n};\n\n/** What `announceReplaced` writes into the room's control map. */\ntype ReplacedSignal = { snapshotId: string; by: string; nonce: string };\n\nconst MUTATIONS_ARRAY_KEY = \"mutations\";\nconst CONTROL_MAP_KEY = \"control\";\nconst REPLACED_KEY = \"replaced\";\n\nexport class WorkbookCollabSession {\n private readonly options: WorkbookCollabSessionOptions;\n private doc: Y.Doc | null = null;\n private awareness: Awareness | null = null;\n private provider: CollabProviderLike | null = null;\n private commandDisposer: { dispose: () => void } | null = null;\n private yArrayObserver:\n ((event: Y.YArrayEvent<CollabMutationInfo>) => void) | null = null;\n private awarenessObserver: (() => void) | null = null;\n private controlObserver: ((event: Y.YMapEvent<unknown>) => void) | null = null;\n /** >0 while this client swaps its own content: its mutations are not edits. */\n private muted = 0;\n private started = false;\n private disposed = false;\n\n constructor(options: WorkbookCollabSessionOptions) {\n this.options = options;\n }\n\n async start(): Promise<void> {\n if (this.started || this.disposed) return;\n this.started = true;\n\n this.doc = new Y.Doc();\n this.awareness = new Awareness(this.doc);\n\n // Seed local awareness with the user identity. Cursor (sheetId/row/col)\n // is set later via setCursor().\n this.awareness.setLocalState({\n uid: this.options.uid,\n clientId: this.options.clientId,\n name: this.options.uid.slice(0, 8),\n color: pickColor(this.options.uid),\n sheetId: null,\n row: null,\n col: null,\n ts: Date.now(),\n });\n\n this.provider = this.options.makeProvider({\n workbookId: this.options.workbookId,\n clientId: this.options.clientId,\n doc: this.doc,\n awareness: this.awareness,\n onLeaderChange: () => {\n if (this.awareness) this.options.onAwarenessChange?.(this.awareness);\n },\n });\n\n // Subscribe to inbound Yjs array changes BEFORE connecting the provider,\n // so the initial-state replay (if any) hits our observer.\n const yArray = this.doc.getArray<CollabMutationInfo>(MUTATIONS_ARRAY_KEY);\n this.yArrayObserver = (event) => {\n // Local transactions are OUR OWN pushes from handleLocalMutation —\n // Univer already executed those mutations. Re-applying them here would\n // double-apply every local edit on the originator. Only act on\n // transactions that arrived via Y.applyUpdate (origin 'remote').\n if (event.transaction.local) return;\n for (const change of event.changes.delta) {\n if (!change.insert || !Array.isArray(change.insert)) continue;\n for (const op of change.insert as CollabMutationInfo[]) {\n this.applyRemoteMutation(op);\n }\n }\n };\n yArray.observe(this.yArrayObserver as Parameters<typeof yArray.observe>[0]);\n\n // Awareness change passthrough.\n if (this.options.onAwarenessChange) {\n const cb = this.options.onAwarenessChange;\n this.awarenessObserver = () => {\n if (this.awareness) cb(this.awareness);\n };\n this.awareness.on(\"change\", this.awarenessObserver);\n }\n\n // Subscribe to Univer's collab-grain mutation stream.\n this.commandDisposer =\n this.options.commandService.onMutationExecutedForCollab((info) =>\n this.handleLocalMutation(info),\n );\n\n await this.provider.connect();\n await this.provider.ready();\n\n // Whole-content replacement from a peer. Bound AFTER the initial sync: a\n // joiner already loaded the newest snapshot, so the signal it finds in\n // the room's state is history, not news (the editor also skips a\n // snapshot it already shows).\n if (this.disposed || !this.doc) return;\n const control = this.doc.getMap<unknown>(CONTROL_MAP_KEY);\n const seen = (control.get(REPLACED_KEY) as ReplacedSignal | undefined)?.nonce ?? null;\n this.controlObserver = (event) => {\n if (event.transaction.local || !event.keysChanged.has(REPLACED_KEY)) return;\n const signal = control.get(REPLACED_KEY) as ReplacedSignal | undefined;\n if (!signal?.snapshotId || signal.nonce === seen) return;\n this.options.onReplaced?.(signal.snapshotId);\n };\n control.observe(this.controlObserver);\n }\n\n stop(): void {\n if (this.disposed) return;\n this.disposed = true;\n\n this.commandDisposer?.dispose();\n this.commandDisposer = null;\n\n if (this.doc && this.yArrayObserver) {\n const yArray = this.doc.getArray<CollabMutationInfo>(MUTATIONS_ARRAY_KEY);\n yArray.unobserve(\n this.yArrayObserver as Parameters<typeof yArray.unobserve>[0],\n );\n this.yArrayObserver = null;\n }\n\n if (this.doc && this.controlObserver) {\n this.doc.getMap<unknown>(CONTROL_MAP_KEY).unobserve(this.controlObserver);\n this.controlObserver = null;\n }\n\n if (this.awareness && this.awarenessObserver) {\n this.awareness.off(\"change\", this.awarenessObserver);\n this.awarenessObserver = null;\n }\n\n this.provider?.disconnect();\n this.provider = null;\n\n if (this.awareness) {\n this.awareness.destroy();\n this.awareness = null;\n }\n\n if (this.doc) {\n this.doc.destroy();\n this.doc = null;\n }\n }\n\n /**\n * How many mutations the shared log holds right now. Read it BEFORE\n * `workbook.save()`: that many entries are guaranteed to be inside the\n * snapshot that save produces.\n */\n logLength(): number {\n return this.doc?.getArray<CollabMutationInfo>(MUTATIONS_ARRAY_KEY).length ?? 0;\n }\n\n /**\n * Drop the first `upTo` mutations from the shared log once a snapshot that\n * contains them has been SAVED. Without this a joiner loads the saved\n * workbook (which already has every mutation baked in) and then catches up\n * the whole log on top of it, replaying edits into a workbook that already\n * holds them — Univer refuses with \"Trying to overwrite value at key\"\n * (feedback 88184e53). Only the host calls this; peers receive it as a\n * delete, which the inbound observer ignores (it acts on inserts only).\n */\n compactLog(upTo: number): void {\n if (!this.doc || upTo <= 0) return;\n const yArray = this.doc.getArray<CollabMutationInfo>(MUTATIONS_ARRAY_KEY);\n const n = Math.min(upTo, yArray.length);\n if (n > 0) this.doc.transact(() => yArray.delete(0, n));\n }\n\n /**\n * This client replaced the whole content and SAVED it as `snapshotId`.\n * One transaction empties the shared mutation log (every entry targets the\n * content that is gone — a joiner must never replay them onto the new\n * snapshot) and tells every peer to reload the newest snapshot.\n */\n announceReplaced(snapshotId: string): void {\n if (!this.doc) return;\n const yArray = this.doc.getArray<CollabMutationInfo>(MUTATIONS_ARRAY_KEY);\n const control = this.doc.getMap<unknown>(CONTROL_MAP_KEY);\n const signal: ReplacedSignal = {\n snapshotId,\n by: this.options.clientId,\n nonce: `${this.options.clientId}:${Date.now()}:${Math.random().toString(36).slice(2, 8)}`,\n };\n this.doc.transact(() => {\n if (yArray.length > 0) yArray.delete(0, yArray.length);\n control.set(REPLACED_KEY, signal);\n });\n }\n\n /**\n * Run `swap` (this client tearing its content down and mounting the new\n * one) without broadcasting what Univer emits while it does: those are not\n * edits, and peers reload the same snapshot themselves.\n */\n muteWhile<T>(swap: () => T): T {\n this.muted += 1;\n try {\n return swap();\n } finally {\n this.muted -= 1;\n }\n }\n\n /**\n * Update the local awareness cursor. Call this from Univer's selection\n * listener (`worksheet.onSelectionChange(...)`). Throttle at the call site\n * (~50ms is plenty); the provider broadcasts on its own debounced cycle.\n */\n setCursor(input: {\n sheetId: string | null;\n row: number | null;\n col: number | null;\n }): void {\n if (!this.awareness) return;\n const current = this.awareness.getLocalState() ?? {};\n this.awareness.setLocalState({\n ...current,\n sheetId: input.sheetId,\n row: input.row,\n col: input.col,\n ts: Date.now(),\n });\n }\n\n /**\n * Returns the deterministic host: the connected peer with the lowest uid\n * (lexicographic). Host is the only client that writes canonical snapshots.\n * No election protocol; recompute whenever awareness changes.\n */\n electHost(opts: { tabTieBreak?: boolean } = {}): { isHost: boolean; hostUid: string | null } {\n if (!this.awareness) return { isHost: false, hostUid: null };\n const states = Array.from(this.awareness.getStates().values()) as Array<\n Record<string, unknown>\n >;\n // Lowest uid wins ACROSS tabs; inside this tab only the room's tab leader\n // may be host, because every same-tab holder carries the same uid.\n const leader = this.provider?.isTabLeader?.() ?? true;\n return electCollabHost(states, this.options.uid, this.options.clientId, leader, opts.tabTieBreak ?? false);\n }\n\n // ─── internals ────────────────────────────────────────────────────────\n\n private handleLocalMutation(info: CollabMutationInfo): void {\n if (this.muted > 0) return;\n if (!this.options.forwardDocumentMutations && isCellEditorScratch(info)) return;\n // Inbound-applied mutations re-fire this listener because Univer's\n // syncExecuteCommand legitimately processes them through the full\n // pipeline. Skip by sniffing the sentinel we stamped on apply.\n if (\n info.params &&\n typeof info.params === \"object\" &&\n !Array.isArray(info.params) &&\n remoteOriginOf(info.params) !== null\n ) {\n return;\n }\n if (!this.doc) return;\n\n // Normalize params through a JSON round-trip BEFORE pushing into Yjs.\n // Yjs (and the Broadcast transport) only carry JSON-encodable values;\n // Univer types its mutation params as \"serializable\" but JS objects can\n // smuggle Date / Map / class instances through that promise. The\n // round-trip makes the failure mode deterministic: a non-encodable\n // mutation is skipped with a warning instead of corrupting the shared\n // doc or desyncing peers mid-session. What we push here is byte-for-byte\n // what remote peers will apply, so local-apply == remote-apply.\n let safeParams: MutationParams;\n try {\n safeParams =\n info.params === undefined || info.params === null\n ? null\n : (JSON.parse(JSON.stringify(info.params)) as MutationParams);\n } catch (err) {\n console.warn(\n `[collab] skipping non-serializable mutation \"${info.id}\" — peers will not receive it`,\n err,\n );\n return;\n }\n\n const yArray = this.doc.getArray<CollabMutationInfo>(MUTATIONS_ARRAY_KEY);\n yArray.push([{ id: info.id, type: info.type, params: safeParams, uid: this.options.uid }]);\n }\n\n private applyRemoteMutation(op: CollabMutationInfo): void {\n // Wrap params with the sentinel so handleLocalMutation knows not to\n // echo the apply back into Yjs.\n const taggedParams: MutationParams =\n op.params && typeof op.params === \"object\" && !Array.isArray(op.params)\n ? {\n ...(op.params as Record<string, unknown>),\n [REMOTE_PARAM_TAG]: op.uid ?? true,\n }\n : op.params;\n\n try {\n this.options.commandService.syncExecuteCommand(op.id, taggedParams, {\n onlyLocal: true,\n fromCollab: true,\n });\n } catch (err) {\n // One unappliable op (already baked into the loaded snapshot, or a\n // stale target) must not throw into the realtime channel handler and\n // abort the rest of the catch-up. Said, never swallowed.\n console.warn(\n `[collab] could not apply remote mutation \"${op.id}\" — skipped`,\n err,\n );\n }\n }\n}\n\n// ─── helpers ────────────────────────────────────────────────────────────\n\n/**\n * A workbook holds no Univer DOCUMENT units of its own: the `doc.mutation.*` stream a sheet emits is\n * the in-cell editor's scratch document (typing into a cell). The finished text reaches the sheet as\n * `sheet.mutation.set-range-values`, which does travel. Sending the scratch edits made a joiner replay\n * them against an editor unit it does not have (\"Trying to overwrite value at key\").\n */\nexport function isCellEditorScratch(info: Pick<CollabMutationInfo, \"id\">): boolean {\n return typeof info.id === \"string\" && info.id.startsWith(\"doc.mutation.\");\n}\n\nconst CURSOR_PALETTE = [\n \"#3b82f6\", // blue\n \"#10b981\", // emerald\n \"#f59e0b\", // amber\n \"#ef4444\", // red\n \"#8b5cf6\", // violet\n \"#06b6d4\", // cyan\n \"#ec4899\", // pink\n \"#84cc16\", // lime\n];\n\nfunction pickColor(uid: string): string {\n // Deterministic FNV-1a-ish hash so the same user is always the same color\n // across sessions and across other users' screens.\n let h = 2166136261;\n for (let i = 0; i < uid.length; i++) {\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A,EACrB;AAAA;AAAA,EACA;AAAA;AAAA,EACA;AAAA;AAAA,EACA;AAAA;AAAA,EACA;AAAA;AAAA,EACA;AAAA;AAAA,EACA;AAAA;AAAA,EACA;AAAA;AACF;AAEA,SAAS,UAAU,KAAqB;AAGtC,MAAI,IAAI;AACR,WAAS,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK;AACnC,SAAK,IAAI,WAAW,CAAC;AACrB,QAAI,KAAK,KAAK,GAAG,QAAQ;AAAA,EAC3B;AACA,SAAO,eAAe,KAAK,IAAI,CAAC,IAAI,eAAe,MAAM,KAAK;AAChE;","names":[]}
@@ -61,6 +61,12 @@ type WorkbookCollabSessionOptions = {
61
61
  * re-announce. See `announceReplaced`.
62
62
  */
63
63
  onReplaced?: (snapshotId: string) => void;
64
+ /**
65
+ * TRUE when the room is bound to a Univer DOCUMENT unit (the Docs editor): there the `doc.mutation.*`
66
+ * stream IS the content, so it must travel. Leave unset for a workbook, where `doc.mutation.*` is the
67
+ * in-cell editor's scratch document and never travels (see `isCellEditorScratch`).
68
+ */
69
+ forwardDocumentMutations?: boolean;
64
70
  };
65
71
  declare class WorkbookCollabSession {
66
72
  private readonly options;
@@ -139,18 +145,4 @@ declare class WorkbookCollabSession {
139
145
  */
140
146
  declare function isCellEditorScratch(info: Pick<CollabMutationInfo, "id">): boolean;
141
147
 
142
- type WorkbookRealtimeEvent = {
143
- snapshotId: string;
144
- createdBy: string | null;
145
- createdAt: string;
146
- };
147
- type SnapshotRow = {
148
- id?: string;
149
- created_by?: string | null;
150
- created_at?: string;
151
- };
152
- declare function useWorkbookRealtime(workbookId: string | null | undefined, onSnapshot: (event: WorkbookRealtimeEvent) => void, options?: {
153
- enabled?: boolean;
154
- }): void;
155
-
156
- export { type CollabMutationInfo, type CollabProviderLike, type CommandServiceLike, type SnapshotRow, WorkbookCollabSession, type WorkbookCollabSessionOptions, type WorkbookRealtimeEvent, isCellEditorScratch, useWorkbookRealtime };
148
+ export { type CollabMutationInfo, type CollabProviderLike, type CommandServiceLike, WorkbookCollabSession, type WorkbookCollabSessionOptions, isCellEditorScratch };
@@ -61,6 +61,12 @@ type WorkbookCollabSessionOptions = {
61
61
  * re-announce. See `announceReplaced`.
62
62
  */
63
63
  onReplaced?: (snapshotId: string) => void;
64
+ /**
65
+ * TRUE when the room is bound to a Univer DOCUMENT unit (the Docs editor): there the `doc.mutation.*`
66
+ * stream IS the content, so it must travel. Leave unset for a workbook, where `doc.mutation.*` is the
67
+ * in-cell editor's scratch document and never travels (see `isCellEditorScratch`).
68
+ */
69
+ forwardDocumentMutations?: boolean;
64
70
  };
65
71
  declare class WorkbookCollabSession {
66
72
  private readonly options;
@@ -139,18 +145,4 @@ declare class WorkbookCollabSession {
139
145
  */
140
146
  declare function isCellEditorScratch(info: Pick<CollabMutationInfo, "id">): boolean;
141
147
 
142
- type WorkbookRealtimeEvent = {
143
- snapshotId: string;
144
- createdBy: string | null;
145
- createdAt: string;
146
- };
147
- type SnapshotRow = {
148
- id?: string;
149
- created_by?: string | null;
150
- created_at?: string;
151
- };
152
- declare function useWorkbookRealtime(workbookId: string | null | undefined, onSnapshot: (event: WorkbookRealtimeEvent) => void, options?: {
153
- enabled?: boolean;
154
- }): void;
155
-
156
- export { type CollabMutationInfo, type CollabProviderLike, type CommandServiceLike, type SnapshotRow, WorkbookCollabSession, type WorkbookCollabSessionOptions, type WorkbookRealtimeEvent, isCellEditorScratch, useWorkbookRealtime };
148
+ export { type CollabMutationInfo, type CollabProviderLike, type CommandServiceLike, WorkbookCollabSession, type WorkbookCollabSessionOptions, isCellEditorScratch };