11import { SpreadsheetChildEnv } from "@odoo/o-spreadsheet-engine/types/spreadsheet_env" ;
2- import { Pixel , Rect } from "../.." ;
2+ import { DOMRectPosition , Pixel , Rect } from "../.." ;
3+ import { zoomCorrectedElementPosition } from "./dom_helpers" ;
34
45export type ZoomedMouseEvent < T extends MouseEvent | PointerEvent > = {
56 clientX : Pixel ;
@@ -13,21 +14,21 @@ export type ZoomedMouseEvent<T extends MouseEvent | PointerEvent> = {
1314 * Return a POJO containing the original event as well as the client position and the client offset
1415 * where the event would target if the spreadsheet was not zoomed
1516 * @param ev unzoomed mouse event
16- * @param originalTargetRect The original target bounding rect the resulting ZoomedMouseEvent offset must refer to
17+ * @param originalTargetPosition The original target bounding rect the resulting ZoomedMouseEvent offset must refer to
1718 * @returns a ZoomedMouseEvent
1819 */
1920export function withZoom < T extends MouseEvent > (
2021 env : SpreadsheetChildEnv ,
2122 ev : T ,
22- originalTargetRect ?: DOMRect | null
23+ originalTargetPosition ?: DOMRectPosition | null
2324) : ZoomedMouseEvent < T > {
2425 const zoomLevel = env . model . getters . getViewportZoomLevel ( ) ;
25- if ( originalTargetRect === undefined ) {
26- originalTargetRect = getZoomTargetBoundingRect ( ev ) ;
26+ if ( originalTargetPosition === undefined ) {
27+ originalTargetPosition = getZoomTargetPosition ( ev , zoomLevel ) ;
2728 }
28- if ( ! originalTargetRect ) return withNoZoom ( ev ) ;
29- const baseOffsetX = ev . clientX - originalTargetRect . left ;
30- const baseOffsetY = ev . clientY - originalTargetRect . top ;
29+ if ( ! originalTargetPosition ) return withNoZoom ( ev ) ;
30+ const baseOffsetX = ev . clientX - originalTargetPosition . left ;
31+ const baseOffsetY = ev . clientY - originalTargetPosition . top ;
3132 const offsetX = baseOffsetX / zoomLevel ;
3233 const offsetY = baseOffsetY / zoomLevel ;
3334 return {
@@ -64,12 +65,10 @@ export function getZoomedRect(zoom: number, rect: Rect): Rect {
6465/**
6566 * Returns the bounding rect of the closest or self element who is targetable by a ZoomedMouseEvent
6667 */
67- function getZoomTargetBoundingRect ( ev : MouseEvent ) : DOMRect | null {
68+ function getZoomTargetPosition ( ev : MouseEvent , zoom : number ) : DOMRectPosition | null {
6869 const target = ev . target ;
6970 if ( ! target || ! ( "classList" in target ) || ! ( target instanceof Element ) ) {
7071 return null ;
7172 }
72- const targetEl = target . classList . contains ( "o-zoomable" ) ? target : target . closest ( ".o-zoomable" ) ;
73- if ( ! targetEl ) return null ;
74- return targetEl . getBoundingClientRect ( ) ;
73+ return zoomCorrectedElementPosition ( target , zoom ) ;
7574}
0 commit comments