11import { useLayoutEffect } from "react" ;
22import { ChevronDown , ChevronLeft , ChevronRight , ChevronUp , Minus , Plus } from "lucide-react" ;
33import { useSelector } from "react-redux" ;
4+ import { default as debounce } from "lodash/debounce" ;
45import { twMerge } from "tailwind-merge" ;
56import { dataAttributes , ids , selectors } from "@/constants" ;
67import { useSkipFirstRender } from "@/hooks" ;
@@ -9,14 +10,18 @@ import { d3Extended } from "@/utils";
910import { Tool } from "../toolbar/data" ;
1011import { showPostOffsetElements , showPreOffsetElements } from "./elements" ;
1112
12- function handleZoom ( e ) {
13- const workspace = d3Extended . select ( selectors . workspaceGroup ) ;
13+ const handleElementVisibility = debounce ( ( workspace , k ) => {
1414 const visibilityOffset = + workspace . attr ( dataAttributes . visibilityOffset ) || 0 ;
15- if ( e . transform . k * 1.1 < visibilityOffset ) {
15+ if ( k * 1.1 < visibilityOffset ) {
1616 showPreOffsetElements ( ) ;
1717 } else {
1818 showPostOffsetElements ( ) ;
1919 }
20+ } , 25 ) ;
21+
22+ function handleZoom ( e ) {
23+ const workspace = d3Extended . select ( selectors . workspaceGroup ) ;
24+ handleElementVisibility ( workspace , e . transform . k ) ;
2025 workspace . attr ( "transform" , e . transform ) ;
2126}
2227
@@ -58,12 +63,18 @@ export const panAndZoom = ({ k, x, y }) => {
5863 d3Extended . selectById ( ids . workspace ) . call ( zoom . transform , d3Extended . zoomIdentity . translate ( x , y ) . scale ( k ) ) ;
5964} ;
6065
61- export const panAndZoomWithTransition = ( { k, x, y } ) => {
66+ export const panAndZoomToArea = ( { k, x, y } ) => {
67+ const transform = d3Extended . zoomTransform ( document . querySelector ( selectors . workspaceGroup ) ) ;
68+ const { height : workspaceheight , width : workspaceWidth } = d3Extended . selectionBounds (
69+ d3Extended . selectById ( ids . workspace )
70+ ) ;
71+ const newX = x * ( x > workspaceWidth / 2 ? - 1 : 1 ) * transform . k * 0.75 ;
72+ const newY = ( workspaceheight - y ) * ( y > workspaceheight / 2 ? - 1 : 1 ) ;
6273 d3Extended
6374 . selectById ( ids . workspace )
6475 . transition ( )
6576 . duration ( 1000 )
66- . call ( zoom . transform , d3Extended . zoomIdentity . translate ( x , y ) . scale ( k ) ) ;
77+ . call ( zoom . transform , d3Extended . zoomIdentity . translate ( newX , newY ) . scale ( k ) ) ;
6778} ;
6879
6980const panHandleClasses =
@@ -122,7 +133,7 @@ const Zoom = (props: Pick<ISTKProps, "mode" | "styles" | "options">) => {
122133 < div
123134 id = { ids . panControls }
124135 className = { twMerge (
125- "absolute bottom-[7.5rem] left-10 flex h-20 w-20 bg-white rounded-full border border-black/20 splash after:bg-black/5" ,
136+ "absolute bottom-[7.5rem] left-10 h-20 w-20 bg-white rounded-full border border-black/20 after:bg-black/5" ,
126137 props . mode === "user" && "bottom-[5.25rem] left-9 md:bottom-[6rem] md:left-12" ,
127138 panStyles ?. root ?. className
128139 ) }
0 commit comments