@rowsncolumns/y-spreadsheet 16.0.0 → 16.1.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +1 -1
- package/dist/cjs/helpers-v5.d.ts +64 -0
- package/dist/cjs/helpers-v5.d.ts.map +1 -0
- package/dist/cjs/helpers-v5.js +970 -0
- package/dist/cjs/helpers.d.ts +3 -2
- package/dist/cjs/helpers.d.ts.map +1 -1
- package/dist/cjs/helpers.js +8 -2
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/cjs/use-yspreadsheet-v2.d.ts +4 -2
- package/dist/cjs/use-yspreadsheet-v2.d.ts.map +1 -1
- package/dist/cjs/use-yspreadsheet-v2.js +125 -34
- package/dist/esm/helpers-v5.d.ts +64 -0
- package/dist/esm/helpers-v5.d.ts.map +1 -0
- package/dist/esm/helpers-v5.js +733 -0
- package/dist/esm/helpers.d.ts +3 -2
- package/dist/esm/helpers.d.ts.map +1 -1
- package/dist/esm/helpers.js +8 -2
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/esm/use-yspreadsheet-v2.d.ts +4 -2
- package/dist/esm/use-yspreadsheet-v2.d.ts.map +1 -1
- package/dist/esm/use-yspreadsheet-v2.js +86 -11
- package/package.json +2 -1
|
@@ -0,0 +1,970 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ============================================================================
|
|
3
|
+
// V5 documents (stable line ids): reader and legacy-path writer
|
|
4
|
+
// ============================================================================
|
|
5
|
+
// Encoding: libs/rnc-yrs-store/docs/v5-document-encoding.md. Names in comments
|
|
6
|
+
// (`RNC_CELLS`, `c0_floor`, …) are the Rust ones in rust/crates/rnc-yrs-store/src
|
|
7
|
+
// (lib.rs unless a module is named); they win over this module.
|
|
8
|
+
//
|
|
9
|
+
// Readers (hot plane only; cold-block cells are not decoded): `sheetDataV5ToV2`,
|
|
10
|
+
// `getCellV5`, `processSheetDataChangesV5`; `uf` from the composed style plane,
|
|
11
|
+
// `note` from `rncNotes`, so a cell read here can be written back unchanged.
|
|
12
|
+
// Writer: `addSheetDataPatchesV5` applies immer `SheetData` patches positionally
|
|
13
|
+
// (a structural edit arrives as rewritten rows, v3 parity); it names lines past
|
|
14
|
+
// an axis with client-0 ids exactly as `YrsStore::ensure_lines` does and logs
|
|
15
|
+
// `uf` changes as `STYLE_LOG_V2` ops. It never writes `ft`: a JS formula is
|
|
16
|
+
// positional in its write frame.
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.deleteSheetV5 = exports.deleteCellV5 = exports.setCellV5 = exports.addSheetDataPatchesV5 = exports.processSheetDataChangesV5 = exports.getCellV5 = exports.createCellReaderV5 = exports.sheetDataV5ToV2 = exports.readStyleOverlayV5 = exports.parseCellKeyV5 = exports.createCellKeyV5 = exports.readLineIds = void 0;
|
|
19
|
+
var tslib_1 = require("tslib");
|
|
20
|
+
var utils_1 = require("@rowsncolumns/utils");
|
|
21
|
+
var decoding = tslib_1.__importStar(require("lib0/decoding"));
|
|
22
|
+
var encoding = tslib_1.__importStar(require("lib0/encoding"));
|
|
23
|
+
var cloneDeep_1 = tslib_1.__importDefault(require("lodash/cloneDeep"));
|
|
24
|
+
var isEqual_1 = tslib_1.__importDefault(require("lodash/isEqual"));
|
|
25
|
+
var set_1 = tslib_1.__importDefault(require("lodash/set"));
|
|
26
|
+
var unset_1 = tslib_1.__importDefault(require("lodash/unset"));
|
|
27
|
+
var CELLS_V5 = "rncCells"; // RNC_CELLS
|
|
28
|
+
var NOTES_V5 = "rncNotes"; // NOTES
|
|
29
|
+
var DIMS_V5 = "rncDimensions"; // DIMS
|
|
30
|
+
var STYLES_SEED_V5 = "rncCellStyles"; // RNC_STYLES (legacy, read-only seed)
|
|
31
|
+
var STYLES_LOG_V5 = "rncCellStylesLog"; // RNC_STYLES_LOG
|
|
32
|
+
var BLOCKS_V5 = "rncColumnBlocks"; // RNC_COLUMN_BLOCKS
|
|
33
|
+
var BLOCK_ROWS = 32768; // BLOCK_ROWS
|
|
34
|
+
var BLOCK_V2_MAGIC = 0x80000000; // BLOCK_V2_MAGIC
|
|
35
|
+
var BLOCK_V2_DENSE = 0x40000000; // BLOCK_V2_DENSE
|
|
36
|
+
var STYLE_LOG_V1 = 1; // STYLE_LOG_V1
|
|
37
|
+
var STYLE_LOG_V2 = 2; // STYLE_LOG_V2
|
|
38
|
+
var CELL_KEY_V5 = /^(-?\d+):(-?\d+):(-?\d+)$/; // cell_key
|
|
39
|
+
var BLOCK_KEY_V5 = /^(-?\d+):(-?\d+):(\d+)$/; // block_key
|
|
40
|
+
var DIM_KEY_V5 = /^(-?\d+):([RC]):(-?\d+)$/; // dim_id_key
|
|
41
|
+
var SEED_KEY = /^(-?\d+)!([A-Z]+)(\d+)$/; // `"{sheetId}!{A1}"`, parse_a1
|
|
42
|
+
// ── ids (axis.rs: make_id, id_client, id_counter) ───────────────────────────
|
|
43
|
+
var idCounter = function (id) {
|
|
44
|
+
return Number((BigInt.asUintN(64, id) >> BigInt(32)) & BigInt(0xffffffff));
|
|
45
|
+
};
|
|
46
|
+
var idClient = function (id) { return Number(BigInt.asUintN(32, id)); };
|
|
47
|
+
/** Decimal client-0 id of `counter` (`make_id(0, counter)`). */
|
|
48
|
+
var c0Id = function (counter) { return (BigInt(counter) << BigInt(32)).toString(); };
|
|
49
|
+
/** Client-0 counter of a decimal id; 0 for another client's id. */
|
|
50
|
+
var c0Counter = function (id) {
|
|
51
|
+
var value = BigInt(id);
|
|
52
|
+
return idClient(value) === 0 ? idCounter(value) : 0;
|
|
53
|
+
};
|
|
54
|
+
/** The array value of a padded id: `counter << 32` is exact as a float (yjs cannot insert a bigint). */
|
|
55
|
+
var idValue = function (counter) { return counter * 4294967296; };
|
|
56
|
+
var lineIdsRootName = function (sheetId, axis) {
|
|
57
|
+
return "".concat(axis === "row" ? "rncRowIds" : "rncColIds", ":").concat(sheetId);
|
|
58
|
+
};
|
|
59
|
+
/** Decimal text of a stored line id (yjs gives a number or a bigint); null otherwise. */
|
|
60
|
+
var lineIdText = function (value) {
|
|
61
|
+
if (typeof value === "bigint")
|
|
62
|
+
return value.toString();
|
|
63
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
64
|
+
return BigInt(Math.trunc(value)).toString();
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* A sheet's line ids in grid order (position = index + 1), as decimal strings.
|
|
70
|
+
* Later duplicates and non-id values are skipped, as the engine's index does.
|
|
71
|
+
*/
|
|
72
|
+
var readLineIds = function (doc, sheetId, axis) {
|
|
73
|
+
var e_1, _a;
|
|
74
|
+
var name = lineIdsRootName(sheetId, axis);
|
|
75
|
+
if (!doc.share.has(name))
|
|
76
|
+
return [];
|
|
77
|
+
var ids = [];
|
|
78
|
+
var seen = new Set();
|
|
79
|
+
try {
|
|
80
|
+
for (var _b = tslib_1.__values(doc.getArray(name).toArray()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
81
|
+
var value = _c.value;
|
|
82
|
+
var id = lineIdText(value);
|
|
83
|
+
if (id === null || seen.has(id))
|
|
84
|
+
continue;
|
|
85
|
+
seen.add(id);
|
|
86
|
+
ids.push(id);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
90
|
+
finally {
|
|
91
|
+
try {
|
|
92
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
93
|
+
}
|
|
94
|
+
finally { if (e_1) throw e_1.error; }
|
|
95
|
+
}
|
|
96
|
+
return ids;
|
|
97
|
+
};
|
|
98
|
+
exports.readLineIds = readLineIds;
|
|
99
|
+
/** The `rncCells` / `rncNotes` key of a cell by its line ids. */
|
|
100
|
+
var createCellKeyV5 = function (sheetId, rowId, colId) { return "".concat(sheetId, ":").concat(rowId, ":").concat(colId); };
|
|
101
|
+
exports.createCellKeyV5 = createCellKeyV5;
|
|
102
|
+
/** Parse a v5 key `"{sheetId}:{rowId}:{colId}"`; null when malformed. */
|
|
103
|
+
var parseCellKeyV5 = function (key) {
|
|
104
|
+
var match = CELL_KEY_V5.exec(key);
|
|
105
|
+
if (!match)
|
|
106
|
+
return null;
|
|
107
|
+
var sheetId = Number(match[1]);
|
|
108
|
+
var rowId = BigInt(match[2]);
|
|
109
|
+
var colId = BigInt(match[3]);
|
|
110
|
+
if (!Number.isSafeInteger(sheetId) ||
|
|
111
|
+
BigInt.asIntN(64, rowId) !== rowId ||
|
|
112
|
+
BigInt.asIntN(64, colId) !== colId) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
return { sheetId: sheetId, rowId: rowId.toString(), colId: colId.toString() };
|
|
116
|
+
};
|
|
117
|
+
exports.parseCellKeyV5 = parseCellKeyV5;
|
|
118
|
+
var isPlainObject = function (value) {
|
|
119
|
+
return value !== null &&
|
|
120
|
+
typeof value === "object" &&
|
|
121
|
+
Object.getPrototypeOf(value) === Object.prototype;
|
|
122
|
+
};
|
|
123
|
+
/** A stored v5 value as `CellData` without the engine-owned `ft`; null for a tombstone. */
|
|
124
|
+
var cellDataV5 = function (value) {
|
|
125
|
+
if (!isPlainObject(value))
|
|
126
|
+
return null;
|
|
127
|
+
var _ft = value.ft, cell = tslib_1.__rest(value, ["ft"]);
|
|
128
|
+
return cell;
|
|
129
|
+
};
|
|
130
|
+
var toPositions = function (ids) {
|
|
131
|
+
return new Map(ids.map(function (id, i) { return [id, i + 1]; }));
|
|
132
|
+
};
|
|
133
|
+
/** The line index of a sheet, read once per call site into `cache`. */
|
|
134
|
+
var sheetLinesV5 = function (doc, cache, sheetId) {
|
|
135
|
+
var lines = cache.get(sheetId);
|
|
136
|
+
if (!lines) {
|
|
137
|
+
lines = {
|
|
138
|
+
rows: toPositions((0, exports.readLineIds)(doc, sheetId, "row")),
|
|
139
|
+
cols: toPositions((0, exports.readLineIds)(doc, sheetId, "column")),
|
|
140
|
+
};
|
|
141
|
+
cache.set(sheetId, lines);
|
|
142
|
+
}
|
|
143
|
+
return lines;
|
|
144
|
+
};
|
|
145
|
+
/** 1-based `[row, col]` of a parsed key; null for an orphan (a deleted or unknown line). */
|
|
146
|
+
var positionV5 = function (doc, cache, _a) {
|
|
147
|
+
var sheetId = _a.sheetId, rowId = _a.rowId, colId = _a.colId;
|
|
148
|
+
var _b = sheetLinesV5(doc, cache, sheetId), rows = _b.rows, cols = _b.cols;
|
|
149
|
+
var row = rows.get(rowId);
|
|
150
|
+
var col = cols.get(colId);
|
|
151
|
+
return row === undefined || col === undefined ? null : [row, col];
|
|
152
|
+
};
|
|
153
|
+
// ── style plane (encode_style_ops, fold_style_ops, CellFormat::merge) ───────
|
|
154
|
+
var isStyleReference = function (format) {
|
|
155
|
+
return isPlainObject(format) && ("sid" in format || "styleId" in format);
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* `CellFormat::merge`: each set property of `patch` wins; `textFormat` and
|
|
159
|
+
* `borders` merge one level deep; a `""` colour removes it. Pure (both inputs
|
|
160
|
+
* may be frozen). `mergeFormat({}, f)` = `into_normalized`.
|
|
161
|
+
*/
|
|
162
|
+
var mergeFormat = function (base, patch) {
|
|
163
|
+
var e_2, _a, e_3, _b;
|
|
164
|
+
var out = tslib_1.__assign({}, base);
|
|
165
|
+
try {
|
|
166
|
+
for (var _c = tslib_1.__values(Object.entries(patch)), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
167
|
+
var _f = tslib_1.__read(_d.value, 2), key = _f[0], value = _f[1];
|
|
168
|
+
if (value === null || value === undefined)
|
|
169
|
+
continue;
|
|
170
|
+
if ((key === "textFormat" || key === "borders") && isPlainObject(value)) {
|
|
171
|
+
var prev = out[key];
|
|
172
|
+
var nested = isPlainObject(prev) ? tslib_1.__assign({}, prev) : {};
|
|
173
|
+
try {
|
|
174
|
+
for (var _g = (e_3 = void 0, tslib_1.__values(Object.entries(value))), _h = _g.next(); !_h.done; _h = _g.next()) {
|
|
175
|
+
var _j = tslib_1.__read(_h.value, 2), k = _j[0], v = _j[1];
|
|
176
|
+
if (v === null || v === undefined)
|
|
177
|
+
continue;
|
|
178
|
+
if (key === "textFormat" && k === "color" && v === "")
|
|
179
|
+
delete nested[k];
|
|
180
|
+
else
|
|
181
|
+
nested[k] = v;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
185
|
+
finally {
|
|
186
|
+
try {
|
|
187
|
+
if (_h && !_h.done && (_b = _g.return)) _b.call(_g);
|
|
188
|
+
}
|
|
189
|
+
finally { if (e_3) throw e_3.error; }
|
|
190
|
+
}
|
|
191
|
+
out[key] = nested;
|
|
192
|
+
}
|
|
193
|
+
else if ((key === "backgroundColor" || key === "foregroundColor") &&
|
|
194
|
+
value === "") {
|
|
195
|
+
delete out[key];
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
out[key] = value;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
203
|
+
finally {
|
|
204
|
+
try {
|
|
205
|
+
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
206
|
+
}
|
|
207
|
+
finally { if (e_2) throw e_2.error; }
|
|
208
|
+
}
|
|
209
|
+
return out;
|
|
210
|
+
};
|
|
211
|
+
/** A cell's `uf` comparable with the composed plane: unset fields dropped; a reference as is. */
|
|
212
|
+
var normalizedFormat = function (format) {
|
|
213
|
+
return isPlainObject(format) && !isStyleReference(format)
|
|
214
|
+
? mergeFormat({}, format)
|
|
215
|
+
: (format !== null && format !== void 0 ? format : null);
|
|
216
|
+
};
|
|
217
|
+
var letterIndex = function (letters) {
|
|
218
|
+
return letters.split("").reduce(function (n, ch) { return n * 26 + ch.charCodeAt(0) - 64; }, 0);
|
|
219
|
+
};
|
|
220
|
+
/** Fold one log buffer onto `overlay`; a truncated buffer keeps the ops before the cut. */
|
|
221
|
+
var foldStyleBuffer = function (overlay, buf) {
|
|
222
|
+
var decoder = decoding.createDecoder(buf);
|
|
223
|
+
if (!decoding.hasContent(decoder))
|
|
224
|
+
return;
|
|
225
|
+
var version = decoding.readUint8(decoder);
|
|
226
|
+
if (version !== STYLE_LOG_V1 && version !== STYLE_LOG_V2)
|
|
227
|
+
return;
|
|
228
|
+
try {
|
|
229
|
+
while (decoding.hasContent(decoder)) {
|
|
230
|
+
var sheet = decoding.readBigInt64(decoder);
|
|
231
|
+
var _a = tslib_1.__read(version === STYLE_LOG_V1
|
|
232
|
+
? [c0Id(decoding.readVarUint(decoder)), c0Id(decoding.readVarUint(decoder))]
|
|
233
|
+
: [
|
|
234
|
+
decoding.readBigInt64(decoder).toString(),
|
|
235
|
+
decoding.readBigInt64(decoder).toString(),
|
|
236
|
+
], 2), row = _a[0], col = _a[1];
|
|
237
|
+
var value = decoding.readAny(decoder);
|
|
238
|
+
var key = "".concat(sheet, ":").concat(row, ":").concat(col);
|
|
239
|
+
// A `STYLE_LOG_BASE_SENTINEL_SHEET` op is a clear of a cell that cannot exist.
|
|
240
|
+
if (value === null || value === undefined) {
|
|
241
|
+
overlay.delete(key);
|
|
242
|
+
}
|
|
243
|
+
else if (isPlainObject(value)) {
|
|
244
|
+
var prev = overlay.get(key);
|
|
245
|
+
overlay.set(key, mergeFormat(prev !== null && prev !== void 0 ? prev : {}, value));
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
catch (_e) {
|
|
250
|
+
// Reading past the end: the buffer stops here, as `fold_style_ops` does.
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
/**
|
|
254
|
+
* The composed style plane of a v5 doc (`refold_style_overlay`): the legacy
|
|
255
|
+
* `rncCellStyles` seed (`"{sheetId}!{A1}"`, the client-0 ids of that position)
|
|
256
|
+
* with every `rncCellStylesLog` buffer folded on in array order. Walks the log.
|
|
257
|
+
*/
|
|
258
|
+
var readStyleOverlayV5 = function (doc) {
|
|
259
|
+
var e_4, _a;
|
|
260
|
+
var overlay = new Map();
|
|
261
|
+
if (doc.share.has(STYLES_SEED_V5)) {
|
|
262
|
+
doc.getMap(STYLES_SEED_V5).forEach(function (value, key) {
|
|
263
|
+
var match = SEED_KEY.exec(key);
|
|
264
|
+
if (!match || !isPlainObject(value))
|
|
265
|
+
return;
|
|
266
|
+
var row = c0Id(Number(match[3]));
|
|
267
|
+
var col = c0Id(letterIndex(match[2]));
|
|
268
|
+
overlay.set("".concat(BigInt(match[1]), ":").concat(row, ":").concat(col), mergeFormat({}, value));
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
if (doc.share.has(STYLES_LOG_V5)) {
|
|
272
|
+
try {
|
|
273
|
+
for (var _b = tslib_1.__values(doc.getArray(STYLES_LOG_V5).toArray()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
274
|
+
var buf = _c.value;
|
|
275
|
+
if (buf instanceof Uint8Array)
|
|
276
|
+
foldStyleBuffer(overlay, buf);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
280
|
+
finally {
|
|
281
|
+
try {
|
|
282
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
283
|
+
}
|
|
284
|
+
finally { if (e_4) throw e_4.error; }
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return overlay;
|
|
288
|
+
};
|
|
289
|
+
exports.readStyleOverlayV5 = readStyleOverlayV5;
|
|
290
|
+
/** `STYLE_LOG_V2`, then per op `i64 sheet, i64 rowId, i64 colId` (big-endian) and one `Any`. */
|
|
291
|
+
var encodeStyleOps = function (ops) {
|
|
292
|
+
var e_5, _a;
|
|
293
|
+
var encoder = encoding.createEncoder();
|
|
294
|
+
encoding.writeUint8(encoder, STYLE_LOG_V2);
|
|
295
|
+
try {
|
|
296
|
+
for (var ops_1 = tslib_1.__values(ops), ops_1_1 = ops_1.next(); !ops_1_1.done; ops_1_1 = ops_1.next()) {
|
|
297
|
+
var _b = tslib_1.__read(ops_1_1.value, 4), sheetId = _b[0], rowId = _b[1], colId = _b[2], format = _b[3];
|
|
298
|
+
encoding.writeBigInt64(encoder, BigInt(sheetId));
|
|
299
|
+
encoding.writeBigInt64(encoder, BigInt(rowId));
|
|
300
|
+
encoding.writeBigInt64(encoder, BigInt(colId));
|
|
301
|
+
encoding.writeAny(encoder, format);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
305
|
+
finally {
|
|
306
|
+
try {
|
|
307
|
+
if (ops_1_1 && !ops_1_1.done && (_a = ops_1.return)) _a.call(ops_1);
|
|
308
|
+
}
|
|
309
|
+
finally { if (e_5) throw e_5.error; }
|
|
310
|
+
}
|
|
311
|
+
return encoding.toUint8Array(encoder);
|
|
312
|
+
};
|
|
313
|
+
// ── cold blocks (encode_cell_block, decode_cell_block_bodies, block_row_offsets) ──
|
|
314
|
+
/** Row offsets of a block from its uncompressed header (no inflate). */
|
|
315
|
+
var blockRowOffsets = function (buf) {
|
|
316
|
+
if (buf.length < 4)
|
|
317
|
+
return [];
|
|
318
|
+
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
319
|
+
var first = view.getUint32(0, true);
|
|
320
|
+
var out = [];
|
|
321
|
+
if (first & BLOCK_V2_MAGIC) {
|
|
322
|
+
var count = Math.min(buf.length >= 8 ? view.getUint32(4, true) : 0, BLOCK_ROWS);
|
|
323
|
+
if (first & BLOCK_V2_DENSE) {
|
|
324
|
+
for (var i = 0; i < count; i++)
|
|
325
|
+
out.push(i);
|
|
326
|
+
return out;
|
|
327
|
+
}
|
|
328
|
+
for (var i = 0, pos = 8; i < count && pos + 4 <= buf.length; i++, pos += 4) {
|
|
329
|
+
out.push(view.getUint32(pos, true));
|
|
330
|
+
}
|
|
331
|
+
return out;
|
|
332
|
+
}
|
|
333
|
+
for (var i = 0, pos = 4; i < first && pos + 8 <= buf.length; i++) {
|
|
334
|
+
out.push(view.getUint32(pos, true));
|
|
335
|
+
pos += 8 + view.getUint32(pos + 4, true);
|
|
336
|
+
}
|
|
337
|
+
return out;
|
|
338
|
+
};
|
|
339
|
+
/** Whether `rowId`'s client-0 counter has an entry in the column's block (header only). */
|
|
340
|
+
var coldCellExists = function (doc, cache, sheetId, rowId, colId) {
|
|
341
|
+
var counter = c0Counter(rowId);
|
|
342
|
+
if (!counter || !doc.share.has(BLOCKS_V5))
|
|
343
|
+
return false;
|
|
344
|
+
var key = "".concat(sheetId, ":").concat(colId, ":").concat(Math.floor((counter - 1) / BLOCK_ROWS));
|
|
345
|
+
var offsets = cache.get(key);
|
|
346
|
+
if (!offsets) {
|
|
347
|
+
var buf = doc.getMap(BLOCKS_V5).get(key);
|
|
348
|
+
offsets = new Set(buf instanceof Uint8Array ? blockRowOffsets(buf) : []);
|
|
349
|
+
cache.set(key, offsets);
|
|
350
|
+
}
|
|
351
|
+
return offsets.has((counter - 1) % BLOCK_ROWS);
|
|
352
|
+
};
|
|
353
|
+
// ── readers ─────────────────────────────────────────────────────────────────
|
|
354
|
+
/**
|
|
355
|
+
* The cell under `key` as the readers show it: the hot value (`ft` stripped),
|
|
356
|
+
* `uf` from `styles` where it has an entry, `note` from `rncNotes` where it has
|
|
357
|
+
* one. Null when none of the three holds anything.
|
|
358
|
+
*/
|
|
359
|
+
var composeCell = function (doc, key, styles) {
|
|
360
|
+
var cell = doc.share.has(CELLS_V5)
|
|
361
|
+
? cellDataV5(doc.getMap(CELLS_V5).get(key))
|
|
362
|
+
: null;
|
|
363
|
+
var uf = styles.get(key);
|
|
364
|
+
if (uf)
|
|
365
|
+
cell = tslib_1.__assign(tslib_1.__assign({}, cell), { uf: uf });
|
|
366
|
+
var note = doc.share.has(NOTES_V5) ? doc.getMap(NOTES_V5).get(key) : undefined;
|
|
367
|
+
if (typeof note === "string")
|
|
368
|
+
cell = tslib_1.__assign(tslib_1.__assign({}, cell), { note: note });
|
|
369
|
+
return cell;
|
|
370
|
+
};
|
|
371
|
+
/**
|
|
372
|
+
* A v5 doc as V2 `SheetData` (same shape as `sheetDataV3ToV2`), hot plane only:
|
|
373
|
+
* every key of `rncCells`, `rncNotes` and the composed style plane composed as
|
|
374
|
+
* `composeCell` does (tombstones and orphans skipped).
|
|
375
|
+
*/
|
|
376
|
+
var sheetDataV5ToV2 = function (doc, styles) {
|
|
377
|
+
var e_6, _a, e_7, _b, e_8, _c;
|
|
378
|
+
var _d, _f;
|
|
379
|
+
var _g;
|
|
380
|
+
if (styles === void 0) { styles = (0, exports.readStyleOverlayV5)(doc); }
|
|
381
|
+
var sheetData = {};
|
|
382
|
+
var cache = new Map();
|
|
383
|
+
var keys = new Set(styles.keys());
|
|
384
|
+
try {
|
|
385
|
+
for (var _h = tslib_1.__values([CELLS_V5, NOTES_V5]), _j = _h.next(); !_j.done; _j = _h.next()) {
|
|
386
|
+
var name_1 = _j.value;
|
|
387
|
+
if (doc.share.has(name_1))
|
|
388
|
+
try {
|
|
389
|
+
for (var _k = (e_7 = void 0, tslib_1.__values(doc.getMap(name_1).keys())), _l = _k.next(); !_l.done; _l = _k.next()) {
|
|
390
|
+
var key = _l.value;
|
|
391
|
+
keys.add(key);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
395
|
+
finally {
|
|
396
|
+
try {
|
|
397
|
+
if (_l && !_l.done && (_b = _k.return)) _b.call(_k);
|
|
398
|
+
}
|
|
399
|
+
finally { if (e_7) throw e_7.error; }
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
404
|
+
finally {
|
|
405
|
+
try {
|
|
406
|
+
if (_j && !_j.done && (_a = _h.return)) _a.call(_h);
|
|
407
|
+
}
|
|
408
|
+
finally { if (e_6) throw e_6.error; }
|
|
409
|
+
}
|
|
410
|
+
try {
|
|
411
|
+
for (var keys_1 = tslib_1.__values(keys), keys_1_1 = keys_1.next(); !keys_1_1.done; keys_1_1 = keys_1.next()) {
|
|
412
|
+
var key = keys_1_1.value;
|
|
413
|
+
var parsed = (0, exports.parseCellKeyV5)(key);
|
|
414
|
+
var pos = parsed && positionV5(doc, cache, parsed);
|
|
415
|
+
var cell = pos && composeCell(doc, key, styles);
|
|
416
|
+
if (!parsed || !pos || !cell)
|
|
417
|
+
continue;
|
|
418
|
+
var _m = tslib_1.__read(pos, 2), rowIdx = _m[0], colIdx = _m[1];
|
|
419
|
+
var rows = ((_d = sheetData[_g = parsed.sheetId]) !== null && _d !== void 0 ? _d : (sheetData[_g] = []));
|
|
420
|
+
while (rows.length <= rowIdx)
|
|
421
|
+
rows.push(null);
|
|
422
|
+
var row = ((_f = rows[rowIdx]) !== null && _f !== void 0 ? _f : (rows[rowIdx] = { values: [] }));
|
|
423
|
+
if (!row.values)
|
|
424
|
+
row.values = [];
|
|
425
|
+
while (row.values.length <= colIdx)
|
|
426
|
+
row.values.push(null);
|
|
427
|
+
row.values[colIdx] = cell;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
catch (e_8_1) { e_8 = { error: e_8_1 }; }
|
|
431
|
+
finally {
|
|
432
|
+
try {
|
|
433
|
+
if (keys_1_1 && !keys_1_1.done && (_c = keys_1.return)) _c.call(keys_1);
|
|
434
|
+
}
|
|
435
|
+
finally { if (e_8) throw e_8.error; }
|
|
436
|
+
}
|
|
437
|
+
return sheetData;
|
|
438
|
+
};
|
|
439
|
+
exports.sheetDataV5ToV2 = sheetDataV5ToV2;
|
|
440
|
+
/**
|
|
441
|
+
* A reader of the cell at a 1-based position (`composeCell`), line ids read once
|
|
442
|
+
* per sheet. Undefined when nothing is there or the line is unnamed.
|
|
443
|
+
*/
|
|
444
|
+
var createCellReaderV5 = function (doc, styles) {
|
|
445
|
+
if (styles === void 0) { styles = (0, exports.readStyleOverlayV5)(doc); }
|
|
446
|
+
var lines = new Map();
|
|
447
|
+
return function (sheetId, rowIndex, columnIndex) {
|
|
448
|
+
var _a;
|
|
449
|
+
var ids = lines.get(sheetId);
|
|
450
|
+
if (!ids) {
|
|
451
|
+
ids = [(0, exports.readLineIds)(doc, sheetId, "row"), (0, exports.readLineIds)(doc, sheetId, "column")];
|
|
452
|
+
lines.set(sheetId, ids);
|
|
453
|
+
}
|
|
454
|
+
var rowId = ids[0][rowIndex - 1];
|
|
455
|
+
var colId = ids[1][columnIndex - 1];
|
|
456
|
+
if (rowId === undefined || colId === undefined)
|
|
457
|
+
return undefined;
|
|
458
|
+
return (_a = composeCell(doc, (0, exports.createCellKeyV5)(sheetId, rowId, colId), styles)) !== null && _a !== void 0 ? _a : undefined;
|
|
459
|
+
};
|
|
460
|
+
};
|
|
461
|
+
exports.createCellReaderV5 = createCellReaderV5;
|
|
462
|
+
/** `createCellReaderV5` for one cell (reads both id arrays: O(lines)). */
|
|
463
|
+
var getCellV5 = function (doc, sheetId, rowIndex, columnIndex, styles) { return (0, exports.createCellReaderV5)(doc, styles)(sheetId, rowIndex, columnIndex); };
|
|
464
|
+
exports.getCellV5 = getCellV5;
|
|
465
|
+
/**
|
|
466
|
+
* Apply changed `rncCells` / `rncNotes` keys to `currentSheetData` (copy-on-write,
|
|
467
|
+
* like `processSheetDataEventsV3`), `uf` from `styles`. Only valid while no id
|
|
468
|
+
* array and no style changed (those need `sheetDataV5ToV2`). Null when nothing changed.
|
|
469
|
+
*/
|
|
470
|
+
var processSheetDataChangesV5 = function (changedKeys, currentSheetData, doc, styles) {
|
|
471
|
+
var e_9, _a;
|
|
472
|
+
var _b, _c, _d;
|
|
473
|
+
if (styles === void 0) { styles = (0, exports.readStyleOverlayV5)(doc); }
|
|
474
|
+
var cache = new Map();
|
|
475
|
+
var next = tslib_1.__assign({}, currentSheetData);
|
|
476
|
+
var clonedSheets = new Set();
|
|
477
|
+
var clonedRows = new Set();
|
|
478
|
+
var hasChanges = false;
|
|
479
|
+
try {
|
|
480
|
+
for (var _f = tslib_1.__values(new Set(changedKeys)), _g = _f.next(); !_g.done; _g = _f.next()) {
|
|
481
|
+
var key = _g.value;
|
|
482
|
+
var parsed = (0, exports.parseCellKeyV5)(key);
|
|
483
|
+
var pos = parsed && positionV5(doc, cache, parsed);
|
|
484
|
+
if (!parsed || !pos)
|
|
485
|
+
continue;
|
|
486
|
+
var _h = tslib_1.__read(pos, 2), rowIdx = _h[0], colIdx = _h[1];
|
|
487
|
+
var sheetId = parsed.sheetId;
|
|
488
|
+
var cell = composeCell(doc, key, styles);
|
|
489
|
+
var row = (_b = next[sheetId]) === null || _b === void 0 ? void 0 : _b[rowIdx];
|
|
490
|
+
if (!cell && !((_c = row === null || row === void 0 ? void 0 : row.values) === null || _c === void 0 ? void 0 : _c[colIdx]))
|
|
491
|
+
continue;
|
|
492
|
+
if (!clonedSheets.has(sheetId)) {
|
|
493
|
+
next[sheetId] = tslib_1.__spreadArray([], tslib_1.__read(((_d = next[sheetId]) !== null && _d !== void 0 ? _d : [])), false);
|
|
494
|
+
clonedSheets.add(sheetId);
|
|
495
|
+
}
|
|
496
|
+
var sheet = next[sheetId];
|
|
497
|
+
if (sheet.length <= rowIdx)
|
|
498
|
+
sheet.length = rowIdx + 1;
|
|
499
|
+
var rowKey = "".concat(sheetId, ":").concat(rowIdx);
|
|
500
|
+
if (!clonedRows.has(rowKey)) {
|
|
501
|
+
var prev = sheet[rowIdx];
|
|
502
|
+
sheet[rowIdx] = tslib_1.__assign(tslib_1.__assign({}, prev), { values: (prev === null || prev === void 0 ? void 0 : prev.values) ? tslib_1.__spreadArray([], tslib_1.__read(prev.values), false) : [] });
|
|
503
|
+
clonedRows.add(rowKey);
|
|
504
|
+
}
|
|
505
|
+
var values = sheet[rowIdx].values;
|
|
506
|
+
if (values.length <= colIdx)
|
|
507
|
+
values.length = colIdx + 1;
|
|
508
|
+
values[colIdx] = cell;
|
|
509
|
+
hasChanges = true;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
catch (e_9_1) { e_9 = { error: e_9_1 }; }
|
|
513
|
+
finally {
|
|
514
|
+
try {
|
|
515
|
+
if (_g && !_g.done && (_a = _f.return)) _a.call(_f);
|
|
516
|
+
}
|
|
517
|
+
finally { if (e_9) throw e_9.error; }
|
|
518
|
+
}
|
|
519
|
+
return hasChanges ? next : null;
|
|
520
|
+
};
|
|
521
|
+
exports.processSheetDataChangesV5 = processSheetDataChangesV5;
|
|
522
|
+
var axisKey = function (sheetId, isRow) {
|
|
523
|
+
return "".concat(sheetId, ":").concat(isRow ? "R" : "C");
|
|
524
|
+
};
|
|
525
|
+
var writerStyles = function (w) { var _a; return ((_a = w.styles) !== null && _a !== void 0 ? _a : (w.styles = (0, exports.readStyleOverlayV5)(w.doc))); };
|
|
526
|
+
var writerAxis = function (w, sheetId, isRow) {
|
|
527
|
+
var key = axisKey(sheetId, isRow);
|
|
528
|
+
var axis = w.axes.get(key);
|
|
529
|
+
if (!axis) {
|
|
530
|
+
var ids = (0, exports.readLineIds)(w.doc, sheetId, isRow ? "row" : "column");
|
|
531
|
+
axis = { ids: ids, maxC0: ids.reduce(function (max, id) { return Math.max(max, c0Counter(id)); }, 0) };
|
|
532
|
+
w.axes.set(key, axis);
|
|
533
|
+
}
|
|
534
|
+
return axis;
|
|
535
|
+
};
|
|
536
|
+
/**
|
|
537
|
+
* `YrsStore::c0_floor` of every sheet and axis, keyed by `axisKey`: the highest
|
|
538
|
+
* client-0 counter any hot key (tombstones and orphans too), composed style,
|
|
539
|
+
* note, dimension or cold block references. One pass over the keys.
|
|
540
|
+
*/
|
|
541
|
+
var c0Floors = function (doc, styles) {
|
|
542
|
+
var e_10, _a;
|
|
543
|
+
var floors = new Map();
|
|
544
|
+
var raise = function (sheetId, isRow, counter) {
|
|
545
|
+
var _a;
|
|
546
|
+
var key = axisKey(Number(sheetId), isRow);
|
|
547
|
+
if (counter > ((_a = floors.get(key)) !== null && _a !== void 0 ? _a : 0))
|
|
548
|
+
floors.set(key, counter);
|
|
549
|
+
};
|
|
550
|
+
var cellKey = function (key) {
|
|
551
|
+
var match = CELL_KEY_V5.exec(key);
|
|
552
|
+
if (!match)
|
|
553
|
+
return;
|
|
554
|
+
raise(match[1], true, c0Counter(match[2]));
|
|
555
|
+
raise(match[1], false, c0Counter(match[3]));
|
|
556
|
+
};
|
|
557
|
+
try {
|
|
558
|
+
for (var _b = tslib_1.__values([CELLS_V5, NOTES_V5]), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
559
|
+
var name_2 = _c.value;
|
|
560
|
+
if (doc.share.has(name_2))
|
|
561
|
+
doc.getMap(name_2).forEach(function (_v, key) { return cellKey(key); });
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
catch (e_10_1) { e_10 = { error: e_10_1 }; }
|
|
565
|
+
finally {
|
|
566
|
+
try {
|
|
567
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
568
|
+
}
|
|
569
|
+
finally { if (e_10) throw e_10.error; }
|
|
570
|
+
}
|
|
571
|
+
styles.forEach(function (_v, key) { return cellKey(key); });
|
|
572
|
+
if (doc.share.has(DIMS_V5)) {
|
|
573
|
+
doc.getMap(DIMS_V5).forEach(function (_v, key) {
|
|
574
|
+
var match = DIM_KEY_V5.exec(key);
|
|
575
|
+
if (match && idCounter(BigInt(match[3])) !== 0) {
|
|
576
|
+
raise(match[1], match[2] === "R", c0Counter(match[3]));
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
if (doc.share.has(BLOCKS_V5)) {
|
|
581
|
+
// Rows: the last offset of each column's highest block (`block_c0_max`).
|
|
582
|
+
var top_1 = new Map();
|
|
583
|
+
doc.getMap(BLOCKS_V5).forEach(function (value, key) {
|
|
584
|
+
var _a, _b;
|
|
585
|
+
var match = BLOCK_KEY_V5.exec(key);
|
|
586
|
+
if (!match || idCounter(BigInt(match[2])) === 0)
|
|
587
|
+
return;
|
|
588
|
+
raise(match[1], false, c0Counter(match[2]));
|
|
589
|
+
var block = Number(match[3]);
|
|
590
|
+
var col = "".concat(match[1], ":").concat(match[2]);
|
|
591
|
+
if (value instanceof Uint8Array && block >= ((_b = (_a = top_1.get(col)) === null || _a === void 0 ? void 0 : _a[1]) !== null && _b !== void 0 ? _b : -1)) {
|
|
592
|
+
top_1.set(col, [match[1], block, value]);
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
top_1.forEach(function (_a) {
|
|
596
|
+
var _b = tslib_1.__read(_a, 3), sheetId = _b[0], block = _b[1], buf = _b[2];
|
|
597
|
+
var offsets = blockRowOffsets(buf);
|
|
598
|
+
if (offsets.length) {
|
|
599
|
+
raise(sheetId, true, block * BLOCK_ROWS + Math.max.apply(Math, tslib_1.__spreadArray([], tslib_1.__read(offsets), false)) + 1);
|
|
600
|
+
}
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
return floors;
|
|
604
|
+
};
|
|
605
|
+
/**
|
|
606
|
+
* The id of 1-based line `pos`, naming every line up to it first: implicit
|
|
607
|
+
* client-0 ids, counters past the axis's highest client-0 counter and past the
|
|
608
|
+
* floor (`AxisIndex::ensure_len`), appended at the array's end.
|
|
609
|
+
*/
|
|
610
|
+
var ensureLine = function (w, sheetId, isRow, pos) {
|
|
611
|
+
var _a;
|
|
612
|
+
var _b, _c;
|
|
613
|
+
var axis = writerAxis(w, sheetId, isRow);
|
|
614
|
+
if (pos > axis.ids.length) {
|
|
615
|
+
(_b = w.floors) !== null && _b !== void 0 ? _b : (w.floors = c0Floors(w.doc, writerStyles(w)));
|
|
616
|
+
var start_1 = Math.max(axis.maxC0, (_c = w.floors.get(axisKey(sheetId, isRow))) !== null && _c !== void 0 ? _c : 0) + 1;
|
|
617
|
+
var counters = Array.from({ length: pos - axis.ids.length }, function (_, i) { return start_1 + i; });
|
|
618
|
+
w.doc
|
|
619
|
+
.getArray(lineIdsRootName(sheetId, isRow ? "row" : "column"))
|
|
620
|
+
.push(counters.map(idValue));
|
|
621
|
+
(_a = axis.ids).push.apply(_a, tslib_1.__spreadArray([], tslib_1.__read(counters.map(c0Id)), false));
|
|
622
|
+
axis.maxC0 = start_1 + counters.length - 1;
|
|
623
|
+
}
|
|
624
|
+
return axis.ids[pos - 1];
|
|
625
|
+
};
|
|
626
|
+
var validPosition = function (row, col) {
|
|
627
|
+
return Number.isInteger(row) && Number.isInteger(col) && row >= 1 && col >= 1;
|
|
628
|
+
};
|
|
629
|
+
/** Queues a style op and applies it to the writer's composed plane. */
|
|
630
|
+
var queueStyle = function (w, sheetId, rowId, colId, format) {
|
|
631
|
+
w.ops.push([sheetId, rowId, colId, format]);
|
|
632
|
+
var key = (0, exports.createCellKeyV5)(sheetId, rowId, colId);
|
|
633
|
+
if (format)
|
|
634
|
+
writerStyles(w).set(key, format);
|
|
635
|
+
else
|
|
636
|
+
writerStyles(w).delete(key);
|
|
637
|
+
};
|
|
638
|
+
/**
|
|
639
|
+
* Writes `cell` at a position. A `uf` that differs from the stored one (the
|
|
640
|
+
* composed plane's entry, else the inline `uf`) is logged as a replace (a
|
|
641
|
+
* clear, then the format; a style reference only clears: the engine cannot
|
|
642
|
+
* resolve it). A note lives in `rncNotes` when the key has an entry there,
|
|
643
|
+
* inline otherwise. An unchanged cell is not rewritten (its `ft` survives).
|
|
644
|
+
*/
|
|
645
|
+
var writeCell = function (w, sheetId, row, col, cell) {
|
|
646
|
+
var _a;
|
|
647
|
+
if (!validPosition(row, col))
|
|
648
|
+
return;
|
|
649
|
+
var rowId = ensureLine(w, sheetId, true, row);
|
|
650
|
+
var colId = ensureLine(w, sheetId, false, col);
|
|
651
|
+
var key = (0, exports.createCellKeyV5)(sheetId, rowId, colId);
|
|
652
|
+
var stored = cellDataV5(w.cells.get(key));
|
|
653
|
+
var format = normalizedFormat((0, utils_1.getCellUserEnteredFormat)(cell));
|
|
654
|
+
var storedFormat = normalizedFormat((_a = writerStyles(w).get(key)) !== null && _a !== void 0 ? _a : (0, utils_1.getCellUserEnteredFormat)(stored));
|
|
655
|
+
if (!(0, isEqual_1.default)(format, storedFormat)) {
|
|
656
|
+
queueStyle(w, sheetId, rowId, colId, null);
|
|
657
|
+
if (isPlainObject(format) && !isStyleReference(format)) {
|
|
658
|
+
queueStyle(w, sheetId, rowId, colId, format);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
var next = cell;
|
|
662
|
+
var notes = w.doc.share.has(NOTES_V5) ? w.doc.getMap(NOTES_V5) : null;
|
|
663
|
+
if (notes === null || notes === void 0 ? void 0 : notes.has(key)) {
|
|
664
|
+
var note = cell.note, rest = tslib_1.__rest(cell, ["note"]);
|
|
665
|
+
if (typeof note === "string" && note) {
|
|
666
|
+
if (notes.get(key) !== note)
|
|
667
|
+
notes.set(key, note);
|
|
668
|
+
}
|
|
669
|
+
else {
|
|
670
|
+
notes.delete(key);
|
|
671
|
+
}
|
|
672
|
+
next = rest;
|
|
673
|
+
}
|
|
674
|
+
if (!stored || !(0, isEqual_1.default)(stored, next))
|
|
675
|
+
w.cells.set(key, next);
|
|
676
|
+
};
|
|
677
|
+
/**
|
|
678
|
+
* Removes the cell at a position (`remove_cell`): a hot `null` when a cold block
|
|
679
|
+
* holds it, else the hot entry is dropped; its composed style is cleared and its
|
|
680
|
+
* `rncNotes` entry removed. An unnamed position holds nothing.
|
|
681
|
+
*/
|
|
682
|
+
var removeCell = function (w, sheetId, row, col) {
|
|
683
|
+
if (!validPosition(row, col))
|
|
684
|
+
return;
|
|
685
|
+
var rowId = writerAxis(w, sheetId, true).ids[row - 1];
|
|
686
|
+
var colId = writerAxis(w, sheetId, false).ids[col - 1];
|
|
687
|
+
if (rowId === undefined || colId === undefined)
|
|
688
|
+
return;
|
|
689
|
+
var key = (0, exports.createCellKeyV5)(sheetId, rowId, colId);
|
|
690
|
+
if (coldCellExists(w.doc, w.offsets, sheetId, rowId, colId)) {
|
|
691
|
+
if (w.cells.get(key) !== null)
|
|
692
|
+
w.cells.set(key, null);
|
|
693
|
+
}
|
|
694
|
+
else if (w.cells.has(key)) {
|
|
695
|
+
w.cells.delete(key);
|
|
696
|
+
}
|
|
697
|
+
if (writerStyles(w).has(key))
|
|
698
|
+
queueStyle(w, sheetId, rowId, colId, null);
|
|
699
|
+
if (w.doc.share.has(NOTES_V5)) {
|
|
700
|
+
var notes = w.doc.getMap(NOTES_V5);
|
|
701
|
+
if (notes.has(key))
|
|
702
|
+
notes.delete(key);
|
|
703
|
+
}
|
|
704
|
+
};
|
|
705
|
+
/** The cells of a row value (`{ values }`), by 1-based column. */
|
|
706
|
+
var rowCells = function (row) {
|
|
707
|
+
var e_11, _a;
|
|
708
|
+
var out = new Map();
|
|
709
|
+
var values = isPlainObject(row) ? row.values : undefined;
|
|
710
|
+
if (values && typeof values === "object") {
|
|
711
|
+
try {
|
|
712
|
+
for (var _b = tslib_1.__values(Object.entries(values)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
713
|
+
var _d = tslib_1.__read(_c.value, 2), col = _d[0], cell = _d[1];
|
|
714
|
+
if (isPlainObject(cell))
|
|
715
|
+
out.set(Number(col), cell);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
catch (e_11_1) { e_11 = { error: e_11_1 }; }
|
|
719
|
+
finally {
|
|
720
|
+
try {
|
|
721
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
722
|
+
}
|
|
723
|
+
finally { if (e_11) throw e_11.error; }
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
return out;
|
|
727
|
+
};
|
|
728
|
+
/** Replaces a whole row: every named column the new row leaves empty is removed. */
|
|
729
|
+
var replaceRow = function (w, sheetId, row, value) {
|
|
730
|
+
if (!Number.isInteger(row) || row < 1)
|
|
731
|
+
return;
|
|
732
|
+
var cells = rowCells(value);
|
|
733
|
+
var named = writerAxis(w, sheetId, false).ids.length;
|
|
734
|
+
for (var col = 1; col <= named; col++) {
|
|
735
|
+
if (!cells.has(col))
|
|
736
|
+
removeCell(w, sheetId, row, col);
|
|
737
|
+
}
|
|
738
|
+
cells.forEach(function (cell, col) { return writeCell(w, sheetId, row, col, cell); });
|
|
739
|
+
};
|
|
740
|
+
var writeSheet = function (w, sheetId, rows) {
|
|
741
|
+
var e_12, _a;
|
|
742
|
+
if (!rows || typeof rows !== "object")
|
|
743
|
+
return;
|
|
744
|
+
var _loop_1 = function (row, value) {
|
|
745
|
+
rowCells(value).forEach(function (cell, col) { return writeCell(w, sheetId, Number(row), col, cell); });
|
|
746
|
+
};
|
|
747
|
+
try {
|
|
748
|
+
for (var _b = tslib_1.__values(Object.entries(rows)), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
749
|
+
var _d = tslib_1.__read(_c.value, 2), row = _d[0], value = _d[1];
|
|
750
|
+
_loop_1(row, value);
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
catch (e_12_1) { e_12 = { error: e_12_1 }; }
|
|
754
|
+
finally {
|
|
755
|
+
try {
|
|
756
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
757
|
+
}
|
|
758
|
+
finally { if (e_12) throw e_12.error; }
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
/**
|
|
762
|
+
* Removes every cell of the matching sheets (`remove_sheet`'s point data): hot
|
|
763
|
+
* keys (orphans and tombstones too), cold blocks, composed styles (clear ops)
|
|
764
|
+
* and legacy seed entries, notes. Walks those planes; id arrays and dimensions stay.
|
|
765
|
+
*/
|
|
766
|
+
var clearSheets = function (w, match) {
|
|
767
|
+
var e_13, _a;
|
|
768
|
+
var sheetOf = function (key) { return Number(key.slice(0, Math.max(0, key.search(/[:!]/)))); };
|
|
769
|
+
var drop = function (name) {
|
|
770
|
+
var e_14, _a;
|
|
771
|
+
if (!w.doc.share.has(name))
|
|
772
|
+
return;
|
|
773
|
+
var map = w.doc.getMap(name);
|
|
774
|
+
try {
|
|
775
|
+
for (var _b = tslib_1.__values(Array.from(map.keys())), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
776
|
+
var key = _c.value;
|
|
777
|
+
if (match(sheetOf(key)))
|
|
778
|
+
map.delete(key);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
catch (e_14_1) { e_14 = { error: e_14_1 }; }
|
|
782
|
+
finally {
|
|
783
|
+
try {
|
|
784
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
785
|
+
}
|
|
786
|
+
finally { if (e_14) throw e_14.error; }
|
|
787
|
+
}
|
|
788
|
+
};
|
|
789
|
+
drop(CELLS_V5);
|
|
790
|
+
drop(BLOCKS_V5);
|
|
791
|
+
drop(NOTES_V5);
|
|
792
|
+
drop(STYLES_SEED_V5);
|
|
793
|
+
try {
|
|
794
|
+
for (var _b = tslib_1.__values(Array.from(writerStyles(w).keys())), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
795
|
+
var key = _c.value;
|
|
796
|
+
var parsed = (0, exports.parseCellKeyV5)(key);
|
|
797
|
+
if (parsed && match(parsed.sheetId)) {
|
|
798
|
+
queueStyle(w, parsed.sheetId, parsed.rowId, parsed.colId, null);
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
catch (e_13_1) { e_13 = { error: e_13_1 }; }
|
|
803
|
+
finally {
|
|
804
|
+
try {
|
|
805
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
806
|
+
}
|
|
807
|
+
finally { if (e_13) throw e_13.error; }
|
|
808
|
+
}
|
|
809
|
+
w.offsets.clear();
|
|
810
|
+
};
|
|
811
|
+
/** The cell a nested patch edits: the stored cell as the readers show it. */
|
|
812
|
+
var nestedBase = function (w, sheetId, row, col) {
|
|
813
|
+
var _a;
|
|
814
|
+
var rowId = writerAxis(w, sheetId, true).ids[row - 1];
|
|
815
|
+
var colId = writerAxis(w, sheetId, false).ids[col - 1];
|
|
816
|
+
if (rowId === undefined || colId === undefined)
|
|
817
|
+
return {};
|
|
818
|
+
var key = (0, exports.createCellKeyV5)(sheetId, rowId, colId);
|
|
819
|
+
if (!w.cells.has(key) && coldCellExists(w.doc, w.offsets, sheetId, rowId, colId)) {
|
|
820
|
+
return null;
|
|
821
|
+
}
|
|
822
|
+
return (_a = composeCell(w.doc, key, writerStyles(w))) !== null && _a !== void 0 ? _a : {};
|
|
823
|
+
};
|
|
824
|
+
var applyPatchV5 = function (w, _a) {
|
|
825
|
+
var e_15, _b;
|
|
826
|
+
var op = _a.op, path = _a.path, value = _a.value;
|
|
827
|
+
if (path.length === 0) {
|
|
828
|
+
if (op === "replace")
|
|
829
|
+
clearSheets(w, function () { return true; });
|
|
830
|
+
if (value && typeof value === "object") {
|
|
831
|
+
try {
|
|
832
|
+
for (var _c = tslib_1.__values(Object.entries(value)), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
833
|
+
var _f = tslib_1.__read(_d.value, 2), sheetId_1 = _f[0], rows = _f[1];
|
|
834
|
+
writeSheet(w, Number(sheetId_1), rows);
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
catch (e_15_1) { e_15 = { error: e_15_1 }; }
|
|
838
|
+
finally {
|
|
839
|
+
try {
|
|
840
|
+
if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
|
|
841
|
+
}
|
|
842
|
+
finally { if (e_15) throw e_15.error; }
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
return;
|
|
846
|
+
}
|
|
847
|
+
var sheetId = Number(path[0]);
|
|
848
|
+
var row = Number(path[1]);
|
|
849
|
+
if (path.length > 4) {
|
|
850
|
+
var col = Number(path[3]);
|
|
851
|
+
if (!validPosition(row, col))
|
|
852
|
+
return;
|
|
853
|
+
var base = nestedBase(w, sheetId, row, col);
|
|
854
|
+
if (base === null) {
|
|
855
|
+
// Cold-only cell: its body needs an async inflate, a sync writer cannot merge into it.
|
|
856
|
+
console.warn("y-spreadsheet v5: nested patch on cold cell ".concat(sheetId, "!R").concat(row, "C").concat(col, " skipped"));
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
var cell = (0, cloneDeep_1.default)(base);
|
|
860
|
+
var nested = path.slice(4);
|
|
861
|
+
if (op === "remove") {
|
|
862
|
+
if (!Object.keys(base).length)
|
|
863
|
+
return;
|
|
864
|
+
(0, unset_1.default)(cell, nested);
|
|
865
|
+
}
|
|
866
|
+
else {
|
|
867
|
+
(0, set_1.default)(cell, nested, (0, cloneDeep_1.default)(value));
|
|
868
|
+
}
|
|
869
|
+
writeCell(w, sheetId, row, col, cell);
|
|
870
|
+
}
|
|
871
|
+
else if (path.length === 3 && path[2] === "values") {
|
|
872
|
+
if (op !== "remove")
|
|
873
|
+
replaceRow(w, sheetId, row, { values: value });
|
|
874
|
+
}
|
|
875
|
+
else if (path.length === 2) {
|
|
876
|
+
replaceRow(w, sheetId, row, op === "remove" ? null : value);
|
|
877
|
+
}
|
|
878
|
+
else if (path.length === 1) {
|
|
879
|
+
clearSheets(w, function (id) { return id === sheetId; });
|
|
880
|
+
if (op !== "remove")
|
|
881
|
+
writeSheet(w, sheetId, value);
|
|
882
|
+
}
|
|
883
|
+
};
|
|
884
|
+
/**
|
|
885
|
+
* Apply immer `SheetData` patches to a v5 doc in one transaction (`origin`, the
|
|
886
|
+
* caller's; nested in an open transaction it joins that one). Positional, like
|
|
887
|
+
* the v3 path: a row insert/delete arrives as rewritten rows and is applied as
|
|
888
|
+
* cell writes/removals at each position's line ids; no line is inserted or
|
|
889
|
+
* deleted. Writes past an axis name lines (client-0 padding, `ensure_lines`).
|
|
890
|
+
* `uf` changes append ONE `STYLE_LOG_V2` buffer to `rncCellStylesLog` and stay
|
|
891
|
+
* inline on the cell. `styles`, when given, is the caller's composed style plane
|
|
892
|
+
* (`readStyleOverlayV5`), used instead of folding the log and updated in place.
|
|
893
|
+
*/
|
|
894
|
+
var addSheetDataPatchesV5 = function (doc, patches, origin, styles) {
|
|
895
|
+
if (origin === void 0) { origin = null; }
|
|
896
|
+
if (!(patches === null || patches === void 0 ? void 0 : patches.length))
|
|
897
|
+
return;
|
|
898
|
+
doc.transact(function () {
|
|
899
|
+
var e_16, _a;
|
|
900
|
+
var w = {
|
|
901
|
+
doc: doc,
|
|
902
|
+
cells: doc.getMap(CELLS_V5),
|
|
903
|
+
axes: new Map(),
|
|
904
|
+
floors: null,
|
|
905
|
+
styles: styles !== null && styles !== void 0 ? styles : null,
|
|
906
|
+
ops: [],
|
|
907
|
+
offsets: new Map(),
|
|
908
|
+
};
|
|
909
|
+
// Consecutive cell patches are consolidated (the later one wins) but never
|
|
910
|
+
// reordered across a row-, sheet- or document-level patch (v3 parity).
|
|
911
|
+
var pending = new Map();
|
|
912
|
+
var flush = function () {
|
|
913
|
+
pending.forEach(function (_a) {
|
|
914
|
+
var _b = tslib_1.__read(_a, 4), sheetId = _b[0], row = _b[1], col = _b[2], cell = _b[3];
|
|
915
|
+
return cell ? writeCell(w, sheetId, row, col, cell) : removeCell(w, sheetId, row, col);
|
|
916
|
+
});
|
|
917
|
+
pending.clear();
|
|
918
|
+
};
|
|
919
|
+
try {
|
|
920
|
+
for (var patches_1 = tslib_1.__values(patches), patches_1_1 = patches_1.next(); !patches_1_1.done; patches_1_1 = patches_1.next()) {
|
|
921
|
+
var patch = patches_1_1.value;
|
|
922
|
+
var op = patch.op, path = patch.path, value = patch.value;
|
|
923
|
+
if (path.length === 4 &&
|
|
924
|
+
(op === "replace" || op === "add" || op === "remove")) {
|
|
925
|
+
var _b = tslib_1.__read(path.map(Number), 4), sheetId = _b[0], row = _b[1], col = _b[3];
|
|
926
|
+
pending.set("".concat(sheetId, ":").concat(row, ":").concat(col), [
|
|
927
|
+
sheetId,
|
|
928
|
+
row,
|
|
929
|
+
col,
|
|
930
|
+
op !== "remove" && isPlainObject(value) ? value : null,
|
|
931
|
+
]);
|
|
932
|
+
continue;
|
|
933
|
+
}
|
|
934
|
+
flush();
|
|
935
|
+
applyPatchV5(w, patch);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
catch (e_16_1) { e_16 = { error: e_16_1 }; }
|
|
939
|
+
finally {
|
|
940
|
+
try {
|
|
941
|
+
if (patches_1_1 && !patches_1_1.done && (_a = patches_1.return)) _a.call(patches_1);
|
|
942
|
+
}
|
|
943
|
+
finally { if (e_16) throw e_16.error; }
|
|
944
|
+
}
|
|
945
|
+
flush();
|
|
946
|
+
if (w.ops.length) {
|
|
947
|
+
doc.getArray(STYLES_LOG_V5).push([encodeStyleOps(w.ops)]);
|
|
948
|
+
}
|
|
949
|
+
}, origin);
|
|
950
|
+
};
|
|
951
|
+
exports.addSheetDataPatchesV5 = addSheetDataPatchesV5;
|
|
952
|
+
/** Set one cell of a v5 doc (see `addSheetDataPatchesV5`). */
|
|
953
|
+
var setCellV5 = function (doc, sheetId, rowIndex, columnIndex, value) {
|
|
954
|
+
return (0, exports.addSheetDataPatchesV5)(doc, [
|
|
955
|
+
{ op: "replace", path: [sheetId, rowIndex, "values", columnIndex], value: value },
|
|
956
|
+
]);
|
|
957
|
+
};
|
|
958
|
+
exports.setCellV5 = setCellV5;
|
|
959
|
+
/** Remove one cell of a v5 doc (see `addSheetDataPatchesV5`). */
|
|
960
|
+
var deleteCellV5 = function (doc, sheetId, rowIndex, columnIndex) {
|
|
961
|
+
return (0, exports.addSheetDataPatchesV5)(doc, [
|
|
962
|
+
{ op: "remove", path: [sheetId, rowIndex, "values", columnIndex] },
|
|
963
|
+
]);
|
|
964
|
+
};
|
|
965
|
+
exports.deleteCellV5 = deleteCellV5;
|
|
966
|
+
/** Remove every cell of a sheet of a v5 doc; its line ids and dimensions stay. */
|
|
967
|
+
var deleteSheetV5 = function (doc, sheetId) {
|
|
968
|
+
return (0, exports.addSheetDataPatchesV5)(doc, [{ op: "remove", path: [sheetId] }]);
|
|
969
|
+
};
|
|
970
|
+
exports.deleteSheetV5 = deleteSheetV5;
|