@jsb188/mday 1.1.11 → 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.
Files changed (210) hide show
  1. package/package.json +58 -23
  2. package/src/constants/agentMemory.ts +14 -45
  3. package/src/constants/agentNote.ts +212 -0
  4. package/src/constants/aiChat.ts +5 -0
  5. package/src/constants/billing.ts +126 -0
  6. package/src/constants/campaign.ts +156 -47
  7. package/src/constants/contact.ts +72 -0
  8. package/src/constants/csvExport.ts +133 -0
  9. package/src/constants/dataTable.ts +12 -38
  10. package/src/constants/dataTableFieldTypes.generated.ts +3 -7
  11. package/src/constants/docAccess.ts +3 -2
  12. package/src/constants/dt.ts +234 -0
  13. package/src/constants/email.ts +114 -12
  14. package/src/constants/fieldDoc.ts +74 -4
  15. package/src/constants/graphqlReactivity.ts +108 -25
  16. package/src/constants/log.ts +9 -0
  17. package/src/constants/logDisplay/README.md +88 -0
  18. package/src/constants/logDisplay/defaults.ts +39 -0
  19. package/src/constants/logDisplay/index.ts +27 -0
  20. package/src/constants/logDisplay/operations/arable.ts +16 -0
  21. package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
  22. package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
  23. package/src/constants/logDisplay/shared/accounting.ts +13 -0
  24. package/src/constants/logDisplay/shared/interactions.ts +13 -0
  25. package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
  26. package/src/constants/logTaxonomy.generated.ts +1834 -730
  27. package/src/constants/organization.ts +13 -14
  28. package/src/constants/qrCode.ts +30 -0
  29. package/src/constants/sheet.ts +65 -101
  30. package/src/constants/sms.ts +18 -0
  31. package/src/constants/storage.ts +33 -9
  32. package/src/constants/usage.ts +3 -0
  33. package/src/constants/workflowSteps.ts +60 -0
  34. package/src/schemas/editLogEntry.ts +106 -283
  35. package/src/schemas/inboundContact.ts +5 -5
  36. package/src/types/agentMemory.d.ts +38 -14
  37. package/src/types/agentNote.d.ts +443 -0
  38. package/src/types/billing.d.ts +537 -0
  39. package/src/types/campaign.d.ts +1043 -193
  40. package/src/types/contact.d.ts +15 -0
  41. package/src/types/csvExport.d.ts +213 -0
  42. package/src/types/dataAnalysis.d.ts +44 -0
  43. package/src/types/dataTable.d.ts +102 -309
  44. package/src/types/definitionElement.d.ts +35 -0
  45. package/src/types/dt.d.ts +257 -0
  46. package/src/types/email.d.ts +144 -55
  47. package/src/types/emailReplyReport.d.ts +76 -0
  48. package/src/types/fieldDoc.d.ts +579 -150
  49. package/src/types/fieldDocContract.d.ts +90 -0
  50. package/src/types/inboundContact.d.ts +8 -0
  51. package/src/types/log.d.ts +94 -3
  52. package/src/types/logDisplay.types.ts +85 -0
  53. package/src/types/organization.d.ts +8 -3
  54. package/src/types/qrCode.d.ts +96 -0
  55. package/src/types/richText.d.ts +81 -0
  56. package/src/types/sheet.d.ts +180 -463
  57. package/src/types/sms.d.ts +38 -0
  58. package/src/types/storage.d.ts +266 -27
  59. package/src/types/workflow.d.ts +196 -66
  60. package/src/utils/agentMemory.ts +15 -0
  61. package/src/utils/agentNote.ts +850 -0
  62. package/src/utils/aiChat.ts +31 -14
  63. package/src/utils/aiChatWork.ts +64 -0
  64. package/src/utils/billing.ts +37 -0
  65. package/src/utils/campaign.ts +642 -209
  66. package/src/utils/campaignAI.ts +72 -0
  67. package/src/utils/campaignAccess.ts +239 -0
  68. package/src/utils/campaignEmail.ts +601 -0
  69. package/src/utils/campaignEmailCompiler.ts +372 -0
  70. package/src/utils/campaignEmailImages.ts +60 -0
  71. package/src/utils/campaignEmailStyles.ts +544 -0
  72. package/src/utils/campaignLifecycle.ts +104 -0
  73. package/src/utils/campaignPreview.ts +285 -0
  74. package/src/utils/contact.ts +50 -0
  75. package/src/utils/csvExport.ts +19 -0
  76. package/src/utils/dataTable.ts +18 -556
  77. package/src/utils/dataTableFieldTypeRegistry.ts +1 -0
  78. package/src/utils/dataTableRelativeDate.ts +447 -0
  79. package/src/utils/definitionElement.ts +84 -0
  80. package/src/utils/email.ts +208 -1
  81. package/src/utils/emailReplyReport.test.ts +187 -0
  82. package/src/utils/emailReplyReport.ts +143 -0
  83. package/src/utils/fieldDoc.ts +81 -37
  84. package/src/utils/fieldDocAnswers.ts +128 -0
  85. package/src/utils/fieldDocDefaults.ts +36 -0
  86. package/src/utils/fieldDocProgram.ts +36 -0
  87. package/src/utils/fieldDocSummary.ts +30 -0
  88. package/src/utils/log.ts +232 -141
  89. package/src/utils/logDisplay.helpers.ts +177 -0
  90. package/src/utils/logDisplay.ts +87 -0
  91. package/src/utils/logStoreRead.ts +9 -13
  92. package/src/utils/logTaxonomy.helpers.ts +17 -0
  93. package/src/utils/logTaxonomyDisplay.ts +80 -124
  94. package/src/utils/noteDoc.ts +66 -284
  95. package/src/utils/organization.ts +117 -283
  96. package/src/utils/qrCode.helpers.ts +189 -0
  97. package/src/utils/qrCode.test.ts +519 -0
  98. package/src/utils/qrCode.ts +273 -0
  99. package/src/utils/richTextHTML.ts +385 -0
  100. package/src/utils/sheet.ts +123 -4834
  101. package/src/utils/sheetAxisMove.ts +77 -291
  102. package/src/utils/sheetBootstrap.ts +771 -1138
  103. package/src/utils/sheetCellLiteral.ts +101 -0
  104. package/src/utils/sheetCommand.ts +23 -88
  105. package/src/utils/sheetDesign.ts +705 -0
  106. package/src/utils/sheetExport.ts +17 -69
  107. package/src/utils/sheetFeatureReference.ts +183 -0
  108. package/src/utils/sheetFormulaArgs.ts +168 -0
  109. package/src/utils/sheetFormulaCompile.ts +475 -0
  110. package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
  111. package/src/utils/sheetFormulaCriteria.ts +656 -0
  112. package/src/utils/sheetFormulaDate.ts +444 -0
  113. package/src/utils/sheetFormulaEvaluation.ts +388 -403
  114. package/src/utils/sheetFormulaEvaluator.ts +589 -444
  115. package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
  116. package/src/utils/sheetFormulaLogical.ts +116 -0
  117. package/src/utils/sheetFormulaLookup.ts +488 -0
  118. package/src/utils/sheetFormulaMath.ts +48 -0
  119. package/src/utils/sheetFormulaParser.ts +729 -0
  120. package/src/utils/sheetFormulaRange.ts +330 -0
  121. package/src/utils/sheetFormulaReference.ts +703 -0
  122. package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
  123. package/src/utils/sheetFormulaReferenceDates.ts +237 -0
  124. package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
  125. package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
  126. package/src/utils/sheetFormulaReferenceMath.ts +95 -0
  127. package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
  128. package/src/utils/sheetFormulaReferenceText.ts +462 -0
  129. package/src/utils/sheetFormulaSyntax.ts +1027 -0
  130. package/src/utils/sheetFormulaText.ts +430 -0
  131. package/src/utils/sheetJournal.ts +140 -18
  132. package/src/utils/sheetNumberFormat.ts +68 -267
  133. package/src/utils/sheetPreview.ts +68 -0
  134. package/src/utils/sheetSequence.ts +13 -43
  135. package/src/utils/sheetSnapshot.ts +176 -429
  136. package/src/utils/sheetStructureEdit.ts +537 -907
  137. package/src/utils/sheetStructureReferences.ts +389 -0
  138. package/src/utils/storage.ts +49 -3
  139. package/src/utils/time.ts +12 -0
  140. package/src/utils/workflow.ts +199 -73
  141. package/src/utils/workflowLogFingerprint.ts +302 -0
  142. package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
  143. package/src/utils/workflowProgress.ts +280 -0
  144. package/src/utils/workflowTriggers.ts +61 -0
  145. package/tests/agentNote.test.ts +395 -0
  146. package/tests/campaignAI.test.ts +119 -0
  147. package/tests/campaignAccess.test.ts +239 -0
  148. package/tests/campaignEmail.test.ts +441 -0
  149. package/tests/campaignLifecycle.test.ts +134 -0
  150. package/tests/campaignPublication.test.ts +309 -0
  151. package/tests/dataTableFieldTypeRegistry.test.ts +2 -1
  152. package/tests/dataTableRelativeDate.test.ts +412 -0
  153. package/tests/email.test.ts +39 -0
  154. package/tests/fieldDocProgram.test.ts +114 -0
  155. package/tests/logDisplay.fixtures.ts +67 -0
  156. package/tests/logDisplay.test.ts +227 -0
  157. package/tests/noteDoc.test.ts +37 -192
  158. package/tests/richTextHTML.test.ts +185 -0
  159. package/tests/sheetAxisMove.test.ts +49 -40
  160. package/tests/sheetBootstrap.test.ts +1967 -0
  161. package/tests/sheetFeatureReference.test.ts +22 -0
  162. package/tests/sheetFormulaCriteria.test.ts +269 -0
  163. package/tests/sheetFormulaEvaluator.test.ts +93 -200
  164. package/tests/sheetFormulaFunctions.test.ts +538 -0
  165. package/tests/sheetFormulaLanguage.test.ts +361 -0
  166. package/tests/sheetFormulaParity.fixtures.ts +872 -0
  167. package/tests/sheetFormulaParity.test.ts +188 -0
  168. package/tests/sheetFormulaRange.test.ts +167 -0
  169. package/tests/sheetFormulaReference.test.ts +78 -0
  170. package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
  171. package/tests/sheetFormulaTextDate.test.ts +512 -0
  172. package/tests/sheetSequence.test.ts +4 -23
  173. package/tests/sheetSnapshot.test.ts +134 -0
  174. package/tests/sheetStructureEdit.test.ts +492 -37
  175. package/tests/sheetStructureReferences.test.ts +589 -0
  176. package/tests/workflowLifecycleStatus.test.ts +60 -0
  177. package/tests/workflowLogFingerprint.test.ts +203 -0
  178. package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
  179. package/tests/workflowProgress.test.ts +180 -0
  180. package/tests/workflowTriggers.test.ts +29 -0
  181. package/src/constants/agentDoc.ts +0 -12
  182. package/src/constants/logActivityGroupColors.generated.ts +0 -19
  183. package/src/constants/schedule.ts +0 -220
  184. package/src/constants/scheduleRuleTemplateSeed.ts +0 -138
  185. package/src/constants/scheduleRuleTemplates.generated.ts +0 -145
  186. package/src/constants/sheetRegionSources.generated.ts +0 -578
  187. package/src/schemas/fieldDocsQuery.ts +0 -12
  188. package/src/schemas/organization.ts +0 -68
  189. package/src/types/agentDoc.d.ts +0 -64
  190. package/src/types/noteDoc.d.ts +0 -86
  191. package/src/types/schedule.d.ts +0 -391
  192. package/src/utils/agentDoc.ts +0 -109
  193. package/src/utils/campaignFormEditor.ts +0 -871
  194. package/src/utils/dataTableRealtimeAuthority.ts +0 -209
  195. package/src/utils/dataTableRealtimeSelection.ts +0 -134
  196. package/src/utils/dataTableRowDocument.ts +0 -508
  197. package/src/utils/dataTableSource.ts +0 -230
  198. package/src/utils/schedule.ts +0 -799
  199. package/src/utils/scheduleEvaluation.ts +0 -1994
  200. package/src/utils/scheduleICS.ts +0 -246
  201. package/src/utils/scheduleRuleTemplates.ts +0 -188
  202. package/src/utils/sheetRegion.ts +0 -374
  203. package/src/utils/sheetRegionFilter.ts +0 -1259
  204. package/src/utils/sheetRegionSourceRegistry.ts +0 -594
  205. package/tests/schedule.test.ts +0 -693
  206. package/tests/scheduleEvaluation.test.ts +0 -1970
  207. package/tests/scheduleICS.test.ts +0 -208
  208. package/tests/scheduleRuleTemplates.test.ts +0 -176
  209. package/tests/scheduleSweepInvariants.test.ts +0 -428
  210. package/tests/sheetRegionFilter.test.ts +0 -1135
