فهرست منبع

Canvas should fit into view on click Center icon at toolbar

dogukanuhn 2 سال پیش
والد
کامیت
23a69d78c8
2فایلهای تغییر یافته به همراه16 افزوده شده و 2 حذف شده
  1. 9 0
      src/components/Graph/index.tsx
  2. 7 2
      src/hooks/store/useConfig.tsx

+ 9 - 0
src/components/Graph/index.tsx

@@ -43,11 +43,15 @@ const StyledEditorWrapper = styled.div<{ isWidget: boolean }>`
 const GraphComponent = ({ isWidget, openModal, setSelectedNode }: LayoutProps) => {
 const GraphComponent = ({ isWidget, openModal, setSelectedNode }: LayoutProps) => {
   const setGraphValue = useGraph(state => state.setGraphValue);
   const setGraphValue = useGraph(state => state.setGraphValue);
   const setConfig = useConfig(state => state.setConfig);
   const setConfig = useConfig(state => state.setConfig);
+  const canvasRef = React.useRef(null);
+  const setCanvasRef = useConfig(state => state.setCanvasRef);
   const loading = useGraph(state => state.loading);
   const loading = useGraph(state => state.loading);
   const layout = useConfig(state => state.layout);
   const layout = useConfig(state => state.layout);
   const nodes = useGraph(state => state.nodes);
   const nodes = useGraph(state => state.nodes);
   const edges = useGraph(state => state.edges);
   const edges = useGraph(state => state.edges);
 
 
+  
+
   const [size, setSize] = React.useState({
   const [size, setSize] = React.useState({
     width: 2000,
     width: 2000,
     height: 2000,
     height: 2000,
@@ -87,6 +91,10 @@ const GraphComponent = ({ isWidget, openModal, setSelectedNode }: LayoutProps) =
     if (input) input.blur();
     if (input) input.blur();
   }, []);
   }, []);
 
 
+  React.useEffect(()=>{
+    setCanvasRef(canvasRef)
+  })
+
   if (nodes.length > 8_000) return <ErrorView />;
   if (nodes.length > 8_000) return <ErrorView />;
 
 
   return (
   return (
@@ -127,6 +135,7 @@ const GraphComponent = ({ isWidget, openModal, setSelectedNode }: LayoutProps) =
             readonly={true}
             readonly={true}
             dragEdge={null}
             dragEdge={null}
             dragNode={null}
             dragNode={null}
+            ref={canvasRef}
             fit={true}
             fit={true}
             node={props => <CustomNode {...props} onClick={handleNodeClick} />}
             node={props => <CustomNode {...props} onClick={handleNodeClick} />}
             edge={props => (
             edge={props => (

+ 7 - 2
src/hooks/store/useConfig.tsx

@@ -2,9 +2,10 @@ import { ReactZoomPanPinchRef } from "react-zoom-pan-pinch";
 import { CanvasDirection } from "reaflow";
 import { CanvasDirection } from "reaflow";
 import { defaultJson } from "src/constants/data";
 import { defaultJson } from "src/constants/data";
 import create from "zustand";
 import create from "zustand";
-
+import {MutableRefObject} from "react";
 interface ConfigActions {
 interface ConfigActions {
   setJson: (json: string) => void;
   setJson: (json: string) => void;
+  setCanvasRef: (canvas: MutableRefObject<HTMLCanvasElement | null> ) => void;
   setConfig: (key: keyof Config, value: unknown) => void;
   setConfig: (key: keyof Config, value: unknown) => void;
   getJson: () => string;
   getJson: () => string;
   zoomIn: () => void;
   zoomIn: () => void;
@@ -15,6 +16,7 @@ interface ConfigActions {
 const initialStates = {
 const initialStates = {
   json: defaultJson,
   json: defaultJson,
   cursorMode: "move" as "move" | "navigation",
   cursorMode: "move" as "move" | "navigation",
+  canvas: undefined as MutableRefObject<HTMLCanvasElement | null> | undefined,
   layout: "RIGHT" as CanvasDirection,
   layout: "RIGHT" as CanvasDirection,
   expand: true,
   expand: true,
   hideEditor: false,
   hideEditor: false,
@@ -29,6 +31,7 @@ const useConfig = create<Config & ConfigActions>()((set, get) => ({
   ...initialStates,
   ...initialStates,
   getJson: () => get().json,
   getJson: () => get().json,
   setJson: (json: string) => set({ json }),
   setJson: (json: string) => set({ json }),
+  setCanvasRef: (canvas: MutableRefObject<HTMLCanvasElement | null> ) => set({ canvas }),
   zoomIn: () => {
   zoomIn: () => {
     const zoomPanPinch = get().zoomPanPinch;
     const zoomPanPinch = get().zoomPanPinch;
     if (zoomPanPinch) {
     if (zoomPanPinch) {
@@ -51,7 +54,9 @@ const useConfig = create<Config & ConfigActions>()((set, get) => ({
   },
   },
   centerView: () => {
   centerView: () => {
     const zoomPanPinch = get().zoomPanPinch;
     const zoomPanPinch = get().zoomPanPinch;
-    if (zoomPanPinch) zoomPanPinch.centerView(0.4);
+    const canvas = get().canvas
+    if(zoomPanPinch && canvas && canvas.current)
+      zoomPanPinch.zoomToElement(canvas.current["containerRef"].current)
   },
   },
   setConfig: (setting: keyof Config, value: unknown) => set({ [setting]: value }),
   setConfig: (setting: keyof Config, value: unknown) => set({ [setting]: value }),
 }));
 }));