import * as React from 'react'; import { ReactSVGPanZoom, type Tool, TOOL_PAN, type Value, ALIGN_CENTER } from 'react-svg-pan-zoom'; import { Container } from './Elements/Container'; import { SelectorContainer } from './Elements/SelectorContainer/SelectorContainer'; import { MAX_FRAMERATE } from '../../utils/default'; import { SymbolLayer } from './Elements/SymbolLayer'; import { DimensionLayer } from './Elements/DimensionLayer'; import { SelectorSymbol } from './Elements/SelectorSymbol/SelectorSymbol'; import { type IToolbarProps, Toolbar } from './SVGReactPanZoom/ui-toolbar/toolbar'; import { type DrawParams } from '../Viewer/Viewer'; interface ISVGProps { className?: string viewerWidth: number viewerHeight: number width: number height: number drawParams: DrawParams selectContainer: (containerId: string) => void } export enum SelectorMode { Nothing, Containers, Symbols } export const ID = 'svg'; export function SVG(props: ISVGProps): JSX.Element { const { mainContainer, selectorMode, selectedContainer, selectedSymbol, containers, symbols } = props.drawParams; const [tool, setTool] = React.useState(TOOL_PAN); // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const [value, setValue] = React.useState({} as Value); const [scale, setScale] = React.useState(value.a ?? 1); const svgViewer = React.useRef(null); // Framerate limiter const delta = React.useRef(0); const timer = React.useRef(performance.now()); const renderCounter = React.useRef(0); // Debug: FPS counter // const startTimer = React.useRef(Date.now()); // console.log(renderCounter.current / ((Date.now() - startTimer.current) / 1000)); UseFitOnce(svgViewer, props.width, props.height); const xmlns = ''; const properties = { width: props.width, height: props.height, xmlns }; const children: React.ReactNode | React.ReactNode[] = ; function Selector(): JSX.Element { switch (selectorMode) { case SelectorMode.Containers: return ; case SelectorMode.Symbols: return ; default: return <>; } } return (
{ // Framerate limiter const newTimer = performance.now(); delta.current += (newTimer - timer.current) / 1000; timer.current = newTimer; if (delta.current <= (1 / MAX_FRAMERATE)) { return; } renderCounter.current = renderCounter.current + 1; delta.current = delta.current % (1 / MAX_FRAMERATE); setValue(value); }} onZoom={(event: unknown) => { const value = event as Value; setScale(value.a); }} onDoubleClick={() => { svgViewer?.current?.setPointOnViewerCenter(props.width / 2, props.height / 2, 0.8); }} background={'#ffffff'} defaultTool='pan' miniatureProps={{ position: 'left', background: '#616264', width: 120, height: 120 }} customToolbar={(props: IToolbarProps) => ( )} > {children}
); } function UseFitOnce(svgViewer: React.RefObject, width: number, height: number): void { React.useCallback(() => { // TODO: Fix this svgViewer?.current?.setPointOnViewerCenter(width / 2, height / 2, 0.8); }, [svgViewer, width, height]); }