All files / src/widgets/viewport/hooks useViewportControls.ts

0% Statements 0/59
0% Branches 0/16
0% Functions 0/9
0% Lines 0/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121                                                                                                                                                                                                                                                 
import React, { useCallback, useRef, useState } from 'react';
 
interface ViewportChange {
	scale: number;
	offsetX: number;
	offsetY: number;
}
 
export function useViewportControls(
	width: number,
	height: number,
	onChange?: (data: ViewportChange) => void,
) {
	const containerRef = useRef<HTMLDivElement | null>(null);
 
	// внутренние рефы
	const scaleRef = useRef(1);
	const offsetXRef = useRef(0);
	const offsetYRef = useRef(0);
 
	const [isPanning, setIsPanning] = useState(false);
	const [showGrid, setShowGrid] = useState(true);
 
	const emit = useCallback(() => {
		onChange?.({
			scale: scaleRef.current,
			offsetX: offsetXRef.current,
			offsetY: offsetYRef.current,
		});
	}, [onChange]);
 
	const applyTransform = useCallback(() => {
		const el = containerRef.current;
		if (!el) return;
 
		el.style.transform = `translate(${offsetXRef.current}px, ${offsetYRef.current}px) scale(${scaleRef.current})`;
		el.style.transformOrigin = 'center';
 
		emit();
	}, [emit]);
 
	const onMouseDown = useCallback((mouseEvent: React.MouseEvent<HTMLDivElement>) => {
		if (mouseEvent.button !== 1) return;
		mouseEvent.preventDefault();
		setIsPanning(true);
	}, []);
 
	const onMouseMove = useCallback(
		(mouseEvent: React.MouseEvent<HTMLDivElement>) => {
			if (!isPanning) return;
			offsetXRef.current += mouseEvent.movementX;
			offsetYRef.current += mouseEvent.movementY;
			applyTransform();
		},
		[applyTransform, isPanning],
	);
 
	const onMouseUp = useCallback(() => {
		setIsPanning(false);
	}, []);
 
	const onWheel = useCallback(
		(wheelEvent: React.WheelEvent<HTMLDivElement>) => {
			if (!wheelEvent.ctrlKey) return;
			if (wheelEvent.cancelable) wheelEvent.preventDefault();
 
			const zoomFactor = wheelEvent.deltaY > 0 ? 0.9 : 1.1;
			scaleRef.current *= zoomFactor;
 
			applyTransform();
		},
		[applyTransform],
	);
 
	const handleFit = useCallback(() => {
		const el = containerRef.current;
		if (!el) return;
 
		const parent = el.parentElement;
		if (!parent) return;
 
		const rect = parent.getBoundingClientRect();
		const padding = 64;
 
		const availW = rect.width - padding * 2;
		const availH = rect.height - padding * 2;
 
		const scaleX = availW / width;
		const scaleY = availH / height;
 
		scaleRef.current = Math.min(scaleX, scaleY);
		offsetXRef.current = 0;
		offsetYRef.current = 0;
 
		applyTransform();
	}, [applyTransform, width, height]);
 
	const handleReset = useCallback(() => {
		scaleRef.current = 1;
		offsetXRef.current = 0;
		offsetYRef.current = 0;
		applyTransform();
	}, [applyTransform]);
 
	return {
		containerRef,
 
		isPanning,
		showGrid,
		setShowGrid,
 
		onMouseDown,
		onMouseMove,
		onMouseUp,
		onWheel,
 
		handleFit,
		handleReset,
	};
}