@@ -1,15 +1,8 @@
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';
1
+ import type { SheetCellGQL, SheetGridPageInfoGQL, SheetGridViewportObj, SheetTabGQL } from '../types/sheet.d.ts';
10
2
  import { requireSheetStorageRevision } from './sheetSequence.ts';
11
3
 
12
- export const SHEET_SNAPSHOT_ENCODING_VERSION = 3;
4
+ /* v5 stores ordinary spreadsheet cells and rejects all earlier snapshot contracts. */
5
+ export const SHEET_SNAPSHOT_ENCODING_VERSION = 5;
13
6
 
14
7
  export type SheetSnapshotAncestry = {
15
8
  organizationId: string;
@@ -26,8 +19,7 @@ export type SheetSnapshotRawData = {
26
19
  sheetId: string;
27
20
  sheetTab: SheetSnapshotTabGQL;
28
21
  cells: SheetCellGQL[];
29
- ranges: SheetRangeGQL[];
30
- regions: SheetRegionGQL[];
22
+
31
23
  pageInfo: SheetGridPageInfoGQL;
32
24
  viewport: SheetGridViewportObj;
33
25
  };
@@ -41,22 +33,12 @@ export type EncodedSheetSnapshot = {
41
33
 
42
34
  type CompactSnapshotRecord = Record<string, unknown>;
43
35
  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
36
 
54
37
  type CompactSheetSnapshotEnvelope = {
55
38
  v: typeof SHEET_SNAPSHOT_ENCODING_VERSION;
56
39
  t: CompactSnapshotRecord;
57
40
  c: CompactSheetCell[];
58
- r: CompactSheetRange[];
59
- g: CompactSheetRegion[];
41
+
60
42
  p: CompactSnapshotRecord;
61
43
  s: CompactSnapshotRecord[];
62
44
  };
@@ -80,13 +62,6 @@ const FILE_CONSTANTS = (() => {
80
62
  style: 's',
81
63
  format: 'a',
82
64
  note: 'h',
83
- sourceType: 'y',
84
- regionId: 'g',
85
- sourceDataTableRowId: 'j',
86
- sourceCellKey: 'l',
87
- sourceColumnId: 'C',
88
- sourceMeta: 'w',
89
- region: 'R',
90
65
  };
91
66
  const compactValueSource = {
92
67
  NONE: 0,
@@ -115,43 +90,46 @@ const FILE_CONSTANTS = (() => {
115
90
  'updatedAt',
116
91
  'deletedAt',
117
92
  'deletionJobId',
118
- 'sourceRowKey',
119
93
  ]);
120
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
+
121
118
  return {
122
- ancestryFields: new Set([
123
- '__table',
124
- 'organizationId',
125
- 'sheetId',
126
- 'sheetTabId',
127
- ]),
119
+ ancestryFields: new Set(['__table', 'organizationId', 'sheetId', 'sheetTabId']),
128
120
  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
- ]),
121
+
146
122
  tabOmitFields: new Set(['id', 'cellsRevision']),
147
123
  cellFieldAliases,
148
- cellFieldNamesByAlias: Object.fromEntries(
149
- Object.entries(cellFieldAliases).map(([key, alias]) => [alias, key]),
150
- ),
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])),
151
126
  compactFormulaSource,
