@rowsncolumns/y-spreadsheet 16.1.1 → 17.0.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 +21 -6
- package/dist/cjs/helpers-v5.d.ts +11 -11
- package/dist/cjs/helpers-v5.d.ts.map +1 -1
- package/dist/cjs/helpers-v5.js +34 -32
- package/dist/cjs/helpers.d.ts +17 -19
- package/dist/cjs/helpers.d.ts.map +1 -1
- package/dist/cjs/helpers.js +89 -86
- package/dist/cjs/observables.d.ts +7 -7
- package/dist/cjs/observables.d.ts.map +1 -1
- package/dist/cjs/observables.js +34 -46
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/cjs/types.d.ts +96 -19
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/cjs/use-yspreadsheet-v2.d.ts +11 -10
- package/dist/cjs/use-yspreadsheet-v2.d.ts.map +1 -1
- package/dist/cjs/use-yspreadsheet-v2.js +79 -90
- package/dist/cjs/use-yspreadsheet.d.ts +6 -6
- package/dist/cjs/use-yspreadsheet.d.ts.map +1 -1
- package/dist/cjs/use-yspreadsheet.js +38 -74
- package/dist/cjs/yjs-runtime.js +5 -5
- package/dist/cjs/ynodes.d.ts +61 -0
- package/dist/cjs/ynodes.d.ts.map +1 -0
- package/dist/cjs/ynodes.js +224 -0
- package/dist/esm/helpers-v5.d.ts +11 -11
- package/dist/esm/helpers-v5.d.ts.map +1 -1
- package/dist/esm/helpers-v5.js +34 -32
- package/dist/esm/helpers.d.ts +17 -19
- package/dist/esm/helpers.d.ts.map +1 -1
- package/dist/esm/helpers.js +90 -87
- package/dist/esm/observables.d.ts +7 -7
- package/dist/esm/observables.d.ts.map +1 -1
- package/dist/esm/observables.js +21 -25
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/esm/types.d.ts +96 -19
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/esm/use-yspreadsheet-v2.d.ts +11 -10
- package/dist/esm/use-yspreadsheet-v2.d.ts.map +1 -1
- package/dist/esm/use-yspreadsheet-v2.js +76 -88
- package/dist/esm/use-yspreadsheet.d.ts +6 -6
- package/dist/esm/use-yspreadsheet.d.ts.map +1 -1
- package/dist/esm/use-yspreadsheet.js +17 -43
- package/dist/esm/yjs-runtime.js +5 -5
- package/dist/esm/ynodes.d.ts +61 -0
- package/dist/esm/ynodes.d.ts.map +1 -0
- package/dist/esm/ynodes.js +146 -0
- package/package.json +4 -3
package/dist/esm/helpers.js
CHANGED
|
@@ -5,6 +5,7 @@ import set from "lodash/set";
|
|
|
5
5
|
import get from "lodash/get";
|
|
6
6
|
import unset from "lodash/unset";
|
|
7
7
|
import { resolveYjsRuntime } from "./yjs-runtime";
|
|
8
|
+
import { arrayJson, hasEntries, listDelete, listInsert, nodeJson, } from "./ynodes";
|
|
8
9
|
import { addSheetDataPatchesV5 } from "./helpers-v5";
|
|
9
10
|
export * from "./helpers-v5";
|
|
10
11
|
/**
|
|
@@ -17,24 +18,26 @@ export * from "./helpers-v5";
|
|
|
17
18
|
*/
|
|
18
19
|
export const applyPatchesToYDoc = (doc, userId, patches, source, sheetDataVersion, yjsRuntime) => {
|
|
19
20
|
const yjs = resolveYjsRuntime(yjsRuntime);
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
const
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
const
|
|
21
|
+
// Maps: sheetDataV2, sheetDataV3, docMeta, sharedStringsMap, cellXfs,
|
|
22
|
+
// disableRecalc. Arrays: everything else.
|
|
23
|
+
const ySheetDataV2 = doc.get("sheetDataV2");
|
|
24
|
+
const ySheetDataV3 = doc.get("sheetDataV3");
|
|
25
|
+
const yDocMeta = doc.get("docMeta");
|
|
26
|
+
const yRecalcCells = doc.get("recalcCells");
|
|
27
|
+
const ySheets = doc.get("sheets");
|
|
28
|
+
const yTables = doc.get("tables");
|
|
29
|
+
const yEmbeds = doc.get("embeds");
|
|
30
|
+
const yCharts = doc.get("charts");
|
|
31
|
+
const yConditionalFormats = doc.get("conditionalFormats");
|
|
32
|
+
const yDataValidations = doc.get("dataValidations");
|
|
33
|
+
const yNamedRanges = doc.get("namedRanges");
|
|
34
|
+
const ySharedStringsMap = doc.get("sharedStringsMap");
|
|
35
|
+
const yProtectedRanges = doc.get("protectedRanges");
|
|
36
|
+
const yCellXfs = doc.get("cellXfs");
|
|
37
|
+
const yDisableRecalc = doc.get("disableRecalc");
|
|
35
38
|
// Check version flag to determine which sheet data format to use
|
|
36
39
|
// Use param if provided, otherwise fall back to docMeta for backwards compatibility
|
|
37
|
-
const resolvedVersion = sheetDataVersion ?? yDocMeta.
|
|
40
|
+
const resolvedVersion = sheetDataVersion ?? yDocMeta.getAttr("sheetDataVersion");
|
|
38
41
|
const isV3 = resolvedVersion === "v3";
|
|
39
42
|
const isV5 = resolvedVersion === "v5";
|
|
40
43
|
for (let [patch, type = "redo"] of patches) {
|
|
@@ -109,8 +112,8 @@ export const applyPatchesToYDoc = (doc, userId, patches, source, sheetDataVersio
|
|
|
109
112
|
}
|
|
110
113
|
}
|
|
111
114
|
// Set disable recalc flag (only if it's currently truthy, to avoid redundant Y.Map writes)
|
|
112
|
-
if (yDisableRecalc.
|
|
113
|
-
yDisableRecalc.
|
|
115
|
+
if (yDisableRecalc.getAttr("disableRecalc") !== false) {
|
|
116
|
+
yDisableRecalc.setAttr("disableRecalc", false);
|
|
114
117
|
}
|
|
115
118
|
};
|
|
116
119
|
/**
|
|
@@ -163,7 +166,7 @@ export const addSheetDataPatches = (ySheetData, patches, yjsRuntime) => {
|
|
|
163
166
|
// Replace. User is replacing the entire data
|
|
164
167
|
if (op === "replace") {
|
|
165
168
|
// Remove all sheet data
|
|
166
|
-
ySheetData.
|
|
169
|
+
ySheetData.clearAttrs();
|
|
167
170
|
}
|
|
168
171
|
// Assign values
|
|
169
172
|
for (const curSheetId in value) {
|
|
@@ -184,7 +187,7 @@ export const addSheetDataPatches = (ySheetData, patches, yjsRuntime) => {
|
|
|
184
187
|
const [, rowIndex] = path;
|
|
185
188
|
const rowData = getRowDataFromSheet(ysheet, rowIndex, yjs);
|
|
186
189
|
// Clear existing row data
|
|
187
|
-
rowData
|
|
190
|
+
listDelete(rowData, 0, rowData.length);
|
|
188
191
|
if (Array.isArray(value)) {
|
|
189
192
|
// Value is the values array directly
|
|
190
193
|
// Sort keys to maintain order during insertion
|
|
@@ -202,7 +205,7 @@ export const addSheetDataPatches = (ySheetData, patches, yjsRuntime) => {
|
|
|
202
205
|
// Replace entire row with new values
|
|
203
206
|
const rowData = getRowDataFromSheet(ysheet, rowIndex, yjs);
|
|
204
207
|
// Clear existing row data
|
|
205
|
-
rowData
|
|
208
|
+
listDelete(rowData, 0, rowData.length);
|
|
206
209
|
// Add new cell values
|
|
207
210
|
// Sort keys to maintain order during insertion
|
|
208
211
|
Object.entries(value.values)
|
|
@@ -220,7 +223,7 @@ export const addSheetDataPatches = (ySheetData, patches, yjsRuntime) => {
|
|
|
220
223
|
else if (path.length === 1) {
|
|
221
224
|
if (value) {
|
|
222
225
|
// Clear entire sheet
|
|
223
|
-
ysheet
|
|
226
|
+
listDelete(ysheet, 0, ysheet.length);
|
|
224
227
|
// Add all new data
|
|
225
228
|
Object.entries(value)
|
|
226
229
|
.sort(([a], [b]) => Number(a) - Number(b))
|
|
@@ -279,16 +282,16 @@ export const addSheetDataPatches = (ySheetData, patches, yjsRuntime) => {
|
|
|
279
282
|
export const getSheetFromYSheetData = (ySheetData, sheetId, yjsRuntime) => {
|
|
280
283
|
const yjs = resolveYjsRuntime(yjsRuntime);
|
|
281
284
|
const id = String(sheetId);
|
|
282
|
-
let sheet = ySheetData.
|
|
285
|
+
let sheet = ySheetData.getAttr(id);
|
|
283
286
|
if (!sheet) {
|
|
284
|
-
sheet =
|
|
285
|
-
ySheetData.
|
|
287
|
+
sheet = yjs.createArray();
|
|
288
|
+
ySheetData.setAttr(id, sheet);
|
|
286
289
|
}
|
|
287
290
|
return sheet;
|
|
288
291
|
};
|
|
289
292
|
export const deleteSheetFromYSheetData = (ySheetData, sheetId) => {
|
|
290
293
|
const id = String(sheetId);
|
|
291
|
-
ySheetData.
|
|
294
|
+
ySheetData.deleteAttr(id);
|
|
292
295
|
};
|
|
293
296
|
export const getRowDataFromSheet = (ysheet, rowIndex, yjsRuntime) => {
|
|
294
297
|
const yjs = resolveYjsRuntime(yjsRuntime);
|
|
@@ -299,11 +302,11 @@ export const getRowDataFromSheet = (ysheet, rowIndex, yjsRuntime) => {
|
|
|
299
302
|
// Extend array if needed
|
|
300
303
|
if (index >= ysheet.length) {
|
|
301
304
|
const padding = new Array(index - ysheet.length).fill(null);
|
|
302
|
-
|
|
305
|
+
listInsert(ysheet, ysheet.length, padding);
|
|
303
306
|
ysheet.insert(index, [null]); // Insert null first
|
|
304
|
-
// Create new row data
|
|
305
|
-
|
|
306
|
-
newRowData.
|
|
307
|
+
// Create new row data (a node is built with its content: it can be read
|
|
308
|
+
// only once it is in the document)
|
|
309
|
+
const newRowData = yjs.createMap([["values", yjs.createArray()]]);
|
|
307
310
|
// Replace the null
|
|
308
311
|
ysheet.insert(index, [newRowData]);
|
|
309
312
|
ysheet.delete(index + 1, 1);
|
|
@@ -315,20 +318,19 @@ export const getRowDataFromSheet = (ysheet, rowIndex, yjsRuntime) => {
|
|
|
315
318
|
// Insert newRowData at index. length = index+2.
|
|
316
319
|
// Delete at index+1. length = index+1.
|
|
317
320
|
// Correct.
|
|
318
|
-
return newRowData.
|
|
321
|
+
return newRowData.getAttr("values");
|
|
319
322
|
}
|
|
320
323
|
// Get or create existing row data
|
|
321
|
-
|
|
322
|
-
if (!rowData || !rowData.
|
|
324
|
+
const rowData = ysheet.get(index);
|
|
325
|
+
if (!rowData || !rowData.getAttr("values")) {
|
|
323
326
|
// Create new row data if it doesn't exist or is incomplete
|
|
324
|
-
const newRowData =
|
|
325
|
-
newRowData.set("values", new yjs.Array());
|
|
327
|
+
const newRowData = yjs.createMap([["values", yjs.createArray()]]);
|
|
326
328
|
// Replace the existing item
|
|
327
329
|
ysheet.insert(index, [newRowData]);
|
|
328
330
|
ysheet.delete(index + 1, 1);
|
|
329
|
-
return newRowData.
|
|
331
|
+
return newRowData.getAttr("values");
|
|
330
332
|
}
|
|
331
|
-
return rowData.
|
|
333
|
+
return rowData.getAttr("values");
|
|
332
334
|
};
|
|
333
335
|
export const setCellDataInRowData = (rowData, columnIndex, cellData = null) => {
|
|
334
336
|
const index = Number(columnIndex);
|
|
@@ -362,13 +364,13 @@ export const removeRowData = (ysheet, rowIndex) => {
|
|
|
362
364
|
* getRowDataFromSheet, which lazily create them).
|
|
363
365
|
*/
|
|
364
366
|
export const getCellDataFromYSheetData = (ySheetData, sheetId, rowIndex, columnIndex) => {
|
|
365
|
-
const sheet = ySheetData.
|
|
367
|
+
const sheet = ySheetData.getAttr(String(sheetId));
|
|
366
368
|
const rowIdx = Number(rowIndex);
|
|
367
369
|
if (!sheet || rowIdx < 0 || rowIdx >= sheet.length) {
|
|
368
370
|
return undefined;
|
|
369
371
|
}
|
|
370
372
|
const row = sheet.get(rowIdx);
|
|
371
|
-
const values = row?.
|
|
373
|
+
const values = row?.getAttr("values");
|
|
372
374
|
const colIdx = Number(columnIndex);
|
|
373
375
|
if (!values || colIdx < 0 || colIdx >= values.length) {
|
|
374
376
|
return undefined;
|
|
@@ -521,8 +523,8 @@ export const updateArrayType = (yarray, patches) => {
|
|
|
521
523
|
flushPending();
|
|
522
524
|
// User is replacing the entire array
|
|
523
525
|
if (op === "replace") {
|
|
524
|
-
yarray
|
|
525
|
-
yarray.
|
|
526
|
+
listDelete(yarray, 0, yarray.length);
|
|
527
|
+
listInsert(yarray, yarray.length, value);
|
|
526
528
|
}
|
|
527
529
|
return;
|
|
528
530
|
}
|
|
@@ -613,7 +615,7 @@ export const updateArrayType = (yarray, patches) => {
|
|
|
613
615
|
function ensureArraySize(yarray, index) {
|
|
614
616
|
if (index >= yarray.length) {
|
|
615
617
|
const gaps = new Array(index - yarray.length).fill(null);
|
|
616
|
-
|
|
618
|
+
listInsert(yarray, yarray.length, gaps);
|
|
617
619
|
yarray.insert(index, [null]); // Insert placeholder that will be replaced
|
|
618
620
|
}
|
|
619
621
|
}
|
|
@@ -657,32 +659,32 @@ export const applyMapPatches = (yMap, patches) => {
|
|
|
657
659
|
if (!path || !path.length) {
|
|
658
660
|
if (op === "remove") {
|
|
659
661
|
// Only clear if there's something to clear
|
|
660
|
-
if (yMap
|
|
661
|
-
yMap.
|
|
662
|
+
if (hasEntries(yMap))
|
|
663
|
+
yMap.clearAttrs();
|
|
662
664
|
}
|
|
663
665
|
else if (op === "replace") {
|
|
664
666
|
// Diff against current contents instead of clear-and-rewrite, so keys
|
|
665
667
|
// whose references haven't changed don't generate Y.Map tombstones.
|
|
666
668
|
const incoming = new Map(value);
|
|
667
|
-
for (const k of Array.from(yMap.
|
|
669
|
+
for (const k of Array.from(yMap.attrKeys())) {
|
|
668
670
|
if (!incoming.has(k))
|
|
669
|
-
yMap.
|
|
671
|
+
yMap.deleteAttr(k);
|
|
670
672
|
}
|
|
671
673
|
for (const [k, v] of incoming) {
|
|
672
|
-
if (yMap.
|
|
673
|
-
yMap.
|
|
674
|
+
if (yMap.getAttr(k) !== v)
|
|
675
|
+
yMap.setAttr(k, v);
|
|
674
676
|
}
|
|
675
677
|
}
|
|
676
678
|
continue;
|
|
677
679
|
}
|
|
678
680
|
const index = path[0];
|
|
679
681
|
if (op === "remove") {
|
|
680
|
-
if (yMap.
|
|
681
|
-
yMap.
|
|
682
|
+
if (yMap.hasAttr(index))
|
|
683
|
+
yMap.deleteAttr(index);
|
|
682
684
|
}
|
|
683
685
|
else if (op === "add" || op === "replace") {
|
|
684
|
-
if (yMap.
|
|
685
|
-
yMap.
|
|
686
|
+
if (yMap.getAttr(index) !== value)
|
|
687
|
+
yMap.setAttr(index, value);
|
|
686
688
|
}
|
|
687
689
|
}
|
|
688
690
|
};
|
|
@@ -690,7 +692,8 @@ export const applyMapPatches = (yMap, patches) => {
|
|
|
690
692
|
* Process events to find changes in sheet data
|
|
691
693
|
* Returns a new sheet data object with only the changes applied if possible, or null if a full reload is needed
|
|
692
694
|
*/
|
|
693
|
-
export const processSheetDataEvents = (events, currentSheetData, ySheetDataV2) => {
|
|
695
|
+
export const processSheetDataEvents = (events, currentSheetData, ySheetDataV2, yjsRuntime) => {
|
|
696
|
+
const yjs = resolveYjsRuntime(yjsRuntime);
|
|
694
697
|
const changes = new Map();
|
|
695
698
|
const fullReloadSheets = new Set();
|
|
696
699
|
for (const event of events) {
|
|
@@ -767,20 +770,20 @@ export const processSheetDataEvents = (events, currentSheetData, ySheetDataV2) =
|
|
|
767
770
|
const next = { ...currentSheetData };
|
|
768
771
|
let hasChanges = false;
|
|
769
772
|
fullReloadSheets.forEach((sheetId) => {
|
|
770
|
-
const ySheet = ySheetDataV2.
|
|
773
|
+
const ySheet = ySheetDataV2.getAttr(sheetId);
|
|
771
774
|
if (!ySheet)
|
|
772
775
|
return;
|
|
773
|
-
const sheetData = ySheet
|
|
776
|
+
const sheetData = arrayJson(ySheet, yjs);
|
|
774
777
|
next[sheetId] = sheetData;
|
|
775
778
|
hasChanges = true;
|
|
776
779
|
});
|
|
777
780
|
changes.forEach((rowIndices, sheetId) => {
|
|
778
|
-
const ySheet = ySheetDataV2.
|
|
781
|
+
const ySheet = ySheetDataV2.getAttr(sheetId);
|
|
779
782
|
if (!ySheet)
|
|
780
783
|
return;
|
|
781
784
|
// If full sheet reload needed
|
|
782
785
|
if (fullReloadSheets.has(sheetId)) {
|
|
783
|
-
const sheetData = ySheet
|
|
786
|
+
const sheetData = arrayJson(ySheet, yjs);
|
|
784
787
|
next[sheetId] = sheetData;
|
|
785
788
|
hasChanges = true;
|
|
786
789
|
return;
|
|
@@ -791,7 +794,7 @@ export const processSheetDataEvents = (events, currentSheetData, ySheetDataV2) =
|
|
|
791
794
|
}
|
|
792
795
|
if (next[sheetId].length < ySheet.length) {
|
|
793
796
|
if (next[sheetId].length !== ySheet.length) {
|
|
794
|
-
const sheetData = ySheet
|
|
797
|
+
const sheetData = arrayJson(ySheet, yjs);
|
|
795
798
|
next[sheetId] = sheetData;
|
|
796
799
|
hasChanges = true;
|
|
797
800
|
return;
|
|
@@ -805,7 +808,7 @@ export const processSheetDataEvents = (events, currentSheetData, ySheetDataV2) =
|
|
|
805
808
|
if (change.full) {
|
|
806
809
|
let normalizedRow = null;
|
|
807
810
|
if (yRow) {
|
|
808
|
-
const rowJson = yRow
|
|
811
|
+
const rowJson = nodeJson(yRow, yjs);
|
|
809
812
|
normalizedRow = rowJson;
|
|
810
813
|
}
|
|
811
814
|
nextSheet[rowIndex] = normalizedRow;
|
|
@@ -818,14 +821,14 @@ export const processSheetDataEvents = (events, currentSheetData, ySheetDataV2) =
|
|
|
818
821
|
}
|
|
819
822
|
return;
|
|
820
823
|
}
|
|
821
|
-
const yValues = yRow.
|
|
824
|
+
const yValues = yRow.getAttr("values");
|
|
822
825
|
if (!yValues) {
|
|
823
826
|
if (currentRow) {
|
|
824
827
|
nextSheet[rowIndex] = currentRow;
|
|
825
828
|
}
|
|
826
829
|
return;
|
|
827
830
|
}
|
|
828
|
-
const baseRow = currentRow ?? yRow
|
|
831
|
+
const baseRow = currentRow ?? nodeJson(yRow, yjs);
|
|
829
832
|
if (!baseRow) {
|
|
830
833
|
return;
|
|
831
834
|
}
|
|
@@ -960,30 +963,30 @@ export const sheetDataV3ToV2 = (v3Data) => {
|
|
|
960
963
|
*/
|
|
961
964
|
export const getCellV3 = (ySheetDataV3, sheetId, rowIndex, columnIndex) => {
|
|
962
965
|
const key = createCellKeyV3(sheetId, rowIndex, columnIndex);
|
|
963
|
-
return ySheetDataV3.
|
|
966
|
+
return ySheetDataV3.getAttr(key);
|
|
964
967
|
};
|
|
965
968
|
/**
|
|
966
969
|
* Set a cell in V3 flat structure
|
|
967
970
|
*/
|
|
968
971
|
export const setCellV3 = (ySheetDataV3, sheetId, rowIndex, columnIndex, value) => {
|
|
969
972
|
const key = createCellKeyV3(sheetId, rowIndex, columnIndex);
|
|
970
|
-
ySheetDataV3.
|
|
973
|
+
ySheetDataV3.setAttr(key, createCellDataV3(value, sheetId, rowIndex, columnIndex));
|
|
971
974
|
};
|
|
972
975
|
/**
|
|
973
976
|
* Delete a cell from V3 flat structure
|
|
974
977
|
*/
|
|
975
978
|
export const deleteCellV3 = (ySheetDataV3, sheetId, rowIndex, columnIndex) => {
|
|
976
979
|
const key = createCellKeyV3(sheetId, rowIndex, columnIndex);
|
|
977
|
-
ySheetDataV3.
|
|
980
|
+
ySheetDataV3.deleteAttr(key);
|
|
978
981
|
};
|
|
979
982
|
/**
|
|
980
983
|
* Delete all cells for a sheet from V3 flat structure
|
|
981
984
|
*/
|
|
982
985
|
export const deleteSheetV3 = (ySheetDataV3, sheetId) => {
|
|
983
986
|
const prefix = `${sheetId}!`;
|
|
984
|
-
for (const key of ySheetDataV3.
|
|
987
|
+
for (const key of ySheetDataV3.attrKeys()) {
|
|
985
988
|
if (key.startsWith(prefix)) {
|
|
986
|
-
ySheetDataV3.
|
|
989
|
+
ySheetDataV3.deleteAttr(key);
|
|
987
990
|
}
|
|
988
991
|
}
|
|
989
992
|
};
|
|
@@ -991,7 +994,7 @@ export const deleteSheetV3 = (ySheetDataV3, sheetId) => {
|
|
|
991
994
|
* Get V3 sheet data map from Y.Doc
|
|
992
995
|
*/
|
|
993
996
|
export const getSheetDataV3 = (doc) => {
|
|
994
|
-
return doc.
|
|
997
|
+
return doc.get("sheetDataV3");
|
|
995
998
|
};
|
|
996
999
|
/**
|
|
997
1000
|
* Apply immer patches to V3 sheet data (flat map structure)
|
|
@@ -1015,7 +1018,7 @@ export const addSheetDataPatchesV3 = (ySheetDataV3, patches) => {
|
|
|
1015
1018
|
return rowCellKeyIndex;
|
|
1016
1019
|
}
|
|
1017
1020
|
rowCellKeyIndex = new Map();
|
|
1018
|
-
for (const [key, cellData] of ySheetDataV3.
|
|
1021
|
+
for (const [key, cellData] of ySheetDataV3.attrEntries()) {
|
|
1019
1022
|
const rk = rowIndexKey(cellData?.sId, cellData?.r);
|
|
1020
1023
|
let keys = rowCellKeyIndex.get(rk);
|
|
1021
1024
|
if (!keys) {
|
|
@@ -1047,7 +1050,7 @@ export const addSheetDataPatchesV3 = (ySheetDataV3, patches) => {
|
|
|
1047
1050
|
return;
|
|
1048
1051
|
}
|
|
1049
1052
|
for (const key of keys) {
|
|
1050
|
-
ySheetDataV3.
|
|
1053
|
+
ySheetDataV3.deleteAttr(key);
|
|
1051
1054
|
}
|
|
1052
1055
|
rowCellKeyIndex.delete(rowIndexKey(sheetId, rowIdx));
|
|
1053
1056
|
};
|
|
@@ -1074,11 +1077,11 @@ export const addSheetDataPatchesV3 = (ySheetDataV3, patches) => {
|
|
|
1074
1077
|
const flushPendingCellUpdates = () => {
|
|
1075
1078
|
for (const [key, { sheetId, rowIdx, colIdx, value }] of pendingCellUpdates) {
|
|
1076
1079
|
if (value) {
|
|
1077
|
-
ySheetDataV3.
|
|
1080
|
+
ySheetDataV3.setAttr(key, createCellDataV3(value, sheetId, rowIdx, colIdx));
|
|
1078
1081
|
trackCellKeySet(sheetId, rowIdx, key);
|
|
1079
1082
|
}
|
|
1080
1083
|
else {
|
|
1081
|
-
ySheetDataV3.
|
|
1084
|
+
ySheetDataV3.deleteAttr(key);
|
|
1082
1085
|
untrackCellKey(sheetId, rowIdx, key);
|
|
1083
1086
|
}
|
|
1084
1087
|
}
|
|
@@ -1091,8 +1094,8 @@ export const addSheetDataPatchesV3 = (ySheetDataV3, patches) => {
|
|
|
1091
1094
|
// Replace entire data
|
|
1092
1095
|
if (op === "replace") {
|
|
1093
1096
|
// Clear all cells
|
|
1094
|
-
for (const key of ySheetDataV3.
|
|
1095
|
-
ySheetDataV3.
|
|
1097
|
+
for (const key of ySheetDataV3.attrKeys()) {
|
|
1098
|
+
ySheetDataV3.deleteAttr(key);
|
|
1096
1099
|
}
|
|
1097
1100
|
if (rowCellKeyIndex) {
|
|
1098
1101
|
rowCellKeyIndex.clear();
|
|
@@ -1112,7 +1115,7 @@ export const addSheetDataPatchesV3 = (ySheetDataV3, patches) => {
|
|
|
1112
1115
|
if (cellData) {
|
|
1113
1116
|
const colIdx = Number(colIndexStr);
|
|
1114
1117
|
const key = createCellKeyV3(sheetId, rowIdx, colIdx);
|
|
1115
|
-
ySheetDataV3.
|
|
1118
|
+
ySheetDataV3.setAttr(key, createCellDataV3(cellData, sheetId, rowIdx, colIdx));
|
|
1116
1119
|
trackCellKeySet(sheetId, rowIdx, key);
|
|
1117
1120
|
}
|
|
1118
1121
|
});
|
|
@@ -1130,11 +1133,11 @@ export const addSheetDataPatchesV3 = (ySheetDataV3, patches) => {
|
|
|
1130
1133
|
const colIdx = Number(columnIndex);
|
|
1131
1134
|
const key = createCellKeyV3(sheetId, rowIdx, colIdx);
|
|
1132
1135
|
// Get existing cell data or create empty
|
|
1133
|
-
const existing = ySheetDataV3.
|
|
1136
|
+
const existing = ySheetDataV3.getAttr(key);
|
|
1134
1137
|
const cellData = existing?.value ? cloneDeep(existing.value) : {};
|
|
1135
1138
|
// Apply nested property change
|
|
1136
1139
|
set(cellData, nestedPath, value);
|
|
1137
|
-
ySheetDataV3.
|
|
1140
|
+
ySheetDataV3.setAttr(key, createCellDataV3(cellData, sheetId, rowIdx, colIdx));
|
|
1138
1141
|
trackCellKeySet(sheetId, rowIdx, key);
|
|
1139
1142
|
}
|
|
1140
1143
|
// [sheetId, rowIndex, 'values'] - Replace values array
|
|
@@ -1148,7 +1151,7 @@ export const addSheetDataPatchesV3 = (ySheetDataV3, patches) => {
|
|
|
1148
1151
|
value.forEach((cellData, colIndex) => {
|
|
1149
1152
|
if (cellData) {
|
|
1150
1153
|
const key = createCellKeyV3(sheetId, rowIdx, colIndex);
|
|
1151
|
-
ySheetDataV3.
|
|
1154
|
+
ySheetDataV3.setAttr(key, createCellDataV3(cellData, sheetId, rowIdx, colIndex));
|
|
1152
1155
|
trackCellKeySet(sheetId, rowIdx, key);
|
|
1153
1156
|
}
|
|
1154
1157
|
});
|
|
@@ -1166,7 +1169,7 @@ export const addSheetDataPatchesV3 = (ySheetDataV3, patches) => {
|
|
|
1166
1169
|
if (cellData) {
|
|
1167
1170
|
const colIndex = Number(colIndexStr);
|
|
1168
1171
|
const key = createCellKeyV3(sheetId, rowIdx, colIndex);
|
|
1169
|
-
ySheetDataV3.
|
|
1172
|
+
ySheetDataV3.setAttr(key, createCellDataV3(cellData, sheetId, rowIdx, colIndex));
|
|
1170
1173
|
trackCellKeySet(sheetId, rowIdx, key);
|
|
1171
1174
|
}
|
|
1172
1175
|
});
|
|
@@ -1176,9 +1179,9 @@ export const addSheetDataPatchesV3 = (ySheetDataV3, patches) => {
|
|
|
1176
1179
|
else if (path.length === 1) {
|
|
1177
1180
|
// Delete all cells for this sheet
|
|
1178
1181
|
const prefix = `${sheetId}!`;
|
|
1179
|
-
for (const key of ySheetDataV3.
|
|
1182
|
+
for (const key of ySheetDataV3.attrKeys()) {
|
|
1180
1183
|
if (key.startsWith(prefix)) {
|
|
1181
|
-
ySheetDataV3.
|
|
1184
|
+
ySheetDataV3.deleteAttr(key);
|
|
1182
1185
|
}
|
|
1183
1186
|
}
|
|
1184
1187
|
dropSheetFromIndex(sheetId);
|
|
@@ -1191,7 +1194,7 @@ export const addSheetDataPatchesV3 = (ySheetDataV3, patches) => {
|
|
|
1191
1194
|
if (cellData) {
|
|
1192
1195
|
const colIndex = Number(colIndexStr);
|
|
1193
1196
|
const key = createCellKeyV3(sheetId, rowIdx, colIndex);
|
|
1194
|
-
ySheetDataV3.
|
|
1197
|
+
ySheetDataV3.setAttr(key, createCellDataV3(cellData, sheetId, rowIdx, colIndex));
|
|
1195
1198
|
trackCellKeySet(sheetId, rowIdx, key);
|
|
1196
1199
|
}
|
|
1197
1200
|
});
|
|
@@ -1208,11 +1211,11 @@ export const addSheetDataPatchesV3 = (ySheetDataV3, patches) => {
|
|
|
1208
1211
|
const rowIdx = Number(rowIndex);
|
|
1209
1212
|
const colIdx = Number(columnIndex);
|
|
1210
1213
|
const key = createCellKeyV3(sheetId, rowIdx, colIdx);
|
|
1211
|
-
const existing = ySheetDataV3.
|
|
1214
|
+
const existing = ySheetDataV3.getAttr(key);
|
|
1212
1215
|
if (existing?.value && typeof existing.value === "object") {
|
|
1213
1216
|
const cellData = cloneDeep(existing.value);
|
|
1214
1217
|
unset(cellData, nestedPath);
|
|
1215
|
-
ySheetDataV3.
|
|
1218
|
+
ySheetDataV3.setAttr(key, createCellDataV3(cellData, sheetId, rowIdx, colIdx));
|
|
1216
1219
|
trackCellKeySet(sheetId, rowIdx, key);
|
|
1217
1220
|
}
|
|
1218
1221
|
}
|
|
@@ -1225,9 +1228,9 @@ export const addSheetDataPatchesV3 = (ySheetDataV3, patches) => {
|
|
|
1225
1228
|
// [sheetId] - Remove entire sheet
|
|
1226
1229
|
else if (path.length === 1) {
|
|
1227
1230
|
const prefix = `${sheetId}!`;
|
|
1228
|
-
for (const key of ySheetDataV3.
|
|
1231
|
+
for (const key of ySheetDataV3.attrKeys()) {
|
|
1229
1232
|
if (key.startsWith(prefix)) {
|
|
1230
|
-
ySheetDataV3.
|
|
1233
|
+
ySheetDataV3.deleteAttr(key);
|
|
1231
1234
|
}
|
|
1232
1235
|
}
|
|
1233
1236
|
dropSheetFromIndex(sheetId);
|
|
@@ -1262,7 +1265,7 @@ export const addSheetDataPatchesV3 = (ySheetDataV3, patches) => {
|
|
|
1262
1265
|
* Process V3 events and return updated sheet data
|
|
1263
1266
|
* @param changedKeys - Pre-extracted changes from Yjs event
|
|
1264
1267
|
* @param currentSheetData - Current sheet data state
|
|
1265
|
-
* @param ySheetDataV3 - The
|
|
1268
|
+
* @param ySheetDataV3 - The map node for V3 data
|
|
1266
1269
|
*/
|
|
1267
1270
|
export const processSheetDataEventsV3 = (changedKeys, currentSheetData, ySheetDataV3) => {
|
|
1268
1271
|
if (changedKeys.size === 0)
|
|
@@ -1310,7 +1313,7 @@ export const processSheetDataEventsV3 = (changedKeys, currentSheetData, ySheetDa
|
|
|
1310
1313
|
}
|
|
1311
1314
|
else {
|
|
1312
1315
|
// Cell was added or updated
|
|
1313
|
-
const cellDataV3 = ySheetDataV3.
|
|
1316
|
+
const cellDataV3 = ySheetDataV3.getAttr(cellKey);
|
|
1314
1317
|
if (cellDataV3) {
|
|
1315
1318
|
// Ensure row exists
|
|
1316
1319
|
if (!sheet[rowIdx]) {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { Patch } from "immer";
|
|
2
2
|
import { Observable } from "rxjs";
|
|
3
|
-
import type
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
export declare const createSheetDataObservable: (ySheetData:
|
|
7
|
-
export declare const getPatchDeltasFromEvent: <S extends PatchCoordinate | Patch, T extends PatchDocument<S>>(event:
|
|
8
|
-
export declare const createObservable:
|
|
9
|
-
export declare const createDeepObservable: (yArr:
|
|
3
|
+
import type { PatchCoordinate, PatchDocument, YEvent, YjsRuntime, YNode } from "./types";
|
|
4
|
+
import { type YDeepEvent } from "./ynodes";
|
|
5
|
+
export type { YDeepEvent } from "./ynodes";
|
|
6
|
+
export declare const createSheetDataObservable: (ySheetData: YNode, yjsRuntime?: YjsRuntime) => Observable<YDeepEvent[]>;
|
|
7
|
+
export declare const getPatchDeltasFromEvent: <S extends PatchCoordinate | Patch, T extends PatchDocument<S>>(event: YEvent) => S[];
|
|
8
|
+
export declare const createObservable: (yArr: YNode) => Observable<YEvent>;
|
|
9
|
+
export declare const createDeepObservable: (yArr: YNode, yjsRuntime?: YjsRuntime) => Observable<YDeepEvent[]>;
|
|
10
10
|
//# sourceMappingURL=observables.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"observables.d.ts","sourceRoot":"","sources":["../../observables.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"observables.d.ts","sourceRoot":"","sources":["../../observables.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAElC,OAAO,KAAK,EACV,eAAe,EACf,aAAa,EACb,MAAM,EACN,UAAU,EACV,KAAK,EAEN,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAkC,KAAK,UAAU,EAAE,MAAM,UAAU,CAAC;AAE3E,YAAY,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAwB3C,eAAO,MAAM,yBAAyB,GACpC,YAAY,KAAK,EACjB,aAAa,UAAU,6BACqB,CAAC;AAE/C,eAAO,MAAM,uBAAuB,GAClC,CAAC,SAAS,eAAe,GAAG,KAAK,EACjC,CAAC,SAAS,aAAa,CAAC,CAAC,CAAC,EAE1B,OAAO,MAAM,QAWd,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAI,MAAM,KAAK,uBAY3C,CAAC;AAEF,eAAO,MAAM,oBAAoB,GAAI,MAAM,KAAK,EAAE,aAAa,UAAU,6BACpC,CAAC"}
|
package/dist/esm/observables.js
CHANGED
|
@@ -1,27 +1,33 @@
|
|
|
1
1
|
import { Observable } from "rxjs";
|
|
2
|
-
|
|
2
|
+
import { resolveYjsRuntime } from "./yjs-runtime";
|
|
3
|
+
import { insertedValues, liveDeepEvents } from "./ynodes";
|
|
4
|
+
/**
|
|
5
|
+
* Every transaction's changes at or below `node`, as Yjs 13's `observeDeep`
|
|
6
|
+
* delivered them: one entry per changed node still in the document, with its
|
|
7
|
+
* path from `node`, top level first (see `liveDeepEvents`). A transaction
|
|
8
|
+
* whose changes all fell inside deleted nodes emits nothing.
|
|
9
|
+
*/
|
|
10
|
+
const observeDeepEvents = (node, yjsRuntime) => {
|
|
11
|
+
const yjs = resolveYjsRuntime(yjsRuntime);
|
|
3
12
|
return new Observable((observer) => {
|
|
4
|
-
const callback = (
|
|
5
|
-
|
|
13
|
+
const callback = (_event, transaction) => {
|
|
14
|
+
const events = liveDeepEvents(node, transaction, yjs);
|
|
15
|
+
if (events.length)
|
|
16
|
+
observer.next(events);
|
|
6
17
|
};
|
|
7
|
-
|
|
18
|
+
node.observeDeep(callback);
|
|
8
19
|
return () => {
|
|
9
|
-
|
|
20
|
+
node.unobserveDeep(callback);
|
|
10
21
|
};
|
|
11
22
|
});
|
|
12
23
|
};
|
|
24
|
+
export const createSheetDataObservable = (ySheetData, yjsRuntime) => observeDeepEvents(ySheetData, yjsRuntime);
|
|
13
25
|
export const getPatchDeltasFromEvent = (event) => {
|
|
14
26
|
const patches = [];
|
|
15
27
|
if (event.transaction.origin !== "local") {
|
|
16
|
-
for (const
|
|
17
|
-
for (const
|
|
18
|
-
|
|
19
|
-
for (const { patches: userPatches } of changes[type]) {
|
|
20
|
-
for (const patch of userPatches) {
|
|
21
|
-
patches.push(patch);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
28
|
+
for (const { patches: userPatches } of insertedValues(event)) {
|
|
29
|
+
for (const patch of userPatches) {
|
|
30
|
+
patches.push(patch);
|
|
25
31
|
}
|
|
26
32
|
}
|
|
27
33
|
}
|
|
@@ -38,14 +44,4 @@ export const createObservable = (yArr) => {
|
|
|
38
44
|
};
|
|
39
45
|
});
|
|
40
46
|
};
|
|
41
|
-
export const createDeepObservable = (yArr) =>
|
|
42
|
-
return new Observable((observer) => {
|
|
43
|
-
const callback = (event) => {
|
|
44
|
-
observer.next(event);
|
|
45
|
-
};
|
|
46
|
-
yArr.observeDeep(callback);
|
|
47
|
-
return () => {
|
|
48
|
-
yArr.unobserveDeep(callback);
|
|
49
|
-
};
|
|
50
|
-
});
|
|
51
|
-
};
|
|
47
|
+
export const createDeepObservable = (yArr, yjsRuntime) => observeDeepEvents(yArr, yjsRuntime);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../../helpers-v5.ts","../../helpers.ts","../../index.ts","../../observables.ts","../../types.ts","../../use-yspreadsheet-v2.ts","../../use-yspreadsheet.ts","../../vitest.config.ts","../../yjs-runtime.ts"],"version":"5.8.3"}
|
|
1
|
+
{"root":["../../helpers-v5.ts","../../helpers.ts","../../index.ts","../../observables.ts","../../types.ts","../../use-yspreadsheet-v2.ts","../../use-yspreadsheet.ts","../../vitest.config.ts","../../yjs-runtime.ts","../../ynodes.ts"],"version":"5.8.3"}
|