@jsb188/mday 1.1.10 → 1.1.12
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 +140 -54
- package/src/constants/agentMemory.ts +123 -0
- package/src/constants/agentNote.ts +212 -0
- package/src/constants/aiChat.ts +10 -0
- package/src/constants/billing.ts +126 -0
- package/src/constants/campaign.ts +236 -0
- package/src/constants/contact.ts +72 -0
- package/src/constants/csvExport.ts +133 -0
- package/src/constants/dataTable.ts +21 -21
- package/src/constants/dataTableFieldTypes.generated.ts +59 -0
- package/src/constants/docAccess.ts +33 -0
- package/src/constants/dt.ts +234 -0
- package/src/constants/email.ts +125 -7
- package/src/constants/fieldDoc.ts +104 -0
- package/src/constants/graphqlReactivity.ts +196 -0
- package/src/constants/integration.ts +8 -9
- package/src/constants/log.ts +14 -408
- package/src/constants/logDisplay/README.md +88 -0
- package/src/constants/logDisplay/defaults.ts +39 -0
- package/src/constants/logDisplay/index.ts +27 -0
- package/src/constants/logDisplay/operations/arable.ts +16 -0
- package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
- package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
- package/src/constants/logDisplay/shared/accounting.ts +13 -0
- package/src/constants/logDisplay/shared/interactions.ts +13 -0
- package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
- package/src/constants/logTaxonomy.generated.ts +2207 -0
- package/src/constants/noteDoc.ts +77 -0
- package/src/constants/organization.ts +18 -21
- package/src/constants/pageRoute.ts +23 -0
- package/src/constants/qrCode.ts +30 -0
- package/src/constants/sheet.ts +126 -14
- package/src/constants/sms.ts +18 -0
- package/src/constants/square.ts +6 -6
- package/src/constants/storage.ts +80 -7
- package/src/constants/usage.ts +82 -0
- package/src/constants/workflowSteps.ts +60 -0
- package/src/schemas/editLogEntry.ts +836 -780
- package/src/schemas/inboundContact.ts +60 -55
- package/src/schemas/logsQuery.ts +55 -49
- package/src/types/account.d.ts +4 -1
- package/src/types/agentMemory.d.ts +98 -0
- package/src/types/agentNote.d.ts +443 -0
- package/src/types/billing.d.ts +537 -0
- package/src/types/campaign.d.ts +1533 -0
- package/src/types/contact.d.ts +15 -0
- package/src/types/csvExport.d.ts +213 -0
- package/src/types/dataAnalysis.d.ts +44 -0
- package/src/types/dataTable.d.ts +115 -403
- package/src/types/definitionElement.d.ts +35 -0
- package/src/types/docAccess.d.ts +96 -0
- package/src/types/dt.d.ts +257 -0
- package/src/types/email.d.ts +184 -24
- package/src/types/emailReplyReport.d.ts +76 -0
- package/src/types/fieldDoc.d.ts +702 -0
- package/src/types/fieldDocContract.d.ts +90 -0
- package/src/types/inboundContact.d.ts +10 -1
- package/src/types/log.d.ts +254 -295
- package/src/types/logDisplay.types.ts +85 -0
- package/src/types/organization.d.ts +91 -43
- package/src/types/pageRoute.d.ts +48 -0
- package/src/types/qrCode.d.ts +96 -0
- package/src/types/richText.d.ts +81 -0
- package/src/types/sheet.d.ts +678 -129
- package/src/types/sms.d.ts +38 -0
- package/src/types/storage.d.ts +449 -129
- package/src/types/usage.d.ts +106 -0
- package/src/types/workflow.d.ts +315 -54
- package/src/utils/agentMemory.ts +15 -0
- package/src/utils/agentNote.ts +850 -0
- package/src/utils/aiChat.ts +43 -0
- package/src/utils/aiChatWork.ts +64 -0
- package/src/utils/billing.ts +37 -0
- package/src/utils/campaign.ts +1174 -0
- package/src/utils/campaignAI.ts +72 -0
- package/src/utils/campaignAccess.ts +239 -0
- package/src/utils/campaignEmail.ts +601 -0
- package/src/utils/campaignEmailCompiler.ts +372 -0
- package/src/utils/campaignEmailImages.ts +60 -0
- package/src/utils/campaignEmailStyles.ts +544 -0
- package/src/utils/campaignLifecycle.ts +104 -0
- package/src/utils/campaignPreview.ts +285 -0
- package/src/utils/contact.ts +50 -0
- package/src/utils/csvExport.ts +19 -0
- package/src/utils/dataTable.ts +95 -618
- package/src/utils/dataTableFieldTypeRegistry.ts +106 -0
- package/src/utils/dataTableRelativeDate.ts +447 -0
- package/src/utils/definitionElement.ts +84 -0
- package/src/utils/docAccess.ts +383 -0
- package/src/utils/documentName.ts +53 -0
- package/src/utils/email.ts +317 -51
- package/src/utils/emailReplyReport.test.ts +187 -0
- package/src/utils/emailReplyReport.ts +143 -0
- package/src/utils/fieldDoc.ts +157 -0
- package/src/utils/fieldDocAnswers.ts +128 -0
- package/src/utils/fieldDocDefaults.ts +36 -0
- package/src/utils/fieldDocProgram.ts +36 -0
- package/src/utils/fieldDocSummary.ts +30 -0
- package/src/utils/log.ts +276 -192
- package/src/utils/logDisplay.helpers.ts +177 -0
- package/src/utils/logDisplay.ts +87 -0
- package/src/utils/logRegistryValue.ts +90 -0
- package/src/utils/logStoreRead.ts +262 -0
- package/src/utils/logTaxonomy.helpers.ts +17 -0
- package/src/utils/logTaxonomy.ts +76 -0
- package/src/utils/logTaxonomyDisplay.ts +395 -0
- package/src/utils/noteDoc.ts +609 -0
- package/src/utils/organization.ts +337 -84
- package/src/utils/pageRoute.ts +71 -0
- package/src/utils/qrCode.helpers.ts +189 -0
- package/src/utils/qrCode.test.ts +519 -0
- package/src/utils/qrCode.ts +273 -0
- package/src/utils/richTextHTML.ts +385 -0
- package/src/utils/sheet.ts +199 -107
- package/src/utils/sheetAxisMove.ts +616 -0
- package/src/utils/sheetBootstrap.ts +1402 -0
- package/src/utils/sheetCellLiteral.ts +101 -0
- package/src/utils/sheetCommand.ts +358 -0
- package/src/utils/sheetDesign.ts +705 -0
- package/src/utils/sheetExport.ts +186 -0
- package/src/utils/sheetFeatureReference.ts +183 -0
- package/src/utils/sheetFormulaArgs.ts +168 -0
- package/src/utils/sheetFormulaCompile.ts +475 -0
- package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
- package/src/utils/sheetFormulaCriteria.ts +656 -0
- package/src/utils/sheetFormulaDate.ts +444 -0
- package/src/utils/sheetFormulaEvaluation.ts +899 -0
- package/src/utils/sheetFormulaEvaluator.ts +997 -0
- package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
- package/src/utils/sheetFormulaLogical.ts +116 -0
- package/src/utils/sheetFormulaLookup.ts +488 -0
- package/src/utils/sheetFormulaMath.ts +48 -0
- package/src/utils/sheetFormulaParser.ts +729 -0
- package/src/utils/sheetFormulaRange.ts +330 -0
- package/src/utils/sheetFormulaReference.ts +703 -0
- package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
- package/src/utils/sheetFormulaReferenceDates.ts +237 -0
- package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
- package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
- package/src/utils/sheetFormulaReferenceMath.ts +95 -0
- package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
- package/src/utils/sheetFormulaReferenceText.ts +462 -0
- package/src/utils/sheetFormulaSyntax.ts +1027 -0
- package/src/utils/sheetFormulaText.ts +430 -0
- package/src/utils/sheetJournal.ts +200 -0
- package/src/utils/sheetNumberFormat.ts +1012 -0
- package/src/utils/sheetPreview.ts +68 -0
- package/src/utils/sheetSequence.ts +118 -0
- package/src/utils/sheetSnapshot.ts +739 -0
- package/src/utils/sheetStructureEdit.ts +914 -0
- package/src/utils/sheetStructureReferences.ts +389 -0
- package/src/utils/square.ts +22 -12
- package/src/utils/storage.ts +198 -0
- package/src/utils/table-route.ts +127 -11
- package/src/utils/time.ts +12 -0
- package/src/utils/workflow.ts +334 -40
- package/src/utils/workflowLogFingerprint.ts +302 -0
- package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
- package/src/utils/workflowProgress.ts +280 -0
- package/src/utils/workflowTriggers.ts +61 -0
- package/tests/agentNote.test.ts +395 -0
- package/tests/campaignAI.test.ts +119 -0
- package/tests/campaignAccess.test.ts +239 -0
- package/tests/campaignEmail.test.ts +441 -0
- package/tests/campaignLifecycle.test.ts +134 -0
- package/tests/campaignPublication.test.ts +309 -0
- package/tests/dataTableFieldTypeRegistry.test.ts +111 -0
- package/tests/dataTableRelativeDate.test.ts +412 -0
- package/tests/docAccess.test.ts +525 -0
- package/tests/editLogEntry.test.ts +112 -0
- package/tests/email.test.ts +39 -0
- package/tests/fieldDocProgram.test.ts +114 -0
- package/tests/logDisplay.fixtures.ts +67 -0
- package/tests/logDisplay.test.ts +227 -0
- package/tests/noteDoc.test.ts +514 -0
- package/tests/richTextHTML.test.ts +185 -0
- package/tests/sheetAxisMove.test.ts +352 -0
- package/tests/sheetBootstrap.test.ts +1967 -0
- package/tests/sheetFeatureReference.test.ts +22 -0
- package/tests/sheetFormulaCriteria.test.ts +269 -0
- package/tests/sheetFormulaEvaluator.test.ts +468 -0
- package/tests/sheetFormulaFunctions.test.ts +538 -0
- package/tests/sheetFormulaLanguage.test.ts +361 -0
- package/tests/sheetFormulaParity.fixtures.ts +872 -0
- package/tests/sheetFormulaParity.test.ts +188 -0
- package/tests/sheetFormulaRange.test.ts +167 -0
- package/tests/sheetFormulaReference.test.ts +78 -0
- package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
- package/tests/sheetFormulaTextDate.test.ts +512 -0
- package/tests/sheetSequence.test.ts +22 -0
- package/tests/sheetSnapshot.test.ts +134 -0
- package/tests/sheetStructureEdit.test.ts +537 -0
- package/tests/sheetStructureReferences.test.ts +589 -0
- package/tests/workflowLifecycleStatus.test.ts +60 -0
- package/tests/workflowLogFingerprint.test.ts +203 -0
- package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
- package/tests/workflowProgress.test.ts +180 -0
- package/tests/workflowTriggers.test.ts +29 -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/organization.ts +0 -65
- 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,739 @@
|
|
|
1
|
+
import type { SheetCellGQL, SheetGridPageInfoGQL, SheetGridViewportObj, SheetTabGQL } from '../types/sheet.d.ts';
|
|
2
|
+
import { requireSheetStorageRevision } from './sheetSequence.ts';
|
|
3
|
+
|
|
4
|
+
/* v5 stores ordinary spreadsheet cells and rejects all earlier snapshot contracts. */
|
|
5
|
+
export const SHEET_SNAPSHOT_ENCODING_VERSION = 5;
|
|
6
|
+
|
|
7
|
+
export type SheetSnapshotAncestry = {
|
|
8
|
+
organizationId: string;
|
|
9
|
+
sheetId: string;
|
|
10
|
+
sheetTabId: string;
|
|
11
|
+
seq: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type SheetSnapshotTabGQL = Omit<SheetTabGQL, 'cellsRevision'> & {
|
|
15
|
+
cellsRevision?: string | null;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type SheetSnapshotRawData = {
|
|
19
|
+
sheetId: string;
|
|
20
|
+
sheetTab: SheetSnapshotTabGQL;
|
|
21
|
+
cells: SheetCellGQL[];
|
|
22
|
+
|
|
23
|
+
pageInfo: SheetGridPageInfoGQL;
|
|
24
|
+
viewport: SheetGridViewportObj;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type EncodedSheetSnapshot = {
|
|
28
|
+
encodingVersion: typeof SHEET_SNAPSHOT_ENCODING_VERSION;
|
|
29
|
+
payload: string;
|
|
30
|
+
cellCount: number;
|
|
31
|
+
payloadBytes: number;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
type CompactSnapshotRecord = Record<string, unknown>;
|
|
35
|
+
type CompactSheetCell = [unknown, number, number, CompactSnapshotRecord?];
|
|
36
|
+
|
|
37
|
+
type CompactSheetSnapshotEnvelope = {
|
|
38
|
+
v: typeof SHEET_SNAPSHOT_ENCODING_VERSION;
|
|
39
|
+
t: CompactSnapshotRecord;
|
|
40
|
+
c: CompactSheetCell[];
|
|
41
|
+
|
|
42
|
+
p: CompactSnapshotRecord;
|
|
43
|
+
s: CompactSnapshotRecord[];
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const FILE_CONSTANTS = (() => {
|
|
47
|
+
const cellFieldAliases: Record<string, string> = {
|
|
48
|
+
rawInput: 'i',
|
|
49
|
+
formulaText: 'f',
|
|
50
|
+
value: 'v',
|
|
51
|
+
formulaValue: 'q',
|
|
52
|
+
textValue: 't',
|
|
53
|
+
numberValue: 'n',
|
|
54
|
+
booleanValue: 'b',
|
|
55
|
+
dateValue: 'd',
|
|
56
|
+
datetimeValue: 'z',
|
|
57
|
+
errorCode: 'e',
|
|
58
|
+
errorMessage: 'm',
|
|
59
|
+
revision: 'x',
|
|
60
|
+
volatile: 'o',
|
|
61
|
+
formula: 'u',
|
|
62
|
+
style: 's',
|
|
63
|
+
format: 'a',
|
|
64
|
+
note: 'h',
|
|
65
|
+
};
|
|
66
|
+
const compactValueSource = {
|
|
67
|
+
NONE: 0,
|
|
68
|
+
TEXT: 1,
|
|
69
|
+
NUMBER: 2,
|
|
70
|
+
BOOLEAN: 3,
|
|
71
|
+
DATE: 4,
|
|
72
|
+
DATETIME: 5,
|
|
73
|
+
} as const;
|
|
74
|
+
const compactFormulaSource = {
|
|
75
|
+
NONE: 0,
|
|
76
|
+
VALUE: 1,
|
|
77
|
+
TEXT: 2,
|
|
78
|
+
NUMBER: 3,
|
|
79
|
+
BOOLEAN: 4,
|
|
80
|
+
DATE: 5,
|
|
81
|
+
DATETIME: 6,
|
|
82
|
+
} as const;
|
|
83
|
+
|
|
84
|
+
const cellOmitFields = new Set([
|
|
85
|
+
'id',
|
|
86
|
+
'rowIndex',
|
|
87
|
+
'columnIndex',
|
|
88
|
+
'computedAt',
|
|
89
|
+
'createdAt',
|
|
90
|
+
'updatedAt',
|
|
91
|
+
'deletedAt',
|
|
92
|
+
'deletionJobId',
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
// Typed-value aliases and value/formula source pairs, keyed by source code
|
|
96
|
+
const typedFieldAliasesByValueSource: Record<number, string> = {
|
|
97
|
+
[compactValueSource.TEXT]: 't',
|
|
98
|
+
[compactValueSource.NUMBER]: 'n',
|
|
99
|
+
[compactValueSource.BOOLEAN]: 'b',
|
|
100
|
+
[compactValueSource.DATE]: 'd',
|
|
101
|
+
[compactValueSource.DATETIME]: 'z',
|
|
102
|
+
};
|
|
103
|
+
const formulaSourcesByValueSource: Record<number, number> = {
|
|
104
|
+
[compactValueSource.TEXT]: compactFormulaSource.TEXT,
|
|
105
|
+
[compactValueSource.NUMBER]: compactFormulaSource.NUMBER,
|
|
106
|
+
[compactValueSource.BOOLEAN]: compactFormulaSource.BOOLEAN,
|
|
107
|
+
[compactValueSource.DATE]: compactFormulaSource.DATE,
|
|
108
|
+
[compactValueSource.DATETIME]: compactFormulaSource.DATETIME,
|
|
109
|
+
};
|
|
110
|
+
const valueSourcesByFormulaSource: Record<number, number> = {
|
|
111
|
+
[compactFormulaSource.TEXT]: compactValueSource.TEXT,
|
|
112
|
+
[compactFormulaSource.NUMBER]: compactValueSource.NUMBER,
|
|
113
|
+
[compactFormulaSource.BOOLEAN]: compactValueSource.BOOLEAN,
|
|
114
|
+
[compactFormulaSource.DATE]: compactValueSource.DATE,
|
|
115
|
+
[compactFormulaSource.DATETIME]: compactValueSource.DATETIME,
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
ancestryFields: new Set(['__table', 'organizationId', 'sheetId', 'sheetTabId']),
|
|
120
|
+
cellOmitFields,
|
|
121
|
+
|
|
122
|
+
tabOmitFields: new Set(['id', 'cellsRevision']),
|
|
123
|
+
cellFieldAliases,
|
|
124
|
+
// A Map so prototype names such as 'constructor' never resolve as aliases
|
|
125
|
+
cellFieldNamesByAlias: new Map(Object.entries(cellFieldAliases).map(([key, alias]) => [alias, key])),
|
|
126
|
+
compactFormulaSource,
|
|
127
|
+
compactValueSource,
|
|
128
|
+
formulaSourcesByValueSource,
|
|
129
|
+
formulaSourceShift: 3,
|
|
130
|
+
typedFieldAliasesByValueSource,
|
|
131
|
+
valueMaskAlias: 'k',
|
|
132
|
+
valueSourcesByFormulaSource,
|
|
133
|
+
};
|
|
134
|
+
})();
|
|
135
|
+
|
|
136
|
+
/*
|
|
137
|
+
* Return a JSON-safe scalar while preserving full integer precision.
|
|
138
|
+
*/
|
|
139
|
+
function normalizeSheetSnapshotScalar(value: unknown): unknown {
|
|
140
|
+
if (typeof value === 'bigint') {
|
|
141
|
+
return value <= BigInt(Number.MAX_SAFE_INTEGER) && value >= BigInt(Number.MIN_SAFE_INTEGER) ? Number(value) : value.toString();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (value instanceof Date) {
|
|
145
|
+
return value.toISOString();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return value;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/*
|
|
152
|
+
* Return a JSON-safe snapshot value with null object fields removed.
|
|
153
|
+
*/
|
|
154
|
+
function compactSheetSnapshotValue(value: unknown): unknown {
|
|
155
|
+
if (value === null || value === undefined) {
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const scalar = normalizeSheetSnapshotScalar(value);
|
|
160
|
+
if (scalar !== value) {
|
|
161
|
+
return scalar;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (Array.isArray(value)) {
|
|
165
|
+
return value.map((item) => compactSheetSnapshotValue(item) ?? null);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (typeof value === 'object') {
|
|
169
|
+
const compacted: CompactSnapshotRecord = {};
|
|
170
|
+
Object.entries(value as Record<string, unknown>).forEach(([key, item]) => {
|
|
171
|
+
const compactedItem = compactSheetSnapshotValue(item);
|
|
172
|
+
if (compactedItem !== undefined) {
|
|
173
|
+
compacted[key] = compactedItem;
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
return compacted;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return value;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/*
|
|
183
|
+
* Return whether a compact record field carries non-default information.
|
|
184
|
+
*/
|
|
185
|
+
function sheetSnapshotFieldHasValue(key: string, value: unknown): boolean {
|
|
186
|
+
if (value === undefined) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (key === 'volatile' && value === false) {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (
|
|
195
|
+
(key === 'style' || key === 'format') &&
|
|
196
|
+
typeof value === 'object' &&
|
|
197
|
+
!Array.isArray(value) &&
|
|
198
|
+
!Object.keys(value as object).length
|
|
199
|
+
) {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if ((key === 'format' || key === 'metadata') && value === '{}') {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/*
|
|
211
|
+
* Compact one tab while removing the design's repeated tab identifier.
|
|
212
|
+
*/
|
|
213
|
+
function compactSheetSnapshotTab(tab: SheetSnapshotTabGQL): CompactSnapshotRecord {
|
|
214
|
+
const compacted = compactSheetSnapshotRecord(tab as unknown as Record<string, unknown>, FILE_CONSTANTS.tabOmitFields);
|
|
215
|
+
if (compacted.design && typeof compacted.design === 'object') {
|
|
216
|
+
const design = { ...(compacted.design as Record<string, unknown>) };
|
|
217
|
+
delete design.id;
|
|
218
|
+
compacted.design = design;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return compacted;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/*
|
|
225
|
+
* Compact one object after removing ancestry, positional fields, and fields
|
|
226
|
+
* that the snapshot contract deliberately excludes.
|
|
227
|
+
*/
|
|
228
|
+
function compactSheetSnapshotRecord(
|
|
229
|
+
value: Record<string, unknown>,
|
|
230
|
+
omitFields: Set<string>,
|
|
231
|
+
aliases?: Record<string, string>,
|
|
232
|
+
): CompactSnapshotRecord {
|
|
233
|
+
const compacted: CompactSnapshotRecord = {};
|
|
234
|
+
|
|
235
|
+
Object.entries(value).forEach(([key, item]) => {
|
|
236
|
+
if (FILE_CONSTANTS.ancestryFields.has(key) || omitFields.has(key) || key === 'compiledFormula') {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const compactedItem = compactSheetSnapshotValue(item);
|
|
241
|
+
if (sheetSnapshotFieldHasValue(key, compactedItem)) {
|
|
242
|
+
compacted[aliases?.[key] || key] = compactedItem;
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
return compacted;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/*
|
|
250
|
+
* Return the exact GraphQL string shadow represented by one compact typed
|
|
251
|
+
* field, or null when the field cannot losslessly reconstruct a string.
|
|
252
|
+
*/
|
|
253
|
+
function getCompactSheetCellTypedString(fields: CompactSnapshotRecord, source: number): string | null {
|
|
254
|
+
const alias = FILE_CONSTANTS.typedFieldAliasesByValueSource[source];
|
|
255
|
+
if (!alias || !Object.hasOwn(fields, alias)) {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const value = fields[alias];
|
|
260
|
+
if (
|
|
261
|
+
source === FILE_CONSTANTS.compactValueSource.TEXT ||
|
|
262
|
+
source === FILE_CONSTANTS.compactValueSource.DATE ||
|
|
263
|
+
source === FILE_CONSTANTS.compactValueSource.DATETIME
|
|
264
|
+
) {
|
|
265
|
+
return typeof value === 'string' ? value : null;
|
|
266
|
+
}
|
|
267
|
+
if (source === FILE_CONSTANTS.compactValueSource.NUMBER) {
|
|
268
|
+
return typeof value === 'number' && Number.isFinite(value) ? JSON.stringify(value) : null;
|
|
269
|
+
}
|
|
270
|
+
if (source === FILE_CONSTANTS.compactValueSource.BOOLEAN) {
|
|
271
|
+
return typeof value === 'boolean' ? String(value) : null;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/*
|
|
278
|
+
* Return the first typed field that exactly reproduces one serialized GraphQL
|
|
279
|
+
* value. The fixed order keeps the compact metadata deterministic.
|
|
280
|
+
*/
|
|
281
|
+
function getCompactSheetCellValueSource(fields: CompactSnapshotRecord, value: unknown): number {
|
|
282
|
+
if (typeof value !== 'string') {
|
|
283
|
+
return FILE_CONSTANTS.compactValueSource.NONE;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
for (let source = FILE_CONSTANTS.compactValueSource.TEXT; source <= FILE_CONSTANTS.compactValueSource.DATETIME; source += 1) {
|
|
287
|
+
if (getCompactSheetCellTypedString(fields, source) === value) {
|
|
288
|
+
return source;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return FILE_CONSTANTS.compactValueSource.NONE;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/*
|
|
296
|
+
* Return the formula-source code corresponding to one typed-value source.
|
|
297
|
+
*/
|
|
298
|
+
function getCompactSheetCellFormulaSource(valueSource: number): number {
|
|
299
|
+
return FILE_CONSTANTS.formulaSourcesByValueSource[valueSource] || FILE_CONSTANTS.compactFormulaSource.NONE;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/*
|
|
303
|
+
* Remove serialized value/formula shadows only when a short source mask can
|
|
304
|
+
* reconstruct the exact decoded SheetCellGQL fields.
|
|
305
|
+
*/
|
|
306
|
+
function compactSheetCellValueShadows(fields: CompactSnapshotRecord): CompactSnapshotRecord {
|
|
307
|
+
const originalValue = fields.v;
|
|
308
|
+
const originalFormulaValue = fields.q;
|
|
309
|
+
const valueSource = getCompactSheetCellValueSource(fields, originalValue);
|
|
310
|
+
let formulaSource: number = FILE_CONSTANTS.compactFormulaSource.NONE;
|
|
311
|
+
|
|
312
|
+
if (typeof originalFormulaValue === 'string' && typeof originalValue === 'string' && originalFormulaValue === originalValue) {
|
|
313
|
+
formulaSource = FILE_CONSTANTS.compactFormulaSource.VALUE;
|
|
314
|
+
} else {
|
|
315
|
+
formulaSource = getCompactSheetCellFormulaSource(getCompactSheetCellValueSource(fields, originalFormulaValue));
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (valueSource !== FILE_CONSTANTS.compactValueSource.NONE) {
|
|
319
|
+
delete fields.v;
|
|
320
|
+
}
|
|
321
|
+
if (formulaSource !== FILE_CONSTANTS.compactFormulaSource.NONE) {
|
|
322
|
+
delete fields.q;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const mask = valueSource | (formulaSource << FILE_CONSTANTS.formulaSourceShift);
|
|
326
|
+
if (mask) {
|
|
327
|
+
fields[FILE_CONSTANTS.valueMaskAlias] = mask;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return fields;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/*
|
|
334
|
+
* Return one compact style's canonical identity when it is dictionary-safe.
|
|
335
|
+
*/
|
|
336
|
+
function getCompactSheetSnapshotStyleKey(value: unknown): string | null {
|
|
337
|
+
const compacted = compactSheetSnapshotValue(value);
|
|
338
|
+
if (!sheetSnapshotFieldHasValue('style', compacted) || !compacted || typeof compacted !== 'object' || Array.isArray(compacted)) {
|
|
339
|
+
return null;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return stringifyCanonicalSheetSnapshotJSON(compacted);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/*
|
|
346
|
+
* Build a deterministic dictionary containing only styles repeated across at
|
|
347
|
+
* least two cells, so a one-off style never increases payload bytes.
|
|
348
|
+
*/
|
|
349
|
+
function getCompactSheetSnapshotStyleDictionary(raw: SheetSnapshotRawData): {
|
|
350
|
+
styles: CompactSnapshotRecord[];
|
|
351
|
+
styleIndexes: Map<string, number>;
|
|
352
|
+
} {
|
|
353
|
+
const styleCounts = new Map<string, number>();
|
|
354
|
+
raw.cells.forEach((record) => {
|
|
355
|
+
const key = getCompactSheetSnapshotStyleKey(record.style);
|
|
356
|
+
if (key) {
|
|
357
|
+
styleCounts.set(key, (styleCounts.get(key) || 0) + 1);
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
const styleKeys = [...styleCounts.entries()]
|
|
361
|
+
.filter(([, count]) => count > 1)
|
|
362
|
+
.map(([key]) => key)
|
|
363
|
+
.sort();
|
|
364
|
+
const styles = styleKeys.map((key) => JSON.parse(key) as CompactSnapshotRecord);
|
|
365
|
+
|
|
366
|
+
return {
|
|
367
|
+
styles,
|
|
368
|
+
styleIndexes: new Map(styleKeys.map((key, index) => [key, index])),
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/*
|
|
373
|
+
* Replace one repeated inline style with its deterministic dictionary index.
|
|
374
|
+
*/
|
|
375
|
+
function compactSheetSnapshotRecordStyle(
|
|
376
|
+
fields: CompactSnapshotRecord,
|
|
377
|
+
styleField: string,
|
|
378
|
+
styleIndexes: Map<string, number>,
|
|
379
|
+
): CompactSnapshotRecord {
|
|
380
|
+
const key = getCompactSheetSnapshotStyleKey(fields[styleField]);
|
|
381
|
+
const styleIndex = key === null ? undefined : styleIndexes.get(key);
|
|
382
|
+
if (styleIndex !== undefined) {
|
|
383
|
+
fields[styleField] = styleIndex;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
return fields;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/*
|
|
390
|
+
* Return one compact cell tuple with ancestry and coordinates encoded once.
|
|
391
|
+
*/
|
|
392
|
+
function compactSheetSnapshotCell(cell: SheetCellGQL, styleIndexes: Map<string, number>): CompactSheetCell {
|
|
393
|
+
const fields = compactSheetSnapshotRecordStyle(
|
|
394
|
+
compactSheetCellValueShadows(
|
|
395
|
+
compactSheetSnapshotRecord(
|
|
396
|
+
cell as unknown as Record<string, unknown>,
|
|
397
|
+
FILE_CONSTANTS.cellOmitFields,
|
|
398
|
+
FILE_CONSTANTS.cellFieldAliases,
|
|
399
|
+
),
|
|
400
|
+
),
|
|
401
|
+
FILE_CONSTANTS.cellFieldAliases.style,
|
|
402
|
+
styleIndexes,
|
|
403
|
+
);
|
|
404
|
+
// Every cell keeps its authoritative
|
|
405
|
+
// revision: same-id replacement on clients compares it against the
|
|
406
|
+
// revisions live packets and journal rows carry
|
|
407
|
+
fields[FILE_CONSTANTS.cellFieldAliases.revision] = requireSheetStorageRevision(cell.revision);
|
|
408
|
+
const tuple: CompactSheetCell = [cell.id, Number(cell.rowIndex || 0), Number(cell.columnIndex || 0)];
|
|
409
|
+
|
|
410
|
+
if (Object.keys(fields).length) {
|
|
411
|
+
tuple.push(fields);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
return tuple;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/*
|
|
418
|
+
* Serialize a JSON value with recursively sorted object keys.
|
|
419
|
+
*/
|
|
420
|
+
function stringifyCanonicalSheetSnapshotJSON(value: unknown): string {
|
|
421
|
+
if (Array.isArray(value)) {
|
|
422
|
+
return `[${value.map((item) => stringifyCanonicalSheetSnapshotJSON(item)).join(',')}]`;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
if (value && typeof value === 'object') {
|
|
426
|
+
return `{${Object.keys(value as object)
|
|
427
|
+
.sort()
|
|
428
|
+
.map((key) => `${JSON.stringify(key)}:${stringifyCanonicalSheetSnapshotJSON((value as Record<string, unknown>)[key])}`)
|
|
429
|
+
.join(',')}}`;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
return JSON.stringify(value);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/*
|
|
436
|
+
* Compare snapshot identifiers by Unicode code units, independent of the
|
|
437
|
+
* process locale and ICU configuration.
|
|
438
|
+
*/
|
|
439
|
+
function compareSheetSnapshotIds(left: unknown, right: unknown): number {
|
|
440
|
+
const leftId = String(left);
|
|
441
|
+
const rightId = String(right);
|
|
442
|
+
|
|
443
|
+
return leftId < rightId ? -1 : leftId > rightId ? 1 : 0;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/*
|
|
447
|
+
* Return records in deterministic cell-coordinate order.
|
|
448
|
+
*/
|
|
449
|
+
function sortSheetSnapshotCells(cells: SheetCellGQL[]): SheetCellGQL[] {
|
|
450
|
+
return [...cells].sort(
|
|
451
|
+
(left, right) =>
|
|
452
|
+
Number(left.rowIndex || 0) - Number(right.rowIndex || 0) ||
|
|
453
|
+
Number(left.columnIndex || 0) - Number(right.columnIndex || 0) ||
|
|
454
|
+
compareSheetSnapshotIds(left.id, right.id),
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/*
|
|
459
|
+
* Encode one raw, non-presentation-expanded tab state into canonical compact
|
|
460
|
+
* JSON. The viewport lives in the snapshot row and authorization-validated
|
|
461
|
+
* ancestry is supplied once at decode time, so both stay out of the payload.
|
|
462
|
+
*/
|
|
463
|
+
export function encodeSheetSnapshot(raw: SheetSnapshotRawData): EncodedSheetSnapshot {
|
|
464
|
+
const tab = compactSheetSnapshotTab(raw.sheetTab);
|
|
465
|
+
const styleDictionary = getCompactSheetSnapshotStyleDictionary(raw);
|
|
466
|
+
const envelope: CompactSheetSnapshotEnvelope = {
|
|
467
|
+
v: SHEET_SNAPSHOT_ENCODING_VERSION,
|
|
468
|
+
t: tab,
|
|
469
|
+
c: sortSheetSnapshotCells(raw.cells).map((cell) => compactSheetSnapshotCell(cell, styleDictionary.styleIndexes)),
|
|
470
|
+
|
|
471
|
+
p: compactSheetSnapshotValue(raw.pageInfo) as CompactSnapshotRecord,
|
|
472
|
+
s: styleDictionary.styles,
|
|
473
|
+
};
|
|
474
|
+
const payload = stringifyCanonicalSheetSnapshotJSON(envelope);
|
|
475
|
+
|
|
476
|
+
return {
|
|
477
|
+
encodingVersion: SHEET_SNAPSHOT_ENCODING_VERSION,
|
|
478
|
+
payload,
|
|
479
|
+
cellCount: raw.cells.length,
|
|
480
|
+
payloadBytes: new TextEncoder().encode(payload).byteLength,
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/*
|
|
485
|
+
* Shallow-copy one sparse tab or page-info record of the parsed
|
|
486
|
+
* envelope. These records keep their raw field names (only cells use aliases).
|
|
487
|
+
*/
|
|
488
|
+
function expandSheetSnapshotRecord(value: CompactSnapshotRecord | undefined): CompactSnapshotRecord {
|
|
489
|
+
const expanded: CompactSnapshotRecord = {};
|
|
490
|
+
|
|
491
|
+
Object.entries(value || {}).forEach(([key, item]) => {
|
|
492
|
+
expanded[key] = item;
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
return expanded;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/*
|
|
499
|
+
* Clone one JSON snapshot value so dictionary expansion never shares mutable
|
|
500
|
+
* style objects between decoded cells.
|
|
501
|
+
*/
|
|
502
|
+
function cloneSheetSnapshotValue(value: unknown): unknown {
|
|
503
|
+
if (Array.isArray(value)) {
|
|
504
|
+
return value.map(cloneSheetSnapshotValue);
|
|
505
|
+
}
|
|
506
|
+
if (value && typeof value === 'object') {
|
|
507
|
+
const cloned: CompactSnapshotRecord = {};
|
|
508
|
+
Object.entries(value as CompactSnapshotRecord).forEach(([key, item]) => {
|
|
509
|
+
cloned[key] = cloneSheetSnapshotValue(item);
|
|
510
|
+
});
|
|
511
|
+
return cloned;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
return value;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/*
|
|
518
|
+
* Resolve one style dictionary index to an isolated style object.
|
|
519
|
+
*/
|
|
520
|
+
function expandSheetSnapshotStyleIndex(styleIndex: number, styles: CompactSnapshotRecord[] | null): unknown {
|
|
521
|
+
if (!styles || !Number.isSafeInteger(styleIndex) || styleIndex < 0 || styleIndex >= styles.length) {
|
|
522
|
+
throw new Error('Invalid sheet snapshot style dictionary index.');
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
return cloneSheetSnapshotValue(styles[styleIndex]);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/*
|
|
529
|
+
* Return the compact formula-source code's corresponding typed-value source code.
|
|
530
|
+
*/
|
|
531
|
+
function getExpandedSheetCellFormulaValueSource(formulaSource: number): number {
|
|
532
|
+
return FILE_CONSTANTS.valueSourcesByFormulaSource[formulaSource] || FILE_CONSTANTS.compactValueSource.NONE;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/*
|
|
536
|
+
* Return one compact cell's value mask as a number after rejecting a mask
|
|
537
|
+
* outside the 6-bit range or naming an unsupported value/formula source.
|
|
538
|
+
*/
|
|
539
|
+
function requireSheetSnapshotCellValueMask(mask: unknown): number {
|
|
540
|
+
if (!Number.isSafeInteger(mask) || Number(mask) <= 0 || Number(mask) > 63) {
|
|
541
|
+
throw new Error('Invalid sheet snapshot cell value mask.');
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
const numericMask = Number(mask);
|
|
545
|
+
if (
|
|
546
|
+
(numericMask & 7) > FILE_CONSTANTS.compactValueSource.DATETIME ||
|
|
547
|
+
numericMask >> FILE_CONSTANTS.formulaSourceShift > FILE_CONSTANTS.compactFormulaSource.DATETIME
|
|
548
|
+
) {
|
|
549
|
+
throw new Error('Unsupported sheet snapshot cell value mask.');
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
return numericMask;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/*
|
|
556
|
+
* Return the serialized value shadow one mask value source reconstructs, or
|
|
557
|
+
* undefined when the mask names no value source. Rejects a compact record
|
|
558
|
+
* that also carries the value or lacks the typed field it names.
|
|
559
|
+
*/
|
|
560
|
+
function getExpandedSheetCellValueShadow(fields: CompactSnapshotRecord, valueSource: number): string | undefined {
|
|
561
|
+
if (valueSource === FILE_CONSTANTS.compactValueSource.NONE) {
|
|
562
|
+
return undefined;
|
|
563
|
+
}
|
|
564
|
+
if (Object.hasOwn(fields, 'v')) {
|
|
565
|
+
throw new Error('Contradictory sheet snapshot serialized value.');
|
|
566
|
+
}
|
|
567
|
+
const valueString = getCompactSheetCellTypedString(fields, valueSource);
|
|
568
|
+
if (valueString === null) {
|
|
569
|
+
throw new Error('Incomplete sheet snapshot serialized value source.');
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
return valueString;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/*
|
|
576
|
+
* Return the formula value shadow one mask formula source reconstructs from
|
|
577
|
+
* the cell's serialized value or a typed field, or undefined when the mask
|
|
578
|
+
* names no formula source. Rejects contradictory or incomplete metadata.
|
|
579
|
+
*/
|
|
580
|
+
function getExpandedSheetCellFormulaValueShadow(
|
|
581
|
+
fields: CompactSnapshotRecord,
|
|
582
|
+
formulaSource: number,
|
|
583
|
+
value: unknown,
|
|
584
|
+
): string | undefined {
|
|
585
|
+
if (formulaSource === FILE_CONSTANTS.compactFormulaSource.NONE) {
|
|
586
|
+
return undefined;
|
|
587
|
+
}
|
|
588
|
+
if (Object.hasOwn(fields, 'q')) {
|
|
589
|
+
throw new Error('Contradictory sheet snapshot formula value.');
|
|
590
|
+
}
|
|
591
|
+
const formulaValue =
|
|
592
|
+
formulaSource === FILE_CONSTANTS.compactFormulaSource.VALUE
|
|
593
|
+
? value
|
|
594
|
+
: getCompactSheetCellTypedString(fields, getExpandedSheetCellFormulaValueSource(formulaSource));
|
|
595
|
+
if (typeof formulaValue !== 'string') {
|
|
596
|
+
throw new Error('Incomplete sheet snapshot formula value source.');
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
return formulaValue;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/*
|
|
603
|
+
* Decode one compact cell tuple and inject its shared ancestry in a single
|
|
604
|
+
* pass over its fields. Value mask errors take precedence over a bad style
|
|
605
|
+
* index, reconstructed value/formula shadows are appended after the compact
|
|
606
|
+
* fields, and a '__proto__' field is dropped instead of assigned.
|
|
607
|
+
*/
|
|
608
|
+
function expandSheetSnapshotCell(
|
|
609
|
+
tuple: CompactSheetCell,
|
|
610
|
+
ancestry: SheetSnapshotAncestry,
|
|
611
|
+
styles: CompactSnapshotRecord[] | null,
|
|
612
|
+
): SheetCellGQL {
|
|
613
|
+
const cell: CompactSnapshotRecord = {
|
|
614
|
+
id: String(tuple[0] || ''),
|
|
615
|
+
organizationId: ancestry.organizationId,
|
|
616
|
+
sheetId: ancestry.sheetId,
|
|
617
|
+
sheetTabId: ancestry.sheetTabId,
|
|
618
|
+
rowIndex: Number(tuple[1]),
|
|
619
|
+
columnIndex: Number(tuple[2]),
|
|
620
|
+
};
|
|
621
|
+
const fields = tuple[3];
|
|
622
|
+
if (!fields) {
|
|
623
|
+
return cell as SheetCellGQL;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
// Without a style dictionary the mask alias stays an ordinary field
|
|
627
|
+
const mask = styles === null ? undefined : fields[FILE_CONSTANTS.valueMaskAlias];
|
|
628
|
+
let value: string | undefined;
|
|
629
|
+
let formulaValue: string | undefined;
|
|
630
|
+
if (mask !== undefined) {
|
|
631
|
+
const numericMask = requireSheetSnapshotCellValueMask(mask);
|
|
632
|
+
value = getExpandedSheetCellValueShadow(fields, numericMask & 7);
|
|
633
|
+
formulaValue = getExpandedSheetCellFormulaValueShadow(
|
|
634
|
+
fields,
|
|
635
|
+
numericMask >> FILE_CONSTANTS.formulaSourceShift,
|
|
636
|
+
value === undefined ? fields.v : value,
|
|
637
|
+
);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
for (const key in fields) {
|
|
641
|
+
const item = fields[key];
|
|
642
|
+
if (key === '__proto__' || (key === FILE_CONSTANTS.valueMaskAlias && mask !== undefined)) {
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
645
|
+
if (key === FILE_CONSTANTS.cellFieldAliases.style && typeof item === 'number') {
|
|
646
|
+
cell.style = expandSheetSnapshotStyleIndex(item, styles);
|
|
647
|
+
continue;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
cell[FILE_CONSTANTS.cellFieldNamesByAlias.get(key) ?? key] = item;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
if (value !== undefined) {
|
|
654
|
+
cell.value = value;
|
|
655
|
+
}
|
|
656
|
+
if (formulaValue !== undefined) {
|
|
657
|
+
cell.formulaValue = formulaValue;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
return cell as SheetCellGQL;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/*
|
|
664
|
+
* Return whether one encoding version is the current snapshot contract.
|
|
665
|
+
*/
|
|
666
|
+
export function isSupportedSheetSnapshotEncodingVersion(value: unknown): value is typeof SHEET_SNAPSHOT_ENCODING_VERSION {
|
|
667
|
+
return value === SHEET_SNAPSHOT_ENCODING_VERSION;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/*
|
|
671
|
+
* Return whether one parsed JSON value is a non-array compact record.
|
|
672
|
+
*/
|
|
673
|
+
function isCompactSheetSnapshotRecord(value: unknown): value is CompactSnapshotRecord {
|
|
674
|
+
return Boolean(value && typeof value === 'object' && !Array.isArray(value));
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/*
|
|
678
|
+
* Return whether every tuple list has the minimum positional shape required
|
|
679
|
+
* for safe decoding. Field-level masks and dictionary indexes validate later.
|
|
680
|
+
*/
|
|
681
|
+
function hasValidCompactSheetSnapshotTuples(envelope: Partial<CompactSheetSnapshotEnvelope>): boolean {
|
|
682
|
+
return Boolean(Array.isArray(envelope.c) && envelope.c.every((tuple) => Array.isArray(tuple) && tuple.length >= 3));
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
/*
|
|
686
|
+
* Parse and validate the current compact envelope. Unknown versions fail closed.
|
|
687
|
+
*/
|
|
688
|
+
function parseSheetSnapshotEnvelope(payload: string): CompactSheetSnapshotEnvelope {
|
|
689
|
+
const envelope = JSON.parse(payload) as Partial<CompactSheetSnapshotEnvelope>;
|
|
690
|
+
const shapeIsValid =
|
|
691
|
+
isSupportedSheetSnapshotEncodingVersion(envelope.v) &&
|
|
692
|
+
isCompactSheetSnapshotRecord(envelope.t) &&
|
|
693
|
+
isCompactSheetSnapshotRecord(envelope.p) &&
|
|
694
|
+
hasValidCompactSheetSnapshotTuples(envelope) &&
|
|
695
|
+
Array.isArray(envelope.s) &&
|
|
696
|
+
envelope.s.every(isCompactSheetSnapshotRecord);
|
|
697
|
+
if (!shapeIsValid) {
|
|
698
|
+
throw new Error(`Unsupported or invalid sheet snapshot encoding: ${String(envelope.v)}`);
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
return envelope as CompactSheetSnapshotEnvelope;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
/*
|
|
705
|
+
* Decode canonical snapshot JSON into raw tab state and inject ancestry once
|
|
706
|
+
* for every tab-owned record. Presentation remains unresolved by design.
|
|
707
|
+
*/
|
|
708
|
+
export function decodeSheetSnapshot(
|
|
709
|
+
payload: string,
|
|
710
|
+
ancestry: SheetSnapshotAncestry,
|
|
711
|
+
viewport: SheetGridViewportObj,
|
|
712
|
+
): SheetSnapshotRawData {
|
|
713
|
+
const envelope = parseSheetSnapshotEnvelope(payload);
|
|
714
|
+
const styles = envelope.s;
|
|
715
|
+
const tabFields = expandSheetSnapshotRecord(envelope.t);
|
|
716
|
+
const design =
|
|
717
|
+
tabFields.design && typeof tabFields.design === 'object'
|
|
718
|
+
? {
|
|
719
|
+
...(tabFields.design as Record<string, unknown>),
|
|
720
|
+
id: ancestry.sheetTabId,
|
|
721
|
+
}
|
|
722
|
+
: tabFields.design;
|
|
723
|
+
|
|
724
|
+
return {
|
|
725
|
+
sheetId: ancestry.sheetId,
|
|
726
|
+
sheetTab: {
|
|
727
|
+
id: ancestry.sheetTabId,
|
|
728
|
+
organizationId: ancestry.organizationId,
|
|
729
|
+
sheetId: ancestry.sheetId,
|
|
730
|
+
cellsRevision: ancestry.seq,
|
|
731
|
+
...tabFields,
|
|
732
|
+
design,
|
|
733
|
+
} as SheetSnapshotTabGQL,
|
|
734
|
+
cells: envelope.c.map((cell) => expandSheetSnapshotCell(cell, ancestry, styles)),
|
|
735
|
+
|
|
736
|
+
pageInfo: expandSheetSnapshotRecord(envelope.p) as unknown as SheetGridPageInfoGQL,
|
|
737
|
+
viewport,
|
|
738
|
+
};
|
|
739
|
+
}
|