152
127
  compactValueSource,
128
+ formulaSourcesByValueSource,
153
129
  formulaSourceShift: 3,
130
+ typedFieldAliasesByValueSource,
154
131
  valueMaskAlias: 'k',
132
+ valueSourcesByFormulaSource,
155
133
  };
156
134
  })();
157
135
 
@@ -160,10 +138,7 @@ const FILE_CONSTANTS = (() => {
160
138
  */
161
139
  function normalizeSheetSnapshotScalar(value: unknown): unknown {
162
140
  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();
141
+ return value <= BigInt(Number.MAX_SAFE_INTEGER) && value >= BigInt(Number.MIN_SAFE_INTEGER) ? Number(value) : value.toString();
167
142
  }
168
143
 
169
144
  if (value instanceof Date) {
@@ -235,13 +210,8 @@ function sheetSnapshotFieldHasValue(key: string, value: unknown): boolean {
235
210
  /*
236
211
  * Compact one tab while removing the design's repeated tab identifier.
237
212
  */
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
- );
213
+ function compactSheetSnapshotTab(tab: SheetSnapshotTabGQL): CompactSnapshotRecord {
214
+ const compacted = compactSheetSnapshotRecord(tab as unknown as Record<string, unknown>, FILE_CONSTANTS.tabOmitFields);
245
215
  if (compacted.design && typeof compacted.design === 'object') {
246
216
  const design = { ...(compacted.design as Record<string, unknown>) };
247
217
  delete design.id;
@@ -263,11 +233,7 @@ function compactSheetSnapshotRecord(
263
233
  const compacted: CompactSnapshotRecord = {};
264
234
 
265
235
  Object.entries(value).forEach(([key, item]) => {
266
- if (
267
- FILE_CONSTANTS.ancestryFields.has(key) ||
268
- omitFields.has(key) ||
269
- key === 'compiledFormula'
270
- ) {
236
+ if (FILE_CONSTANTS.ancestryFields.has(key) || omitFields.has(key) || key === 'compiledFormula') {
271
237
  return;
272
238
  }
273
239
 
@@ -284,18 +250,8 @@ function compactSheetSnapshotRecord(
284
250
  * Return the exact GraphQL string shadow represented by one compact typed
285
251
  * field, or null when the field cannot losslessly reconstruct a string.
286
252
  */
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];
253
+ function getCompactSheetCellTypedString(fields: CompactSnapshotRecord, source: number): string | null {
254
+ const alias = FILE_CONSTANTS.typedFieldAliasesByValueSource[source];
299
255
  if (!alias || !Object.hasOwn(fields, alias)) {
300
256
  return null;
301
257
  }
@@ -309,9 +265,7 @@ function getCompactSheetCellTypedString(
309
265
  return typeof value === 'string' ? value : null;
310
266
  }
311
267
  if (source === FILE_CONSTANTS.compactValueSource.NUMBER) {
312
- return typeof value === 'number' && Number.isFinite(value)
313
- ? JSON.stringify(value)
314
- : null;
268
+ return typeof value === 'number' && Number.isFinite(value) ? JSON.stringify(value) : null;
315
269
  }
316
270
  if (source === FILE_CONSTANTS.compactValueSource.BOOLEAN) {
317
271
  return typeof value === 'boolean' ? String(value) : null;
@@ -324,19 +278,12 @@ function getCompactSheetCellTypedString(
324
278
  * Return the first typed field that exactly reproduces one serialized GraphQL
325
279
  * value. The fixed order keeps the compact metadata deterministic.
326
280
  */
327
- function getCompactSheetCellValueSource(
328
- fields: CompactSnapshotRecord,
329
- value: unknown,
330
- ): number {
281
+ function getCompactSheetCellValueSource(fields: CompactSnapshotRecord, value: unknown): number {
331
282
  if (typeof value !== 'string') {
332
283
  return FILE_CONSTANTS.compactValueSource.NONE;
333
284
  }
334
285
 
335
- for (
336
- let source = FILE_CONSTANTS.compactValueSource.TEXT;
337
- source <= FILE_CONSTANTS.compactValueSource.DATETIME;
338
- source += 1
339
- ) {
286
+ for (let source = FILE_CONSTANTS.compactValueSource.TEXT; source <= FILE_CONSTANTS.compactValueSource.DATETIME; source += 1) {
340
287
  if (getCompactSheetCellTypedString(fields, source) === value) {
341
288
  return source;
342
289
  }
@@ -349,47 +296,23 @@ function getCompactSheetCellValueSource(
349
296
  * Return the formula-source code corresponding to one typed-value source.
350
297
  */
351
298
  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
- );
299
+ return FILE_CONSTANTS.formulaSourcesByValueSource[valueSource] || FILE_CONSTANTS.compactFormulaSource.NONE;
369
300
  }
370
301
 
371
302
  /*
372
303
  * Remove serialized value/formula shadows only when a short source mask can
373
304
  * reconstruct the exact decoded SheetCellGQL fields.
374
305
  */
375
- function compactSheetCellValueShadows(
376
- fields: CompactSnapshotRecord,
377
- ): CompactSnapshotRecord {
306
+ function compactSheetCellValueShadows(fields: CompactSnapshotRecord): CompactSnapshotRecord {
378
307
  const originalValue = fields.v;
379
308
  const originalFormulaValue = fields.q;
380
309
  const valueSource = getCompactSheetCellValueSource(fields, originalValue);
381
310
  let formulaSource: number = FILE_CONSTANTS.compactFormulaSource.NONE;
382
311
 
383
- if (
384
- typeof originalFormulaValue === 'string' &&
385
- typeof originalValue === 'string' &&
386
- originalFormulaValue === originalValue
387
- ) {
312
+ if (typeof originalFormulaValue === 'string' && typeof originalValue === 'string' && originalFormulaValue === originalValue) {
388
313
  formulaSource = FILE_CONSTANTS.compactFormulaSource.VALUE;
389
314
  } else {
390
- formulaSource = getCompactSheetCellFormulaSource(
391
- getCompactSheetCellValueSource(fields, originalFormulaValue),
392
- );
315
+ formulaSource = getCompactSheetCellFormulaSource(getCompactSheetCellValueSource(fields, originalFormulaValue));
393
316
  }
394
317
 
395
318
  if (valueSource !== FILE_CONSTANTS.compactValueSource.NONE) {
@@ -399,8 +322,7 @@ function compactSheetCellValueShadows(
399
322
  delete fields.q;
400
323
  }
401
324
 
402
- const mask =
403
- valueSource | (formulaSource << FILE_CONSTANTS.formulaSourceShift);
325
+ const mask = valueSource | (formulaSource << FILE_CONSTANTS.formulaSourceShift);
404
326
  if (mask) {
405
327
  fields[FILE_CONSTANTS.valueMaskAlias] = mask;
406
328
  }
@@ -413,12 +335,7 @@ function compactSheetCellValueShadows(
413
335
  */
414
336
  function getCompactSheetSnapshotStyleKey(value: unknown): string | null {
415
337
  const compacted = compactSheetSnapshotValue(value);
416
- if (
417
- !sheetSnapshotFieldHasValue('style', compacted) ||
418
- !compacted ||
419
- typeof compacted !== 'object' ||
420
- Array.isArray(compacted)
421
- ) {
338
+ if (!sheetSnapshotFieldHasValue('style', compacted) || !compacted || typeof compacted !== 'object' || Array.isArray(compacted)) {
422
339
  return null;
423
340
  }
424
341
 
@@ -427,14 +344,14 @@ function getCompactSheetSnapshotStyleKey(value: unknown): string | null {
427
344
 
428
345
  /*
429
346
  * Build a deterministic dictionary containing only styles repeated across at
430
- * least two cells/ranges, so a one-off style never increases payload bytes.
347
+ * least two cells, so a one-off style never increases payload bytes.
431
348
  */
432
349
  function getCompactSheetSnapshotStyleDictionary(raw: SheetSnapshotRawData): {
433
350
  styles: CompactSnapshotRecord[];
434
351
  styleIndexes: Map<string, number>;
435
352
  } {
436
353
  const styleCounts = new Map<string, number>();
437
- [...raw.cells, ...raw.ranges].forEach((record) => {
354
+ raw.cells.forEach((record) => {
438
355
  const key = getCompactSheetSnapshotStyleKey(record.style);
439
356
  if (key) {
440
357
  styleCounts.set(key, (styleCounts.get(key) || 0) + 1);
@@ -444,9 +361,7 @@ function getCompactSheetSnapshotStyleDictionary(raw: SheetSnapshotRawData): {
444
361
  .filter(([, count]) => count > 1)
445
362
  .map(([key]) => key)
446
363
  .sort();
447
- const styles = styleKeys.map(
448
- (key) => JSON.parse(key) as CompactSnapshotRecord,
449
- );
364
+ const styles = styleKeys.map((key) => JSON.parse(key) as CompactSnapshotRecord);
450
365
 
451
366
  return {
452
367
  styles,
@@ -474,86 +389,23 @@ function compactSheetSnapshotRecordStyle(
474
389
  /*
475
390
  * Return one compact cell tuple with ancestry and coordinates encoded once.
476
391
  */
477
- function compactSheetSnapshotCell(
478
- cell: SheetCellGQL,
479
- styleIndexes: Map<string, number>,
480
- ): CompactSheetCell {
481
- const generatedCell = cell.sourceType === 'REGION_GENERATED';
392
+ function compactSheetSnapshotCell(cell: SheetCellGQL, styleIndexes: Map<string, number>): CompactSheetCell {
482
393
  const fields = compactSheetSnapshotRecordStyle(
483
394
  compactSheetCellValueShadows(
484
395
  compactSheetSnapshotRecord(
485
396
  cell as unknown as Record<string, unknown>,
486
- generatedCell
487
- ? FILE_CONSTANTS.generatedCellOmitFields
488
- : FILE_CONSTANTS.cellOmitFields,
397
+ FILE_CONSTANTS.cellOmitFields,
489
398
  FILE_CONSTANTS.cellFieldAliases,
490
399
  ),
491
400
  ),
492
401
  FILE_CONSTANTS.cellFieldAliases.style,
493
402
  styleIndexes,
494
403
  );
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
- ];
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)];
557
409
 
558
410
  if (Object.keys(fields).length) {
559
411
  tuple.push(fields);
@@ -573,12 +425,7 @@ function stringifyCanonicalSheetSnapshotJSON(value: unknown): string {
573
425
  if (value && typeof value === 'object') {
574
426
  return `{${Object.keys(value as object)
575
427
  .sort()
576
- .map(
577
- (key) =>
578
- `${JSON.stringify(key)}:${stringifyCanonicalSheetSnapshotJSON(
579
- (value as Record<string, unknown>)[key],
580
- )}`,
581
- )
428
+ .map((key) => `${JSON.stringify(key)}:${stringifyCanonicalSheetSnapshotJSON((value as Record<string, unknown>)[key])}`)
582
429
  .join(',')}}`;
583
430
  }
584
431
 
@@ -608,50 +455,19 @@ function sortSheetSnapshotCells(cells: SheetCellGQL[]): SheetCellGQL[] {
608
455
  );
609
456
  }
610
457
 
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
458
  /*
636
459
  * Encode one raw, non-presentation-expanded tab state into canonical compact
637
460
  * JSON. The viewport lives in the snapshot row and authorization-validated
638
461
  * ancestry is supplied once at decode time, so both stay out of the payload.
639
462
  */
640
- export function encodeSheetSnapshot(
641
- raw: SheetSnapshotRawData,
642
- ): EncodedSheetSnapshot {
463
+ export function encodeSheetSnapshot(raw: SheetSnapshotRawData): EncodedSheetSnapshot {
643
464
  const tab = compactSheetSnapshotTab(raw.sheetTab);
644
465
  const styleDictionary = getCompactSheetSnapshotStyleDictionary(raw);
645
466
  const envelope: CompactSheetSnapshotEnvelope = {
646
467
  v: SHEET_SNAPSHOT_ENCODING_VERSION,
647
468
  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),
469
+ c: sortSheetSnapshotCells(raw.cells).map((cell) => compactSheetSnapshotCell(cell, styleDictionary.styleIndexes)),
470
+
655
471
  p: compactSheetSnapshotValue(raw.pageInfo) as CompactSnapshotRecord,
656
472
  s: styleDictionary.styles,
657
473
  };
@@ -666,16 +482,14 @@ export function encodeSheetSnapshot(
666
482
  }
667
483
 
668
484
  /*
669
- * Expand one sparse record's aliases to its raw field names.
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).
670
487
  */
671
- function expandSheetSnapshotRecord(
672
- value: CompactSnapshotRecord | undefined,
673
- fieldNamesByAlias?: Record<string, string>,
674
- ): CompactSnapshotRecord {
488
+ function expandSheetSnapshotRecord(value: CompactSnapshotRecord | undefined): CompactSnapshotRecord {
675
489
  const expanded: CompactSnapshotRecord = {};
676
490
 
677
491
  Object.entries(value || {}).forEach(([key, item]) => {
678
- expanded[fieldNamesByAlias?.[key] || key] = item;
492
+ expanded[key] = item;
679
493
  });
680
494
 
681
495
  return expanded;
@@ -683,7 +497,7 @@ function expandSheetSnapshotRecord(
683
497
 
684
498
  /*
685
499
  * Clone one JSON snapshot value so dictionary expansion never shares mutable
686
- * style objects between decoded cells/ranges.
500
+ * style objects between decoded cells.
687
501
  */
688
502
  function cloneSheetSnapshotValue(value: unknown): unknown {
689
503
  if (Array.isArray(value)) {
@@ -701,207 +515,162 @@ function cloneSheetSnapshotValue(value: unknown): unknown {
701
515
  }
702
516
 
703
517
  /*
704
- * Resolve one style index to an isolated object while leaving unique inline
705
- * styles unchanged.
518
+ * Resolve one style dictionary index to an isolated style object.
706
519
  */
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
- ) {
520
+ function expandSheetSnapshotStyleIndex(styleIndex: number, styles: CompactSnapshotRecord[] | null): unknown {
521
+ if (!styles || !Number.isSafeInteger(styleIndex) || styleIndex < 0 || styleIndex >= styles.length) {
722
522
  throw new Error('Invalid sheet snapshot style dictionary index.');
723
523
  }
724
524
 
725
- fields[styleField] = cloneSheetSnapshotValue(styles[styleValue]);
726
- return fields;
525
+ return cloneSheetSnapshotValue(styles[styleIndex]);
727
526
  }
728
527
 
729
528
  /*
730
529
  * Return the compact formula-source code's corresponding typed-value source code.
731
530
  */
732
531
  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
- );
532
+ return FILE_CONSTANTS.valueSourcesByFormulaSource[formulaSource] || FILE_CONSTANTS.compactValueSource.NONE;
750
533
  }
751
534
 
752
535
  /*
753
- * Reconstruct omitted serialized value/formula shadows from the explicit
754
- * source mask and reject contradictory or incomplete compact metadata.
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.
755
538
  */
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];
539
+ function requireSheetSnapshotCellValueMask(mask: unknown): number {
765
540
  if (!Number.isSafeInteger(mask) || Number(mask) <= 0 || Number(mask) > 63) {
766
541
  throw new Error('Invalid sheet snapshot cell value mask.');
767
542
  }
768
543
 
769
544
  const numericMask = Number(mask);
770
- const valueSource = numericMask & 7;
771
- const formulaSource = numericMask >> FILE_CONSTANTS.formulaSourceShift;
772
545
  if (
773
- valueSource > FILE_CONSTANTS.compactValueSource.DATETIME ||
774
- formulaSource > FILE_CONSTANTS.compactFormulaSource.DATETIME
546
+ (numericMask & 7) > FILE_CONSTANTS.compactValueSource.DATETIME ||
547
+ numericMask >> FILE_CONSTANTS.formulaSourceShift > FILE_CONSTANTS.compactFormulaSource.DATETIME
775
548
  ) {
776
549
  throw new Error('Unsupported sheet snapshot cell value mask.');
777
550
  }
778
551
 
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;
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.');
788
570
  }
789
571
 
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;
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.');
805
597
  }
806
598
 
807
- return fields;
599
+ return formulaValue;
808
600
  }
809
601
 
810
602
  /*
811
- * Decode one compact cell tuple and inject its shared ancestry.
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.
812
607
  */
813
608
  function expandSheetSnapshotCell(
814
609
  tuple: CompactSheetCell,
815
610
  ancestry: SheetSnapshotAncestry,
816
611
  styles: CompactSnapshotRecord[] | null,
817
612
  ): 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 {
613
+ const cell: CompactSnapshotRecord = {
835
614
  id: String(tuple[0] || ''),
836
615
  organizationId: ancestry.organizationId,
837
616
  sheetId: ancestry.sheetId,
838
617
  sheetTabId: ancestry.sheetTabId,
839
618
  rowIndex: Number(tuple[1]),
840
619
  columnIndex: Number(tuple[2]),
841
- ...expandedFields,
842
- } as SheetCellGQL;
843
- }
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
+ }
844
639
 
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
- }
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
+ }
870
649
 
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);
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;
888
661
  }
889
662
 
890
663
  /*
891
664
  * Return whether one encoding version is the current snapshot contract.
892
665
  */
893
- export function isSupportedSheetSnapshotEncodingVersion(
894
- value: unknown,
895
- ): value is typeof SHEET_SNAPSHOT_ENCODING_VERSION {
666
+ export function isSupportedSheetSnapshotEncodingVersion(value: unknown): value is typeof SHEET_SNAPSHOT_ENCODING_VERSION {
896
667
  return value === SHEET_SNAPSHOT_ENCODING_VERSION;
897
668
  }
898
669
 
899
670
  /*
900
671
  * Return whether one parsed JSON value is a non-array compact record.
901
672
  */
902
- function isCompactSheetSnapshotRecord(
903
- value: unknown,
904
- ): value is CompactSnapshotRecord {
673
+ function isCompactSheetSnapshotRecord(value: unknown): value is CompactSnapshotRecord {
905
674
  return Boolean(value && typeof value === 'object' && !Array.isArray(value));
906
675
  }
907
676
 
@@ -909,25 +678,14 @@ function isCompactSheetSnapshotRecord(
909
678
  * Return whether every tuple list has the minimum positional shape required
910
679
  * for safe decoding. Field-level masks and dictionary indexes validate later.
911
680
  */
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
- );
681
+ function hasValidCompactSheetSnapshotTuples(envelope: Partial<CompactSheetSnapshotEnvelope>): boolean {
682
+ return Boolean(Array.isArray(envelope.c) && envelope.c.every((tuple) => Array.isArray(tuple) && tuple.length >= 3));
923
683
  }
924
684
 
925
685
  /*
926
686
  * Parse and validate the current compact envelope. Unknown versions fail closed.
927
687
  */
928
- function parseSheetSnapshotEnvelope(
929
- payload: string,
930
- ): CompactSheetSnapshotEnvelope {
688
+ function parseSheetSnapshotEnvelope(payload: string): CompactSheetSnapshotEnvelope {
931
689
  const envelope = JSON.parse(payload) as Partial<CompactSheetSnapshotEnvelope>;
932
690
  const shapeIsValid =
933
691
  isSupportedSheetSnapshotEncodingVersion(envelope.v) &&
@@ -937,9 +695,7 @@ function parseSheetSnapshotEnvelope(
937
695
  Array.isArray(envelope.s) &&
938
696
  envelope.s.every(isCompactSheetSnapshotRecord);
939
697
  if (!shapeIsValid) {
940
- throw new Error(
941
- `Unsupported or invalid sheet snapshot encoding: ${String(envelope.v)}`,
942
- );
698
+ throw new Error(`Unsupported or invalid sheet snapshot encoding: ${String(envelope.v)}`);
943
699
  }
944
700
 
945
701
  return envelope as CompactSheetSnapshotEnvelope;
@@ -975,18 +731,9 @@ export function decodeSheetSnapshot(
975
731
  ...tabFields,
976
732
  design,
977
733
  } 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,
734
+ cells: envelope.c.map((cell) => expandSheetSnapshotCell(cell, ancestry, styles)),
735
+
736
+ pageInfo: expandSheetSnapshotRecord(envelope.p) as unknown as SheetGridPageInfoGQL,
990
737
  viewport,
991
738
  };
992
739
  }