From 40cfc4b9606cfc7da687b9f42dbe96acc216cc90 Mon Sep 17 00:00:00 2001 From: Lukas Masuch Date: Fri, 6 Oct 2023 17:48:46 +0200 Subject: [PATCH] Add support for returning bounds of entire scroll area (#788) * Support returning bounds of entire scroll area * Add scale to calculation * Apply PR feedback * Fix check * Additional fix * Fix docs --- packages/core/API.md | 4 ++-- packages/core/src/data-editor/data-editor.tsx | 18 +++++++++++++++++- packages/core/src/data-grid/data-grid.tsx | 6 +++--- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/core/API.md b/packages/core/API.md index f24b41d12..6012f1fe6 100644 --- a/packages/core/API.md +++ b/packages/core/API.md @@ -368,10 +368,10 @@ Causes the data grid to rerender these specific cells. Rerendering a single cell ## getBounds ```ts -getBounds: (col: number, row?: number) => Rectangle | undefined; +getBounds: (col?: number, row?: number) => Rectangle | undefined; ``` -`getBounds` returns the current bounding box of a cell. This does not need to be a currently rendered cell. +`getBounds` returns the current bounding box of a cell. This does not need to be a currently rendered cell. If called with `col` and `row` as undefined, the bounding box of the entire data grid scroll area is returned. --- diff --git a/packages/core/src/data-editor/data-editor.tsx b/packages/core/src/data-editor/data-editor.tsx index 157dccdad..3ae8d71d8 100644 --- a/packages/core/src/data-editor/data-editor.tsx +++ b/packages/core/src/data-editor/data-editor.tsx @@ -3440,7 +3440,23 @@ const DataEditorImpl: React.ForwardRefRenderFunction { - return gridRef.current?.getBounds(col + rowMarkerOffset, row); + + if (canvasRef?.current === null || scrollRef?.current === null) { + return undefined + } + + if (col === undefined && row === undefined) { + // Return the bounds of the entire scroll area: + const rect = canvasRef.current.getBoundingClientRect() + const scale = rect.width / scrollRef.current.clientWidth + return { + x: rect.x - scrollRef.current.scrollLeft * scale, + y: rect.y - scrollRef.current.scrollTop * scale, + width: scrollRef.current.scrollWidth * scale, + height: scrollRef.current.scrollHeight * scale, + }; + } + return gridRef.current?.getBounds( col ?? 0 + rowMarkerOffset, row); }, focus: () => gridRef.current?.focus(), emit: async e => { diff --git a/packages/core/src/data-grid/data-grid.tsx b/packages/core/src/data-grid/data-grid.tsx index 85e3f5686..ff36b07f0 100644 --- a/packages/core/src/data-grid/data-grid.tsx +++ b/packages/core/src/data-grid/data-grid.tsx @@ -283,7 +283,7 @@ type DamageUpdateList = readonly { export interface DataGridRef { focus: () => void; - getBounds: (col: number, row?: number) => Rectangle | undefined; + getBounds: (col?: number, row?: number) => Rectangle | undefined; damage: (cells: DamageUpdateList) => void; } @@ -1499,12 +1499,12 @@ const DataGrid: React.ForwardRefRenderFunction = (p, }); } }, - getBounds: (col: number, row?: number) => { + getBounds: (col?: number, row?: number) => { if (canvasRef === undefined || canvasRef.current === null) { return undefined; } - return getBoundsForItem(canvasRef.current, col, row ?? -1); + return getBoundsForItem(canvasRef.current, col ?? 0, row ?? -1); }, damage, }),