grist-widget-sdk 0.2.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.
- package/CHANGELOG.md +730 -0
- package/LICENSE +15 -0
- package/README.md +249 -0
- package/dist/advanced.cjs +2749 -0
- package/dist/advanced.cjs.map +1 -0
- package/dist/advanced.d.cts +790 -0
- package/dist/advanced.d.ts +790 -0
- package/dist/advanced.js +825 -0
- package/dist/advanced.js.map +1 -0
- package/dist/chunk-64SVRWW7.js +758 -0
- package/dist/chunk-64SVRWW7.js.map +1 -0
- package/dist/chunk-BNBV24WM.js +479 -0
- package/dist/chunk-BNBV24WM.js.map +1 -0
- package/dist/chunk-ELGVOHFD.js +32 -0
- package/dist/chunk-ELGVOHFD.js.map +1 -0
- package/dist/chunk-GTTD5HPO.js +36 -0
- package/dist/chunk-GTTD5HPO.js.map +1 -0
- package/dist/chunk-JASMM2YC.js +243 -0
- package/dist/chunk-JASMM2YC.js.map +1 -0
- package/dist/chunk-PM3B4WYV.js +12 -0
- package/dist/chunk-PM3B4WYV.js.map +1 -0
- package/dist/chunk-QKXKZ27D.js +1189 -0
- package/dist/chunk-QKXKZ27D.js.map +1 -0
- package/dist/chunk-QZG7SNUG.js +463 -0
- package/dist/chunk-QZG7SNUG.js.map +1 -0
- package/dist/chunk-REV5AUXD.js +25 -0
- package/dist/chunk-REV5AUXD.js.map +1 -0
- package/dist/chunk-XE2SKNLH.js +1641 -0
- package/dist/chunk-XE2SKNLH.js.map +1 -0
- package/dist/create-grist-emulator-C_0I4wax.d.ts +80 -0
- package/dist/create-grist-emulator-DBS6gmiI.d.cts +80 -0
- package/dist/emulator/devtools.cjs +1367 -0
- package/dist/emulator/devtools.cjs.map +1 -0
- package/dist/emulator/devtools.d.cts +46 -0
- package/dist/emulator/devtools.d.ts +46 -0
- package/dist/emulator/devtools.js +480 -0
- package/dist/emulator/devtools.js.map +1 -0
- package/dist/emulator/testing.cjs +2061 -0
- package/dist/emulator/testing.cjs.map +1 -0
- package/dist/emulator/testing.d.cts +50 -0
- package/dist/emulator/testing.d.ts +50 -0
- package/dist/emulator/testing.js +124 -0
- package/dist/emulator/testing.js.map +1 -0
- package/dist/emulator/transports.cjs +1132 -0
- package/dist/emulator/transports.cjs.map +1 -0
- package/dist/emulator/transports.d.cts +37 -0
- package/dist/emulator/transports.d.ts +37 -0
- package/dist/emulator/transports.js +12 -0
- package/dist/emulator/transports.js.map +1 -0
- package/dist/emulator.cjs +1796 -0
- package/dist/emulator.cjs.map +1 -0
- package/dist/emulator.d.cts +107 -0
- package/dist/emulator.d.ts +107 -0
- package/dist/emulator.js +26 -0
- package/dist/emulator.js.map +1 -0
- package/dist/index.cjs +5511 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +825 -0
- package/dist/index.d.ts +825 -0
- package/dist/index.js +2447 -0
- package/dist/index.js.map +1 -0
- package/dist/inline-CRwQQ-GW.d.cts +311 -0
- package/dist/inline-QitZwXKP.d.ts +311 -0
- package/dist/templates-BC7FoSdW.d.ts +24 -0
- package/dist/templates-DBq719O_.d.cts +24 -0
- package/dist/types-Fb2wplkv.d.cts +228 -0
- package/dist/types-Fb2wplkv.d.ts +228 -0
- package/dist/use-grist-widget-options-from-context-BFhQBGRY.d.ts +522 -0
- package/dist/use-grist-widget-options-from-context-qpFMdoBJ.d.cts +522 -0
- package/package.json +131 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/emulator/transports/index.ts","../../src/emulator/transports/iframe.ts","../../src/emulator/theme-message.ts","../../src/sdk/helpers/grist-user-actions.ts","../../src/sdk/helpers/grist-value-codec.ts","../../src/sdk/helpers/grist-safe-parse.ts","../../src/emulator/core/document.ts","../../src/emulator/core/action-engine.ts","../../src/emulator/core/mappings.ts","../../src/emulator/transports/inline.ts"],"sourcesContent":["export { createIframeTransport } from \"./iframe\"\nexport type { IframeTransportOptions } from \"./iframe\"\nexport { createInlineTransport } from \"./inline\"\nexport type { InlineGristGlobal, InlineTransportOptions } from \"./inline\"\nexport type { EmulatorTransport, TransportContext } from \"./types\"\n","/**\n * Iframe transport — the production-fidelity path.\n *\n * - The iframe loads the real `https://docs.getgrist.com/grist-plugin-api.js`\n * so widget code talks to `window.grist` exactly like in production.\n * - The host (this file) instantiates a `grain-rpc` `Rpc` and wires:\n * * outgoing → `iframe.contentWindow.postMessage(msg, targetOrigin)`\n * * incoming → `window.addEventListener(\"message\", ...)` filtered by\n * `event.source === iframe.contentWindow`.\n * - The Rpc registers the six host impls under their canonical names so\n * `grist-plugin-api.js`'s stubs find them.\n * - Custom messages (cursor / data / options / theme) are pushed via\n * `rpc.postMessage(...)` which `grist-plugin-api.js` dispatches to its\n * internal \"message\" event listeners (the ones used by `onRecord` etc.).\n *\n * Sequence:\n * 1. Host calls `rpc.queueOutgoingUntilReadyMessage()` so messages are\n * buffered until the widget signals it is ready.\n * 2. Widget loads, runs `grist-plugin-api.js`, calls `grist.ready(...)`.\n * 3. Widget's `grist-plugin-api.js` posts a \"ready\" message → host's Rpc\n * flushes the buffered outgoing messages.\n * 4. Widget's `grist.ready(...)` triggers `CustomSectionAPI.configure(...)`\n * on us; we react by emitting initial state (options + cursor + data).\n */\n\nimport { MsgType, Rpc } from \"grain-rpc\"\n\nimport type { GristWidgetInteraction } from \"../../sdk/types\"\nimport type { EmulatorEvent } from \"../core/event-bus\"\nimport { buildGristThemeHostMessage } from \"../theme-message\"\nimport type { EmulatorTransport, TransportContext } from \"./types\"\n\nexport type IframeTransportOptions = TransportContext & {\n iframe: HTMLIFrameElement\n /** `postMessage` target origin. Defaults to \"*\" — fine for emulator usage. */\n targetOrigin?: string\n}\n\nexport function createIframeTransport(options: IframeTransportOptions): EmulatorTransport {\n const { iframe, state, bus, api, targetOrigin = \"*\" } = options\n const timers = new Set<ReturnType<typeof setTimeout>>()\n\n const rpc = new Rpc({\n sendMessage: (msg) => {\n const target = iframe.contentWindow\n if (!target) return\n target.postMessage(msg, targetOrigin)\n },\n logger: { info: () => {}, warn: () => {} },\n })\n\n rpc.queueOutgoingUntilReadyMessage()\n\n /**\n * `grist-plugin-api.js` obtains `docApi` via `getStub(\"GristDocAPI@grist\", …)`, which sends\n * `RpcCall` messages with `mdest: \"grist\"`. In full Grist that forwarder reaches a doc-worker\n * `Rpc`; here all host impls live on this same `Rpc`, so forwarding to `this` would re-emit\n * the call to the iframe and the widget would reject it as `Unknown interface`. Strip `mdest`\n * so `GristDocAPI` resolves to our local `registerImpl` surface (same effective path as tests\n * that use `getStub(\"GristDocAPI\")` without `@grist`).\n */\n const receiveFromWidget = rpc.receiveMessage.bind(rpc)\n rpc.receiveMessage = (msg: unknown) => {\n if (\n msg &&\n typeof msg === \"object\" &&\n (msg as { mtype?: unknown }).mtype === MsgType.RpcCall &&\n (msg as { mdest?: unknown }).mdest === \"grist\"\n ) {\n const copy = { ...(msg as Record<string, unknown>) }\n delete copy.mdest\n receiveFromWidget(copy as never)\n return\n }\n receiveFromWidget(msg as never)\n }\n\n const onMessage = (event: MessageEvent) => {\n if (event.source !== iframe.contentWindow) return\n // Dev runtimes may post unrelated messages (HMR, devtools). Forwarding\n // them to grain-rpc can throw and disturb the ready handshake.\n if (!isRpcEnvelope(event.data)) return\n rpc.receiveMessage(event.data)\n }\n window.addEventListener(\"message\", onMessage)\n\n rpc.registerImpl(\"GristView\", api.GristView)\n rpc.registerImpl(\"GristDocAPI\", api.GristDocAPI)\n rpc.registerImpl(\"WidgetAPI\", api.WidgetAPI)\n rpc.registerImpl(\"CustomSectionAPI\", api.CustomSectionAPI)\n rpc.registerImpl(\"_grist_api\", api.GristAPI)\n rpc.registerImpl(\"CommandAPI\", api.CommandAPI)\n\n const buildInteraction = options.buildInteraction ?? defaultInteraction(state)\n const cursorOf = options.cursor ?? (() => state.selection.cursor)\n const initialCursorOf = options.initialCursor ?? (() => state.selection.rowId)\n\n type HostPushOptions = { mappingsChange?: boolean }\n\n function fireDataChange(tableId?: string, opts?: HostPushOptions) {\n safePostMessage({\n tableId: tableId ?? state.selection.tableId,\n dataChange: true,\n ...(opts?.mappingsChange ? { mappingsChange: true } : {}),\n })\n }\n function fireCursor(rowId?: number | \"new\" | null, opts?: HostPushOptions) {\n const id = rowId === undefined ? initialCursorOf() : rowId\n if (id === null) return\n safePostMessage({\n tableId: state.selection.tableId,\n rowId: id,\n cursor: cursorOf(),\n ...(opts?.mappingsChange ? { mappingsChange: true } : {}),\n })\n }\n function fireOptions() {\n safePostMessage({\n options: hasOptions(state.widgetOptions) ? { ...state.widgetOptions } : null,\n settings: { ...buildInteraction() },\n })\n }\n function fireTheme(message: Record<string, unknown>) {\n safePostMessage(message)\n }\n function emitInitialState(opts?: HostPushOptions) {\n fireOptions()\n if (options.initialTheme) {\n fireTheme(buildGristThemeHostMessage(options.initialTheme, { fromReady: true }))\n }\n fireDataChange(undefined, opts)\n fireCursor(undefined, opts)\n }\n\n function emitInitialStateWithReplay(opts?: HostPushOptions) {\n // Keep initial state delivery resilient when widget subscriptions are\n // attached slightly after ready (common in dev/HMR): send now, then replay\n // on the next microtask and next macrotask.\n emitInitialState(opts)\n queueMicrotask(() => emitInitialState(opts))\n const timer = setTimeout(() => {\n timers.delete(timer)\n emitInitialState(opts)\n }, 0)\n timers.add(timer)\n }\n\n // Bus → transport push. Listens on every emulator state mutation and pushes\n // the matching protocol message to the iframe.\n const offBus = bus.on((event: EmulatorEvent) => {\n switch (event.type) {\n case \"ready\":\n emitInitialStateWithReplay()\n break\n case \"data-change\":\n fireDataChange(event.tableId)\n break\n case \"cursor-change\": {\n const rowId = event.rowId === null ? state.selection.rowId : event.rowId\n if (rowId !== null) {\n safePostMessage({\n tableId: event.tableId,\n rowId,\n cursor: cursorOf(),\n })\n }\n // Match inline transport: cursor moves must also signal `dataChange` so\n // `grist-plugin-api.js` refetches the row and calls `onRecord` again.\n // Cursor-only postMessages left widgets stuck on the first row (#playground).\n fireDataChange(event.tableId)\n break\n }\n case \"options-change\":\n fireOptions()\n break\n case \"mappings-change\":\n // grist-plugin-api only refetches `sectionApi.mappings()` when `mappingsChange`\n // is set on the host message (see getMappingsIfChanged in grist-core).\n emitInitialStateWithReplay({ mappingsChange: true })\n break\n case \"theme-change\":\n fireTheme(event.message)\n break\n }\n })\n\n rpc.processIncoming()\n\n function safePostMessage(payload: Record<string, unknown>): void {\n try {\n rpc.postMessage(payload)\n } catch (err) {\n // Transport failures (broken iframe, crashed widget script, etc.) should\n // never break host-side emulator mutations. We keep the emulator state\n // authoritative and treat iframe delivery as best-effort.\n // eslint-disable-next-line no-console\n console.warn(\"[grist-emulator/iframe] failed to deliver postMessage\", err)\n }\n }\n\n return {\n dispose() {\n window.removeEventListener(\"message\", onMessage)\n offBus()\n for (const timer of timers) clearTimeout(timer)\n timers.clear()\n // grain-rpc 0.1.7 doesn't expose `close()`. We rely on listener removal\n // + GC to dispose the rpc instance.\n },\n fireDataChange,\n fireCursor,\n fireOptions,\n fireTheme,\n emitInitialState,\n }\n}\n\nfunction hasOptions(options: Record<string, unknown>): boolean {\n return Object.keys(options).length > 0\n}\n\nfunction isRpcEnvelope(data: unknown): data is { mtype: number } {\n if (!data || typeof data !== \"object\") return false\n return typeof (data as { mtype?: unknown }).mtype === \"number\"\n}\n\nfunction defaultInteraction(state: { widgetInteraction: GristWidgetInteraction; configuration: { requiredAccess?: string } | null }) {\n return (): GristWidgetInteraction => ({\n access_level: state.configuration?.requiredAccess ?? \"read table\",\n ...state.widgetInteraction,\n })\n}\n","export type GristThemeAppearance = \"light\" | \"dark\"\n\n/** Host → widget postMessage shape used by `grist-plugin-api.js` (`onThemeChange`). */\nexport type GristThemeHostMessage = {\n theme: {\n appearance: GristThemeAppearance\n name: string\n colors: Record<string, string>\n }\n fromReady?: boolean\n}\n\nexport function buildGristThemeHostMessage(\n appearance: GristThemeAppearance,\n options?: { fromReady?: boolean }\n): GristThemeHostMessage {\n return {\n theme: {\n appearance,\n name: appearance,\n colors: {},\n },\n ...(options?.fromReady ? { fromReady: true } : {}),\n }\n}\n","/**\n * Grist `docApi.applyUserActions` payloads are arrays of user-action tuples.\n * @see https://support.getgrist.com/code/interfaces/grist_plugin_api.GristDocAPI/#applyuseractions\n */\n\nexport type GristUserActionTuple = unknown[]\n\n/** `[\"UpdateRecord\", tableId, rowId, { col: value, ... }]` */\nexport function gristUpdateRecordAction(\n tableId: string,\n rowId: number,\n patch: Record<string, unknown>\n): GristUserActionTuple {\n return [\"UpdateRecord\", tableId, rowId, patch]\n}\n\n/** `[\"AddRecord\", tableId, null, { col: value, ... }]` */\nexport function gristAddRecordAction(tableId: string, values: Record<string, unknown>): GristUserActionTuple {\n return [\"AddRecord\", tableId, null, values]\n}\n\n/** `[\"RemoveRecord\", tableId, rowId]` */\nexport function gristRemoveRecordAction(tableId: string, rowId: number): GristUserActionTuple {\n return [\"RemoveRecord\", tableId, rowId]\n}\n\n/** `[\"BulkUpdateRecord\", tableId, rowIds, { col: value[], ... }]` — column arrays must align with `rowIds`. */\nexport function gristBulkUpdateRecordAction(\n tableId: string,\n rowIds: number[],\n columnarPatch: Record<string, unknown[]>\n): GristUserActionTuple {\n return [\"BulkUpdateRecord\", tableId, rowIds, columnarPatch]\n}\n\n/**\n * `[\"BulkAddRecord\", tableId, rowIds | nullArray, { col: value[], ... }]`.\n * Pass `null` (or omit) for `rowIds` to let Grist allocate ids; in user-action\n * format Grist expects an array of nulls of the same length as the value arrays.\n */\nexport function gristBulkAddRecordAction(\n tableId: string,\n values: Record<string, unknown[]>,\n rowIds?: Array<number | null>\n): GristUserActionTuple {\n const length = inferBulkLength(values)\n const ids = rowIds ?? new Array<number | null>(length).fill(null)\n return [\"BulkAddRecord\", tableId, ids, values]\n}\n\n/** `[\"BulkRemoveRecord\", tableId, rowIds]` */\nexport function gristBulkRemoveRecordAction(tableId: string, rowIds: number[]): GristUserActionTuple {\n return [\"BulkRemoveRecord\", tableId, rowIds]\n}\n\n/** `[\"AddColumn\", tableId, colId, colInfo]` — `colInfo` shape depends on Grist version / column type. */\nexport function gristAddColumnAction(tableId: string, colId: string, colInfo: Record<string, unknown>): GristUserActionTuple {\n return [\"AddColumn\", tableId, colId, colInfo]\n}\n\n/** `[\"AddVisibleColumn\", tableId, colId, colInfo]` — like `AddColumn` but also adds the column to the current view section. */\nexport function gristAddVisibleColumnAction(tableId: string, colId: string, colInfo: Record<string, unknown>): GristUserActionTuple {\n return [\"AddVisibleColumn\", tableId, colId, colInfo]\n}\n\n/** `[\"RenameColumn\", tableId, colId, newColId]` */\nexport function gristRenameColumnAction(tableId: string, colId: string, newColId: string): GristUserActionTuple {\n return [\"RenameColumn\", tableId, colId, newColId]\n}\n\n/** `[\"ModifyColumn\", tableId, colId, colInfo]` */\nexport function gristModifyColumnAction(tableId: string, colId: string, colInfo: Record<string, unknown>): GristUserActionTuple {\n return [\"ModifyColumn\", tableId, colId, colInfo]\n}\n\n/** `[\"RemoveColumn\", tableId, colId]` */\nexport function gristRemoveColumnAction(tableId: string, colId: string): GristUserActionTuple {\n return [\"RemoveColumn\", tableId, colId]\n}\n\n/** `[\"AddTable\", tableId, tableInfo]` */\nexport function gristAddTableAction(tableId: string, tableInfo: Record<string, unknown>): GristUserActionTuple {\n return [\"AddTable\", tableId, tableInfo]\n}\n\n/** `[\"RenameTable\", tableId, newTableId]` */\nexport function gristRenameTableAction(tableId: string, newTableId: string): GristUserActionTuple {\n return [\"RenameTable\", tableId, newTableId]\n}\n\n/** `[\"RemoveTable\", tableId]` */\nexport function gristRemoveTableAction(tableId: string): GristUserActionTuple {\n return [\"RemoveTable\", tableId]\n}\n\n/** `[\"DuplicateTable\", tableId, newTableId, options?]` */\nexport function gristDuplicateTableAction(\n tableId: string,\n newTableId: string,\n options?: Record<string, unknown>\n): GristUserActionTuple {\n return options ? [\"DuplicateTable\", tableId, newTableId, options] : [\"DuplicateTable\", tableId, newTableId]\n}\n\n/** `[\"ReplaceTableData\", tableId, rowIds, columnarData]` */\nexport function gristReplaceTableDataAction(\n tableId: string,\n rowIds: Array<number | null>,\n columnarData: Record<string, unknown[]>\n): GristUserActionTuple {\n return [\"ReplaceTableData\", tableId, rowIds, columnarData]\n}\n\n/** `[\"SetDocumentInfo\", info]` */\nexport function gristSetDocumentInfoAction(info: Record<string, unknown>): GristUserActionTuple {\n return [\"SetDocumentInfo\", info]\n}\n\nfunction inferBulkLength(values: Record<string, unknown[]>): number {\n for (const arr of Object.values(values)) if (Array.isArray(arr)) return arr.length\n return 0\n}\n","/**\n * Encoding helpers for Grist cell values.\n *\n * Grist sends some types as marshalled tuples or numeric epochs. This file\n * exposes pure decode/encode functions so widget code never has to spelunk\n * Grist's wire format.\n *\n * References: Grist `grist/RowRecord` and `grist/DocActions` typings.\n */\n\nimport type { GristReplicaColumn } from \"../types\"\n\n/** `[\"E\", \"ErrorClass\", \"message\", traceback?]` — Grist error sentinel. */\nexport type GristEncodedError = [\"E\", string, string?, string?]\n/** `[\"L\", ...items]` — list (used for ChoiceList, RefList, Attachments). */\nexport type GristEncodedList = [\"L\", ...unknown[]]\n/** `[\"D\", epochSeconds, \"tz/Name\"]` — DateTime with timezone. */\nexport type GristEncodedDateTime = [\"D\", number, string]\n/** `[\"d\", epochSeconds]` — Date (no time). */\nexport type GristEncodedDate = [\"d\", number]\n/** `[\"R\", tableId, rowId]` — explicit reference. */\nexport type GristEncodedRef = [\"R\", string, number]\n/** `[\"r\", rowId]` — bare reference (when table is contextual). */\nexport type GristEncodedBareRef = [\"r\", number]\n/** `[\"P\", encoded]` — pending marker. */\nexport type GristEncodedPending = [\"P\"]\n\nexport type GristDecodedValue =\n | null\n | string\n | number\n | boolean\n | Date\n | GristDecodedValue[]\n | { __error: true; type: string; message?: string; traceback?: string }\n | { __ref: true; tableId?: string; rowId: number }\n\nconst MARSHALLED_TAGS = new Set([\"E\", \"L\", \"D\", \"d\", \"R\", \"r\", \"P\"])\n\nfunction isMarshalledTuple(value: unknown): value is unknown[] {\n return (\n Array.isArray(value) &&\n typeof value[0] === \"string\" &&\n value[0].length <= 2 &&\n MARSHALLED_TAGS.has(value[0])\n )\n}\n\n/**\n * Normalises a Grist `fetchTable` cell that points at a row in another table.\n * Most payloads use a bare numeric row id, but `Ref:*` values may appear as\n * `[\"R\", tableId, rowId]` or `[\"r\", rowId]` (same shape as {@link decodeGristValue}).\n */\nexport function coerceGristDocApiRefRowId(value: unknown): number | undefined {\n if (typeof value === \"number\" && Number.isFinite(value)) return value\n if (!Array.isArray(value) || value.length < 2) return undefined\n const tag = value[0]\n if (tag === \"R\" && typeof value[2] === \"number\" && Number.isFinite(value[2])) return value[2]\n if (tag === \"r\" && typeof value[1] === \"number\" && Number.isFinite(value[1])) return value[1]\n return undefined\n}\n\nfunction looksLikeAttachmentColumn(column?: GristReplicaColumn): boolean {\n return column?.type === \"Attachments\"\n}\n\nfunction looksLikeChoiceListColumn(column?: GristReplicaColumn): boolean {\n return column?.type === \"ChoiceList\"\n}\n\nfunction looksLikeRefListColumn(column?: GristReplicaColumn): boolean {\n return typeof column?.type === \"string\" && column.type.startsWith(\"RefList:\")\n}\n\nfunction looksLikeRefColumn(column?: GristReplicaColumn): boolean {\n return typeof column?.type === \"string\" && column.type.startsWith(\"Ref:\")\n}\n\nfunction looksLikeDateColumn(column?: GristReplicaColumn): boolean {\n return column?.type === \"Date\"\n}\n\nfunction looksLikeDateTimeColumn(column?: GristReplicaColumn): boolean {\n return typeof column?.type === \"string\" && column.type.startsWith(\"DateTime\")\n}\n\n/**\n * Decodes a Grist cell value into an idiomatic JS value.\n *\n * Pass the column's metadata (from `useGristSchema` or `_grist_Tables_column`)\n * for type-aware decoding (e.g. `Date` columns return `Date` even when stored as a number).\n */\nexport function decodeGristValue(value: unknown, column?: GristReplicaColumn): GristDecodedValue {\n if (value === null || value === undefined) return null\n\n if (isMarshalledTuple(value)) {\n const tag = value[0] as string\n switch (tag) {\n case \"E\":\n return {\n __error: true,\n type: typeof value[1] === \"string\" ? value[1] : \"Error\",\n message: typeof value[2] === \"string\" ? value[2] : undefined,\n traceback: typeof value[3] === \"string\" ? value[3] : undefined,\n }\n case \"L\":\n return value.slice(1).map((v) => decodeGristValue(v, undefined)) as GristDecodedValue[]\n case \"D\":\n if (typeof value[1] === \"number\") return new Date(value[1] * 1000)\n return null\n case \"d\":\n if (typeof value[1] === \"number\") return new Date(value[1] * 1000)\n return null\n case \"R\":\n return {\n __ref: true,\n tableId: typeof value[1] === \"string\" ? value[1] : undefined,\n rowId: typeof value[2] === \"number\" ? value[2] : 0,\n }\n case \"r\":\n return { __ref: true, rowId: typeof value[1] === \"number\" ? value[1] : 0 }\n case \"P\":\n return null\n default:\n return null\n }\n }\n\n if (looksLikeDateColumn(column) || looksLikeDateTimeColumn(column)) {\n if (typeof value === \"number\" && Number.isFinite(value)) return new Date(value * 1000)\n }\n\n if (looksLikeRefColumn(column) && typeof value === \"number\") {\n return { __ref: true, rowId: value }\n }\n\n if ((looksLikeRefListColumn(column) || looksLikeChoiceListColumn(column) || looksLikeAttachmentColumn(column)) && Array.isArray(value)) {\n return value.map((v) => decodeGristValue(v, undefined)) as GristDecodedValue[]\n }\n\n if (typeof value === \"string\" || typeof value === \"number\" || typeof value === \"boolean\") return value\n if (Array.isArray(value)) return value.map((v) => decodeGristValue(v, undefined)) as GristDecodedValue[]\n return null\n}\n\n/** Convenience: true when the cell is a Grist error sentinel. */\nexport function isGristCellError(value: unknown): value is GristEncodedError {\n return Array.isArray(value) && value[0] === \"E\"\n}\n\n/**\n * Encodes a JS value into Grist's expected wire form for a given column type.\n *\n * Use before passing values to `applyUserActions` or `TableOperations.update`\n * for `Date`, `DateTime`, `Ref`, `RefList`, and `ChoiceList` columns.\n */\nexport function encodeGristValue(value: unknown, column?: GristReplicaColumn): unknown {\n if (value === null || value === undefined) return null\n\n if (looksLikeDateColumn(column) || looksLikeDateTimeColumn(column)) {\n if (value instanceof Date) return Math.floor(value.getTime() / 1000)\n if (typeof value === \"string\") {\n const parsed = Date.parse(value)\n if (!Number.isNaN(parsed)) return Math.floor(parsed / 1000)\n }\n if (typeof value === \"number\") return Math.floor(value)\n }\n\n if (looksLikeRefColumn(column)) {\n if (typeof value === \"number\") return value\n if (value && typeof value === \"object\" && \"rowId\" in value) {\n return (value as { rowId: number }).rowId\n }\n }\n\n if (looksLikeRefListColumn(column) || looksLikeChoiceListColumn(column) || looksLikeAttachmentColumn(column)) {\n if (Array.isArray(value)) {\n const items = value.map((v) => {\n if (v && typeof v === \"object\" && \"rowId\" in v) return (v as { rowId: number }).rowId\n return v\n })\n return [\"L\", ...items]\n }\n }\n\n return value\n}\n","import { z, type ZodType } from \"zod\"\n\nimport type { GristReplicaColumn, GristRowRecord } from \"../types\"\nimport type { GristColumnarTable } from \"./grist-table-data\"\nimport { normalizeGristChoiceListEntries, type GristChoiceListEntry } from \"./grist-widget-options\"\nimport { decodeGristValue, type GristDecodedValue } from \"./grist-value-codec\"\n\nexport type GristSafeParseIssueCode =\n | \"grist_error\"\n | \"invalid_format\"\n | \"type_mismatch\"\n | \"helper_mismatch\"\n | \"zod_error\"\n\nexport type GristSafeParseIssue = {\n code: GristSafeParseIssueCode\n message: string\n}\n\nexport type GristSafeRefValue = {\n rowId: number\n display?: string | null\n}\n\nexport type GristSafeRefListValue = {\n rowIds: number[]\n displays?: string[]\n}\n\nexport const gristSafeRefValueSchema = z.object({\n rowId: z.number(),\n display: z.string().nullable().optional(),\n})\n\nexport const gristSafeRefListValueSchema = z.object({\n rowIds: z.array(z.number()),\n displays: z.array(z.string()).optional(),\n})\n\nexport const gristCellValueSchema = z.union([\n z.null(),\n z.string(),\n z.number(),\n z.boolean(),\n z.date(),\n z.array(z.unknown()),\n gristSafeRefValueSchema,\n gristSafeRefListValueSchema,\n])\n\nexport type GristCellValue = z.infer<typeof gristCellValueSchema>\n\nexport type GristSafeParseCellResult<TValue = unknown> = {\n ok: boolean\n typedValue: TValue | null\n displayValue: unknown\n raw: unknown\n issues: GristSafeParseIssue[]\n}\n\ntype RefMode = \"id\" | \"detailed\"\n\nexport type SafeParseGristValuesOptions<TValue = unknown> = {\n values: readonly unknown[]\n column?: GristReplicaColumn\n helperValues?: readonly unknown[]\n schema?: ZodType<TValue>\n fallbackUntypedToString?: boolean\n refMode?: RefMode\n}\n\nexport type SafeParseGristTableDataOptions = {\n columns?: Record<string, GristReplicaColumn>\n displayHelpersByRef?: Record<string, string>\n order?: \"none\" | \"id\" | \"grist\"\n fallbackUntypedToString?: boolean\n refMode?: RefMode\n}\n\nexport type GristSafeTableCellIssue = GristSafeParseIssue & {\n rowIndex: number\n colId: string\n}\n\nexport type GristSafeChoiceTokenMeta = {\n value: string\n label: string\n fillColor?: string\n textColor?: string\n valid: boolean\n}\n\nexport type GristSafeTableCellMeta = {\n rowIndex: number\n colId: string\n ok: boolean\n issues: GristSafeParseIssue[]\n choiceTokens?: GristSafeChoiceTokenMeta[]\n}\n\nexport type SafeParseGristTableDataResult<TRow extends Record<string, unknown> = Record<string, unknown>> = {\n rows: GristRowRecord<TRow>[]\n cellIssues: GristSafeTableCellIssue[]\n cellMeta: GristSafeTableCellMeta[]\n cellMetaByKey: Record<string, GristSafeTableCellMeta>\n cellResultsByColumn: Record<string, GristSafeParseCellResult[]>\n}\n\n/** Maps `Ref:*` / `RefList:*` column ids to their paired `gristHelper_*` column id (same rules as safe-parse). */\nexport function inferDisplayHelpersByRef(\n columns: Record<string, GristReplicaColumn>\n): Record<string, string> {\n const out: Record<string, string> = {}\n const helperColumns = Object.entries(columns).filter(([colId]) =>\n colId.startsWith(\"gristHelper_\")\n )\n const usedHelpers = new Set<string>()\n const colRefToColId = new Map<number, string>()\n for (const [colId, col] of Object.entries(columns)) {\n if (typeof col?.colRef === \"number\" && Number.isFinite(col.colRef)) {\n colRefToColId.set(col.colRef, colId)\n }\n }\n for (const [colId, col] of Object.entries(columns)) {\n const type = col?.type\n if (typeof type !== \"string\" || (!type.startsWith(\"Ref:\") && !type.startsWith(\"RefList:\"))) {\n continue\n }\n if (typeof col.displayColRef === \"number\" && Number.isFinite(col.displayColRef)) {\n const helperColId = colRefToColId.get(col.displayColRef)\n if (helperColId) {\n out[colId] = helperColId\n usedHelpers.add(helperColId)\n continue\n }\n }\n const match = helperColumns.find(([, helper]) => {\n const formula = helper?.formula\n return typeof formula === \"string\" && (formula.includes(`$${colId}.`) || formula.includes(`$${colId}`))\n })\n if (match) {\n out[colId] = match[0]\n usedHelpers.add(match[0])\n }\n }\n\n // Secondary fallback for schemas where helper formulas are unavailable:\n // pair unresolved Ref/RefList columns with nearest following gristHelper_*.\n const unresolvedRefs = Object.entries(columns)\n .filter(([colId, col]) => {\n const type = col?.type\n return (\n !out[colId] &&\n typeof type === \"string\" &&\n (type.startsWith(\"Ref:\") || type.startsWith(\"RefList:\"))\n )\n })\n .sort((a, b) => (a[1]?.parentPos ?? Number.MAX_SAFE_INTEGER) - (b[1]?.parentPos ?? Number.MAX_SAFE_INTEGER))\n const helperByPos = helperColumns\n .filter(([helperId]) => !usedHelpers.has(helperId))\n .sort((a, b) => (a[1]?.parentPos ?? Number.MAX_SAFE_INTEGER) - (b[1]?.parentPos ?? Number.MAX_SAFE_INTEGER))\n for (const [refId, refCol] of unresolvedRefs) {\n const refPos = refCol?.parentPos ?? Number.MAX_SAFE_INTEGER\n const candidate = helperByPos.find(([, helperCol]) => {\n const helperPos = helperCol?.parentPos ?? Number.MAX_SAFE_INTEGER\n return helperPos > refPos\n })\n if (!candidate) continue\n out[refId] = candidate[0]\n usedHelpers.add(candidate[0])\n }\n // If multiple unresolved refs remain, greedily map by order.\n for (const [refId] of unresolvedRefs) {\n if (out[refId]) continue\n const candidate = helperByPos.find(([helperId]) => !usedHelpers.has(helperId))\n if (!candidate) continue\n out[refId] = candidate[0]\n usedHelpers.add(candidate[0])\n }\n return out\n}\n\nfunction computeRowCount(data: Record<string, unknown[]>): number {\n const idCol = data.id\n if (Array.isArray(idCol)) return idCol.length\n for (const key of Object.keys(data)) {\n const col = data[key]\n if (Array.isArray(col)) return col.length\n }\n return 0\n}\n\nfunction manualSortKey(row: Record<string, unknown>): number | null {\n const v = row.manualSort\n if (typeof v === \"number\" && !Number.isNaN(v)) return v\n if (typeof v === \"string\" && v.trim() !== \"\") {\n const n = Number(v)\n if (!Number.isNaN(n)) return n\n }\n return null\n}\n\nfunction sortRowsByGristOrder<T = Record<string, unknown>>(\n rows: readonly GristRowRecord<T>[]\n): GristRowRecord<T>[] {\n const hasManual = rows.some((r) => manualSortKey(r as Record<string, unknown>) !== null)\n if (!hasManual) {\n return [...rows].sort((a, b) => {\n const ida = typeof a.id === \"number\" ? a.id : 0\n const idb = typeof b.id === \"number\" ? b.id : 0\n return ida - idb\n })\n }\n return [...rows].sort((a, b) => {\n const ma = manualSortKey(a as Record<string, unknown>)\n const mb = manualSortKey(b as Record<string, unknown>)\n if (ma !== null && mb !== null && ma !== mb) return ma - mb\n if (ma !== null && mb === null) return -1\n if (ma === null && mb !== null) return 1\n const ida = typeof a.id === \"number\" ? a.id : 0\n const idb = typeof b.id === \"number\" ? b.id : 0\n return ida - idb\n })\n}\n\nfunction columnKind(column?: GristReplicaColumn):\n | \"text\"\n | \"numeric\"\n | \"int\"\n | \"bool\"\n | \"date\"\n | \"datetime\"\n | \"choice\"\n | \"choiceList\"\n | \"ref\"\n | \"refList\"\n | \"attachments\"\n | \"any\"\n | \"unknown\" {\n const type = column?.type\n if (!type) return \"unknown\"\n if (type === \"Text\") return \"text\"\n if (type === \"Numeric\") return \"numeric\"\n if (type === \"Int\") return \"int\"\n if (type === \"Bool\") return \"bool\"\n if (type === \"Date\") return \"date\"\n if (type === \"DateTime\" || type.startsWith(\"DateTime:\")) return \"datetime\"\n if (type === \"Choice\") return \"choice\"\n if (type === \"ChoiceList\") return \"choiceList\"\n if (type.startsWith(\"Ref:\")) return \"ref\"\n if (type.startsWith(\"RefList:\")) return \"refList\"\n if (type === \"Attachments\") return \"attachments\"\n if (type === \"Any\") return \"any\"\n return \"unknown\"\n}\n\nfunction primitiveToString(value: unknown): string | null {\n if (value === null || value === undefined) return null\n if (typeof value === \"string\") return value\n if (typeof value === \"number\" || typeof value === \"boolean\" || typeof value === \"bigint\") {\n return String(value)\n }\n return null\n}\n\nfunction normalizeRefDisplay(value: unknown): { value?: string | null; issue?: GristSafeParseIssue } {\n if (value === null || value === undefined) return { value: null }\n if (typeof value === \"string\") return { value }\n if (typeof value === \"number\" || typeof value === \"boolean\") return { value: String(value) }\n return {\n issue: {\n code: \"helper_mismatch\",\n message: \"Ref display helper value should be a scalar value.\",\n },\n }\n}\n\nfunction normalizeRefListDisplays(value: unknown): { values?: string[]; issue?: GristSafeParseIssue } {\n if (value === null || value === undefined) return { values: [] }\n if (typeof value === \"string\") {\n const s = value.trim()\n if (s === \"\") return { values: [] }\n if (s.startsWith(\"[\")) {\n try {\n const parsed = JSON.parse(s)\n if (Array.isArray(parsed)) {\n return normalizeRefListDisplays(parsed)\n }\n } catch {\n // Keep existing error below for non-array/non-JSON helper strings.\n }\n }\n }\n if (!Array.isArray(value)) {\n return {\n issue: {\n code: \"helper_mismatch\",\n message: \"RefList display helper value should be an array.\",\n },\n }\n }\n // Grist list wire format may leak through as [\"L\", ...items].\n // Treat a leading \"L\" marker as metadata, not a display label.\n const items =\n value.length > 0 && value[0] === \"L\"\n ? value.slice(1)\n : value\n const out: string[] = []\n for (const item of items) {\n const s = primitiveToString(item)\n if (s === null) {\n return {\n issue: {\n code: \"helper_mismatch\",\n message: \"RefList helper items should be scalar values.\",\n },\n }\n }\n out.push(s)\n }\n return { values: out }\n}\n\nfunction normalizeRefValue(\n decoded: GristDecodedValue,\n helperValue: unknown,\n refMode: RefMode\n): { value: number | GristSafeRefValue | null; issues: GristSafeParseIssue[] } {\n const issues: GristSafeParseIssue[] = []\n if (decoded === null) return { value: null, issues }\n\n const rowId =\n typeof decoded === \"number\" && Number.isFinite(decoded)\n ? decoded\n : decoded && typeof decoded === \"object\" && \"__ref\" in decoded\n ? (decoded as { rowId: number }).rowId\n : null\n\n // Grist empty refs can surface as 0; treat as empty cell.\n if (rowId === 0) return { value: null, issues }\n if (rowId === null || !Number.isFinite(rowId)) {\n issues.push({\n code: \"type_mismatch\",\n message: \"Expected a Ref row id.\",\n })\n return { value: null, issues }\n }\n\n if (refMode === \"id\") return { value: rowId, issues }\n\n const helper = normalizeRefDisplay(helperValue)\n if (helper.issue) issues.push(helper.issue)\n return {\n value: { rowId, display: helper.value },\n issues,\n }\n}\n\nfunction normalizeRefListValue(\n decoded: GristDecodedValue,\n helperValue: unknown,\n refMode: RefMode\n): { value: number[] | GristSafeRefListValue | null; issues: GristSafeParseIssue[] } {\n const issues: GristSafeParseIssue[] = []\n if (decoded === null) return { value: null, issues }\n if (!Array.isArray(decoded)) {\n issues.push({\n code: \"type_mismatch\",\n message: \"Expected an array for RefList.\",\n })\n return { value: null, issues }\n }\n\n const rowIds: number[] = []\n for (const item of decoded) {\n if (typeof item === \"number\" && Number.isFinite(item) && item !== 0) {\n rowIds.push(item)\n continue\n }\n if (item && typeof item === \"object\" && \"__ref\" in item) {\n const rowId = (item as { rowId: number }).rowId\n if (typeof rowId === \"number\" && Number.isFinite(rowId) && rowId !== 0) {\n rowIds.push(rowId)\n continue\n }\n }\n issues.push({\n code: \"type_mismatch\",\n message: \"RefList items should be row ids.\",\n })\n return { value: null, issues }\n }\n\n if (refMode === \"id\") return { value: rowIds, issues }\n\n const helper = normalizeRefListDisplays(helperValue)\n if (helper.issue) {\n issues.push(helper.issue)\n } else if (helper.values && helper.values.length > 0 && helper.values.length !== rowIds.length) {\n issues.push({\n code: \"helper_mismatch\",\n message: \"RefList helper labels count does not match row ids count.\",\n })\n }\n return {\n value: { rowIds, displays: helper.values },\n issues,\n }\n}\n\nfunction normalizeByColumnKind(\n decoded: GristDecodedValue,\n kind: ReturnType<typeof columnKind>,\n helperValue: unknown,\n fallbackUntypedToString: boolean,\n refMode: RefMode\n): { value: unknown; issues: GristSafeParseIssue[] } {\n const issues: GristSafeParseIssue[] = []\n if (decoded && typeof decoded === \"object\" && \"__error\" in decoded) {\n issues.push({\n code: \"grist_error\",\n message: (decoded as { message?: string }).message ?? \"Grist returned a cell error.\",\n })\n return { value: null, issues }\n }\n\n switch (kind) {\n case \"ref\":\n return normalizeRefValue(decoded, helperValue, refMode)\n case \"refList\":\n return normalizeRefListValue(decoded, helperValue, refMode)\n case \"date\":\n case \"datetime\":\n if (decoded === null || decoded instanceof Date) return { value: decoded, issues }\n issues.push({ code: \"type_mismatch\", message: \"Expected a Date value.\" })\n return { value: null, issues }\n case \"bool\":\n if (decoded === null || typeof decoded === \"boolean\") return { value: decoded, issues }\n issues.push({ code: \"type_mismatch\", message: \"Expected a boolean value.\" })\n return { value: null, issues }\n case \"numeric\":\n if (decoded === null || (typeof decoded === \"number\" && Number.isFinite(decoded))) {\n return { value: decoded, issues }\n }\n issues.push({ code: \"type_mismatch\", message: \"Expected a numeric value.\" })\n return {\n value:\n typeof decoded === \"string\" ||\n typeof decoded === \"number\" ||\n typeof decoded === \"boolean\"\n ? decoded\n : null,\n issues,\n }\n case \"int\":\n if (decoded === null || (typeof decoded === \"number\" && Number.isInteger(decoded))) {\n return { value: decoded, issues }\n }\n issues.push({ code: \"type_mismatch\", message: \"Expected an integer value.\" })\n return {\n value:\n typeof decoded === \"string\" ||\n typeof decoded === \"number\" ||\n typeof decoded === \"boolean\"\n ? decoded\n : null,\n issues,\n }\n case \"choice\":\n case \"text\":\n if (decoded === null || typeof decoded === \"string\") return { value: decoded, issues }\n issues.push({ code: \"type_mismatch\", message: \"Expected a string value.\" })\n return { value: null, issues }\n case \"choiceList\":\n case \"attachments\":\n if (decoded === null || Array.isArray(decoded)) return { value: decoded, issues }\n issues.push({ code: \"type_mismatch\", message: \"Expected a list value.\" })\n return { value: null, issues }\n case \"any\":\n case \"unknown\":\n // Heuristic fallback: when schema typing is missing but a helper column is\n // attached, infer Ref/RefList from the decoded shape.\n if (helperValue !== undefined) {\n if (typeof decoded === \"number\" || (decoded && typeof decoded === \"object\" && \"__ref\" in decoded)) {\n return normalizeRefValue(decoded, helperValue, refMode)\n }\n if (Array.isArray(decoded)) {\n return normalizeRefListValue(decoded, helperValue, refMode)\n }\n }\n if (!fallbackUntypedToString) return { value: decoded, issues }\n if (decoded === null) return { value: null, issues }\n if (typeof decoded === \"string\") return { value: decoded, issues }\n if (\n typeof decoded === \"number\" ||\n typeof decoded === \"boolean\" ||\n decoded instanceof Date\n ) {\n return {\n value: decoded instanceof Date ? decoded.toISOString() : String(decoded),\n issues,\n }\n }\n if (Array.isArray(decoded)) {\n try {\n return { value: JSON.stringify(decoded), issues }\n } catch {\n issues.push({\n code: \"invalid_format\",\n message: \"Unable to stringify list value.\",\n })\n return { value: null, issues }\n }\n }\n issues.push({\n code: \"type_mismatch\",\n message: \"Untyped value could not be represented as a string.\",\n })\n return { value: null, issues }\n }\n}\n\nfunction buildChoiceTokens(\n col: GristReplicaColumn | undefined,\n value: unknown\n): GristSafeChoiceTokenMeta[] | undefined {\n const type = col?.type\n if (type !== \"Choice\" && type !== \"ChoiceList\") return undefined\n const entries = normalizeGristChoiceListEntries(col?.widgetOptions)\n const byValue = new Map<string, GristChoiceListEntry>(entries.map((entry) => [entry.value, entry]))\n\n if (type === \"Choice\") {\n if (value == null || typeof value !== \"string\" || value.trim() === \"\") return []\n const meta = byValue.get(value)\n if (!meta) return [{ value, label: value, valid: false }]\n return [\n {\n value,\n label: meta.label,\n fillColor: meta.fillColor,\n textColor: meta.textColor,\n valid: true,\n },\n ]\n }\n\n if (!Array.isArray(value)) return []\n const out: GristSafeChoiceTokenMeta[] = []\n for (const item of value) {\n const v = typeof item === \"string\" ? item : String(item)\n const meta = byValue.get(v)\n if (!meta) {\n out.push({ value: v, label: v, valid: false })\n continue\n }\n out.push({\n value: v,\n label: meta.label,\n fillColor: meta.fillColor,\n textColor: meta.textColor,\n valid: true,\n })\n }\n return out\n}\n\nexport function safeParseGristValues<TValue = unknown>(\n options: SafeParseGristValuesOptions<TValue>\n): GristSafeParseCellResult<TValue>[] {\n const fallbackUntypedToString = options.fallbackUntypedToString ?? true\n const refMode = options.refMode ?? \"id\"\n const kind = columnKind(options.column)\n return options.values.map((raw, index) => {\n const decoded = decodeGristValue(raw, options.column)\n const helperValue = options.helperValues?.[index]\n const normalized = normalizeByColumnKind(\n decoded,\n kind,\n helperValue,\n fallbackUntypedToString,\n refMode\n )\n const issues = [...normalized.issues]\n\n if (options.schema) {\n const result = options.schema.safeParse(normalized.value)\n if (!result.success) {\n issues.push({\n code: \"zod_error\",\n message: result.error.issues.map((issue) => issue.message).join(\"; \"),\n })\n }\n }\n\n return {\n ok: issues.length === 0,\n typedValue: normalized.value as TValue | null,\n displayValue: normalized.value,\n raw,\n issues,\n }\n })\n}\n\nexport function safeParseGristTableData<TRow extends Record<string, unknown> = Record<string, unknown>>(\n data: GristColumnarTable,\n options?: SafeParseGristTableDataOptions\n): SafeParseGristTableDataResult<TRow> {\n const columns = options?.columns ?? {}\n const inferredDisplayHelpersByRef = inferDisplayHelpersByRef(columns)\n const displayHelpersByRef: Record<string, string> = {\n ...inferredDisplayHelpersByRef,\n ...(options?.displayHelpersByRef ?? {}),\n }\n // Fallback convention: if `owner` exists and `gristHelper_owner` is fetched,\n // use it as display helper even when formula metadata is unavailable.\n for (const [colId, col] of Object.entries(columns)) {\n const type = col?.type\n if (typeof type !== \"string\" || (!type.startsWith(\"Ref:\") && !type.startsWith(\"RefList:\"))) {\n continue\n }\n if (displayHelpersByRef[colId]) continue\n const byName = `gristHelper_${colId}`\n if (Array.isArray(data[byName])) {\n displayHelpersByRef[colId] = byName\n }\n }\n const fallbackUntypedToString = options?.fallbackUntypedToString ?? true\n const refMode = options?.refMode ?? \"id\"\n const colIds = Object.keys(data)\n const parsedByColumn: Record<string, GristSafeParseCellResult[]> = {}\n\n for (const colId of colIds) {\n const rawValues = Array.isArray(data[colId]) ? data[colId] : []\n const helperColId = displayHelpersByRef[colId]\n const helperValues = helperColId && Array.isArray(data[helperColId]) ? data[helperColId] : undefined\n parsedByColumn[colId] =\n colId === \"id\"\n ? rawValues.map((raw) => ({\n ok: typeof raw === \"number\" && Number.isFinite(raw),\n typedValue: typeof raw === \"number\" && Number.isFinite(raw) ? raw : null,\n displayValue: raw,\n raw,\n issues:\n typeof raw === \"number\" && Number.isFinite(raw)\n ? []\n : [{ code: \"type_mismatch\", message: \"Row id should be numeric.\" }],\n }))\n : safeParseGristValues({\n values: rawValues,\n column: columns[colId],\n helperValues,\n fallbackUntypedToString,\n refMode,\n })\n }\n\n const rowCount = computeRowCount(data)\n const rows: GristRowRecord<TRow>[] = []\n const cellIssues: GristSafeTableCellIssue[] = []\n const cellMeta: GristSafeTableCellMeta[] = []\n for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {\n const row: Record<string, unknown> = {}\n for (const colId of colIds) {\n const parsed = parsedByColumn[colId]?.[rowIndex]\n row[colId] = parsed?.typedValue ?? null\n const choiceTokens = buildChoiceTokens(columns[colId], parsed?.displayValue)\n if (parsed) {\n if (parsed.issues.length > 0 || (choiceTokens && choiceTokens.length > 0)) {\n cellMeta.push({\n rowIndex,\n colId,\n ok: parsed.ok,\n issues: parsed.issues,\n choiceTokens,\n })\n }\n }\n if (!parsed || parsed.issues.length === 0) continue\n for (const issue of parsed.issues) {\n cellIssues.push({ ...issue, rowIndex, colId })\n }\n }\n rows.push(row as GristRowRecord<TRow>)\n }\n\n const cellMetaByKey: Record<string, GristSafeTableCellMeta> = {}\n for (const meta of cellMeta) {\n const rowId = rows[meta.rowIndex]?.id\n if (typeof rowId !== \"number\") continue\n cellMetaByKey[`${rowId}:${meta.colId}`] = meta\n }\n\n const order = options?.order ?? \"grist\"\n if (order === \"none\") {\n return { rows, cellIssues, cellMeta, cellMetaByKey, cellResultsByColumn: parsedByColumn }\n }\n if (order === \"id\") {\n return {\n rows: [...rows].sort((a, b) => {\n const ida = typeof a.id === \"number\" ? a.id : 0\n const idb = typeof b.id === \"number\" ? b.id : 0\n return ida - idb\n }),\n cellIssues,\n cellMeta,\n cellMetaByKey,\n cellResultsByColumn: parsedByColumn,\n }\n }\n return {\n rows: sortRowsByGristOrder(rows),\n cellIssues,\n cellMeta,\n cellMetaByKey,\n cellResultsByColumn: parsedByColumn,\n }\n}\n","import { inferDisplayHelpersByRef } from \"../../sdk/helpers/grist-safe-parse\"\nimport { normalizeReplicaDocumentInput } from \"../../sdk/helpers/replica-normalize\"\nimport { coerceGristDocApiRefRowId } from \"../../sdk/helpers/grist-value-codec\"\nimport type { GristReplicaDocument, GristReplicaTable } from \"../../sdk/replica-document\"\nimport type { GristRowRecord } from \"../../sdk/types\"\nimport type { EmulatorColumn, EmulatorDocumentState, EmulatorTable } from \"./types\"\n\nconst SYSTEM_COLUMN = \"id\"\n\nconst EMULATOR_JS_FORMULA_PLACEHOLDER = \"(emulator JavaScript formula)\"\n\n/** Replica table for {@link createDocument}; columns may include `formulaFn` / `inferredFormula` (emulator-only). */\nexport type CreateDocumentTableInput = Omit<GristReplicaTable, \"columns\"> & {\n columns?: Record<string, EmulatorColumn>\n}\n\n/** Replica document for {@link createDocument}; widens table columns to {@link EmulatorColumn}. */\nexport type CreateDocumentInput = Omit<GristReplicaDocument, \"tables\"> & {\n tables: Record<string, CreateDocumentTableInput>\n}\n\n/**\n * Ensure `formula` (original) and `inferredFormula` strings exist alongside optional `formulaFn`.\n */\nexport function enrichEmulatorColumnMetadata(col: EmulatorColumn): EmulatorColumn {\n const originalTrimmed = col.formula?.trim() ?? \"\"\n const explicitInferred = col.inferredFormula?.trim() ?? \"\"\n\n if (col.formulaFn) {\n const inferredFormula =\n explicitInferred || originalTrimmed || EMULATOR_JS_FORMULA_PLACEHOLDER\n return {\n ...col,\n ...(originalTrimmed ? { formula: col.formula } : {}),\n inferredFormula,\n }\n }\n\n if (explicitInferred || originalTrimmed) {\n return {\n ...col,\n inferredFormula: explicitInferred || originalTrimmed || undefined,\n }\n }\n\n return col\n}\n\n/** Order merged column metadata for stable `fetchTable` / RDG column order. */\nexport function sortEmulatorColumnsByParentPos(columns: Record<string, EmulatorColumn>): Record<string, EmulatorColumn> {\n const entries = Object.entries(columns)\n entries.sort(([ka, a], [kb, b]) => {\n const pa = a.parentPos\n const pb = b.parentPos\n if (pa !== undefined && pb !== undefined && pa !== pb) return pa - pb\n if (pa !== undefined && pb === undefined) return -1\n if (pa === undefined && pb !== undefined) return 1\n return ka.localeCompare(kb)\n })\n return Object.fromEntries(entries)\n}\n\nfunction inferColumnsFromRows(rows: GristRowRecord[]): Record<string, EmulatorColumn> {\n const columns: Record<string, EmulatorColumn> = {}\n for (const row of rows) {\n for (const key of Object.keys(row)) {\n if (key === SYSTEM_COLUMN) continue\n if (!columns[key]) columns[key] = inferColumnType(row[key])\n }\n }\n return columns\n}\n\nfunction inferColumnType(sample: unknown): EmulatorColumn {\n switch (typeof sample) {\n case \"number\":\n return { type: \"Numeric\" }\n case \"boolean\":\n return { type: \"Bool\" }\n case \"string\":\n return { type: \"Text\" }\n default:\n return { type: \"Any\" }\n }\n}\n\nfunction buildTable(tableId: string, input: CreateDocumentTableInput): EmulatorTable {\n const rows = input.rows ?? []\n const inferred = inferColumnsFromRows(rows)\n const merged: Record<string, EmulatorColumn> = {}\n for (const [colId, col] of Object.entries({ ...inferred, ...(input.columns ?? {}) })) {\n merged[colId] = enrichEmulatorColumnMetadata(col)\n }\n const columns = sortEmulatorColumnsByParentPos(merged)\n return {\n tableId,\n label: input.label,\n columns,\n rows: rows.map((row) => ({ ...row })),\n }\n}\n\n/**\n * Build an immutable initial document state from a user-provided\n * {@link GristReplicaDocument}. The state is **not** frozen — the action\n * engine mutates it in place under deterministic event diffusion.\n */\nexport function createDocument(input: CreateDocumentInput): EmulatorDocumentState {\n if (!input.tables || Object.keys(input.tables).length === 0) {\n throw new Error(\"createDocument: at least one table is required\")\n }\n\n const normalized = normalizeReplicaDocumentInput(input)\n\n const tables = new Map<string, EmulatorTable>()\n for (const [tableId, tableInput] of Object.entries(normalized.tables)) {\n tables.set(tableId, buildTable(tableId, tableInput))\n }\n\n const firstTableId = tables.keys().next().value as string\n const requestedTableId = normalized.selection?.tableId\n const selectedTableId =\n requestedTableId && tables.has(requestedTableId) ? requestedTableId : firstTableId\n const selectedTable = tables.get(selectedTableId)!\n const fallbackRowId =\n typeof selectedTable.rows[0]?.id === \"number\" ? selectedTable.rows[0].id : null\n const selection = {\n tableId: selectedTableId,\n rowId: normalized.selection?.rowId ?? fallbackRowId,\n mappings: normalized.selection?.mappings,\n cursor: normalized.selection?.cursor,\n } as EmulatorDocumentState[\"selection\"]\n\n return {\n docName: normalized.docName ?? \"Untitled emulator document\",\n tables,\n selection,\n widgetOptions: { ...(normalized.widgetOptions ?? {}) },\n widgetInteraction: { ...(normalized.widgetInteraction ?? {}) },\n configuration: normalized.configuration ?? null,\n accessToken: normalized.accessToken ?? null,\n }\n}\n\nexport function getTable(state: EmulatorDocumentState, tableId: string): EmulatorTable {\n const table = state.tables.get(tableId)\n if (!table) throw new Error(`Unknown table: ${tableId}`)\n return table\n}\n\nexport function getSelectedTable(state: EmulatorDocumentState): EmulatorTable {\n return getTable(state, state.selection.tableId)\n}\n\nexport function nextRowId(table: EmulatorTable): number {\n let max = 0\n for (const row of table.rows) {\n if (typeof row.id === \"number\" && row.id > max) max = row.id\n }\n return max + 1\n}\n\nexport function findRow(table: EmulatorTable, rowId: number): GristRowRecord | undefined {\n return table.rows.find((r) => r.id === rowId)\n}\n\nexport function findRowIndex(table: EmulatorTable, rowId: number): number {\n return table.rows.findIndex((r) => r.id === rowId)\n}\n\n/**\n * Re-evaluate every formula column for a given row in-place. Called by the\n * action engine after Add/Update operations.\n */\nexport function evaluateRowFormulas(\n state: EmulatorDocumentState,\n table: EmulatorTable,\n row: GristRowRecord\n): void {\n for (const [colId, col] of Object.entries(table.columns)) {\n if (col.formulaFn) {\n try {\n row[colId] = col.formulaFn(row, { table, document: state })\n } catch (err) {\n row[colId] = formulaError(err)\n }\n }\n }\n}\n\nfunction escapeRegExp(s: string): string {\n return s.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\")\n}\n\n/** Pull stored row ids from a `RefList` cell (`[\"L\", ...]` or legacy shapes). */\nfunction extractRefListIdsFromWire(raw: unknown): number[] {\n if (raw == null) return []\n if (!Array.isArray(raw)) return []\n const items = raw[0] === \"L\" ? raw.slice(1) : raw\n const out: number[] = []\n for (const item of items) {\n if (typeof item === \"number\" && Number.isFinite(item) && item > 0) {\n out.push(Math.trunc(item))\n continue\n }\n if (item && typeof item === \"object\" && \"rowId\" in item) {\n const r = (item as { rowId: unknown }).rowId\n if (typeof r === \"number\" && Number.isFinite(r) && r > 0) {\n out.push(Math.trunc(r))\n continue\n }\n }\n const t = coerceGristDocApiRefRowId(item)\n if (t !== undefined && t > 0) out.push(Math.trunc(t))\n }\n return Array.from(new Set(out))\n}\n\nfunction fieldFromRefListHelperFormula(formula: string | undefined, refColId: string): string | null {\n if (!formula) return null\n const re = new RegExp(`\\\\$${escapeRegExp(refColId)}\\\\.([A-Za-z_]\\\\w*)`)\n const m = formula.match(re)\n return m?.[1] ?? null\n}\n\nfunction firstTextLikeColumnId(columns: Record<string, EmulatorColumn>): string | null {\n let best: { id: string; pos: number } | null = null\n for (const [colId, col] of Object.entries(columns)) {\n if (colId === \"id\" || colId.startsWith(\"gristHelper_\")) continue\n const t = col.type\n if (t !== \"Text\" && t !== \"Choice\") continue\n const pos = col.parentPos ?? 99999\n if (!best || pos < best.pos) best = { id: colId, pos }\n }\n return best?.id ?? null\n}\n\nfunction formatLabelFromCell(value: unknown): string {\n if (value == null) return \"\"\n if (typeof value === \"string\") return value.trim()\n if (typeof value === \"number\" || typeof value === \"boolean\") return String(value)\n if (value instanceof Date) return value.toISOString()\n if (Array.isArray(value) && value[0] === \"E\") return \"\"\n if (typeof value === \"object\" && value && \"__ref\" in value) {\n const r = (value as { rowId?: unknown }).rowId\n return typeof r === \"number\" && Number.isFinite(r) ? String(r) : \"\"\n }\n return \"\"\n}\n\n/**\n * Keep `gristHelper_*` display columns aligned with `RefList:*` ids after writes.\n *\n * Real Grist evaluates helper formulas (`$multiref.Nom`); replica JSON only\n * carries the formula string (no `formulaFn`), so helpers would stay stale and\n * safe-parse would flag `helper_mismatch` with empty labels.\n *\n * Skips helpers that define `formulaFn` so custom JS formulas keep ownership.\n */\nexport function syncRefListDisplayHelpers(\n state: EmulatorDocumentState,\n table: EmulatorTable,\n row: GristRowRecord\n): void {\n const displayHelpersByRef = inferDisplayHelpersByRef(table.columns)\n for (const [colId, col] of Object.entries(table.columns)) {\n const type = col.type\n if (typeof type !== \"string\" || !type.startsWith(\"RefList:\")) continue\n const helperColId = displayHelpersByRef[colId]\n if (!helperColId) continue\n const helperMeta = table.columns[helperColId]\n if (!helperMeta || helperMeta.formulaFn) continue\n\n const targetTableId = type.slice(\"RefList:\".length)\n const targetTable = state.tables.get(targetTableId)\n if (!targetTable) continue\n\n const ids = extractRefListIdsFromWire(row[colId])\n if (ids.length === 0) {\n row[helperColId] = []\n continue\n }\n\n const fieldId =\n fieldFromRefListHelperFormula(helperMeta.formula, colId) ??\n firstTextLikeColumnId(targetTable.columns)\n\n const labels = ids.map((rid) => {\n if (!fieldId) return String(rid)\n const trow = findRow(targetTable, rid)\n if (!trow) return String(rid)\n const lbl = formatLabelFromCell(trow[fieldId])\n return lbl !== \"\" ? lbl : String(rid)\n })\n row[helperColId] = labels\n }\n}\n\nfunction formulaError(err: unknown): unknown {\n // Match Grist's encoded error format: [\"E\", code, message]\n const message = err instanceof Error ? err.message : String(err)\n return [\"E\", \"Error\", message]\n}\n\n/**\n * Synthesise `_grist_Tables` metadata from the emulator's in-memory tables.\n * Returns Grist's columnar wire shape.\n */\nexport function synthesizeTablesMeta(state: EmulatorDocumentState): Record<string, unknown[]> {\n const ids: number[] = []\n const tableIds: string[] = []\n const labels: (string | null)[] = []\n\n let nextId = 1\n for (const [tableId, table] of state.tables.entries()) {\n ids.push(nextId)\n tableIds.push(tableId)\n labels.push(table.label ?? null)\n nextId += 1\n }\n\n return {\n id: ids,\n tableId: tableIds,\n // Optional human-readable label; real Grist keeps more fields, but these\n // are sufficient for `buildReplicaDocumentFromDocApi` to resolve table ids.\n label: labels,\n }\n}\n\n/**\n * Synthesise `_grist_Tables_column` metadata from emulator tables/columns.\n * Mirrors the subset of fields consumed by `buildReplicaDocumentFromDocApi`.\n */\nexport function synthesizeTablesColumnMeta(state: EmulatorDocumentState): Record<string, unknown[]> {\n const parentIds: number[] = []\n const colIds: string[] = []\n const columnIds: number[] = []\n const types: (string | null)[] = []\n const labels: (string | null)[] = []\n const parentPos: (number | null)[] = []\n const isFormula: boolean[] = []\n const formulas: (string | null)[] = []\n const descriptions: (string | null)[] = []\n const widgetOptions: unknown[] = []\n const displayCol: (number | null)[] = []\n const visibleCol: (number | null)[] = []\n\n // Mirror the synthetic table ids from `synthesizeTablesMeta`.\n const tableIdToInternalId = new Map<string, number>()\n {\n let nextId = 1\n for (const [tableId] of state.tables.entries()) {\n tableIdToInternalId.set(tableId, nextId)\n nextId += 1\n }\n }\n\n for (const [tableId, table] of state.tables.entries()) {\n const parentInternalId = tableIdToInternalId.get(tableId)\n if (parentInternalId === undefined) continue\n\n for (const [colId, col] of Object.entries(table.columns)) {\n parentIds.push(parentInternalId)\n colIds.push(colId)\n // Prefer explicit `colRef` when present; fall back to 1-based index of insertion.\n const ref = typeof col.colRef === \"number\" ? col.colRef : columnIds.length + 1\n columnIds.push(ref)\n types.push(col.type ?? null)\n labels.push(col.label ?? null)\n parentPos.push(col.parentPos ?? null)\n isFormula.push(Boolean(col.isFormula))\n const formula = typeof col.formula === \"string\" ? col.formula : null\n formulas.push(formula)\n descriptions.push(col.description ?? null)\n widgetOptions.push(col.widgetOptions ?? null)\n displayCol.push(typeof col.displayColRef === \"number\" ? col.displayColRef : null)\n visibleCol.push(typeof col.visibleColRef === \"number\" ? col.visibleColRef : null)\n }\n }\n\n return {\n parentId: parentIds,\n colId: colIds,\n id: columnIds,\n type: types,\n label: labels,\n parentPos,\n isFormula,\n formula: formulas,\n description: descriptions,\n // Also expose legacy `descr` for robustness.\n descr: descriptions,\n widgetOptions,\n displayCol,\n visibleCol,\n }\n}\n","/**\n * Pure action engine: applies Grist user actions to an\n * {@link EmulatorDocumentState} and emits the resulting events on the bus.\n *\n * This is the single place where state mutates. Every API impl ultimately\n * routes through `applyUserActions` so behaviour is consistent regardless of\n * the surface (`grist.docApi.applyUserActions`, `selectedTable.create`,\n * `setCursorPos`, etc.).\n *\n * Supported user actions:\n * - AddRecord, BulkAddRecord\n * - UpdateRecord, BulkUpdateRecord\n * - RemoveRecord, BulkRemoveRecord\n * - AddColumn, RemoveColumn, RenameColumn, ModifyColumn\n * - AddTable, RemoveTable, RenameTable\n *\n * Unknown actions throw a structured error instead of silently succeeding,\n * which surfaces protocol gaps loudly during widget development.\n */\n\nimport { coerceGristDocApiRefRowId } from \"../../sdk/helpers/grist-value-codec\"\nimport {\n evaluateRowFormulas,\n findRow,\n findRowIndex,\n getTable,\n nextRowId,\n syncRefListDisplayHelpers,\n} from \"./document\"\nimport type { EventBus } from \"./event-bus\"\nimport type { EmulatorColumn, EmulatorDocumentState, EmulatorTable } from \"./types\"\n\nexport class EmulatorActionError extends Error {\n constructor(\n public readonly action: unknown,\n message: string\n ) {\n super(message)\n this.name = \"EmulatorActionError\"\n }\n}\n\nexport type ApplyUserActionResult = {\n /** Result mirroring Grist's `applyUserActions`: per-action retValues. */\n retValues: unknown[]\n /** Tables that received row-level changes (de-duped). */\n changedTableIds: string[]\n}\n\nexport function applyUserActions(\n state: EmulatorDocumentState,\n bus: EventBus,\n actions: unknown[][]\n): ApplyUserActionResult {\n const retValues: unknown[] = []\n const changed = new Set<string>()\n\n for (const action of actions) {\n bus.emit({ type: \"user-action\", action })\n const ret = applySingle(state, action, changed)\n retValues.push(ret)\n }\n\n for (const tableId of changed) {\n bus.emit({ type: \"data-change\", tableId })\n }\n\n return { retValues, changedTableIds: [...changed] }\n}\n\nfunction applySingle(\n state: EmulatorDocumentState,\n action: unknown[],\n changed: Set<string>\n): unknown {\n const [name, ...args] = action\n switch (name) {\n case \"AddRecord\":\n return addRecord(state, args, changed)\n case \"BulkAddRecord\":\n return bulkAddRecord(state, args, changed)\n case \"UpdateRecord\":\n return updateRecord(state, args, changed)\n case \"BulkUpdateRecord\":\n return bulkUpdateRecord(state, args, changed)\n case \"RemoveRecord\":\n return removeRecord(state, args, changed)\n case \"BulkRemoveRecord\":\n return bulkRemoveRecord(state, args, changed)\n case \"AddColumn\":\n return addColumn(state, args)\n case \"AddVisibleColumn\":\n // The emulator has no concept of section view fields, so the \"visible\"\n // part of `AddVisibleColumn` is a no-op here. We still create the\n // column so widgets that issue this user action (the higher-level form\n // recommended by Grist for \"add a column to the active view\") work\n // identically against the emulator.\n return addColumn(state, args)\n case \"RemoveColumn\":\n return removeColumn(state, args, changed)\n case \"RenameColumn\":\n return renameColumn(state, args, changed)\n case \"ModifyColumn\":\n return modifyColumn(state, args, changed)\n case \"AddTable\":\n return addTable(state, args)\n case \"RemoveTable\":\n return removeTable(state, args)\n case \"RenameTable\":\n return renameTable(state, args)\n default:\n throw new EmulatorActionError(action, `Unsupported user action: ${String(name)}`)\n }\n}\n\n// ---------- row ops ----------\n\nfunction addRecord(\n state: EmulatorDocumentState,\n [tableId, rowId, values]: unknown[],\n changed: Set<string>\n): number {\n const table = getTable(state, requireString(tableId, \"AddRecord.tableId\"))\n const id = typeof rowId === \"number\" ? rowId : nextRowId(table)\n const normalizedValues = normalizeValuesForTable(\n table,\n (values as Record<string, unknown>) ?? {}\n )\n const row = { id, ...normalizedValues }\n ensureColumns(table, normalizedValues)\n evaluateRowFormulas(state, table, row)\n syncRefListDisplayHelpers(state, table, row)\n table.rows.push(row)\n changed.add(table.tableId)\n return id\n}\n\nfunction bulkAddRecord(\n state: EmulatorDocumentState,\n [tableId, rowIds, columnar]: unknown[],\n changed: Set<string>\n): number[] {\n const table = getTable(state, requireString(tableId, \"BulkAddRecord.tableId\"))\n const cols = normalizeColumnarForTable(\n table,\n (columnar as Record<string, unknown[]>) ?? {}\n )\n const ids = Array.isArray(rowIds) ? rowIds : []\n const length = ids.length || lengthOf(cols)\n const out: number[] = []\n ensureColumns(table, cols, true)\n for (let i = 0; i < length; i++) {\n const id = typeof ids[i] === \"number\" ? (ids[i] as number) : nextRowId(table)\n const row: Record<string, unknown> & { id: number } = { id }\n for (const colId of Object.keys(cols)) row[colId] = cols[colId][i]\n evaluateRowFormulas(state, table, row)\n syncRefListDisplayHelpers(state, table, row)\n table.rows.push(row)\n out.push(id)\n }\n changed.add(table.tableId)\n return out\n}\n\nfunction updateRecord(\n state: EmulatorDocumentState,\n [tableId, rowId, values]: unknown[],\n changed: Set<string>\n): null {\n const table = getTable(state, requireString(tableId, \"UpdateRecord.tableId\"))\n const id = requireNumber(rowId, \"UpdateRecord.rowId\")\n const row = findRow(table, id)\n if (!row) throw new EmulatorActionError([\"UpdateRecord\", tableId, rowId, values], `Row ${id} not found in ${table.tableId}`)\n const normalizedValues = normalizeValuesForTable(\n table,\n (values as Record<string, unknown>) ?? {}\n )\n ensureColumns(table, normalizedValues)\n Object.assign(row, normalizedValues)\n evaluateRowFormulas(state, table, row)\n syncRefListDisplayHelpers(state, table, row)\n changed.add(table.tableId)\n return null\n}\n\nfunction bulkUpdateRecord(\n state: EmulatorDocumentState,\n [tableId, rowIds, columnar]: unknown[],\n changed: Set<string>\n): null {\n const table = getTable(state, requireString(tableId, \"BulkUpdateRecord.tableId\"))\n const ids = rowIds as number[]\n const cols = normalizeColumnarForTable(\n table,\n (columnar as Record<string, unknown[]>) ?? {}\n )\n ensureColumns(table, cols, true)\n ids.forEach((id, idx) => {\n const row = findRow(table, id)\n if (!row) throw new EmulatorActionError([\"BulkUpdateRecord\", tableId, ids, cols], `Row ${id} not found in ${table.tableId}`)\n for (const colId of Object.keys(cols)) row[colId] = cols[colId][idx]\n evaluateRowFormulas(state, table, row)\n syncRefListDisplayHelpers(state, table, row)\n })\n changed.add(table.tableId)\n return null\n}\n\nfunction removeRecord(\n state: EmulatorDocumentState,\n [tableId, rowId]: unknown[],\n changed: Set<string>\n): null {\n const table = getTable(state, requireString(tableId, \"RemoveRecord.tableId\"))\n const id = requireNumber(rowId, \"RemoveRecord.rowId\")\n const idx = findRowIndex(table, id)\n if (idx >= 0) table.rows.splice(idx, 1)\n if (state.selection.tableId === table.tableId && state.selection.rowId === id) {\n state.selection.rowId = table.rows[0]?.id ?? null\n }\n changed.add(table.tableId)\n return null\n}\n\nfunction bulkRemoveRecord(\n state: EmulatorDocumentState,\n [tableId, rowIds]: unknown[],\n changed: Set<string>\n): null {\n const table = getTable(state, requireString(tableId, \"BulkRemoveRecord.tableId\"))\n const ids = new Set(rowIds as number[])\n table.rows = table.rows.filter((r) => !ids.has(r.id))\n if (state.selection.tableId === table.tableId && typeof state.selection.rowId === \"number\" && ids.has(state.selection.rowId)) {\n state.selection.rowId = table.rows[0]?.id ?? null\n }\n changed.add(table.tableId)\n return null\n}\n\n// ---------- column ops ----------\n\nfunction addColumn(\n state: EmulatorDocumentState,\n [tableId, colId, info]: unknown[]\n): null {\n const table = getTable(state, requireString(tableId, \"AddColumn.tableId\"))\n const col = requireString(colId, \"AddColumn.colId\")\n if (table.columns[col]) throw new EmulatorActionError([\"AddColumn\", tableId, colId], `Column ${col} already exists`)\n table.columns[col] = (info as EmulatorColumn) ?? {}\n for (const row of table.rows) {\n if (!(col in row)) row[col] = null\n }\n return null\n}\n\nfunction removeColumn(\n state: EmulatorDocumentState,\n [tableId, colId]: unknown[],\n changed: Set<string>\n): null {\n const table = getTable(state, requireString(tableId, \"RemoveColumn.tableId\"))\n const col = requireString(colId, \"RemoveColumn.colId\")\n delete table.columns[col]\n for (const row of table.rows) delete row[col]\n changed.add(table.tableId)\n return null\n}\n\nfunction renameColumn(\n state: EmulatorDocumentState,\n [tableId, colId, newColId]: unknown[],\n changed: Set<string>\n): null {\n const table = getTable(state, requireString(tableId, \"RenameColumn.tableId\"))\n const from = requireString(colId, \"RenameColumn.colId\")\n const to = requireString(newColId, \"RenameColumn.newColId\")\n if (!table.columns[from]) throw new EmulatorActionError([\"RenameColumn\", tableId, from, to], `Column ${from} not found`)\n if (from === to) return null\n table.columns[to] = table.columns[from]\n delete table.columns[from]\n for (const row of table.rows) {\n if (from in row) {\n row[to] = row[from]\n delete row[from]\n }\n }\n changed.add(table.tableId)\n return null\n}\n\nfunction modifyColumn(\n state: EmulatorDocumentState,\n [tableId, colId, info]: unknown[],\n changed: Set<string>\n): null {\n const table = getTable(state, requireString(tableId, \"ModifyColumn.tableId\"))\n const col = requireString(colId, \"ModifyColumn.colId\")\n if (!table.columns[col]) throw new EmulatorActionError([\"ModifyColumn\", tableId, colId], `Column ${col} not found`)\n table.columns[col] = { ...table.columns[col], ...(info as EmulatorColumn) }\n changed.add(table.tableId)\n return null\n}\n\n// ---------- table ops ----------\n\nfunction addTable(state: EmulatorDocumentState, [tableId, columns]: unknown[]): string {\n const id = requireString(tableId, \"AddTable.tableId\")\n if (state.tables.has(id)) throw new EmulatorActionError([\"AddTable\", tableId], `Table ${id} already exists`)\n const cols: Record<string, EmulatorColumn> = {}\n for (const c of (columns as Array<{ id: string } & EmulatorColumn>) ?? []) {\n cols[c.id] = { ...c }\n }\n state.tables.set(id, { tableId: id, columns: cols, rows: [] })\n return id\n}\n\nfunction removeTable(state: EmulatorDocumentState, [tableId]: unknown[]): null {\n const id = requireString(tableId, \"RemoveTable.tableId\")\n state.tables.delete(id)\n if (state.selection.tableId === id) {\n const fallback = state.tables.keys().next().value as string | undefined\n if (fallback) {\n state.selection.tableId = fallback\n state.selection.rowId = null\n }\n }\n return null\n}\n\nfunction renameTable(state: EmulatorDocumentState, [from, to]: unknown[]): null {\n const fromId = requireString(from, \"RenameTable.from\")\n const toId = requireString(to, \"RenameTable.to\")\n const table = state.tables.get(fromId)\n if (!table) throw new EmulatorActionError([\"RenameTable\", fromId], `Table ${fromId} not found`)\n state.tables.delete(fromId)\n table.tableId = toId\n state.tables.set(toId, table)\n if (state.selection.tableId === fromId) state.selection.tableId = toId\n return null\n}\n\n// ---------- helpers ----------\n\nfunction ensureColumns(\n table: EmulatorTable,\n values: Record<string, unknown> | Record<string, unknown[]>,\n bulk = false\n): void {\n for (const col of Object.keys(values)) {\n if (!table.columns[col]) {\n const sample = bulk ? (values as Record<string, unknown[]>)[col]?.[0] : (values as Record<string, unknown>)[col]\n table.columns[col] = inferType(sample)\n }\n }\n}\n\nfunction inferType(sample: unknown): EmulatorColumn {\n switch (typeof sample) {\n case \"number\":\n return { type: \"Numeric\" }\n case \"boolean\":\n return { type: \"Bool\" }\n case \"string\":\n return { type: \"Text\" }\n default:\n return { type: \"Any\" }\n }\n}\n\nfunction lengthOf(cols: Record<string, unknown[]>): number {\n for (const arr of Object.values(cols)) if (Array.isArray(arr)) return arr.length\n return 0\n}\n\nfunction requireString(value: unknown, name: string): string {\n if (typeof value !== \"string\" || !value) throw new Error(`Expected non-empty string for ${name}`)\n return value\n}\n\nfunction requireNumber(value: unknown, name: string): number {\n if (typeof value !== \"number\" || Number.isNaN(value)) throw new Error(`Expected number for ${name}`)\n return value\n}\n\nfunction normalizeValuesForTable(\n table: EmulatorTable,\n values: Record<string, unknown>\n): Record<string, unknown> {\n const out: Record<string, unknown> = {}\n for (const [colId, value] of Object.entries(values)) {\n out[colId] = normalizeCellValueForType(value, table.columns[colId]?.type)\n }\n return out\n}\n\nfunction normalizeColumnarForTable(\n table: EmulatorTable,\n columns: Record<string, unknown[]>\n): Record<string, unknown[]> {\n const out: Record<string, unknown[]> = {}\n for (const [colId, values] of Object.entries(columns)) {\n const type = table.columns[colId]?.type\n out[colId] = Array.isArray(values)\n ? values.map((value) => normalizeCellValueForType(value, type))\n : []\n }\n return out\n}\n\nfunction normalizeCellValueForType(value: unknown, type: string | undefined): unknown {\n if (value === null || value === undefined || typeof type !== \"string\") return value\n\n if (type.startsWith(\"Ref:\")) return normalizeRefValue(value)\n if (type.startsWith(\"RefList:\")) return normalizeRefListValue(value)\n return value\n}\n\n/**\n * Canonical ref row id for emulator storage (`Ref:*` columns).\n * Accepts bare ids, `\"12\"`, Grist tuples `[\"r\", id]` / `[\"R\", tableId, id]`,\n * and detailed-ref shapes `{ rowId }` used after safe-parse / RDG edits.\n */\nfunction extractRefRowId(value: unknown): number | null {\n if (value === null || value === undefined) return null\n if (value && typeof value === \"object\" && \"__ref\" in value) {\n const inner = (value as { rowId?: unknown }).rowId\n return extractRefRowId(inner)\n }\n if (value && typeof value === \"object\" && \"rowId\" in value) {\n return extractRefRowId((value as { rowId: unknown }).rowId)\n }\n const fromTuple = coerceGristDocApiRefRowId(value)\n if (fromTuple !== undefined && fromTuple > 0) return Math.trunc(fromTuple)\n return toPositiveInt(value)\n}\n\nfunction normalizeRefValue(value: unknown): number | null {\n return extractRefRowId(value)\n}\n\nfunction normalizeRefListValue(value: unknown): unknown[] {\n const toIdList = (items: unknown[]): number[] =>\n Array.from(\n new Set(items.map((item) => extractRefRowId(item)).filter((item): item is number => item !== null))\n )\n\n // Grist tuple form: [\"L\", ...ids]\n if (Array.isArray(value) && value[0] === \"L\") {\n return [\"L\", ...toIdList(value.slice(1))]\n }\n if (Array.isArray(value)) {\n return [\"L\", ...toIdList(value)]\n }\n if (typeof value === \"string\") {\n const tokens = value.split(\",\").map((t) => t.trim()).filter(Boolean)\n return [\"L\", ...toIdList(tokens)]\n }\n if (value && typeof value === \"object\" && Array.isArray((value as { rowIds?: unknown }).rowIds)) {\n return [\"L\", ...toIdList((value as { rowIds: unknown[] }).rowIds)]\n }\n return [\"L\"]\n}\n\nfunction toPositiveInt(value: unknown): number | null {\n if (typeof value === \"number\" && Number.isFinite(value) && value > 0) return Math.trunc(value)\n if (typeof value === \"string\") {\n const n = Number.parseInt(value.trim(), 10)\n if (Number.isFinite(n) && n > 0) return n\n }\n return null\n}\n","/**\n * Resolves the `mappings` payload that `CustomSectionAPI.mappings()` returns.\n *\n * The playground emulator does **not** infer logical → real columns from\n * names. Mappings come only from `state.selection.mappings` (playground\n * document inspector or tests). When unset, returns `null`.\n */\n\nimport type { GristWidgetColumnMap } from \"../../sdk/types\"\nimport type { EmulatorDocumentState } from \"./types\"\n\nexport function resolveMappings(state: EmulatorDocumentState): GristWidgetColumnMap | null {\n if (state.selection.mappings !== undefined && state.selection.mappings !== null) {\n return state.selection.mappings\n }\n return null\n}\n","/**\n * Inline transport — synthesises a `window.grist` global wired directly to\n * the emulator state. No iframe, no postMessage, no serialization.\n *\n * Used for:\n * - SDK unit tests in jsdom (`renderWithGrist(...)`),\n * - IDE-style live previews where widget code lives in the same window\n * as the emulator host.\n *\n * The synthesised `grist` object exposes the **same** typed surface as the\n * production `grist-plugin-api.js` (see `hooks/grist-global.d.ts`). Hooks\n * call `grist.docApi.fetchTable(...)` etc. and get the answer directly from\n * the action engine.\n */\n\nimport type {\n GristCursorPos,\n GristFetchSelectedOptions,\n GristMinimalRecord,\n GristReadyOptions,\n GristTableNewRecord,\n GristTableOpOptions,\n GristTableOperations,\n GristTableUpdateRecord,\n GristTableUpsertOptions,\n GristTableUpsertRecord,\n GristWidgetColumnMap,\n GristWidgetInteraction,\n} from \"../../sdk/types\"\nimport {\n gristAddRecordAction,\n gristBulkAddRecordAction,\n gristBulkRemoveRecordAction,\n gristBulkUpdateRecordAction,\n gristRemoveRecordAction,\n gristUpdateRecordAction,\n} from \"../../sdk/helpers/grist-user-actions\"\nimport { applyUserActions } from \"../core/action-engine\"\nimport type { EmulatorEvent } from \"../core/event-bus\"\nimport { findRow, getTable } from \"../core/document\"\nimport { resolveMappings } from \"../core/mappings\"\nimport type { EmulatorDocumentState } from \"../core/types\"\nimport { buildGristThemeHostMessage } from \"../theme-message\"\nimport type { EmulatorTransport, TransportContext } from \"./types\"\n\n/**\n * Shape of the synthesised global. Matches `globalThis.grist` from\n * `grist-global.d.ts`.\n */\nexport type InlineGristGlobal = {\n ready: (options?: GristReadyOptions) => Promise<void>\n onRecord: (\n cb: (record: Record<string, unknown> | null, mappings?: GristWidgetColumnMap | null) => void,\n options?: GristFetchSelectedOptions\n ) => void\n onRecords: (\n cb: (records: Record<string, unknown>[], mappings?: GristWidgetColumnMap | null) => void,\n options?: GristFetchSelectedOptions\n ) => void\n onOptions: (cb: (options: Record<string, unknown> | null, interaction: GristWidgetInteraction) => void) => void\n onNewRecord: (cb: (mappings: GristWidgetColumnMap | null) => void) => void\n mapColumnNames: (data: Record<string, unknown>) => Record<string, unknown> | null\n mapColumnNamesBack: (data: Record<string, unknown>) => Record<string, unknown>\n getOptions: () => Promise<Record<string, unknown> | null>\n getOption: (key: string) => Promise<unknown>\n setOption: (key: string, value: unknown) => Promise<void>\n setOptions: (options: Record<string, unknown>) => Promise<void>\n clearOptions: () => Promise<void>\n setCursorPos: (pos: GristCursorPos) => Promise<void>\n setSelectedRows: (rowIds: number[] | null) => Promise<void>\n fetchSelectedTable: (options?: GristFetchSelectedOptions) => Promise<Record<string, unknown[]>>\n fetchSelectedRecord: (rowId: number, options?: GristFetchSelectedOptions) => Promise<Record<string, unknown>>\n docApi: {\n getDocName: () => Promise<string>\n listTables: () => Promise<string[]>\n fetchTable: (tableId: string) => Promise<Record<string, unknown[]>>\n applyUserActions: (actions: unknown[][], options?: { desc?: string }) => Promise<{ retValues: unknown[] }>\n getAccessToken?: (options?: { readOnly?: boolean }) => Promise<{ token: string; baseUrl: string }>\n fetchSelectedTable: (options?: GristFetchSelectedOptions) => Promise<Record<string, unknown[]>>\n fetchSelectedRecord: (rowId: number, options?: GristFetchSelectedOptions) => Promise<Record<string, unknown>>\n }\n selectedTable: GristTableOperations\n getTable: (tableId?: string) => GristTableOperations\n sectionApi: {\n configure: (settings: GristReadyOptions) => Promise<void>\n mappings: () => Promise<GristWidgetColumnMap | null>\n }\n on: (eventName: string, listener: (...args: unknown[]) => void) => () => void\n}\n\nexport type InlineTransportOptions = TransportContext & {\n /**\n * Where to mount the synthesised `grist` global.\n * Defaults to the current window. Pass a different target (e.g. an iframe's\n * `contentWindow`) to install it in another realm.\n */\n target?: Window & typeof globalThis\n}\n\n/**\n * Returns an {@link EmulatorTransport} **and** the synthesised `grist`\n * object. `target.grist = global` is set as a side-effect for parity with\n * the production runtime.\n */\nexport function createInlineTransport(\n options: InlineTransportOptions\n): EmulatorTransport & { grist: InlineGristGlobal } {\n const { state, bus, api } = options\n const target = options.target ?? (globalThis as unknown as Window & typeof globalThis)\n\n // The SDK's `useGristAvailability` checks `window.self !== window.top`.\n // The inline transport runs in a top-level window where that's false. We\n // shim `top` so widget code thinks it's embedded — production doesn't use\n // this transport so there's no risk of leaking the shim.\n installFakeIframeContext(target)\n\n type RecordCb = (record: Record<string, unknown> | null, mappings?: GristWidgetColumnMap | null) => void\n type RecordsCb = (records: Record<string, unknown>[], mappings?: GristWidgetColumnMap | null) => void\n type OptionsCb = (options: Record<string, unknown> | null, interaction: GristWidgetInteraction) => void\n type NewRecordCb = (mappings: GristWidgetColumnMap | null) => void\n\n const recordCbs = new Map<RecordCb, GristFetchSelectedOptions | undefined>()\n const recordsCbs = new Map<RecordsCb, GristFetchSelectedOptions | undefined>()\n const optionsCbs = new Set<OptionsCb>()\n const newRecordCbs = new Set<NewRecordCb>()\n const generic = new Map<string, Set<(...args: unknown[]) => void>>()\n\n // Replay caches — production `grist-plugin-api.js` immediately delivers the\n // latest record / records / options to a freshly-subscribed listener, so\n // hooks that subscribe **after** `grist.ready()` still get the initial\n // payload. We mirror that behaviour here.\n type LastRecord = { record: Record<string, unknown> | null; mappings: GristWidgetColumnMap | null }\n type LastRecords = { records: Record<string, unknown>[]; mappings: GristWidgetColumnMap | null }\n type LastOptions = { options: Record<string, unknown> | null; interaction: GristWidgetInteraction }\n let lastRecord: LastRecord | null = null\n let lastRecords: LastRecords | null = null\n let lastOptions: LastOptions | null = null\n let lastNewRecord: GristWidgetColumnMap | null | undefined = undefined\n\n const buildInteraction = options.buildInteraction ?? (() => ({\n access_level: state.configuration?.requiredAccess ?? \"read table\",\n ...state.widgetInteraction,\n }))\n\n async function pushRecord(rowId: number | \"new\" | null) {\n const mappings = resolveMappings(state)\n if (rowId === \"new\") {\n lastNewRecord = mappings\n for (const cb of newRecordCbs) {\n try { cb(mappings) } catch (err) { logErr(err) }\n }\n lastRecord = { record: null, mappings }\n for (const [cb] of recordCbs) {\n try { cb(null, mappings) } catch (err) { logErr(err) }\n }\n return\n }\n if (rowId === null) {\n lastRecord = { record: null, mappings }\n for (const [cb] of recordCbs) {\n try { cb(null, mappings) } catch (err) { logErr(err) }\n }\n return\n }\n let record: Record<string, unknown> | null = null\n try {\n record = await api.GristView.fetchSelectedRecord(rowId)\n } catch (err) {\n logErr(err)\n }\n lastRecord = { record, mappings }\n for (const [cb, opts] of recordCbs) {\n try {\n const r = opts?.keepEncoded\n ? await api.GristView.fetchSelectedRecord(rowId, { keepEncoded: true })\n : record\n cb(r, mappings)\n } catch (err) {\n logErr(err)\n }\n }\n }\n\n async function pushRecords() {\n const mappings = resolveMappings(state)\n let baseRows: Record<string, unknown>[] = []\n try {\n const columnar = await api.GristView.fetchSelectedTable({})\n baseRows = columnsToRows(columnar)\n } catch (err) {\n logErr(err)\n }\n lastRecords = { records: baseRows, mappings }\n for (const [cb, opts] of recordsCbs) {\n try {\n const fetchOptions = coerceFetchOptions(opts)\n // Re-fetch only when the subscriber asked for a different shape\n // (e.g. `keepEncoded: true`). Otherwise reuse the decoded rows we\n // already have to avoid extra work and to match production where\n // the same fetch is broadcast to every listener.\n const rows = fetchOptions?.keepEncoded\n ? columnsToRows(await api.GristView.fetchSelectedTable({ keepEncoded: true }))\n : baseRows\n cb(rows, mappings)\n } catch (err) {\n logErr(err)\n }\n }\n }\n\n /**\n * Mirror what `viewApi.fetchSelectedTable` does inside `grist-plugin-api.js`:\n * the host RPC stub always returns columnar; the wrapper applies the\n * `format: \"rows\"` transpose (and would decode encoded values when\n * `keepEncoded: false` — a no-op here because our `rowsToColumnar` already\n * emits JS-native primitives in that case).\n *\n * Keeping this logic on the inline side guarantees that widgets see the\n * same result whether they go through the iframe transport (real plugin\n * wrapper) or the inline transport (this wrapper).\n */\n async function fetchSelectedTableShaped(\n opts: GristFetchSelectedOptions | undefined\n ): Promise<Record<string, unknown[]> | Record<string, unknown>[]> {\n const fetchOptions = coerceFetchOptions(opts)\n const columnar = (await api.GristView.fetchSelectedTable(\n fetchOptions ? { keepEncoded: fetchOptions.keepEncoded } : {}\n )) as Record<string, unknown[]>\n if (fetchOptions?.format === \"rows\") return columnsToRows(columnar)\n return columnar\n }\n\n function pushOptions() {\n const opts = Object.keys(state.widgetOptions).length > 0 ? { ...state.widgetOptions } : null\n const interaction = buildInteraction()\n lastOptions = { options: opts, interaction }\n for (const cb of optionsCbs) {\n try { cb(opts, interaction) } catch (err) { logErr(err) }\n }\n }\n\n const offBus = bus.on(async (event: EmulatorEvent) => {\n switch (event.type) {\n case \"data-change\":\n await pushRecords()\n await pushRecord(state.selection.rowId)\n break\n case \"cursor-change\":\n await pushRecord(event.rowId)\n break\n case \"options-change\":\n pushOptions()\n break\n case \"ready\":\n pushOptions()\n if (options.initialTheme) {\n notify(\"message\", buildGristThemeHostMessage(options.initialTheme, { fromReady: true }))\n }\n await pushRecords()\n await pushRecord(state.selection.rowId)\n break\n case \"mappings-change\":\n await pushRecords()\n await pushRecord(state.selection.rowId)\n break\n case \"theme-change\":\n notify(\"message\", event.message)\n break\n }\n })\n\n function makeTableOps(tableId: string): GristTableOperations {\n return {\n async getTableId() { return tableId },\n async create(records, options) {\n const list = Array.isArray(records) ? records : [records]\n const ids: GristMinimalRecord[] = []\n for (const r of list) {\n const result = applyUserActions(state, bus, [\n gristAddRecordAction(tableId, parseFields(r.fields, options)),\n ])\n ids.push({ id: result.retValues[0] as number })\n }\n return Array.isArray(records) ? ids : ids[0]\n },\n async update(records: GristTableUpdateRecord | GristTableUpdateRecord[], options?: GristTableOpOptions) {\n const list = Array.isArray(records) ? records : [records]\n const actions = list.map((r) => gristUpdateRecordAction(tableId, r.id, parseFields(r.fields, options)))\n applyUserActions(state, bus, actions)\n },\n async upsert(records: GristTableUpsertRecord | GristTableUpsertRecord[], options?: GristTableUpsertOptions) {\n const list = Array.isArray(records) ? records : [records]\n const allowAdd = options?.add !== false\n const allowUpdate = options?.update !== false\n const table = getTable(state, tableId)\n const actions: unknown[][] = []\n for (const r of list) {\n const matches = table.rows.filter((row) =>\n Object.entries(r.require).every(([k, v]) => row[k] === v)\n )\n if (matches.length > 0) {\n if (!allowUpdate) continue\n const fields = parseFields(r.fields ?? {}, options)\n for (const m of matches) actions.push(gristUpdateRecordAction(tableId, m.id, fields))\n } else if (allowAdd) {\n actions.push(gristAddRecordAction(tableId, { ...r.require, ...parseFields(r.fields ?? {}, options) }))\n }\n }\n if (actions.length > 0) applyUserActions(state, bus, actions)\n },\n async destroy(recordIds: number | number[]) {\n if (Array.isArray(recordIds)) {\n applyUserActions(state, bus, [gristBulkRemoveRecordAction(tableId, recordIds)])\n } else {\n applyUserActions(state, bus, [gristRemoveRecordAction(tableId, recordIds)])\n }\n // touch the unused bulk-add reference for tree-shaking parity, no-op\n void gristBulkAddRecordAction\n void gristBulkUpdateRecordAction\n },\n }\n }\n\n function notify(event: string, ...args: unknown[]) {\n const set = generic.get(event)\n if (!set) return\n for (const fn of set) {\n try { fn(...args) } catch (err) { logErr(err) }\n }\n }\n\n const grist: InlineGristGlobal = {\n async ready(opts) {\n await api.CustomSectionAPI.configure(opts ? { ...opts, onEditOptions: undefined } as unknown as Record<string, unknown> : {})\n },\n\n onRecord(cb, opts) {\n recordCbs.set(cb, opts)\n // Replay last value so late subscribers don't miss the initial event.\n if (lastRecord) {\n try { cb(lastRecord.record, lastRecord.mappings) } catch (err) { logErr(err) }\n }\n },\n onRecords(cb, opts) {\n recordsCbs.set(cb, opts)\n if (lastRecords) {\n try { cb(lastRecords.records, lastRecords.mappings) } catch (err) { logErr(err) }\n }\n },\n onOptions(cb) {\n optionsCbs.add(cb)\n if (lastOptions) {\n try { cb(lastOptions.options, lastOptions.interaction) } catch (err) { logErr(err) }\n }\n },\n onNewRecord(cb) {\n newRecordCbs.add(cb)\n if (lastNewRecord !== undefined) {\n try { cb(lastNewRecord) } catch (err) { logErr(err) }\n }\n },\n\n mapColumnNames(data) {\n const mappings = resolveMappings(state)\n if (!mappings) return null\n const out: Record<string, unknown> = {}\n let allMapped = true\n for (const [logical, real] of Object.entries(mappings)) {\n if (Array.isArray(real)) {\n out[logical] = real.map((r) => data[r])\n } else if (typeof real === \"string\") {\n out[logical] = data[real]\n } else {\n allMapped = false\n }\n }\n return allMapped ? out : null\n },\n mapColumnNamesBack(data) {\n const mappings = resolveMappings(state)\n if (!mappings) return data\n const out: Record<string, unknown> = {}\n for (const [logical, real] of Object.entries(mappings)) {\n if (typeof real === \"string\" && logical in data) {\n out[real] = data[logical]\n }\n }\n return out\n },\n\n getOption: (k) => api.WidgetAPI.getOption(k),\n setOption: (k, v) => api.WidgetAPI.setOption(k, v),\n getOptions: () => api.WidgetAPI.getOptions(),\n setOptions: (o) => api.WidgetAPI.setOptions(o),\n clearOptions: () => api.WidgetAPI.clearOptions(),\n\n setCursorPos: (pos) => api.GristView.setCursorPos(pos),\n setSelectedRows: (rowIds) => api.GristView.setSelectedRows(rowIds),\n fetchSelectedTable: (opts) =>\n fetchSelectedTableShaped(opts) as Promise<Record<string, unknown[]>>,\n fetchSelectedRecord: (rowId, opts) => api.GristView.fetchSelectedRecord(rowId, coerceFetchOptions(opts)),\n\n docApi: {\n getDocName: () => api.GristDocAPI.getDocName(),\n listTables: () => api.GristDocAPI.listTables(),\n fetchTable: (id) => api.GristDocAPI.fetchTable(id),\n applyUserActions: (actions, options) => api.GristDocAPI.applyUserActions(actions, options),\n getAccessToken: (opts) => api.GristDocAPI.getAccessToken(opts),\n fetchSelectedTable: (opts) =>\n fetchSelectedTableShaped(opts) as Promise<Record<string, unknown[]>>,\n fetchSelectedRecord: (rowId, opts) =>\n api.GristView.fetchSelectedRecord(rowId, coerceFetchOptions(opts)),\n },\n\n get selectedTable() {\n return makeTableOps(state.selection.tableId)\n },\n\n getTable(tableId) {\n return makeTableOps(tableId ?? state.selection.tableId)\n },\n\n sectionApi: {\n configure: (settings) => api.CustomSectionAPI.configure(settings as unknown as Record<string, unknown>),\n mappings: () => api.CustomSectionAPI.mappings(),\n },\n\n on(eventName, listener) {\n let set = generic.get(eventName)\n if (!set) generic.set(eventName, (set = new Set()))\n set.add(listener)\n return () => set!.delete(listener)\n },\n }\n\n ;(target as unknown as { grist: InlineGristGlobal }).grist = grist\n\n return {\n grist,\n dispose() {\n offBus()\n // Restore the global (only if we still own it).\n const t = target as unknown as { grist?: unknown }\n if (t.grist === grist) delete t.grist\n recordCbs.clear()\n recordsCbs.clear()\n optionsCbs.clear()\n newRecordCbs.clear()\n generic.clear()\n lastRecord = null\n lastRecords = null\n lastOptions = null\n lastNewRecord = undefined\n },\n fireDataChange(tableId) {\n bus.emit({ type: \"data-change\", tableId: tableId ?? state.selection.tableId })\n },\n fireCursor(rowId) {\n const id = rowId === undefined ? state.selection.rowId : rowId\n bus.emit({ type: \"cursor-change\", tableId: state.selection.tableId, rowId: id })\n },\n fireOptions() {\n pushOptions()\n },\n fireTheme(message) {\n bus.emit({ type: \"theme-change\", message })\n },\n emitInitialState() {\n pushOptions()\n void pushRecords().then(() => pushRecord(state.selection.rowId))\n },\n }\n}\n\nfunction columnsToRows(columnar: Record<string, unknown[]>): Record<string, unknown>[] {\n const cols = Object.keys(columnar)\n if (cols.length === 0) return []\n const length = (columnar[cols[0]] ?? []).length\n const rows: Record<string, unknown>[] = []\n for (let i = 0; i < length; i++) {\n const row: Record<string, unknown> = {}\n for (const c of cols) row[c] = columnar[c][i]\n rows.push(row)\n }\n return rows\n}\n\nfunction parseFields(fields: Record<string, unknown>, _options?: GristTableOpOptions): Record<string, unknown> {\n // The emulator currently doesn't implement string-parsing; pass-through.\n // Hook into here to add date/number parsing when we surface that toggle.\n return { ...fields }\n}\n\nfunction coerceFetchOptions(\n opts: GristFetchSelectedOptions | undefined\n): { keepEncoded?: boolean; format?: \"rows\" | \"columns\" } | undefined {\n if (!opts) return undefined\n const format = opts.format === \"rows\" || opts.format === \"columns\" ? opts.format : undefined\n return { keepEncoded: opts.keepEncoded, format }\n}\n\nfunction logErr(err: unknown) {\n // eslint-disable-next-line no-console\n console.error(\"[grist-emulator/inline]\", err)\n}\n\n// Touch helpers to keep them tree-shake-friendly even if unused in current scope.\nvoid findRow\n\n/**\n * Make `window.self !== window.top` evaluate to `true`. The SDK uses this\n * exact comparison in `useGristAvailability` to detect an iframe environment.\n *\n * Inline transports run inside the host window (tests, IDE preview) and would\n * otherwise be flagged as \"not embedded\" — this shim is the same trick we use\n * in the testing harness and is safe because the inline transport is never\n * shipped to production.\n */\nfunction installFakeIframeContext(target: Window & typeof globalThis) {\n try {\n if (target.self !== target.top) return\n Object.defineProperty(target, \"top\", {\n configurable: true,\n get: () => ({}),\n })\n } catch {\n // Some environments lock these globals; ignore silently.\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACyBA,uBAA6B;;;ACbtB,SAAS,2BACd,YACA,SACuB;AACvB,SAAO;AAAA,IACL,OAAO;AAAA,MACL;AAAA,MACA,MAAM;AAAA,MACN,QAAQ,CAAC;AAAA,IACX;AAAA,IACA,GAAI,SAAS,YAAY,EAAE,WAAW,KAAK,IAAI,CAAC;AAAA,EAClD;AACF;;;ADcO,SAAS,sBAAsB,SAAoD;AACxF,QAAM,EAAE,QAAQ,OAAO,KAAK,KAAK,eAAe,IAAI,IAAI;AACxD,QAAM,SAAS,oBAAI,IAAmC;AAEtD,QAAM,MAAM,IAAI,qBAAI;AAAA,IAClB,aAAa,CAAC,QAAQ;AACpB,YAAM,SAAS,OAAO;AACtB,UAAI,CAAC,OAAQ;AACb,aAAO,YAAY,KAAK,YAAY;AAAA,IACtC;AAAA,IACA,QAAQ,EAAE,MAAM,MAAM;AAAA,IAAC,GAAG,MAAM,MAAM;AAAA,IAAC,EAAE;AAAA,EAC3C,CAAC;AAED,MAAI,+BAA+B;AAUnC,QAAM,oBAAoB,IAAI,eAAe,KAAK,GAAG;AACrD,MAAI,iBAAiB,CAAC,QAAiB;AACrC,QACE,OACA,OAAO,QAAQ,YACd,IAA4B,UAAU,yBAAQ,WAC9C,IAA4B,UAAU,SACvC;AACA,YAAM,OAAO,EAAE,GAAI,I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I as InlineGristGlobal, h as InlineTransportOptions, i as createInlineTransport } from '../inline-CRwQQ-GW.cjs';
|
|
3
|
+
import '../types-Fb2wplkv.cjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Iframe transport — the production-fidelity path.
|
|
7
|
+
*
|
|
8
|
+
* - The iframe loads the real `https://docs.getgrist.com/grist-plugin-api.js`
|
|
9
|
+
* so widget code talks to `window.grist` exactly like in production.
|
|
10
|
+
* - The host (this file) instantiates a `grain-rpc` `Rpc` and wires:
|
|
11
|
+
* * outgoing → `iframe.contentWindow.postMessage(msg, targetOrigin)`
|
|
12
|
+
* * incoming → `window.addEventListener("message", ...)` filtered by
|
|
13
|
+
* `event.source === iframe.contentWindow`.
|
|
14
|
+
* - The Rpc registers the six host impls under their canonical names so
|
|
15
|
+
* `grist-plugin-api.js`'s stubs find them.
|
|
16
|
+
* - Custom messages (cursor / data / options / theme) are pushed via
|
|
17
|
+
* `rpc.postMessage(...)` which `grist-plugin-api.js` dispatches to its
|
|
18
|
+
* internal "message" event listeners (the ones used by `onRecord` etc.).
|
|
19
|
+
*
|
|
20
|
+
* Sequence:
|
|
21
|
+
* 1. Host calls `rpc.queueOutgoingUntilReadyMessage()` so messages are
|
|
22
|
+
* buffered until the widget signals it is ready.
|
|
23
|
+
* 2. Widget loads, runs `grist-plugin-api.js`, calls `grist.ready(...)`.
|
|
24
|
+
* 3. Widget's `grist-plugin-api.js` posts a "ready" message → host's Rpc
|
|
25
|
+
* flushes the buffered outgoing messages.
|
|
26
|
+
* 4. Widget's `grist.ready(...)` triggers `CustomSectionAPI.configure(...)`
|
|
27
|
+
* on us; we react by emitting initial state (options + cursor + data).
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
type IframeTransportOptions = TransportContext & {
|
|
31
|
+
iframe: HTMLIFrameElement;
|
|
32
|
+
/** `postMessage` target origin. Defaults to "*" — fine for emulator usage. */
|
|
33
|
+
targetOrigin?: string;
|
|
34
|
+
};
|
|
35
|
+
declare function createIframeTransport(options: IframeTransportOptions): EmulatorTransport;
|
|
36
|
+
|
|
37
|
+
export { EmulatorTransport, type IframeTransportOptions, TransportContext, createIframeTransport };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { T as TransportContext, f as EmulatorTransport } from '../inline-QitZwXKP.js';
|
|
2
|
+
export { I as InlineGristGlobal, h as InlineTransportOptions, i as createInlineTransport } from '../inline-QitZwXKP.js';
|
|
3
|
+
import '../types-Fb2wplkv.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Iframe transport — the production-fidelity path.
|
|
7
|
+
*
|
|
8
|
+
* - The iframe loads the real `https://docs.getgrist.com/grist-plugin-api.js`
|
|
9
|
+
* so widget code talks to `window.grist` exactly like in production.
|
|
10
|
+
* - The host (this file) instantiates a `grain-rpc` `Rpc` and wires:
|
|
11
|
+
* * outgoing → `iframe.contentWindow.postMessage(msg, targetOrigin)`
|
|
12
|
+
* * incoming → `window.addEventListener("message", ...)` filtered by
|
|
13
|
+
* `event.source === iframe.contentWindow`.
|
|
14
|
+
* - The Rpc registers the six host impls under their canonical names so
|
|
15
|
+
* `grist-plugin-api.js`'s stubs find them.
|
|
16
|
+
* - Custom messages (cursor / data / options / theme) are pushed via
|
|
17
|
+
* `rpc.postMessage(...)` which `grist-plugin-api.js` dispatches to its
|
|
18
|
+
* internal "message" event listeners (the ones used by `onRecord` etc.).
|
|
19
|
+
*
|
|
20
|
+
* Sequence:
|
|
21
|
+
* 1. Host calls `rpc.queueOutgoingUntilReadyMessage()` so messages are
|
|
22
|
+
* buffered until the widget signals it is ready.
|
|
23
|
+
* 2. Widget loads, runs `grist-plugin-api.js`, calls `grist.ready(...)`.
|
|
24
|
+
* 3. Widget's `grist-plugin-api.js` posts a "ready" message → host's Rpc
|
|
25
|
+
* flushes the buffered outgoing messages.
|
|
26
|
+
* 4. Widget's `grist.ready(...)` triggers `CustomSectionAPI.configure(...)`
|
|
27
|
+
* on us; we react by emitting initial state (options + cursor + data).
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
type IframeTransportOptions = TransportContext & {
|
|
31
|
+
iframe: HTMLIFrameElement;
|
|
32
|
+
/** `postMessage` target origin. Defaults to "*" — fine for emulator usage. */
|
|
33
|
+
targetOrigin?: string;
|
|
34
|
+
};
|
|
35
|
+
declare function createIframeTransport(options: IframeTransportOptions): EmulatorTransport;
|
|
36
|
+
|
|
37
|
+
export { EmulatorTransport, type IframeTransportOptions, TransportContext, createIframeTransport };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createIframeTransport,
|
|
3
|
+
createInlineTransport
|
|
4
|
+
} from "../chunk-QKXKZ27D.js";
|
|
5
|
+
import "../chunk-GTTD5HPO.js";
|
|
6
|
+
import "../chunk-PM3B4WYV.js";
|
|
7
|
+
import "../chunk-64SVRWW7.js";
|
|
8
|
+
export {
|
|
9
|
+
createIframeTransport,
|
|
10
|
+
createInlineTransport
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=transports.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|