@file-viewer/renderer-spreadsheet 3.0.0 → 3.0.2

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.
@@ -1,4 +1,4 @@
1
- import { createFileViewerWorkerController, createFileViewerTranslator, createFileViewerZoomChangeEmitter as createZoomChangeEmitter, registerFileViewerZoomProvider, resolveFileViewerColorScheme, resolveFileViewerFitScale, resolveFileViewerRuntimeAssetBaseUrl, resolveFileViewerSpreadsheetWorkerUrl, unregisterFileViewerZoomProvider, } from '@file-viewer/core';
1
+ import { createFileViewerWorkerController, createFileViewerTranslator, createFileViewerZoomChangeEmitter as createZoomChangeEmitter, createEmptyFileViewerSearchState, registerFileViewerSearchProvider, registerFileViewerZoomProvider, resolveFileViewerColorScheme, resolveFileViewerFitScale, resolveFileViewerRuntimeAssetBaseUrl, resolveFileViewerSpreadsheetWorkerUrl, unregisterFileViewerSearchProvider, unregisterFileViewerZoomProvider, } from '@file-viewer/core';
2
2
  import { renderSpreadsheetChart } from './spreadsheet/chartRenderer.js';
3
3
  import { createSpreadsheetImageSourceResolver } from './spreadsheet/imageSource.js';
4
4
  import { buildRows, clampWindowStart, collectWindowStarts, createEmptyVirtualState, DEFAULT_SHEET_DEFAULTS, displayCellKey, getDataKey, INDEX_COLUMN_KEY, markWindowState, ROW_STATE_FIELD, RowState, WINDOW_SIZE, } from './spreadsheet/state.js';
