@rowsncolumns/rnc-engine 1.6.1 → 1.7.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/dist/esm/command-handlers.d.ts +29 -2
- package/dist/esm/command-handlers.d.ts.map +1 -1
- package/dist/esm/command-handlers.js +19 -2
- package/dist/esm/display-locale.d.ts +38 -0
- package/dist/esm/display-locale.d.ts.map +1 -0
- package/dist/esm/display-locale.js +108 -0
- package/dist/esm/flash-fill.d.ts +42 -0
- package/dist/esm/flash-fill.d.ts.map +1 -0
- package/dist/esm/flash-fill.js +146 -0
- package/dist/esm/fold.d.ts.map +1 -1
- package/dist/esm/fold.js +3 -1
- package/dist/esm/index.d.ts +17 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +7 -2
- package/dist/esm/use-engine-core.d.ts +56 -1
- package/dist/esm/use-engine-core.d.ts.map +1 -1
- package/dist/esm/use-engine-core.js +366 -10
- package/dist/esm/wasm/rnc_wasm_bg.wasm +0 -0
- package/package.json +6 -6
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
// real `getPrecedents`/`getDependents` results, which the engine now backs with actual graph data.
|
|
3
3
|
import { CellNode, CellRangeNode, isCellCoordinate, isCellNode, makeKey, } from "@rowsncolumns/dag";
|
|
4
4
|
import { DEFAULT_ACTIVE_CELL, DEFAULT_ARRAY, defaultSpreadsheetTheme, getBandedCells, getInitialActiveSheetId, ICON_SET_COLORS, ICON_SET_GLYPHS, useFocusEmbedOrChart, useFonts, useGetSheetDimensions, useSetTableAutoExpansion, useSetCalculatedColumnFill, } from "@rowsncolumns/spreadsheet";
|
|
5
|
-
import { applyAdvancedFilterPlan, applySubtotalPlan, CellXfsRegistry, collectPaintMerges, generateDataFromClipboard, getBandedCellFormat, layerBandedFormat, paintSourceForActiveCell, pickFormula, pickValue, planAdvancedFilter, planSubtotals, resolvePasteSpecialOptions, SharedStringRegistry, spreadMergeAnchorFormats, tilePaintMerges, transpose, useOnCopy, previewTableStyle, useOnTraceDependentsPrecedents, useShowError, CIRCULAR_REFERENCE_WARNING_TITLE, CircularReferenceTracker, formatCircularReferenceWarning, getDefaultCellFormat, } from "@rowsncolumns/spreadsheet-state";
|
|
5
|
+
import { applyAdvancedFilterPlan, applySubtotalPlan, CellXfsRegistry, collectPaintMerges, generateDataFromClipboard, getBandedCellFormat, layerBandedFormat, paintSourceForActiveCell, pickFormula, pickValue, planAdvancedFilter, planSubtotals, resolvePasteSpecialOptions, SharedStringRegistry, spreadMergeAnchorFormats, tilePaintMerges, transpose, useOnCopy, previewTableStyle, useOnTraceDependentsPrecedents, useShowError, CIRCULAR_REFERENCE_WARNING_TITLE, planConsolidate, clearOutlineCalls, outlineStyleTargets, outlineSummaryAt, planAutoOutline, FLASH_FILL_NO_PATTERN_MESSAGE, FLASH_FILL_NO_PATTERN_TITLE, CircularReferenceTracker, formatCircularReferenceWarning, getDefaultCellFormat, } from "@rowsncolumns/spreadsheet-state";
|
|
6
6
|
import { getFormulaLocale, normalizeExternalUrl, resolveDefaultHorizontalAlignment, uuidString, } from "@rowsncolumns/utils";
|
|
7
7
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
8
8
|
import { createCommandHandlers, rejectionDialog, } from "./command-handlers";
|
|
9
|
+
import { ENGINE_DEFAULT_LOCALE, isForeignDisplayLocale, localizeDisplayValue, localizeEnteredNumber, } from "./display-locale";
|
|
9
10
|
import { emptyRenderState, foldCommit, foldCommitStructuralOnly, foldWindow, foldWindows, frozenBandRanges, reconcileSheets, } from "./fold";
|
|
10
11
|
import { RncEngine, } from "./index";
|
|
11
12
|
import { cellKey, parseCellKey } from "./keys";
|
|
@@ -16,9 +17,25 @@ import { pastedMerges } from "./paste-merges";
|
|
|
16
17
|
import { replayPasteEvent, snapshotClipboard } from "./paste-reapply";
|
|
17
18
|
import { tileClipboardToSelection } from "./paste-tiling";
|
|
18
19
|
import { windowRectReader } from "./rect-reader";
|
|
20
|
+
import { flashFillTargetAt, planFlashFillInWindow, planFlashFillSuggestion, } from "./flash-fill";
|
|
19
21
|
/** The width a column at no stored width takes (the grid's default, as `onCopy` reads it). */
|
|
20
22
|
const DEFAULT_PASTE_COLUMN_WIDTH = 100;
|
|
21
23
|
const EMPTY_ALLOW_EDIT_RANGES = [];
|
|
24
|
+
/** The selection the active cell sits in. */
|
|
25
|
+
const activeRange = (activeCell, selections) => selections?.find((s) => s.range &&
|
|
26
|
+
activeCell.rowIndex >= s.range.startRowIndex &&
|
|
27
|
+
activeCell.rowIndex <= s.range.endRowIndex &&
|
|
28
|
+
activeCell.columnIndex >= s.range.startColumnIndex &&
|
|
29
|
+
activeCell.columnIndex <= s.range.endColumnIndex)?.range;
|
|
30
|
+
const isSingleCell = (range) => !range ||
|
|
31
|
+
(range.startRowIndex === range.endRowIndex &&
|
|
32
|
+
range.startColumnIndex === range.endColumnIndex);
|
|
33
|
+
const indexRun = (start, end) => {
|
|
34
|
+
const out = [];
|
|
35
|
+
for (let i = start; i <= end; i++)
|
|
36
|
+
out.push(i);
|
|
37
|
+
return out;
|
|
38
|
+
};
|
|
22
39
|
/** Minimum extra rows/columns to read on EACH side of the visible range so a small scroll reveals
|
|
23
40
|
* already-loaded cells (no flash) before the next `readWindow`. The buffer actually used scales with
|
|
24
41
|
* the viewport (see {@link windowBuffer}) and is never smaller than this. The window size stays
|
|
@@ -282,6 +299,24 @@ function withDocumentTheme(document, host) {
|
|
|
282
299
|
};
|
|
283
300
|
}
|
|
284
301
|
/** Seed for the parsed facets before the first commit (stable reference). */
|
|
302
|
+
/**
|
|
303
|
+
* The sheet a workbook opens on: the one the document names (the imported file's active tab, Excel's
|
|
304
|
+
* `<workbookView activeTab>`) when it is still a visible sheet, else the first visible titled sheet.
|
|
305
|
+
*/
|
|
306
|
+
export const getOpeningSheetId = (sheets, documentActiveSheetId) => {
|
|
307
|
+
const named = sheets.find((sheet) => sheet.sheetId === documentActiveSheetId && !sheet.hidden);
|
|
308
|
+
return named?.sheetId ?? getInitialActiveSheetId(sheets);
|
|
309
|
+
};
|
|
310
|
+
/** `documentFacets.activeSheetId` from the raw facets JSON (`null` when unreadable). */
|
|
311
|
+
const documentActiveSheetId = (facetsJson) => {
|
|
312
|
+
try {
|
|
313
|
+
const id = JSON.parse(facetsJson).activeSheetId;
|
|
314
|
+
return typeof id === "number" ? id : null;
|
|
315
|
+
}
|
|
316
|
+
catch {
|
|
317
|
+
return null;
|
|
318
|
+
}
|
|
319
|
+
};
|
|
285
320
|
const EMPTY_FACETS = {
|
|
286
321
|
conditionalFormats: [],
|
|
287
322
|
dataValidations: [],
|
|
@@ -518,6 +553,15 @@ export function useEngineCore(options) {
|
|
|
518
553
|
customAutofillListsRef.current = options.customAutofillLists;
|
|
519
554
|
const autoExpandTablesRef = useRef(options.autoExpandTables);
|
|
520
555
|
autoExpandTablesRef.current = options.autoExpandTables;
|
|
556
|
+
// The person's Regional Format vs the locale the document formats in (`locale`, else en-US).
|
|
557
|
+
const documentLocale = options.locale ?? ENGINE_DEFAULT_LOCALE;
|
|
558
|
+
const displayLocale = isForeignDisplayLocale(options.displayLocale, documentLocale)
|
|
559
|
+
? options.displayLocale
|
|
560
|
+
: undefined;
|
|
561
|
+
const entryLocaleRef = useRef({
|
|
562
|
+
document: documentLocale,
|
|
563
|
+
});
|
|
564
|
+
entryLocaleRef.current = { display: displayLocale, document: documentLocale };
|
|
521
565
|
const autoCalculatedColumnsRef = useRef(options.autoCalculatedColumns);
|
|
522
566
|
autoCalculatedColumnsRef.current = options.autoCalculatedColumns;
|
|
523
567
|
// Excel's AutoCorrect smart tag after a table auto-expanded — armed from the commit's
|
|
@@ -781,7 +825,7 @@ export function useEngineCore(options) {
|
|
|
781
825
|
engineSheets.some((s) => !seedSheetIdsRef.current.has(s.sheetId))) {
|
|
782
826
|
activeSheetRederivedRef.current = true;
|
|
783
827
|
if (!userPickedSheetRef.current) {
|
|
784
|
-
const derivedId =
|
|
828
|
+
const derivedId = getOpeningSheetId(engineSheets, documentActiveSheetId(engine.documentFacetsJson())) ?? engineSheets[0].sheetId;
|
|
785
829
|
// Move through `activateSheetView`, never a bare `setActiveSheetId`: the tab strip
|
|
786
830
|
// reads `activeSheetId` while the windowed grid paints `windowRef`'s sheet, and a
|
|
787
831
|
// bare set moved only the former — after a peer (agent) created the first non-seed
|
|
@@ -867,8 +911,17 @@ export function useEngineCore(options) {
|
|
|
867
911
|
});
|
|
868
912
|
}
|
|
869
913
|
});
|
|
870
|
-
if (initial.current.initialDocument)
|
|
914
|
+
if (initial.current.initialDocument) {
|
|
871
915
|
engine.load(initial.current.initialDocument);
|
|
916
|
+
// The loaded workbook opens on the sheet it names (an imported file's active tab).
|
|
917
|
+
const opening = getOpeningSheetId(engine.sheets(), documentActiveSheetId(engine.documentFacetsJson()));
|
|
918
|
+
if (opening != null &&
|
|
919
|
+
!userPickedSheetRef.current &&
|
|
920
|
+
opening !== viewRef.current.activeSheetId) {
|
|
921
|
+
viewRef.current = viewOfSheet(opening);
|
|
922
|
+
setActiveSheetId(opening);
|
|
923
|
+
}
|
|
924
|
+
}
|
|
872
925
|
// Push the host's starting locale so the first render is already localized (re-derives + streams
|
|
873
926
|
// every formatted cell). en-US is the engine default, so only a non-default locale needs this.
|
|
874
927
|
if (initial.current.locale && initial.current.locale !== "en-US")
|
|
@@ -880,7 +933,7 @@ export function useEngineCore(options) {
|
|
|
880
933
|
// first scroll event) and read the engine's extents. The load above already ran with `windowRef`
|
|
881
934
|
// null (so its commit folded only structurally — no 1M-cell accumulation); we now read the window.
|
|
882
935
|
if (windowedRef.current) {
|
|
883
|
-
const firstSheet = initial.current.sheets[0]?.sheetId;
|
|
936
|
+
const firstSheet = viewRef.current.activeSheetId ?? initial.current.sheets[0]?.sheetId;
|
|
884
937
|
if (firstSheet != null) {
|
|
885
938
|
windowRef.current = {
|
|
886
939
|
sheetId: firstSheet,
|
|
@@ -1295,7 +1348,11 @@ export function useEngineCore(options) {
|
|
|
1295
1348
|
type: es.type,
|
|
1296
1349
|
chartId: es.chartId,
|
|
1297
1350
|
}));
|
|
1298
|
-
|
|
1351
|
+
// The import opens on the file's active tab (Excel's `<workbookView activeTab>`), else the first
|
|
1352
|
+
// visible sheet.
|
|
1353
|
+
const firstId = getOpeningSheetId(engineSheets, documentActiveSheetId(engine.documentFacetsJson())) ??
|
|
1354
|
+
engineSheets[0]?.sheetId ??
|
|
1355
|
+
viewRef.current.activeSheetId;
|
|
1299
1356
|
windowRef.current = windowedRef.current
|
|
1300
1357
|
? {
|
|
1301
1358
|
sheetId: firstId,
|
|
@@ -1352,7 +1409,7 @@ export function useEngineCore(options) {
|
|
|
1352
1409
|
setHist({ canUndo: engine.canUndo(), canRedo: engine.canRedo() });
|
|
1353
1410
|
// The document was REPLACED: the old workbook's per-sheet cursors are meaningless on the new one
|
|
1354
1411
|
// (imported sheet ids typically collide with the old ids), and the view undo/redo stacks pair with
|
|
1355
|
-
// engine history that no longer exists. Reset all of it; the import opens at
|
|
1412
|
+
// engine history that no longer exists. Reset all of it; the import opens at its opening sheet's A1.
|
|
1356
1413
|
activeCellBySheetIdRef.current = {};
|
|
1357
1414
|
selectionsBySheetIdRef.current = {};
|
|
1358
1415
|
setActiveCellBySheetId({});
|
|
@@ -1521,8 +1578,14 @@ export function useEngineCore(options) {
|
|
|
1521
1578
|
if (cell?.ue?.sv === "'") {
|
|
1522
1579
|
return "";
|
|
1523
1580
|
}
|
|
1581
|
+
// The person's Regional Format: a number renders with the cell's own format in their locale.
|
|
1582
|
+
if (displayLocale && typeof cell?.ev?.nv === "number" && cell.ev.ev == null) {
|
|
1583
|
+
const localized = localizeDisplayValue(cell.ev.nv, getEffectiveFormat(sheetId, rowIndex, columnIndex)?.numberFormat, displayLocale, documentLocale);
|
|
1584
|
+
if (localized !== undefined)
|
|
1585
|
+
return localized;
|
|
1586
|
+
}
|
|
1524
1587
|
return cell?.fv ?? undefined;
|
|
1525
|
-
}, [getCellData]);
|
|
1588
|
+
}, [getCellData, getEffectiveFormat, displayLocale, documentLocale]);
|
|
1526
1589
|
// The cell's effective value as a SCALAR (bool ?? number ?? string) — the shape `<CanvasGrid>` and
|
|
1527
1590
|
// chart series want. Mirrors useSheetProperties' `getEffectiveValue`. Errors → undefined.
|
|
1528
1591
|
const getEffectiveValue = useCallback((sheetId, rowIndex, columnIndex) => {
|
|
@@ -1561,7 +1624,9 @@ export function useEngineCore(options) {
|
|
|
1561
1624
|
}
|
|
1562
1625
|
return ev;
|
|
1563
1626
|
}, [getCellData]);
|
|
1564
|
-
|
|
1627
|
+
// The stored entry in the document's canonical spelling (`1.5`): what range readers hand
|
|
1628
|
+
// programmatic consumers.
|
|
1629
|
+
const getCanonicalUserEnteredValue = useCallback((sheetId, rowIndex, columnIndex) => {
|
|
1565
1630
|
// Ignores holds: the editor and formula bar open on the real value.
|
|
1566
1631
|
const ue = getUnheldCellData(sheetId, rowIndex, columnIndex)?.ue;
|
|
1567
1632
|
if (!ue)
|
|
@@ -1574,6 +1639,16 @@ export function useEngineCore(options) {
|
|
|
1574
1639
|
return ue.bv ? "TRUE" : "FALSE";
|
|
1575
1640
|
return ue.sv ?? undefined;
|
|
1576
1641
|
}, [getUnheldCellData]);
|
|
1642
|
+
// What the editor and formula bar open with: under a Regional Format a number literal shows with
|
|
1643
|
+
// its decimal mark (`1,5`), so committing it unchanged reads back as the same number.
|
|
1644
|
+
const getUserEnteredValue = useCallback((sheetId, rowIndex, columnIndex) => {
|
|
1645
|
+
const value = getCanonicalUserEnteredValue(sheetId, rowIndex, columnIndex);
|
|
1646
|
+
if (!displayLocale || value === undefined)
|
|
1647
|
+
return value;
|
|
1648
|
+
return getUnheldCellData(sheetId, rowIndex, columnIndex)?.ue?.nv != null
|
|
1649
|
+
? localizeEnteredNumber(value, displayLocale)
|
|
1650
|
+
: value;
|
|
1651
|
+
}, [getCanonicalUserEnteredValue, getUnheldCellData, displayLocale]);
|
|
1577
1652
|
const getHyperlink = useCallback((sheetId, rowIndex, columnIndex) => getCellData(sheetId, rowIndex, columnIndex)?.hyperlink, [getCellData]);
|
|
1578
1653
|
// ── Additional cell accessors (mirror useSheetProperties / useSpreadsheetState, typed via SS) ──
|
|
1579
1654
|
// Rich-text runs ride on the cell (`textFormatRuns`, emitted by the engine from the imported
|
|
@@ -1807,10 +1882,10 @@ export function useEngineCore(options) {
|
|
|
1807
1882
|
if ((skipRows && isHiddenRow(sheetId, r)) ||
|
|
1808
1883
|
(skipCols && isHiddenColumn(sheetId, c)))
|
|
1809
1884
|
continue;
|
|
1810
|
-
out.push(
|
|
1885
|
+
out.push(getCanonicalUserEnteredValue(sheetId, r, c));
|
|
1811
1886
|
}
|
|
1812
1887
|
return out;
|
|
1813
|
-
}, [
|
|
1888
|
+
}, [getCanonicalUserEnteredValue, isHiddenRow, isHiddenColumn]);
|
|
1814
1889
|
const getFormulasFromRange = useCallback((sheetRange, strategy) => {
|
|
1815
1890
|
const { sheetId, startRowIndex, startColumnIndex, endRowIndex, endColumnIndex, } = sheetRange;
|
|
1816
1891
|
const { skipRows, skipCols } = hiddenStrategyAxes(strategy);
|
|
@@ -2194,6 +2269,7 @@ export function useEngineCore(options) {
|
|
|
2194
2269
|
const handlers = useMemo(() => createCommandHandlers(onCommand, {
|
|
2195
2270
|
getCustomAutofillLists: () => customAutofillListsRef.current,
|
|
2196
2271
|
getAutoExpandTables: () => autoExpandTablesRef.current,
|
|
2272
|
+
getEntryLocale: () => entryLocaleRef.current,
|
|
2197
2273
|
getAutoCalculatedColumns: () => autoCalculatedColumnsRef.current,
|
|
2198
2274
|
getActiveCell: (sheetId) => viewRef.current.activeSheetId === sheetId
|
|
2199
2275
|
? viewRef.current.activeCell
|
|
@@ -2257,6 +2333,264 @@ export function useEngineCore(options) {
|
|
|
2257
2333
|
});
|
|
2258
2334
|
return plan;
|
|
2259
2335
|
}, [handlers, readRect]);
|
|
2336
|
+
// Data ▸ Consolidate: each source range is read straight from the engine (any sheet — a
|
|
2337
|
+
// windowed host holds only the active sheet's viewport), the shared planner combines them, and
|
|
2338
|
+
// the result block is written at the destination in ONE `batchHistory` — one undo step.
|
|
2339
|
+
const onConsolidate = useCallback((sheetId, destination, options) => {
|
|
2340
|
+
const cells = new Map();
|
|
2341
|
+
for (const reference of options.references) {
|
|
2342
|
+
const { range } = reference;
|
|
2343
|
+
const snapshot = engineRef.current?.readWindow(reference.sheetId, range.startRowIndex, range.startColumnIndex, range.endRowIndex, range.endColumnIndex);
|
|
2344
|
+
const raw = windowRectReader(snapshot?.cells ?? [], "raw");
|
|
2345
|
+
const shown = windowRectReader(snapshot?.cells ?? [], "display");
|
|
2346
|
+
for (let r = range.startRowIndex; r <= range.endRowIndex; r++) {
|
|
2347
|
+
for (let c = range.startColumnIndex; c <= range.endColumnIndex; c++) {
|
|
2348
|
+
const value = raw.getValue(r, c);
|
|
2349
|
+
if (value === undefined)
|
|
2350
|
+
continue;
|
|
2351
|
+
cells.set(`${reference.sheetId}:${r}:${c}`, {
|
|
2352
|
+
value,
|
|
2353
|
+
text: String(shown.getValue(r, c) ?? ""),
|
|
2354
|
+
});
|
|
2355
|
+
}
|
|
2356
|
+
}
|
|
2357
|
+
}
|
|
2358
|
+
const plan = planConsolidate({ sheetId, ...destination }, options, {
|
|
2359
|
+
getValue: (s, r, c) => cells.get(`${s}:${r}:${c}`)?.value,
|
|
2360
|
+
getText: (s, r, c) => cells.get(`${s}:${r}:${c}`)?.text ?? "",
|
|
2361
|
+
getSheetName: (s) => stateSheetsRef.current.find((sheet) => sheet.sheetId === s)?.title,
|
|
2362
|
+
});
|
|
2363
|
+
if (!plan)
|
|
2364
|
+
return undefined;
|
|
2365
|
+
batchHistory(() => handlers.onChangeBatch(sheetId, plan.range, plan.values.map((row) => row.map((v) => (v === null ? "" : v)))));
|
|
2366
|
+
return plan;
|
|
2367
|
+
}, [batchHistory, handlers]);
|
|
2368
|
+
// Data ▸ Outline beyond Group / Ungroup. Every command reads the sheet's folded row/column
|
|
2369
|
+
// metadata and `outlinePr`, then issues ordinary group / ungroup / collapse / expand commands —
|
|
2370
|
+
// inside `batchHistory` where it issues several, so each is one undo step.
|
|
2371
|
+
const outlineOf = useCallback((sheetId) => {
|
|
2372
|
+
const sheet = sheetById.get(sheetId);
|
|
2373
|
+
return {
|
|
2374
|
+
rows: sheet?.rowMetadata,
|
|
2375
|
+
columns: sheet?.columnMetadata,
|
|
2376
|
+
summaryBelow: sheet?.outlinePr?.summaryBelow !== false,
|
|
2377
|
+
summaryRight: sheet?.outlinePr?.summaryRight !== false,
|
|
2378
|
+
};
|
|
2379
|
+
}, [sheetById]);
|
|
2380
|
+
/** Auto Outline: the selection, or the current region around a single cell (Excel). Replaces the
|
|
2381
|
+
* existing outline in that range with the groups its summary formulas imply. */
|
|
2382
|
+
const onAutoOutline = useCallback((sheetId, activeCell, selections) => {
|
|
2383
|
+
const selection = activeRange(activeCell, selections);
|
|
2384
|
+
const range = isSingleCell(selection)
|
|
2385
|
+
? getDataRegion(sheetId, activeCell)
|
|
2386
|
+
: selection;
|
|
2387
|
+
if (!range)
|
|
2388
|
+
return undefined;
|
|
2389
|
+
const outline = outlineOf(sheetId);
|
|
2390
|
+
const plan = planAutoOutline(range, readRect(sheetId, range, "raw"), outline);
|
|
2391
|
+
if (!plan.rows.length && !plan.columns.length) {
|
|
2392
|
+
showError({
|
|
2393
|
+
title: "Auto Outline",
|
|
2394
|
+
message: "Cannot create an outline.",
|
|
2395
|
+
});
|
|
2396
|
+
return plan;
|
|
2397
|
+
}
|
|
2398
|
+
batchHistory(() => {
|
|
2399
|
+
if (plan.rows.length) {
|
|
2400
|
+
for (const indexes of clearOutlineCalls(outline.rows, {
|
|
2401
|
+
start: range.startRowIndex,
|
|
2402
|
+
end: range.endRowIndex,
|
|
2403
|
+
})) {
|
|
2404
|
+
handlers.onUngroupRows(sheetId, indexes);
|
|
2405
|
+
}
|
|
2406
|
+
}
|
|
2407
|
+
if (plan.columns.length) {
|
|
2408
|
+
for (const indexes of clearOutlineCalls(outline.columns, {
|
|
2409
|
+
start: range.startColumnIndex,
|
|
2410
|
+
end: range.endColumnIndex,
|
|
2411
|
+
})) {
|
|
2412
|
+
handlers.onUngroupColumns(sheetId, indexes);
|
|
2413
|
+
}
|
|
2414
|
+
}
|
|
2415
|
+
for (const span of plan.rows) {
|
|
2416
|
+
handlers.onGroupRows(sheetId, indexRun(span.start, span.end));
|
|
2417
|
+
}
|
|
2418
|
+
for (const span of plan.columns) {
|
|
2419
|
+
handlers.onGroupColumns(sheetId, indexRun(span.start, span.end));
|
|
2420
|
+
}
|
|
2421
|
+
});
|
|
2422
|
+
return plan;
|
|
2423
|
+
}, [batchHistory, getDataRegion, handlers, outlineOf, readRect, showError]);
|
|
2424
|
+
/** Clear Outline: ungroup every level — of the selected rows / columns, or of the whole sheet
|
|
2425
|
+
* from a single cell. Hidden detail is shown first so nothing stays hidden without a group. */
|
|
2426
|
+
const onClearOutline = useCallback((sheetId, activeCell, selections) => {
|
|
2427
|
+
const selection = activeRange(activeCell, selections);
|
|
2428
|
+
const whole = isSingleCell(selection);
|
|
2429
|
+
const outline = outlineOf(sheetId);
|
|
2430
|
+
const rowCalls = clearOutlineCalls(outline.rows, whole || !selection
|
|
2431
|
+
? undefined
|
|
2432
|
+
: { start: selection.startRowIndex, end: selection.endRowIndex });
|
|
2433
|
+
const columnCalls = clearOutlineCalls(outline.columns, whole || !selection
|
|
2434
|
+
? undefined
|
|
2435
|
+
: { start: selection.startColumnIndex, end: selection.endColumnIndex });
|
|
2436
|
+
if (!rowCalls.length && !columnCalls.length)
|
|
2437
|
+
return;
|
|
2438
|
+
batchHistory(() => {
|
|
2439
|
+
if (rowCalls.length)
|
|
2440
|
+
handlers.onSetOutlineDepth(sheetId, "row", 8);
|
|
2441
|
+
if (columnCalls.length)
|
|
2442
|
+
handlers.onSetOutlineDepth(sheetId, "column", 8);
|
|
2443
|
+
for (const indexes of rowCalls)
|
|
2444
|
+
handlers.onUngroupRows(sheetId, indexes);
|
|
2445
|
+
for (const indexes of columnCalls)
|
|
2446
|
+
handlers.onUngroupColumns(sheetId, indexes);
|
|
2447
|
+
});
|
|
2448
|
+
}, [batchHistory, handlers, outlineOf]);
|
|
2449
|
+
/** Show Detail / Hide Detail: expand / collapse the group around the active cell — its rows,
|
|
2450
|
+
* unless whole columns are selected or the row is in no group. */
|
|
2451
|
+
const toggleDetail = useCallback((sheetId, activeCell, selections, show) => {
|
|
2452
|
+
const outline = outlineOf(sheetId);
|
|
2453
|
+
const selection = activeRange(activeCell, selections);
|
|
2454
|
+
const wholeColumns = !!selection &&
|
|
2455
|
+
selection.startRowIndex <= 1 &&
|
|
2456
|
+
selection.endRowIndex >= getSheetRowCount(sheetId);
|
|
2457
|
+
const row = wholeColumns
|
|
2458
|
+
? undefined
|
|
2459
|
+
: outlineSummaryAt(outline.rows, activeCell.rowIndex, outline.summaryBelow);
|
|
2460
|
+
if (row !== undefined) {
|
|
2461
|
+
if (show)
|
|
2462
|
+
handlers.onExpandRowGroup(sheetId, row);
|
|
2463
|
+
else
|
|
2464
|
+
handlers.onCollapseRowGroup(sheetId, row);
|
|
2465
|
+
return true;
|
|
2466
|
+
}
|
|
2467
|
+
const column = outlineSummaryAt(outline.columns, activeCell.columnIndex, outline.summaryRight);
|
|
2468
|
+
if (column === undefined)
|
|
2469
|
+
return false;
|
|
2470
|
+
if (show)
|
|
2471
|
+
handlers.onExpandColumnGroup(sheetId, column);
|
|
2472
|
+
else
|
|
2473
|
+
handlers.onCollapseColumnGroup(sheetId, column);
|
|
2474
|
+
return true;
|
|
2475
|
+
}, [getSheetRowCount, handlers, outlineOf]);
|
|
2476
|
+
const onShowDetail = useCallback((sheetId, activeCell, selections) => toggleDetail(sheetId, activeCell, selections, true), [toggleDetail]);
|
|
2477
|
+
const onHideDetail = useCallback((sheetId, activeCell, selections) => toggleDetail(sheetId, activeCell, selections, false), [toggleDetail]);
|
|
2478
|
+
/** Outline settings: summary rows below / columns right and "Automatic styles", stored on the
|
|
2479
|
+
* sheet's `outlinePr` (one `update-sheet`; round-trips through xlsx `<outlinePr>`). */
|
|
2480
|
+
const onUpdateOutlineSettings = useCallback((sheetId, settings) => onCommand({
|
|
2481
|
+
command: "update-sheet",
|
|
2482
|
+
sheetId,
|
|
2483
|
+
sheetSpec: { outlinePr: settings },
|
|
2484
|
+
}), [onCommand]);
|
|
2485
|
+
/** Outline settings ▸ Apply Styles: Excel's RowLevel_1 / ColLevel_1 (bold) and RowLevel_2 /
|
|
2486
|
+
* ColLevel_2 (italic) on the summary rows and columns, over the sheet's data — one undo step. */
|
|
2487
|
+
const onApplyOutlineStyles = useCallback((sheetId) => {
|
|
2488
|
+
const outline = outlineOf(sheetId);
|
|
2489
|
+
const rows = outlineStyleTargets(outline.rows, outline.summaryBelow);
|
|
2490
|
+
const columns = outlineStyleTargets(outline.columns, outline.summaryRight);
|
|
2491
|
+
const lastRow = Math.max(1, getDataRowCount(sheetId) ?? 1);
|
|
2492
|
+
const lastColumn = Math.max(1, getDataColumnCount(sheetId) ?? 1);
|
|
2493
|
+
const apply = (ranges, textFormat) => {
|
|
2494
|
+
if (!ranges.length)
|
|
2495
|
+
return;
|
|
2496
|
+
handlers.onChangeFormatting(sheetId, { rowIndex: ranges[0].startRowIndex, columnIndex: ranges[0].startColumnIndex }, ranges.map((range) => ({ range })), "textFormat", textFormat);
|
|
2497
|
+
};
|
|
2498
|
+
const rowRange = (r) => ({
|
|
2499
|
+
startRowIndex: r,
|
|
2500
|
+
endRowIndex: r,
|
|
2501
|
+
startColumnIndex: 1,
|
|
2502
|
+
endColumnIndex: lastColumn,
|
|
2503
|
+
});
|
|
2504
|
+
const columnRange = (c) => ({
|
|
2505
|
+
startRowIndex: 1,
|
|
2506
|
+
endRowIndex: lastRow,
|
|
2507
|
+
startColumnIndex: c,
|
|
2508
|
+
endColumnIndex: c,
|
|
2509
|
+
});
|
|
2510
|
+
batchHistory(() => {
|
|
2511
|
+
apply([...rows.bold.map(rowRange), ...columns.bold.map(columnRange)], { bold: true });
|
|
2512
|
+
apply([...rows.italic.map(rowRange), ...columns.italic.map(columnRange)], {
|
|
2513
|
+
italic: true,
|
|
2514
|
+
});
|
|
2515
|
+
});
|
|
2516
|
+
}, [batchHistory, getDataColumnCount, getDataRowCount, handlers, outlineOf]);
|
|
2517
|
+
// Data ▸ Flash Fill / Ctrl+E: learn the pattern from the column's examples over ONE engine window
|
|
2518
|
+
// read of the block, then write every filled run inside `batchHistory` — one undo step. When no
|
|
2519
|
+
// pattern explains the examples, Excel's "couldn't find a pattern" message, and nothing is written.
|
|
2520
|
+
const runFlashFill = useCallback((sheetId, target) => {
|
|
2521
|
+
const result = target
|
|
2522
|
+
? planFlashFillInWindow(target, engineRef.current?.readWindow(sheetId, target.region.startRowIndex, target.region.startColumnIndex, target.region.endRowIndex, target.region.endColumnIndex))
|
|
2523
|
+
: { ok: false, reason: "no-examples" };
|
|
2524
|
+
if (!result.ok) {
|
|
2525
|
+
showError({
|
|
2526
|
+
title: FLASH_FILL_NO_PATTERN_TITLE,
|
|
2527
|
+
message: FLASH_FILL_NO_PATTERN_MESSAGE,
|
|
2528
|
+
});
|
|
2529
|
+
return result;
|
|
2530
|
+
}
|
|
2531
|
+
batchHistory(() => {
|
|
2532
|
+
for (const write of result.writes) {
|
|
2533
|
+
handlers.onChangeBatch(sheetId, write.range, write.values);
|
|
2534
|
+
}
|
|
2535
|
+
});
|
|
2536
|
+
return result;
|
|
2537
|
+
}, [batchHistory, handlers, showError]);
|
|
2538
|
+
const onFlashFill = useCallback((sheetId, activeCell, selections) => {
|
|
2539
|
+
const selection = selections?.find((s) => s.range &&
|
|
2540
|
+
activeCell.rowIndex >= s.range.startRowIndex &&
|
|
2541
|
+
activeCell.rowIndex <= s.range.endRowIndex &&
|
|
2542
|
+
activeCell.columnIndex >= s.range.startColumnIndex &&
|
|
2543
|
+
activeCell.columnIndex <= s.range.endColumnIndex)?.range;
|
|
2544
|
+
return runFlashFill(sheetId, flashFillTargetAt(activeCell, getDataRegion(sheetId, activeCell), selection));
|
|
2545
|
+
}, [getDataRegion, runFlashFill]);
|
|
2546
|
+
const onFlashFillSuggest = useCallback((sheetId, cell, text) => {
|
|
2547
|
+
const target = flashFillTargetAt(cell, getDataRegion(sheetId, cell), undefined);
|
|
2548
|
+
if (!target)
|
|
2549
|
+
return undefined;
|
|
2550
|
+
return planFlashFillSuggestion(target, engineRef.current?.readWindow(sheetId, target.region.startRowIndex, target.region.startColumnIndex, target.region.endRowIndex, target.region.endColumnIndex), { rowIndex: cell.rowIndex, text });
|
|
2551
|
+
}, [getDataRegion]);
|
|
2552
|
+
const onAcceptFlashFill = useCallback((sheetId, writes) => {
|
|
2553
|
+
if (!writes.length)
|
|
2554
|
+
return;
|
|
2555
|
+
batchHistory(() => {
|
|
2556
|
+
for (const write of writes) {
|
|
2557
|
+
handlers.onChangeBatch(sheetId, write.range, write.values);
|
|
2558
|
+
}
|
|
2559
|
+
});
|
|
2560
|
+
}, [batchHistory, handlers]);
|
|
2561
|
+
// The fill handle with Auto Fill Options ▸ Flash Fill: the dragged source rows are the examples
|
|
2562
|
+
// and the rest of the drag is filled from them, one column at a time. Every other fill type goes
|
|
2563
|
+
// to the engine's own fill.
|
|
2564
|
+
const onFill = useCallback(async (sheetId, activeCell, fillSelection, selections, fillableSelections, fillType, options) => {
|
|
2565
|
+
if ((fillType ?? options?.fillType) !== "FlashFill") {
|
|
2566
|
+
return handlers.onFill(sheetId, activeCell, fillSelection, selections, fillableSelections, fillType, options);
|
|
2567
|
+
}
|
|
2568
|
+
const source = selections[0]?.range;
|
|
2569
|
+
const fill = fillSelection.range;
|
|
2570
|
+
if (!source || !fill)
|
|
2571
|
+
return;
|
|
2572
|
+
const exampleRowIndexes = [];
|
|
2573
|
+
for (let r = source.startRowIndex; r <= source.endRowIndex; r++)
|
|
2574
|
+
exampleRowIndexes.push(r);
|
|
2575
|
+
const targetRowIndexes = [];
|
|
2576
|
+
for (let r = fill.startRowIndex; r <= fill.endRowIndex; r++) {
|
|
2577
|
+
if (r < source.startRowIndex || r > source.endRowIndex)
|
|
2578
|
+
targetRowIndexes.push(r);
|
|
2579
|
+
}
|
|
2580
|
+
batchHistory(() => {
|
|
2581
|
+
for (let c = fill.startColumnIndex; c <= fill.endColumnIndex; c++) {
|
|
2582
|
+
const region = getDataRegion(sheetId, { rowIndex: source.startRowIndex, columnIndex: c });
|
|
2583
|
+
const target = flashFillTargetAt({ rowIndex: source.startRowIndex, columnIndex: c }, region, undefined);
|
|
2584
|
+
if (!target)
|
|
2585
|
+
continue;
|
|
2586
|
+
target.region.startRowIndex = Math.min(target.region.startRowIndex, fill.startRowIndex);
|
|
2587
|
+
target.region.endRowIndex = Math.max(target.region.endRowIndex, fill.endRowIndex);
|
|
2588
|
+
target.exampleRowIndexes = exampleRowIndexes;
|
|
2589
|
+
target.targetRowIndexes = targetRowIndexes;
|
|
2590
|
+
runFlashFill(sheetId, target);
|
|
2591
|
+
}
|
|
2592
|
+
});
|
|
2593
|
+
}, [batchHistory, getDataRegion, handlers, runFlashFill]);
|
|
2260
2594
|
// Grid-shaped handlers that need view state (the active sheet). Kept as STABLE useCallbacks here (not
|
|
2261
2595
|
// in the pure factory) so `<CanvasGrid>` props don't churn every render — they read `viewRef.current`
|
|
2262
2596
|
// (the live active sheet) instead of closing over `activeSheetId`, so identity never changes. The
|
|
@@ -2993,6 +3327,17 @@ export function useEngineCore(options) {
|
|
|
2993
3327
|
return useMemo(() => ({
|
|
2994
3328
|
...state,
|
|
2995
3329
|
...handlers,
|
|
3330
|
+
onConsolidate,
|
|
3331
|
+
onAutoOutline,
|
|
3332
|
+
onClearOutline,
|
|
3333
|
+
onShowDetail,
|
|
3334
|
+
onHideDetail,
|
|
3335
|
+
onUpdateOutlineSettings,
|
|
3336
|
+
onApplyOutlineStyles,
|
|
3337
|
+
onFill,
|
|
3338
|
+
onFlashFill,
|
|
3339
|
+
onFlashFillSuggest,
|
|
3340
|
+
onAcceptFlashFill,
|
|
2996
3341
|
onRemoveTable,
|
|
2997
3342
|
ready,
|
|
2998
3343
|
engine: engineRef.current,
|
|
@@ -3165,6 +3510,17 @@ export function useEngineCore(options) {
|
|
|
3165
3510
|
formulaLocale,
|
|
3166
3511
|
state,
|
|
3167
3512
|
handlers,
|
|
3513
|
+
onConsolidate,
|
|
3514
|
+
onAutoOutline,
|
|
3515
|
+
onClearOutline,
|
|
3516
|
+
onShowDetail,
|
|
3517
|
+
onHideDetail,
|
|
3518
|
+
onUpdateOutlineSettings,
|
|
3519
|
+
onApplyOutlineStyles,
|
|
3520
|
+
onFill,
|
|
3521
|
+
onFlashFill,
|
|
3522
|
+
onFlashFillSuggest,
|
|
3523
|
+
onAcceptFlashFill,
|
|
3168
3524
|
onRemoveTable,
|
|
3169
3525
|
ready,
|
|
3170
3526
|
hist,
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rowsncolumns/rnc-engine",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"author": "Vinay M",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
6
6
|
"description": "Typed JS/TS interop for the Rust→WASM spreadsheet calc engine (rnc-wasm). Ergonomic JSON-in/out wrapper over the wasm-bindgen surface: applyCommand(Command)→CommitResult, readWindow→WindowSnapshot, and Map<\"sheet!A1\",CellFormat> style folding. Pluggable backend — single-user `SpreadsheetEngine` (default) or collaborative `CollabSpreadsheetEngine` (Yjs sync) — plus the Option-B `useSpreadsheetEngine` React hook.",
|
|
@@ -49,13 +49,13 @@
|
|
|
49
49
|
"react": ">=18"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@rowsncolumns/common-types": ">=5.
|
|
52
|
+
"@rowsncolumns/common-types": ">=5.4.0",
|
|
53
53
|
"@rowsncolumns/dag": ">=7.2.31",
|
|
54
|
-
"@rowsncolumns/grid": ">=13.
|
|
55
|
-
"@rowsncolumns/spreadsheet": ">=10.
|
|
56
|
-
"@rowsncolumns/spreadsheet-state": ">=29.
|
|
54
|
+
"@rowsncolumns/grid": ">=13.4.0",
|
|
55
|
+
"@rowsncolumns/spreadsheet": ">=10.8.0",
|
|
56
|
+
"@rowsncolumns/spreadsheet-state": ">=29.4.0",
|
|
57
57
|
"@rowsncolumns/toolkit": ">=24.5.21",
|
|
58
|
-
"@rowsncolumns/utils": ">=9.
|
|
58
|
+
"@rowsncolumns/utils": ">=9.7.0",
|
|
59
59
|
"fflate": "^0.8.2"
|
|
60
60
|
}
|
|
61
61
|
}
|