Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/components/svg-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,30 @@ const SvgWrapper = () => {
isOpen: false,
position: { x: 0, y: 0 },
});

// background moving related
const [offset, setOffset] = React.useState({ x: 0, y: 0 }); // pos relative to the viewport
const [svgViewBoxMinTmp, setSvgViewBoxMinTmp] = React.useState({ x: 0, y: 0 }); // temp copy of svgViewBoxMin

// prevent browser zoom when ctrl + wheel or cmd + wheel
const svgRef = React.useRef<SVGSVGElement>(null);

React.useEffect(() => {
const svgInfo = svgRef.current;
if (!svgInfo) return;

const preventBrowserZoom = (e: WheelEvent) => {
if (e.ctrlKey || e.metaKey) {
e.preventDefault();
}
};

svgInfo.addEventListener('wheel', preventBrowserZoom, { passive: false });
return () => {
svgInfo.removeEventListener('wheel', preventBrowserZoom);
};
}, []);

const handleBackgroundDown = useEvent(async (e: React.PointerEvent<SVGSVGElement>) => {
if (contextMenu.isOpen) {
// close context menu if it's open
Expand Down Expand Up @@ -386,6 +405,7 @@ const SvgWrapper = () => {
viewBox={`${svgViewBoxMin.x} ${svgViewBoxMin.y} ${(width * svgViewBoxZoom) / 100} ${
(height * svgViewBoxZoom) / 100
}`}
ref={svgRef}
onPointerDown={handleBackgroundDown}
onPointerMove={handleBackgroundMove}
onPointerUp={handleBackgroundUp}
Expand Down