@jsb188/mday 1.1.10 → 1.1.11
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/package.json +105 -54
- package/src/constants/agentDoc.ts +12 -0
- package/src/constants/agentMemory.ts +154 -0
- package/src/constants/aiChat.ts +5 -0
- package/src/constants/campaign.ts +127 -0
- package/src/constants/dataTable.ts +35 -9
- package/src/constants/dataTableFieldTypes.generated.ts +63 -0
- package/src/constants/docAccess.ts +32 -0
- package/src/constants/email.ts +20 -4
- package/src/constants/fieldDoc.ts +34 -0
- package/src/constants/graphqlReactivity.ts +113 -0
- package/src/constants/integration.ts +8 -9
- package/src/constants/log.ts +7 -410
- package/src/constants/logActivityGroupColors.generated.ts +19 -0
- package/src/constants/logTaxonomy.generated.ts +1103 -0
- package/src/constants/noteDoc.ts +77 -0
- package/src/constants/organization.ts +13 -15
- package/src/constants/pageRoute.ts +23 -0
- package/src/constants/schedule.ts +220 -0
- package/src/constants/scheduleRuleTemplateSeed.ts +138 -0
- package/src/constants/scheduleRuleTemplates.generated.ts +145 -0
- package/src/constants/sheet.ts +154 -6
- package/src/constants/sheetRegionSources.generated.ts +578 -0
- package/src/constants/square.ts +6 -6
- package/src/constants/storage.ts +56 -7
- package/src/constants/usage.ts +79 -0
- package/src/schemas/editLogEntry.ts +1010 -777
- package/src/schemas/fieldDocsQuery.ts +12 -0
- package/src/schemas/inboundContact.ts +60 -55
- package/src/schemas/logsQuery.ts +55 -49
- package/src/schemas/organization.ts +49 -46
- package/src/types/account.d.ts +4 -1
- package/src/types/agentDoc.d.ts +64 -0
- package/src/types/agentMemory.d.ts +74 -0
- package/src/types/campaign.d.ts +683 -0
- package/src/types/dataTable.d.ts +175 -256
- package/src/types/docAccess.d.ts +96 -0
- package/src/types/email.d.ts +83 -12
- package/src/types/fieldDoc.d.ts +273 -0
- package/src/types/inboundContact.d.ts +2 -1
- package/src/types/log.d.ts +209 -341
- package/src/types/noteDoc.d.ts +86 -0
- package/src/types/organization.d.ts +83 -40
- package/src/types/pageRoute.d.ts +48 -0
- package/src/types/schedule.d.ts +391 -0
- package/src/types/sheet.d.ts +862 -30
- package/src/types/storage.d.ts +206 -125
- package/src/types/usage.d.ts +106 -0
- package/src/types/workflow.d.ts +149 -18
- package/src/utils/agentDoc.ts +109 -0
- package/src/utils/aiChat.ts +26 -0
- package/src/utils/campaign.ts +741 -0
- package/src/utils/campaignFormEditor.ts +871 -0
- package/src/utils/dataTable.ts +425 -410
- package/src/utils/dataTableFieldTypeRegistry.ts +105 -0
- package/src/utils/dataTableRealtimeAuthority.ts +209 -0
- package/src/utils/dataTableRealtimeSelection.ts +134 -0
- package/src/utils/dataTableRowDocument.ts +508 -0
- package/src/utils/dataTableSource.ts +230 -0
- package/src/utils/docAccess.ts +383 -0
- package/src/utils/documentName.ts +53 -0
- package/src/utils/email.ts +112 -53
- package/src/utils/fieldDoc.ts +113 -0
- package/src/utils/log.ts +169 -176
- package/src/utils/logRegistryValue.ts +90 -0
- package/src/utils/logStoreRead.ts +266 -0
- package/src/utils/logTaxonomy.ts +76 -0
- package/src/utils/logTaxonomyDisplay.ts +439 -0
- package/src/utils/noteDoc.ts +827 -0
- package/src/utils/organization.ts +510 -91
- package/src/utils/pageRoute.ts +71 -0
- package/src/utils/schedule.ts +799 -0
- package/src/utils/scheduleEvaluation.ts +1994 -0
- package/src/utils/scheduleICS.ts +246 -0
- package/src/utils/scheduleRuleTemplates.ts +188 -0
- package/src/utils/sheet.ts +4842 -39
- package/src/utils/sheetAxisMove.ts +830 -0
- package/src/utils/sheetBootstrap.ts +1769 -0
- package/src/utils/sheetCommand.ts +423 -0
- package/src/utils/sheetExport.ts +238 -0
- package/src/utils/sheetFormulaEvaluation.ts +914 -0
- package/src/utils/sheetFormulaEvaluator.ts +852 -0
- package/src/utils/sheetJournal.ts +78 -0
- package/src/utils/sheetNumberFormat.ts +1211 -0
- package/src/utils/sheetRegion.ts +374 -0
- package/src/utils/sheetRegionFilter.ts +1259 -0
- package/src/utils/sheetRegionSourceRegistry.ts +594 -0
- package/src/utils/sheetSequence.ts +148 -0
- package/src/utils/sheetSnapshot.ts +992 -0
- package/src/utils/sheetStructureEdit.ts +1284 -0
- package/src/utils/square.ts +22 -12
- package/src/utils/storage.ts +152 -0
- package/src/utils/table-route.ts +127 -11
- package/src/utils/workflow.ts +191 -23
- package/tests/dataTableFieldTypeRegistry.test.ts +110 -0
- package/tests/docAccess.test.ts +525 -0
- package/tests/editLogEntry.test.ts +112 -0
- package/tests/noteDoc.test.ts +669 -0
- package/tests/schedule.test.ts +693 -0
- package/tests/scheduleEvaluation.test.ts +1970 -0
- package/tests/scheduleICS.test.ts +208 -0
- package/tests/scheduleRuleTemplates.test.ts +176 -0
- package/tests/scheduleSweepInvariants.test.ts +428 -0
- package/tests/sheetAxisMove.test.ts +343 -0
- package/tests/sheetFormulaEvaluator.test.ts +575 -0
- package/tests/sheetRegionFilter.test.ts +1135 -0
- package/tests/sheetSequence.test.ts +41 -0
- package/tests/sheetStructureEdit.test.ts +82 -0
- package/tsconfig.json +10 -10
- package/vitest.config.ts +8 -0
- package/src/constants/log-activityTexts.ts +0 -235
- package/src/constants/product.ts +0 -42
- package/src/constants/report.ts +0 -44
- package/src/schemas/productsQuery.ts +0 -53
- package/src/schemas/reportsQuery.ts +0 -20
- package/src/types/product.d.ts +0 -84
- package/src/types/report.d.ts +0 -290
- package/src/utils/product-calEvent.ts +0 -455
- package/src/utils/report.ts +0 -158
|
@@ -0,0 +1,992 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SheetCellGQL,
|
|
3
|
+
SheetGridPageInfoGQL,
|
|
4
|
+
SheetGridViewportObj,
|
|
5
|
+
SheetRangeGQL,
|
|
6
|
+
SheetRegionGQL,
|
|
7
|
+
SheetTabGQL,
|
|
8
|
+
} from '../types/sheet.d.ts';
|
|
9
|
+
import { normalizeSheetRegionProjectionLifecycle } from './sheetRegion.ts';
|
|
10
|
+
import { requireSheetStorageRevision } from './sheetSequence.ts';
|
|
11
|
+
|
|
12
|
+
export const SHEET_SNAPSHOT_ENCODING_VERSION = 3;
|
|
13
|
+
|
|
14
|
+
export type SheetSnapshotAncestry = {
|
|
15
|
+
organizationId: string;
|
|
16
|
+
sheetId: string;
|
|
17
|
+
sheetTabId: string;
|
|
18
|
+
seq: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type SheetSnapshotTabGQL = Omit<SheetTabGQL, 'cellsRevision'> & {
|
|
22
|
+
cellsRevision?: string | null;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type SheetSnapshotRawData = {
|
|
26
|
+
sheetId: string;
|
|
27
|
+
sheetTab: SheetSnapshotTabGQL;
|
|
28
|
+
cells: SheetCellGQL[];
|
|
29
|
+
ranges: SheetRangeGQL[];
|
|
30
|
+
regions: SheetRegionGQL[];
|
|
31
|
+
pageInfo: SheetGridPageInfoGQL;
|
|
32
|
+
viewport: SheetGridViewportObj;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type EncodedSheetSnapshot = {
|
|
36
|
+
encodingVersion: typeof SHEET_SNAPSHOT_ENCODING_VERSION;
|
|
37
|
+
payload: string;
|
|
38
|
+
cellCount: number;
|
|
39
|
+
payloadBytes: number;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
type CompactSnapshotRecord = Record<string, unknown>;
|
|
43
|
+
type CompactSheetCell = [unknown, number, number, CompactSnapshotRecord?];
|
|
44
|
+
type CompactSheetRange = [
|
|
45
|
+
unknown,
|
|
46
|
+
number,
|
|
47
|
+
number,
|
|
48
|
+
number,
|
|
49
|
+
number,
|
|
50
|
+
CompactSnapshotRecord?,
|
|
51
|
+
];
|
|
52
|
+
type CompactSheetRegion = [unknown, number, number, CompactSnapshotRecord?];
|
|
53
|
+
|
|
54
|
+
type CompactSheetSnapshotEnvelope = {
|
|
55
|
+
v: typeof SHEET_SNAPSHOT_ENCODING_VERSION;
|
|
56
|
+
t: CompactSnapshotRecord;
|
|
57
|
+
c: CompactSheetCell[];
|
|
58
|
+
r: CompactSheetRange[];
|
|
59
|
+
g: CompactSheetRegion[];
|
|
60
|
+
p: CompactSnapshotRecord;
|
|
61
|
+
s: CompactSnapshotRecord[];
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const FILE_CONSTANTS = (() => {
|
|
65
|
+
const cellFieldAliases: Record<string, string> = {
|
|
66
|
+
rawInput: 'i',
|
|
67
|
+
formulaText: 'f',
|
|
68
|
+
value: 'v',
|
|
69
|
+
formulaValue: 'q',
|
|
70
|
+
textValue: 't',
|
|
71
|
+
numberValue: 'n',
|
|
72
|
+
booleanValue: 'b',
|
|
73
|
+
dateValue: 'd',
|
|
74
|
+
datetimeValue: 'z',
|
|
75
|
+
errorCode: 'e',
|
|
76
|
+
errorMessage: 'm',
|
|
77
|
+
revision: 'x',
|
|
78
|
+
volatile: 'o',
|
|
79
|
+
formula: 'u',
|
|
80
|
+
style: 's',
|
|
81
|
+
format: 'a',
|
|
82
|
+
note: 'h',
|
|
83
|
+
sourceType: 'y',
|
|
84
|
+
regionId: 'g',
|
|
85
|
+
sourceDataTableRowId: 'j',
|
|
86
|
+
sourceCellKey: 'l',
|
|
87
|
+
sourceColumnId: 'C',
|
|
88
|
+
sourceMeta: 'w',
|
|
89
|
+
region: 'R',
|
|
90
|
+
};
|
|
91
|
+
const compactValueSource = {
|
|
92
|
+
NONE: 0,
|
|
93
|
+
TEXT: 1,
|
|
94
|
+
NUMBER: 2,
|
|
95
|
+
BOOLEAN: 3,
|
|
96
|
+
DATE: 4,
|
|
97
|
+
DATETIME: 5,
|
|
98
|
+
} as const;
|
|
99
|
+
const compactFormulaSource = {
|
|
100
|
+
NONE: 0,
|
|
101
|
+
VALUE: 1,
|
|
102
|
+
TEXT: 2,
|
|
103
|
+
NUMBER: 3,
|
|
104
|
+
BOOLEAN: 4,
|
|
105
|
+
DATE: 5,
|
|
106
|
+
DATETIME: 6,
|
|
107
|
+
} as const;
|
|
108
|
+
|
|
109
|
+
const cellOmitFields = new Set([
|
|
110
|
+
'id',
|
|
111
|
+
'rowIndex',
|
|
112
|
+
'columnIndex',
|
|
113
|
+
'computedAt',
|
|
114
|
+
'createdAt',
|
|
115
|
+
'updatedAt',
|
|
116
|
+
'deletedAt',
|
|
117
|
+
'deletionJobId',
|
|
118
|
+
'sourceRowKey',
|
|
119
|
+
]);
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
ancestryFields: new Set([
|
|
123
|
+
'__table',
|
|
124
|
+
'organizationId',
|
|
125
|
+
'sheetId',
|
|
126
|
+
'sheetTabId',
|
|
127
|
+
]),
|
|
128
|
+
cellOmitFields,
|
|
129
|
+
generatedCellOmitFields: new Set([...cellOmitFields, 'revision']),
|
|
130
|
+
rangeOmitFields: new Set([
|
|
131
|
+
'id',
|
|
132
|
+
'startRowIndex',
|
|
133
|
+
'startColumnIndex',
|
|
134
|
+
'endRowIndex',
|
|
135
|
+
'endColumnIndex',
|
|
136
|
+
'createdAt',
|
|
137
|
+
'updatedAt',
|
|
138
|
+
]),
|
|
139
|
+
regionOmitFields: new Set([
|
|
140
|
+
'id',
|
|
141
|
+
'startRowIndex',
|
|
142
|
+
'startColumnIndex',
|
|
143
|
+
'createdAt',
|
|
144
|
+
'updatedAt',
|
|
145
|
+
]),
|
|
146
|
+
tabOmitFields: new Set(['id', 'cellsRevision']),
|
|
147
|
+
cellFieldAliases,
|
|
148
|
+
cellFieldNamesByAlias: Object.fromEntries(
|
|
149
|
+
Object.entries(cellFieldAliases).map(([key, alias]) => [alias, key]),
|
|
150
|
+
),
|
|
151
|
+
compactFormulaSource,
|
|
152
|
+
compactValueSource,
|
|
153
|
+
formulaSourceShift: 3,
|
|
154
|
+
valueMaskAlias: 'k',
|
|
155
|
+
};
|
|
156
|
+
})();
|
|
157
|
+
|
|
158
|
+
/*
|
|
159
|
+
* Return a JSON-safe scalar while preserving full integer precision.
|
|
160
|
+
*/
|
|
161
|
+
function normalizeSheetSnapshotScalar(value: unknown): unknown {
|
|
162
|
+
if (typeof value === 'bigint') {
|
|
163
|
+
return value <= BigInt(Number.MAX_SAFE_INTEGER) &&
|
|
164
|
+
value >= BigInt(Number.MIN_SAFE_INTEGER)
|
|
165
|
+
? Number(value)
|
|
166
|
+
: value.toString();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (value instanceof Date) {
|
|
170
|
+
return value.toISOString();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return value;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/*
|
|
177
|
+
* Return a JSON-safe snapshot value with null object fields removed.
|
|
178
|
+
*/
|
|
179
|
+
function compactSheetSnapshotValue(value: unknown): unknown {
|
|
180
|
+
if (value === null || value === undefined) {
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const scalar = normalizeSheetSnapshotScalar(value);
|
|
185
|
+
if (scalar !== value) {
|
|
186
|
+
return scalar;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (Array.isArray(value)) {
|
|
190
|
+
return value.map((item) => compactSheetSnapshotValue(item) ?? null);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (typeof value === 'object') {
|
|
194
|
+
const compacted: CompactSnapshotRecord = {};
|
|
195
|
+
Object.entries(value as Record<string, unknown>).forEach(([key, item]) => {
|
|
196
|
+
const compactedItem = compactSheetSnapshotValue(item);
|
|
197
|
+
if (compactedItem !== undefined) {
|
|
198
|
+
compacted[key] = compactedItem;
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
return compacted;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return value;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/*
|
|
208
|
+
* Return whether a compact record field carries non-default information.
|
|
209
|
+
*/
|
|
210
|
+
function sheetSnapshotFieldHasValue(key: string, value: unknown): boolean {
|
|
211
|
+
if (value === undefined) {
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (key === 'volatile' && value === false) {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (
|
|
220
|
+
(key === 'style' || key === 'format') &&
|
|
221
|
+
typeof value === 'object' &&
|
|
222
|
+
!Array.isArray(value) &&
|
|
223
|
+
!Object.keys(value as object).length
|
|
224
|
+
) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if ((key === 'format' || key === 'metadata') && value === '{}') {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/*
|
|
236
|
+
* Compact one tab while removing the design's repeated tab identifier.
|
|
237
|
+
*/
|
|
238
|
+
function compactSheetSnapshotTab(
|
|
239
|
+
tab: SheetSnapshotTabGQL,
|
|
240
|
+
): CompactSnapshotRecord {
|
|
241
|
+
const compacted = compactSheetSnapshotRecord(
|
|
242
|
+
tab as unknown as Record<string, unknown>,
|
|
243
|
+
FILE_CONSTANTS.tabOmitFields,
|
|
244
|
+
);
|
|
245
|
+
if (compacted.design && typeof compacted.design === 'object') {
|
|
246
|
+
const design = { ...(compacted.design as Record<string, unknown>) };
|
|
247
|
+
delete design.id;
|
|
248
|
+
compacted.design = design;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return compacted;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/*
|
|
255
|
+
* Compact one object after removing ancestry, positional fields, and fields
|
|
256
|
+
* that the snapshot contract deliberately excludes.
|
|
257
|
+
*/
|
|
258
|
+
function compactSheetSnapshotRecord(
|
|
259
|
+
value: Record<string, unknown>,
|
|
260
|
+
omitFields: Set<string>,
|
|
261
|
+
aliases?: Record<string, string>,
|
|
262
|
+
): CompactSnapshotRecord {
|
|
263
|
+
const compacted: CompactSnapshotRecord = {};
|
|
264
|
+
|
|
265
|
+
Object.entries(value).forEach(([key, item]) => {
|
|
266
|
+
if (
|
|
267
|
+
FILE_CONSTANTS.ancestryFields.has(key) ||
|
|
268
|
+
omitFields.has(key) ||
|
|
269
|
+
key === 'compiledFormula'
|
|
270
|
+
) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const compactedItem = compactSheetSnapshotValue(item);
|
|
275
|
+
if (sheetSnapshotFieldHasValue(key, compactedItem)) {
|
|
276
|
+
compacted[aliases?.[key] || key] = compactedItem;
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
return compacted;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/*
|
|
284
|
+
* Return the exact GraphQL string shadow represented by one compact typed
|
|
285
|
+
* field, or null when the field cannot losslessly reconstruct a string.
|
|
286
|
+
*/
|
|
287
|
+
function getCompactSheetCellTypedString(
|
|
288
|
+
fields: CompactSnapshotRecord,
|
|
289
|
+
source: number,
|
|
290
|
+
): string | null {
|
|
291
|
+
const sourceAliases: Record<number, string> = {
|
|
292
|
+
[FILE_CONSTANTS.compactValueSource.TEXT]: 't',
|
|
293
|
+
[FILE_CONSTANTS.compactValueSource.NUMBER]: 'n',
|
|
294
|
+
[FILE_CONSTANTS.compactValueSource.BOOLEAN]: 'b',
|
|
295
|
+
[FILE_CONSTANTS.compactValueSource.DATE]: 'd',
|
|
296
|
+
[FILE_CONSTANTS.compactValueSource.DATETIME]: 'z',
|
|
297
|
+
};
|
|
298
|
+
const alias = sourceAliases[source];
|
|
299
|
+
if (!alias || !Object.hasOwn(fields, alias)) {
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const value = fields[alias];
|
|
304
|
+
if (
|
|
305
|
+
source === FILE_CONSTANTS.compactValueSource.TEXT ||
|
|
306
|
+
source === FILE_CONSTANTS.compactValueSource.DATE ||
|
|
307
|
+
source === FILE_CONSTANTS.compactValueSource.DATETIME
|
|
308
|
+
) {
|
|
309
|
+
return typeof value === 'string' ? value : null;
|
|
310
|
+
}
|
|
311
|
+
if (source === FILE_CONSTANTS.compactValueSource.NUMBER) {
|
|
312
|
+
return typeof value === 'number' && Number.isFinite(value)
|
|
313
|
+
? JSON.stringify(value)
|
|
314
|
+
: null;
|
|
315
|
+
}
|
|
316
|
+
if (source === FILE_CONSTANTS.compactValueSource.BOOLEAN) {
|
|
317
|
+
return typeof value === 'boolean' ? String(value) : null;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/*
|
|
324
|
+
* Return the first typed field that exactly reproduces one serialized GraphQL
|
|
325
|
+
* value. The fixed order keeps the compact metadata deterministic.
|
|
326
|
+
*/
|
|
327
|
+
function getCompactSheetCellValueSource(
|
|
328
|
+
fields: CompactSnapshotRecord,
|
|
329
|
+
value: unknown,
|
|
330
|
+
): number {
|
|
331
|
+
if (typeof value !== 'string') {
|
|
332
|
+
return FILE_CONSTANTS.compactValueSource.NONE;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
for (
|
|
336
|
+
let source = FILE_CONSTANTS.compactValueSource.TEXT;
|
|
337
|
+
source <= FILE_CONSTANTS.compactValueSource.DATETIME;
|
|
338
|
+
source += 1
|
|
339
|
+
) {
|
|
340
|
+
if (getCompactSheetCellTypedString(fields, source) === value) {
|
|
341
|
+
return source;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return FILE_CONSTANTS.compactValueSource.NONE;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/*
|
|
349
|
+
* Return the formula-source code corresponding to one typed-value source.
|
|
350
|
+
*/
|
|
351
|
+
function getCompactSheetCellFormulaSource(valueSource: number): number {
|
|
352
|
+
const formulaSourcesByValueSource: Record<number, number> = {
|
|
353
|
+
[FILE_CONSTANTS.compactValueSource.TEXT]:
|
|
354
|
+
FILE_CONSTANTS.compactFormulaSource.TEXT,
|
|
355
|
+
[FILE_CONSTANTS.compactValueSource.NUMBER]:
|
|
356
|
+
FILE_CONSTANTS.compactFormulaSource.NUMBER,
|
|
357
|
+
[FILE_CONSTANTS.compactValueSource.BOOLEAN]:
|
|
358
|
+
FILE_CONSTANTS.compactFormulaSource.BOOLEAN,
|
|
359
|
+
[FILE_CONSTANTS.compactValueSource.DATE]:
|
|
360
|
+
FILE_CONSTANTS.compactFormulaSource.DATE,
|
|
361
|
+
[FILE_CONSTANTS.compactValueSource.DATETIME]:
|
|
362
|
+
FILE_CONSTANTS.compactFormulaSource.DATETIME,
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
return (
|
|
366
|
+
formulaSourcesByValueSource[valueSource] ||
|
|
367
|
+
FILE_CONSTANTS.compactFormulaSource.NONE
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/*
|
|
372
|
+
* Remove serialized value/formula shadows only when a short source mask can
|
|
373
|
+
* reconstruct the exact decoded SheetCellGQL fields.
|
|
374
|
+
*/
|
|
375
|
+
function compactSheetCellValueShadows(
|
|
376
|
+
fields: CompactSnapshotRecord,
|
|
377
|
+
): CompactSnapshotRecord {
|
|
378
|
+
const originalValue = fields.v;
|
|
379
|
+
const originalFormulaValue = fields.q;
|
|
380
|
+
const valueSource = getCompactSheetCellValueSource(fields, originalValue);
|
|
381
|
+
let formulaSource: number = FILE_CONSTANTS.compactFormulaSource.NONE;
|
|
382
|
+
|
|
383
|
+
if (
|
|
384
|
+
typeof originalFormulaValue === 'string' &&
|
|
385
|
+
typeof originalValue === 'string' &&
|
|
386
|
+
originalFormulaValue === originalValue
|
|
387
|
+
) {
|
|
388
|
+
formulaSource = FILE_CONSTANTS.compactFormulaSource.VALUE;
|
|
389
|
+
} else {
|
|
390
|
+
formulaSource = getCompactSheetCellFormulaSource(
|
|
391
|
+
getCompactSheetCellValueSource(fields, originalFormulaValue),
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (valueSource !== FILE_CONSTANTS.compactValueSource.NONE) {
|
|
396
|
+
delete fields.v;
|
|
397
|
+
}
|
|
398
|
+
if (formulaSource !== FILE_CONSTANTS.compactFormulaSource.NONE) {
|
|
399
|
+
delete fields.q;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const mask =
|
|
403
|
+
valueSource | (formulaSource << FILE_CONSTANTS.formulaSourceShift);
|
|
404
|
+
if (mask) {
|
|
405
|
+
fields[FILE_CONSTANTS.valueMaskAlias] = mask;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
return fields;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/*
|
|
412
|
+
* Return one compact style's canonical identity when it is dictionary-safe.
|
|
413
|
+
*/
|
|
414
|
+
function getCompactSheetSnapshotStyleKey(value: unknown): string | null {
|
|
415
|
+
const compacted = compactSheetSnapshotValue(value);
|
|
416
|
+
if (
|
|
417
|
+
!sheetSnapshotFieldHasValue('style', compacted) ||
|
|
418
|
+
!compacted ||
|
|
419
|
+
typeof compacted !== 'object' ||
|
|
420
|
+
Array.isArray(compacted)
|
|
421
|
+
) {
|
|
422
|
+
return null;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return stringifyCanonicalSheetSnapshotJSON(compacted);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/*
|
|
429
|
+
* Build a deterministic dictionary containing only styles repeated across at
|
|
430
|
+
* least two cells/ranges, so a one-off style never increases payload bytes.
|
|
431
|
+
*/
|
|
432
|
+
function getCompactSheetSnapshotStyleDictionary(raw: SheetSnapshotRawData): {
|
|
433
|
+
styles: CompactSnapshotRecord[];
|
|
434
|
+
styleIndexes: Map<string, number>;
|
|
435
|
+
} {
|
|
436
|
+
const styleCounts = new Map<string, number>();
|
|
437
|
+
[...raw.cells, ...raw.ranges].forEach((record) => {
|
|
438
|
+
const key = getCompactSheetSnapshotStyleKey(record.style);
|
|
439
|
+
if (key) {
|
|
440
|
+
styleCounts.set(key, (styleCounts.get(key) || 0) + 1);
|
|
441
|
+
}
|
|
442
|
+
});
|
|
443
|
+
const styleKeys = [...styleCounts.entries()]
|
|
444
|
+
.filter(([, count]) => count > 1)
|
|
445
|
+
.map(([key]) => key)
|
|
446
|
+
.sort();
|
|
447
|
+
const styles = styleKeys.map(
|
|
448
|
+
(key) => JSON.parse(key) as CompactSnapshotRecord,
|
|
449
|
+
);
|
|
450
|
+
|
|
451
|
+
return {
|
|
452
|
+
styles,
|
|
453
|
+
styleIndexes: new Map(styleKeys.map((key, index) => [key, index])),
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/*
|
|
458
|
+
* Replace one repeated inline style with its deterministic dictionary index.
|
|
459
|
+
*/
|
|
460
|
+
function compactSheetSnapshotRecordStyle(
|
|
461
|
+
fields: CompactSnapshotRecord,
|
|
462
|
+
styleField: string,
|
|
463
|
+
styleIndexes: Map<string, number>,
|
|
464
|
+
): CompactSnapshotRecord {
|
|
465
|
+
const key = getCompactSheetSnapshotStyleKey(fields[styleField]);
|
|
466
|
+
const styleIndex = key === null ? undefined : styleIndexes.get(key);
|
|
467
|
+
if (styleIndex !== undefined) {
|
|
468
|
+
fields[styleField] = styleIndex;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
return fields;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/*
|
|
475
|
+
* Return one compact cell tuple with ancestry and coordinates encoded once.
|
|
476
|
+
*/
|
|
477
|
+
function compactSheetSnapshotCell(
|
|
478
|
+
cell: SheetCellGQL,
|
|
479
|
+
styleIndexes: Map<string, number>,
|
|
480
|
+
): CompactSheetCell {
|
|
481
|
+
const generatedCell = cell.sourceType === 'REGION_GENERATED';
|
|
482
|
+
const fields = compactSheetSnapshotRecordStyle(
|
|
483
|
+
compactSheetCellValueShadows(
|
|
484
|
+
compactSheetSnapshotRecord(
|
|
485
|
+
cell as unknown as Record<string, unknown>,
|
|
486
|
+
generatedCell
|
|
487
|
+
? FILE_CONSTANTS.generatedCellOmitFields
|
|
488
|
+
: FILE_CONSTANTS.cellOmitFields,
|
|
489
|
+
FILE_CONSTANTS.cellFieldAliases,
|
|
490
|
+
),
|
|
491
|
+
),
|
|
492
|
+
FILE_CONSTANTS.cellFieldAliases.style,
|
|
493
|
+
styleIndexes,
|
|
494
|
+
);
|
|
495
|
+
if (!generatedCell) {
|
|
496
|
+
fields[FILE_CONSTANTS.cellFieldAliases.revision] =
|
|
497
|
+
requireSheetStorageRevision(cell.revision);
|
|
498
|
+
}
|
|
499
|
+
const tuple: CompactSheetCell = [
|
|
500
|
+
cell.id,
|
|
501
|
+
Number(cell.rowIndex || 0),
|
|
502
|
+
Number(cell.columnIndex || 0),
|
|
503
|
+
];
|
|
504
|
+
|
|
505
|
+
if (Object.keys(fields).length) {
|
|
506
|
+
tuple.push(fields);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
return tuple;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/*
|
|
513
|
+
* Return one compact formatting-range tuple.
|
|
514
|
+
*/
|
|
515
|
+
function compactSheetSnapshotRange(
|
|
516
|
+
range: SheetRangeGQL,
|
|
517
|
+
styleIndexes: Map<string, number>,
|
|
518
|
+
): CompactSheetRange {
|
|
519
|
+
const fields = compactSheetSnapshotRecordStyle(
|
|
520
|
+
compactSheetSnapshotRecord(
|
|
521
|
+
range as unknown as Record<string, unknown>,
|
|
522
|
+
FILE_CONSTANTS.rangeOmitFields,
|
|
523
|
+
),
|
|
524
|
+
'style',
|
|
525
|
+
styleIndexes,
|
|
526
|
+
);
|
|
527
|
+
const tuple: CompactSheetRange = [
|
|
528
|
+
range.id,
|
|
529
|
+
Number(range.startRowIndex || 0),
|
|
530
|
+
Number(range.startColumnIndex || 0),
|
|
531
|
+
Number(range.endRowIndex || 0),
|
|
532
|
+
Number(range.endColumnIndex || 0),
|
|
533
|
+
];
|
|
534
|
+
|
|
535
|
+
if (Object.keys(fields).length) {
|
|
536
|
+
tuple.push(fields);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
return tuple;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/*
|
|
543
|
+
* Return one compact region tuple, including persisted/effective bounds.
|
|
544
|
+
*/
|
|
545
|
+
function compactSheetSnapshotRegion(
|
|
546
|
+
region: SheetRegionGQL,
|
|
547
|
+
): CompactSheetRegion {
|
|
548
|
+
const fields = compactSheetSnapshotRecord(
|
|
549
|
+
region as unknown as Record<string, unknown>,
|
|
550
|
+
FILE_CONSTANTS.regionOmitFields,
|
|
551
|
+
);
|
|
552
|
+
const tuple: CompactSheetRegion = [
|
|
553
|
+
region.id,
|
|
554
|
+
Number(region.startRowIndex || 0),
|
|
555
|
+
Number(region.startColumnIndex || 0),
|
|
556
|
+
];
|
|
557
|
+
|
|
558
|
+
if (Object.keys(fields).length) {
|
|
559
|
+
tuple.push(fields);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
return tuple;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/*
|
|
566
|
+
* Serialize a JSON value with recursively sorted object keys.
|
|
567
|
+
*/
|
|
568
|
+
function stringifyCanonicalSheetSnapshotJSON(value: unknown): string {
|
|
569
|
+
if (Array.isArray(value)) {
|
|
570
|
+
return `[${value.map((item) => stringifyCanonicalSheetSnapshotJSON(item)).join(',')}]`;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
if (value && typeof value === 'object') {
|
|
574
|
+
return `{${Object.keys(value as object)
|
|
575
|
+
.sort()
|
|
576
|
+
.map(
|
|
577
|
+
(key) =>
|
|
578
|
+
`${JSON.stringify(key)}:${stringifyCanonicalSheetSnapshotJSON(
|
|
579
|
+
(value as Record<string, unknown>)[key],
|
|
580
|
+
)}`,
|
|
581
|
+
)
|
|
582
|
+
.join(',')}}`;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
return JSON.stringify(value);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/*
|
|
589
|
+
* Compare snapshot identifiers by Unicode code units, independent of the
|
|
590
|
+
* process locale and ICU configuration.
|
|
591
|
+
*/
|
|
592
|
+
function compareSheetSnapshotIds(left: unknown, right: unknown): number {
|
|
593
|
+
const leftId = String(left);
|
|
594
|
+
const rightId = String(right);
|
|
595
|
+
|
|
596
|
+
return leftId < rightId ? -1 : leftId > rightId ? 1 : 0;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/*
|
|
600
|
+
* Return records in deterministic cell-coordinate order.
|
|
601
|
+
*/
|
|
602
|
+
function sortSheetSnapshotCells(cells: SheetCellGQL[]): SheetCellGQL[] {
|
|
603
|
+
return [...cells].sort(
|
|
604
|
+
(left, right) =>
|
|
605
|
+
Number(left.rowIndex || 0) - Number(right.rowIndex || 0) ||
|
|
606
|
+
Number(left.columnIndex || 0) - Number(right.columnIndex || 0) ||
|
|
607
|
+
compareSheetSnapshotIds(left.id, right.id),
|
|
608
|
+
);
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/*
|
|
612
|
+
* Return formatting ranges in deterministic presentation order.
|
|
613
|
+
*/
|
|
614
|
+
function sortSheetSnapshotRanges(ranges: SheetRangeGQL[]): SheetRangeGQL[] {
|
|
615
|
+
return [...ranges].sort(
|
|
616
|
+
(left, right) =>
|
|
617
|
+
Number(left.position) - Number(right.position) ||
|
|
618
|
+
compareSheetSnapshotIds(left.id, right.id),
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/*
|
|
623
|
+
* Return regions in deterministic grid order.
|
|
624
|
+
*/
|
|
625
|
+
function sortSheetSnapshotRegions(regions: SheetRegionGQL[]): SheetRegionGQL[] {
|
|
626
|
+
return [...regions].sort(
|
|
627
|
+
(left, right) =>
|
|
628
|
+
Number(left.startRowIndex || 0) - Number(right.startRowIndex || 0) ||
|
|
629
|
+
Number(left.startColumnIndex || 0) -
|
|
630
|
+
Number(right.startColumnIndex || 0) ||
|
|
631
|
+
compareSheetSnapshotIds(left.id, right.id),
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/*
|
|
636
|
+
* Encode one raw, non-presentation-expanded tab state into canonical compact
|
|
637
|
+
* JSON. The viewport lives in the snapshot row and authorization-validated
|
|
638
|
+
* ancestry is supplied once at decode time, so both stay out of the payload.
|
|
639
|
+
*/
|
|
640
|
+
export function encodeSheetSnapshot(
|
|
641
|
+
raw: SheetSnapshotRawData,
|
|
642
|
+
): EncodedSheetSnapshot {
|
|
643
|
+
const tab = compactSheetSnapshotTab(raw.sheetTab);
|
|
644
|
+
const styleDictionary = getCompactSheetSnapshotStyleDictionary(raw);
|
|
645
|
+
const envelope: CompactSheetSnapshotEnvelope = {
|
|
646
|
+
v: SHEET_SNAPSHOT_ENCODING_VERSION,
|
|
647
|
+
t: tab,
|
|
648
|
+
c: sortSheetSnapshotCells(raw.cells).map((cell) =>
|
|
649
|
+
compactSheetSnapshotCell(cell, styleDictionary.styleIndexes),
|
|
650
|
+
),
|
|
651
|
+
r: sortSheetSnapshotRanges(raw.ranges).map((range) =>
|
|
652
|
+
compactSheetSnapshotRange(range, styleDictionary.styleIndexes),
|
|
653
|
+
),
|
|
654
|
+
g: sortSheetSnapshotRegions(raw.regions).map(compactSheetSnapshotRegion),
|
|
655
|
+
p: compactSheetSnapshotValue(raw.pageInfo) as CompactSnapshotRecord,
|
|
656
|
+
s: styleDictionary.styles,
|
|
657
|
+
};
|
|
658
|
+
const payload = stringifyCanonicalSheetSnapshotJSON(envelope);
|
|
659
|
+
|
|
660
|
+
return {
|
|
661
|
+
encodingVersion: SHEET_SNAPSHOT_ENCODING_VERSION,
|
|
662
|
+
payload,
|
|
663
|
+
cellCount: raw.cells.length,
|
|
664
|
+
payloadBytes: new TextEncoder().encode(payload).byteLength,
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
/*
|
|
669
|
+
* Expand one sparse record's aliases to its raw field names.
|
|
670
|
+
*/
|
|
671
|
+
function expandSheetSnapshotRecord(
|
|
672
|
+
value: CompactSnapshotRecord | undefined,
|
|
673
|
+
fieldNamesByAlias?: Record<string, string>,
|
|
674
|
+
): CompactSnapshotRecord {
|
|
675
|
+
const expanded: CompactSnapshotRecord = {};
|
|
676
|
+
|
|
677
|
+
Object.entries(value || {}).forEach(([key, item]) => {
|
|
678
|
+
expanded[fieldNamesByAlias?.[key] || key] = item;
|
|
679
|
+
});
|
|
680
|
+
|
|
681
|
+
return expanded;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/*
|
|
685
|
+
* Clone one JSON snapshot value so dictionary expansion never shares mutable
|
|
686
|
+
* style objects between decoded cells/ranges.
|
|
687
|
+
*/
|
|
688
|
+
function cloneSheetSnapshotValue(value: unknown): unknown {
|
|
689
|
+
if (Array.isArray(value)) {
|
|
690
|
+
return value.map(cloneSheetSnapshotValue);
|
|
691
|
+
}
|
|
692
|
+
if (value && typeof value === 'object') {
|
|
693
|
+
const cloned: CompactSnapshotRecord = {};
|
|
694
|
+
Object.entries(value as CompactSnapshotRecord).forEach(([key, item]) => {
|
|
695
|
+
cloned[key] = cloneSheetSnapshotValue(item);
|
|
696
|
+
});
|
|
697
|
+
return cloned;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
return value;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/*
|
|
704
|
+
* Resolve one style index to an isolated object while leaving unique inline
|
|
705
|
+
* styles unchanged.
|
|
706
|
+
*/
|
|
707
|
+
function expandSheetSnapshotRecordStyle(
|
|
708
|
+
fields: CompactSnapshotRecord,
|
|
709
|
+
styleField: string,
|
|
710
|
+
styles: CompactSnapshotRecord[] | null,
|
|
711
|
+
): CompactSnapshotRecord {
|
|
712
|
+
const styleValue = fields[styleField];
|
|
713
|
+
if (typeof styleValue !== 'number') {
|
|
714
|
+
return fields;
|
|
715
|
+
}
|
|
716
|
+
if (
|
|
717
|
+
!styles ||
|
|
718
|
+
!Number.isSafeInteger(styleValue) ||
|
|
719
|
+
styleValue < 0 ||
|
|
720
|
+
styleValue >= styles.length
|
|
721
|
+
) {
|
|
722
|
+
throw new Error('Invalid sheet snapshot style dictionary index.');
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
fields[styleField] = cloneSheetSnapshotValue(styles[styleValue]);
|
|
726
|
+
return fields;
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
/*
|
|
730
|
+
* Return the compact formula-source code's corresponding typed-value source code.
|
|
731
|
+
*/
|
|
732
|
+
function getExpandedSheetCellFormulaValueSource(formulaSource: number): number {
|
|
733
|
+
const valueSourcesByFormulaSource: Record<number, number> = {
|
|
734
|
+
[FILE_CONSTANTS.compactFormulaSource.TEXT]:
|
|
735
|
+
FILE_CONSTANTS.compactValueSource.TEXT,
|
|
736
|
+
[FILE_CONSTANTS.compactFormulaSource.NUMBER]:
|
|
737
|
+
FILE_CONSTANTS.compactValueSource.NUMBER,
|
|
738
|
+
[FILE_CONSTANTS.compactFormulaSource.BOOLEAN]:
|
|
739
|
+
FILE_CONSTANTS.compactValueSource.BOOLEAN,
|
|
740
|
+
[FILE_CONSTANTS.compactFormulaSource.DATE]:
|
|
741
|
+
FILE_CONSTANTS.compactValueSource.DATE,
|
|
742
|
+
[FILE_CONSTANTS.compactFormulaSource.DATETIME]:
|
|
743
|
+
FILE_CONSTANTS.compactValueSource.DATETIME,
|
|
744
|
+
};
|
|
745
|
+
|
|
746
|
+
return (
|
|
747
|
+
valueSourcesByFormulaSource[formulaSource] ||
|
|
748
|
+
FILE_CONSTANTS.compactValueSource.NONE
|
|
749
|
+
);
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
/*
|
|
753
|
+
* Reconstruct omitted serialized value/formula shadows from the explicit
|
|
754
|
+
* source mask and reject contradictory or incomplete compact metadata.
|
|
755
|
+
*/
|
|
756
|
+
function expandSheetCellValueShadows(
|
|
757
|
+
value: CompactSnapshotRecord | undefined,
|
|
758
|
+
): CompactSnapshotRecord {
|
|
759
|
+
const fields = { ...(value || {}) };
|
|
760
|
+
const mask = fields[FILE_CONSTANTS.valueMaskAlias];
|
|
761
|
+
if (mask === undefined) {
|
|
762
|
+
return fields;
|
|
763
|
+
}
|
|
764
|
+
delete fields[FILE_CONSTANTS.valueMaskAlias];
|
|
765
|
+
if (!Number.isSafeInteger(mask) || Number(mask) <= 0 || Number(mask) > 63) {
|
|
766
|
+
throw new Error('Invalid sheet snapshot cell value mask.');
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
const numericMask = Number(mask);
|
|
770
|
+
const valueSource = numericMask & 7;
|
|
771
|
+
const formulaSource = numericMask >> FILE_CONSTANTS.formulaSourceShift;
|
|
772
|
+
if (
|
|
773
|
+
valueSource > FILE_CONSTANTS.compactValueSource.DATETIME ||
|
|
774
|
+
formulaSource > FILE_CONSTANTS.compactFormulaSource.DATETIME
|
|
775
|
+
) {
|
|
776
|
+
throw new Error('Unsupported sheet snapshot cell value mask.');
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
if (valueSource !== FILE_CONSTANTS.compactValueSource.NONE) {
|
|
780
|
+
if (Object.hasOwn(fields, 'v')) {
|
|
781
|
+
throw new Error('Contradictory sheet snapshot serialized value.');
|
|
782
|
+
}
|
|
783
|
+
const valueString = getCompactSheetCellTypedString(fields, valueSource);
|
|
784
|
+
if (valueString === null) {
|
|
785
|
+
throw new Error('Incomplete sheet snapshot serialized value source.');
|
|
786
|
+
}
|
|
787
|
+
fields.v = valueString;
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
if (formulaSource !== FILE_CONSTANTS.compactFormulaSource.NONE) {
|
|
791
|
+
if (Object.hasOwn(fields, 'q')) {
|
|
792
|
+
throw new Error('Contradictory sheet snapshot formula value.');
|
|
793
|
+
}
|
|
794
|
+
const formulaValue =
|
|
795
|
+
formulaSource === FILE_CONSTANTS.compactFormulaSource.VALUE
|
|
796
|
+
? fields.v
|
|
797
|
+
: getCompactSheetCellTypedString(
|
|
798
|
+
fields,
|
|
799
|
+
getExpandedSheetCellFormulaValueSource(formulaSource),
|
|
800
|
+
);
|
|
801
|
+
if (typeof formulaValue !== 'string') {
|
|
802
|
+
throw new Error('Incomplete sheet snapshot formula value source.');
|
|
803
|
+
}
|
|
804
|
+
fields.q = formulaValue;
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
return fields;
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/*
|
|
811
|
+
* Decode one compact cell tuple and inject its shared ancestry.
|
|
812
|
+
*/
|
|
813
|
+
function expandSheetSnapshotCell(
|
|
814
|
+
tuple: CompactSheetCell,
|
|
815
|
+
ancestry: SheetSnapshotAncestry,
|
|
816
|
+
styles: CompactSnapshotRecord[] | null,
|
|
817
|
+
): SheetCellGQL {
|
|
818
|
+
const fields = expandSheetSnapshotRecordStyle(
|
|
819
|
+
styles === null
|
|
820
|
+
? { ...(tuple[3] || {}) }
|
|
821
|
+
: expandSheetCellValueShadows(tuple[3]),
|
|
822
|
+
FILE_CONSTANTS.cellFieldAliases.style,
|
|
823
|
+
styles,
|
|
824
|
+
);
|
|
825
|
+
const expandedFields = expandSheetSnapshotRecord(
|
|
826
|
+
fields,
|
|
827
|
+
FILE_CONSTANTS.cellFieldNamesByAlias,
|
|
828
|
+
);
|
|
829
|
+
if (expandedFields.sourceType === 'REGION_GENERATED') {
|
|
830
|
+
// Generated projections are immutable within the snapshot address. Their
|
|
831
|
+
// authoritative wire revision is therefore the snapshot's shared seq.
|
|
832
|
+
expandedFields.revision = ancestry.seq;
|
|
833
|
+
}
|
|
834
|
+
return {
|
|
835
|
+
id: String(tuple[0] || ''),
|
|
836
|
+
organizationId: ancestry.organizationId,
|
|
837
|
+
sheetId: ancestry.sheetId,
|
|
838
|
+
sheetTabId: ancestry.sheetTabId,
|
|
839
|
+
rowIndex: Number(tuple[1]),
|
|
840
|
+
columnIndex: Number(tuple[2]),
|
|
841
|
+
...expandedFields,
|
|
842
|
+
} as SheetCellGQL;
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
/*
|
|
846
|
+
* Decode one compact range tuple and inject its shared ancestry.
|
|
847
|
+
*/
|
|
848
|
+
function expandSheetSnapshotRange(
|
|
849
|
+
tuple: CompactSheetRange,
|
|
850
|
+
ancestry: SheetSnapshotAncestry,
|
|
851
|
+
styles: CompactSnapshotRecord[] | null,
|
|
852
|
+
): SheetRangeGQL {
|
|
853
|
+
const fields = expandSheetSnapshotRecordStyle(
|
|
854
|
+
{ ...(tuple[5] || {}) },
|
|
855
|
+
'style',
|
|
856
|
+
styles,
|
|
857
|
+
);
|
|
858
|
+
return {
|
|
859
|
+
id: String(tuple[0] || ''),
|
|
860
|
+
organizationId: ancestry.organizationId,
|
|
861
|
+
sheetId: ancestry.sheetId,
|
|
862
|
+
sheetTabId: ancestry.sheetTabId,
|
|
863
|
+
startRowIndex: Number(tuple[1]),
|
|
864
|
+
startColumnIndex: Number(tuple[2]),
|
|
865
|
+
endRowIndex: Number(tuple[3]),
|
|
866
|
+
endColumnIndex: Number(tuple[4]),
|
|
867
|
+
...expandSheetSnapshotRecord(fields),
|
|
868
|
+
} as SheetRangeGQL;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/*
|
|
872
|
+
* Decode one compact region tuple and inject its shared ancestry.
|
|
873
|
+
*/
|
|
874
|
+
function expandSheetSnapshotRegion(
|
|
875
|
+
tuple: CompactSheetRegion,
|
|
876
|
+
ancestry: SheetSnapshotAncestry,
|
|
877
|
+
): SheetRegionGQL {
|
|
878
|
+
const region = {
|
|
879
|
+
id: String(tuple[0] || ''),
|
|
880
|
+
organizationId: ancestry.organizationId,
|
|
881
|
+
sheetId: ancestry.sheetId,
|
|
882
|
+
sheetTabId: ancestry.sheetTabId,
|
|
883
|
+
startRowIndex: Number(tuple[1]),
|
|
884
|
+
startColumnIndex: Number(tuple[2]),
|
|
885
|
+
...expandSheetSnapshotRecord(tuple[3]),
|
|
886
|
+
} as SheetRegionGQL;
|
|
887
|
+
return normalizeSheetRegionProjectionLifecycle(region);
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
/*
|
|
891
|
+
* Return whether one encoding version is the current snapshot contract.
|
|
892
|
+
*/
|
|
893
|
+
export function isSupportedSheetSnapshotEncodingVersion(
|
|
894
|
+
value: unknown,
|
|
895
|
+
): value is typeof SHEET_SNAPSHOT_ENCODING_VERSION {
|
|
896
|
+
return value === SHEET_SNAPSHOT_ENCODING_VERSION;
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
/*
|
|
900
|
+
* Return whether one parsed JSON value is a non-array compact record.
|
|
901
|
+
*/
|
|
902
|
+
function isCompactSheetSnapshotRecord(
|
|
903
|
+
value: unknown,
|
|
904
|
+
): value is CompactSnapshotRecord {
|
|
905
|
+
return Boolean(value && typeof value === 'object' && !Array.isArray(value));
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
/*
|
|
909
|
+
* Return whether every tuple list has the minimum positional shape required
|
|
910
|
+
* for safe decoding. Field-level masks and dictionary indexes validate later.
|
|
911
|
+
*/
|
|
912
|
+
function hasValidCompactSheetSnapshotTuples(
|
|
913
|
+
envelope: Partial<CompactSheetSnapshotEnvelope>,
|
|
914
|
+
): boolean {
|
|
915
|
+
return Boolean(
|
|
916
|
+
Array.isArray(envelope.c) &&
|
|
917
|
+
envelope.c.every((tuple) => Array.isArray(tuple) && tuple.length >= 3) &&
|
|
918
|
+
Array.isArray(envelope.r) &&
|
|
919
|
+
envelope.r.every((tuple) => Array.isArray(tuple) && tuple.length >= 5) &&
|
|
920
|
+
Array.isArray(envelope.g) &&
|
|
921
|
+
envelope.g.every((tuple) => Array.isArray(tuple) && tuple.length >= 3),
|
|
922
|
+
);
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
/*
|
|
926
|
+
* Parse and validate the current compact envelope. Unknown versions fail closed.
|
|
927
|
+
*/
|
|
928
|
+
function parseSheetSnapshotEnvelope(
|
|
929
|
+
payload: string,
|
|
930
|
+
): CompactSheetSnapshotEnvelope {
|
|
931
|
+
const envelope = JSON.parse(payload) as Partial<CompactSheetSnapshotEnvelope>;
|
|
932
|
+
const shapeIsValid =
|
|
933
|
+
isSupportedSheetSnapshotEncodingVersion(envelope.v) &&
|
|
934
|
+
isCompactSheetSnapshotRecord(envelope.t) &&
|
|
935
|
+
isCompactSheetSnapshotRecord(envelope.p) &&
|
|
936
|
+
hasValidCompactSheetSnapshotTuples(envelope) &&
|
|
937
|
+
Array.isArray(envelope.s) &&
|
|
938
|
+
envelope.s.every(isCompactSheetSnapshotRecord);
|
|
939
|
+
if (!shapeIsValid) {
|
|
940
|
+
throw new Error(
|
|
941
|
+
`Unsupported or invalid sheet snapshot encoding: ${String(envelope.v)}`,
|
|
942
|
+
);
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
return envelope as CompactSheetSnapshotEnvelope;
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
/*
|
|
949
|
+
* Decode canonical snapshot JSON into raw tab state and inject ancestry once
|
|
950
|
+
* for every tab-owned record. Presentation remains unresolved by design.
|
|
951
|
+
*/
|
|
952
|
+
export function decodeSheetSnapshot(
|
|
953
|
+
payload: string,
|
|
954
|
+
ancestry: SheetSnapshotAncestry,
|
|
955
|
+
viewport: SheetGridViewportObj,
|
|
956
|
+
): SheetSnapshotRawData {
|
|
957
|
+
const envelope = parseSheetSnapshotEnvelope(payload);
|
|
958
|
+
const styles = envelope.s;
|
|
959
|
+
const tabFields = expandSheetSnapshotRecord(envelope.t);
|
|
960
|
+
const design =
|
|
961
|
+
tabFields.design && typeof tabFields.design === 'object'
|
|
962
|
+
? {
|
|
963
|
+
...(tabFields.design as Record<string, unknown>),
|
|
964
|
+
id: ancestry.sheetTabId,
|
|
965
|
+
}
|
|
966
|
+
: tabFields.design;
|
|
967
|
+
|
|
968
|
+
return {
|
|
969
|
+
sheetId: ancestry.sheetId,
|
|
970
|
+
sheetTab: {
|
|
971
|
+
id: ancestry.sheetTabId,
|
|
972
|
+
organizationId: ancestry.organizationId,
|
|
973
|
+
sheetId: ancestry.sheetId,
|
|
974
|
+
cellsRevision: ancestry.seq,
|
|
975
|
+
...tabFields,
|
|
976
|
+
design,
|
|
977
|
+
} as SheetSnapshotTabGQL,
|
|
978
|
+
cells: envelope.c.map((cell) =>
|
|
979
|
+
expandSheetSnapshotCell(cell, ancestry, styles),
|
|
980
|
+
),
|
|
981
|
+
ranges: envelope.r.map((range) =>
|
|
982
|
+
expandSheetSnapshotRange(range, ancestry, styles),
|
|
983
|
+
),
|
|
984
|
+
regions: envelope.g.map((region) =>
|
|
985
|
+
expandSheetSnapshotRegion(region, ancestry),
|
|
986
|
+
),
|
|
987
|
+
pageInfo: expandSheetSnapshotRecord(
|
|
988
|
+
envelope.p,
|
|
989
|
+
) as unknown as SheetGridPageInfoGQL,
|
|
990
|
+
viewport,
|
|
991
|
+
};
|
|
992
|
+
}
|