@@ -556,6 +556,7 @@ const renderFileViewerSpreadsheet = async (buffer, target, type, context) => {
556
556
  root.className = 'excel-wrapper';
557
557
  root.dataset.fileViewerSpreadsheetRoot = 'true';
558
558
  root.dataset.spreadsheetTheme = darkMode ? 'dark' : 'light';
559
+ root.dataset.viewerSearchProvider = 'spreadsheet';
559
560
  root.dataset.viewerZoomProvider = 'xlsx';
560
561
  const loading = documentRef.createElement('div');
561
562
  loading.className = 'loading';
@@ -666,15 +667,67 @@ const renderFileViewerSpreadsheet = async (buffer, target, type, context) => {
666
667
  let imageLightboxPreviousFocus = null;
667
668
  let hasNotifiedFirstPaint = false;
668
669
  let hasAppliedDefaultInitialFit = false;
670
+ let searchState = createEmptyFileViewerSearchState();
671
+ let searchMatches = [];
672
+ let searchOptions = {};
673
+ let searchRequestSequence = 0;
674
+ let latestSearchRequest = 0;
675
+ let pendingSearchJump = null;
676
+ let resolveWorkbookReady = null;
677
+ const workbookReady = new Promise(resolve => {
678
+ resolveWorkbookReady = resolve;
679
+ });
669
680
  const resizableColumns = ((_f = (_e = context === null || context === void 0 ? void 0 : context.options) === null || _e === void 0 ? void 0 : _e.spreadsheet) === null || _f === void 0 ? void 0 : _f.resizableColumns) === true;
670
681
  const resizableRows = ((_h = (_g = context === null || context === void 0 ? void 0 : context.options) === null || _g === void 0 ? void 0 : _g.spreadsheet) === null || _h === void 0 ? void 0 : _h.resizableRows) === true;
671
682
  const controller = createFileViewerWorkerController(createSpreadsheetWorkerFactory(target, buffer.byteLength, context), { logErrors: false });
683
+ const pendingSearchRequests = new Map();
672
684
  const getActiveSheet = () => sheets.find(sheet => sheet.id === sheetIndex);
673
685
  const getSheetTabs = () => {
674
686
  const visible = sheets.filter(sheet => !sheet.hidden);
675
687
  return visible.length ? visible : sheets;
676
688
  };
677
689
  const getActiveSheetId = () => { var _a; return sheetIndex !== null && sheetIndex !== void 0 ? sheetIndex : (_a = sheets[0]) === null || _a === void 0 ? void 0 : _a.id; };
690
+ const getColumnLabel = (column) => {
691
+ let value = Math.max(0, column) + 1;
692
+ let label = '';
693
+ while (value > 0) {
694
+ const remainder = (value - 1) % 26;
695
+ label = String.fromCharCode(65 + remainder) + label;
696
+ value = Math.floor((value - 1) / 26);
697
+ }
698
+ return label;
699
+ };
700
+ const createSpreadsheetSearchAnchor = (match, index) => ({
701
+ id: `spreadsheet-cell-${match.sheet}-${match.row}-${match.col}`,
702
+ index,
703
+ line: match.row + 1,
704
+ type: 'block',
705
+ label: `${match.sheetName}!${getColumnLabel(match.col)}${match.row + 1}`,
706
+ text: match.text,
707
+ top: match.row * sheetDefaults.rowHeight,
708
+ left: match.col * sheetDefaults.colWidth,
709
+ width: sheetDefaults.colWidth,
710
+ height: sheetDefaults.rowHeight,
711
+ spreadsheet: {
712
+ sheetId: match.sheet,
713
+ row: match.row,
714
+ col: match.col,
715
+ },
716
+ });
717
+ const setSearchState = (query, matches, currentIndex = matches.length ? 0 : -1) => {
718
+ const normalizedIndex = matches.length
719
+ ? ((currentIndex % matches.length) + matches.length) % matches.length
720
+ : -1;
721
+ searchMatches = matches;
722
+ searchState = {
723
+ query,
724
+ total: matches.length,
725
+ currentIndex: normalizedIndex,
726
+ current: normalizedIndex >= 0 ? matches[normalizedIndex] : null,
727
+ matches,
728
+ };
729
+ return searchState;
730
+ };
678
731
  const getHostHeight = () => tableHost.clientHeight || 0;
679
732
  const showBlockingLoading = () => !errorMessage && !hasInitialWindow && (loadingState || sheetInitializing);
680
733
  const showStreamingLoading = () => !showBlockingLoading() &&
@@ -922,6 +975,17 @@ const renderFileViewerSpreadsheet = async (buffer, target, type, context) => {
922
975
  setLoading(true);
923
976
  controller.emit(type, payload);
924
977
  };
978
+ const requestWorkbookSearch = (query, options) => new Promise((resolve, reject) => {
979
+ const requestId = ++searchRequestSequence;
980
+ pendingSearchRequests.set(requestId, { resolve, reject });
981
+ controller.emit('searchWorkbook', {
982
+ requestId,
983
+ query,
984
+ caseSensitive: options.caseSensitive === true,
985
+ wholeWord: options.wholeWord === true,
986
+ maxMatches: options.maxMatches,
987
+ });
988
+ });
925
989
  const applyRowHeight = (row, baseHeight) => {
926
990
  row.__baseHeight = baseHeight;
927
991
  row._height = scaleRowHeight(baseHeight);
@@ -1540,6 +1604,8 @@ const renderFileViewerSpreadsheet = async (buffer, target, type, context) => {
1540
1604
  if (isFirstWindow) {
1541
1605
  scheduleStableFirstPaintRefresh();
1542
1606
  }
1607
+ requestPendingSearchWindow();
1608
+ flushPendingSearchJump();
1543
1609
  const start = viewportRange.start || meta.startRow;
1544
1610
  const end = Math.max(viewportRange.end, meta.endRow - 1, meta.startRow);
1545
1611
  ensureViewportWindows(start, end);
@@ -1615,6 +1681,66 @@ const renderFileViewerSpreadsheet = async (buffer, target, type, context) => {
1615
1681
  resetViewState();
1616
1682
  requestWindow(0, false);
1617
1683
  };
1684
+ const getSearchRowTop = (rowIndex) => {
1685
+ var _a;
1686
+ let top = scalePx(HEADER_HEIGHT);
1687
+ for (let index = 0; index < rowIndex; index += 1) {
1688
+ const height = (_a = virtualState.rowHeightCache.get(index)) !== null && _a !== void 0 ? _a : virtualState.defaults.rowHeight;
1689
+ top += scaleRowHeight(height);
1690
+ }
1691
+ return top;
1692
+ };
1693
+ function flushPendingSearchJump() {
1694
+ const pending = pendingSearchJump;
1695
+ if (!pending ||
1696
+ pending.sheetId !== getActiveSheetId() ||
1697
+ !virtualState.loadedWindows.has(clampWindowStart(pending.row, virtualState.totalRows)) ||
1698
+ !table) {
1699
+ return;
1700
+ }
1701
+ pendingSearchJump = null;
1702
+ requestAnimationFrame(() => {
1703
+ if (disposed || !table) {
1704
+ return;
1705
+ }
1706
+ const targetTop = Math.max(0, getSearchRowTop(pending.row) - Math.max(0, (tableHost.clientHeight || 0) * 0.35));
1707
+ table.scrollTo(0, targetTop);
1708
+ table.draw();
1709
+ syncImageViewport();
1710
+ scheduleViewportLoad();
1711
+ });
1712
+ }
1713
+ function requestPendingSearchWindow() {
1714
+ const pending = pendingSearchJump;
1715
+ if (!pending || pending.sheetId !== getActiveSheetId() || !virtualState.totalRows) {
1716
+ return;
1717
+ }
1718
+ const windowStart = clampWindowStart(pending.row, virtualState.totalRows);
1719
+ if (virtualState.loadedWindows.has(windowStart)) {
1720
+ flushPendingSearchJump();
1721
+ return;
1722
+ }
1723
+ requestWindow(windowStart, false);
1724
+ }
1725
+ function jumpToSpreadsheetSearchMatch(match) {
1726
+ var _a, _b;
1727
+ const spreadsheet = (_a = match === null || match === void 0 ? void 0 : match.anchor) === null || _a === void 0 ? void 0 : _a.spreadsheet;
1728
+ if (!spreadsheet) {
1729
+ return;
1730
+ }
1731
+ pendingSearchJump = spreadsheet;
1732
+ if (spreadsheet.sheetId !== getActiveSheetId()) {
1733
+ cacheCurrentSheetState();
1734
+ sheetIndex = spreadsheet.sheetId;
1735
+ renderChrome();
1736
+ startSheetSession();
1737
+ scrollActiveSheetIntoView();
1738
+ const view = getTargetWindow(target);
1739
+ (_b = view === null || view === void 0 ? void 0 : view.queueMicrotask) === null || _b === void 0 ? void 0 : _b.call(view, requestPendingSearchWindow);
1740
+ return;
1741
+ }
1742
+ requestPendingSearchWindow();
1743
+ }
1618
1744
  function handleSheet(index) {
1619
1745
  if (sheetIndex === index) {
1620
1746
  scrollActiveSheetIntoView();
@@ -1626,6 +1752,68 @@ const renderFileViewerSpreadsheet = async (buffer, target, type, context) => {
1626
1752
  startSheetSession();
1627
1753
  scrollActiveSheetIntoView();
1628
1754
  }
1755
+ controller.onWorkerEvent('searchWorkbook', ({ requestId, matches }) => {
1756
+ if (typeof requestId !== 'number') {
1757
+ return;
1758
+ }
1759
+ const pending = pendingSearchRequests.get(requestId);
1760
+ if (!pending) {
1761
+ return;
1762
+ }
1763
+ pendingSearchRequests.delete(requestId);
1764
+ pending.resolve(Array.isArray(matches) ? matches : []);
1765
+ });
1766
+ const searchProvider = {
1767
+ async search(query, options = {}) {
1768
+ const normalizedQuery = query.replace(/\s+/g, ' ').trim();
1769
+ searchOptions = options;
1770
+ const requestNumber = ++latestSearchRequest;
1771
+ if (!normalizedQuery) {
1772
+ pendingSearchJump = null;
1773
+ return setSearchState('', []);
1774
+ }
1775
+ await workbookReady;
1776
+ const matches = await requestWorkbookSearch(normalizedQuery, searchOptions);
1777
+ if (requestNumber !== latestSearchRequest || disposed) {
1778
+ return searchState;
1779
+ }
1780
+ const nextMatches = matches.map((match, index) => ({
1781
+ id: `spreadsheet-search-${match.sheet}-${match.row}-${match.col}-${index}`,
1782
+ index,
1783
+ text: match.matchText,
1784
+ anchor: createSpreadsheetSearchAnchor(match, index),
1785
+ line: match.row + 1,
1786
+ }));
1787
+ const nextState = setSearchState(normalizedQuery, nextMatches);
1788
+ jumpToSpreadsheetSearchMatch(nextState.current);
1789
+ return nextState;
1790
+ },
1791
+ next() {
1792
+ if (!searchMatches.length) {
1793
+ return searchState;
1794
+ }
1795
+ const nextState = setSearchState(searchState.query, searchMatches, searchState.currentIndex + 1);
1796
+ jumpToSpreadsheetSearchMatch(nextState.current);
1797
+ return nextState;
1798
+ },
1799
+ previous() {
1800
+ if (!searchMatches.length) {
1801
+ return searchState;
1802
+ }
1803
+ const nextState = setSearchState(searchState.query, searchMatches, searchState.currentIndex - 1);
1804
+ jumpToSpreadsheetSearchMatch(nextState.current);
1805
+ return nextState;
1806
+ },
1807
+ clear() {
1808
+ latestSearchRequest += 1;
1809
+ pendingSearchJump = null;
1810
+ return setSearchState('', []);
1811
+ },
1812
+ getState() {
1813
+ return searchState;
1814
+ },
1815
+ };
1816
+ registerFileViewerSearchProvider(root, searchProvider);
1629
1817
  const emitParseWorkbook = () => {
1630
1818
  var _a, _b;
1631
1819
  emitWorker('parseWorkbook', {
@@ -1637,6 +1825,8 @@ const renderFileViewerSpreadsheet = async (buffer, target, type, context) => {
1637
1825
  };
1638
1826
  controller.onWorkerEvent('sheets', ({ sheets: list }) => {
1639
1827
  sheets = list;
1828
+ resolveWorkbookReady === null || resolveWorkbookReady === void 0 ? void 0 : resolveWorkbookReady();
1829
+ resolveWorkbookReady = null;
1640
1830
  const firstSheet = list.find((sheet) => !sheet.hidden) || list[0];
1641
1831
  if (firstSheet) {
1642
1832
  sheetIndex = firstSheet.id;
@@ -1656,6 +1846,8 @@ const renderFileViewerSpreadsheet = async (buffer, target, type, context) => {
1656
1846
  applyVirtualWindow(ws);
1657
1847
  });
1658
1848
  controller.onWorkerEvent('parseError', ({ sessionId, startRow, message }) => {
1849
+ resolveWorkbookReady === null || resolveWorkbookReady === void 0 ? void 0 : resolveWorkbookReady();
1850
+ resolveWorkbookReady = null;
1659
1851
  if (sessionId && sessionId !== sheetSessionId) {
1660
1852
  return;
1661
1853
  }
@@ -1743,6 +1935,14 @@ const renderFileViewerSpreadsheet = async (buffer, target, type, context) => {
1743
1935
  clearScheduledLayoutRefresh();
1744
1936
  resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.disconnect();
1745
1937
  resizeObserver = null;
1938
+ pendingSearchJump = null;
1939
+ pendingSearchRequests.forEach(({ reject }) => {
1940
+ reject(new Error('Spreadsheet renderer was unmounted before search completed.'));
1941
+ });
1942
+ pendingSearchRequests.clear();
1943
+ resolveWorkbookReady === null || resolveWorkbookReady === void 0 ? void 0 : resolveWorkbookReady();
1944
+ resolveWorkbookReady = null;
1945
+ unregisterFileViewerSearchProvider(root);
1746
1946
  unregisterFileViewerZoomProvider(root);
1747
1947
  controller.destroy();
1748
1948
  table === null || table === void 0 ? void 0 : table.destroy();