File size: 2,576 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { DndContext, DragMoveEvent, useDraggable } from '@dnd-kit/core';
import { useThrottle } from '@wordpress/compose';
import { useRef } from 'react';

interface ResizeHandleProps {
	disabled?: boolean;
	itemId?: string;
	onResize?: ( delta: { width: number; height: number } ) => void;
	onResizeEnd?: () => void;
}

function ResizeHandle( { disabled = false, itemId }: ResizeHandleProps ) {
	const { attributes, listeners, setNodeRef } = useDraggable( {
		id: 'draggable',
		data: { itemId },
	} );

	const resizeHandleStyle = {
		position: 'absolute' as const,
		bottom: '0',
		right: '0',
		width: '0',
		height: '0',
		cursor: 'nwse-resize',
		borderStyle: 'solid',
		borderWidth: '0 0 12px 12px',
		borderColor: 'transparent transparent var(--wp-admin-theme-color, #0087be) transparent',
		zIndex: 1,
		display: disabled ? 'none' : 'block',
	};

	return <div ref={ setNodeRef } style={ resizeHandleStyle } { ...listeners } { ...attributes } />;
}

export default function ResizeHandleWrapper( props: ResizeHandleProps ) {
	const initialAnchorPosition = useRef< DOMRect | null >( null );

	// Throttle the resize event to avoid excessive calls
	// and improve performance during drag operations
	const throttleDelay = 60;
	const throttledResize = useThrottle( ( delta: { width: number; height: number } ) => {
		if ( props.onResize ) {
			props.onResize( delta );
		}
	}, throttleDelay );

	const handleDragStart = ( event: DragMoveEvent ) => {
		// @ts-expect-error I expect this to be always defined.
		initialAnchorPosition.current = event.activatorEvent.target.getBoundingClientRect();
	};

	const handleDragMove = ( event: DragMoveEvent ) => {
		if ( ! initialAnchorPosition.current ) {
			return;
		}
		// @ts-expect-error I expect this to be always defined.
		const currentPosition = event.activatorEvent.target.getBoundingClientRect();
		const deltaX = currentPosition.x - initialAnchorPosition.current.x;
		const deltaY = currentPosition.y - initialAnchorPosition.current.y;
		const anchorDelta = {
			width: deltaX,
			height: deltaY,
		};

		if ( event.active.id === 'draggable' ) {
			const delta = {
				width: event.delta.x - anchorDelta.width,
				height: event.delta.y - anchorDelta.height,
			};

			throttledResize( delta );
		}
	};

	const handleDragEnd = () => {
		initialAnchorPosition.current = null;

		if ( props.onResizeEnd ) {
			props.onResizeEnd();
		}
	};

	return (
		<DndContext
			onDragStart={ handleDragStart }
			onDragMove={ handleDragMove }
			onDragEnd={ handleDragEnd }
		>
			<ResizeHandle { ...props } />
		</DndContext>
	);
}