| 1 |
import { clamp } from '@agent/lib/util'; |
| 2 |
import { useLayoutEffect, useRef } from '@wordpress/element'; |
| 3 |
|
| 4 |
export const useResizable = ({ |
| 5 |
el, |
| 6 |
open, |
| 7 |
initialSize, |
| 8 |
onResizeEnd, |
| 9 |
minWidth = 250, |
| 10 |
minHeight = 400, |
| 11 |
maxWidth = 500, |
| 12 |
maxHeight = window.innerHeight, |
| 13 |
}) => { |
| 14 |
const start = useRef({ x: 0, y: 0, width: 0, height: 0 }); |
| 15 |
const pointerIdRef = useRef(null); |
| 16 |
|
| 17 |
useLayoutEffect(() => { |
| 18 |
const bg = |
| 19 |
document.getElementById('wpwrap') || |
| 20 |
// TODO: is this on all block themes? |
| 21 |
document.querySelector('.wp-site-blocks'); |
| 22 |
if (!el || !open || !bg) return; |
| 23 |
|
| 24 |
el.style.width = `${initialSize.width}px`; |
| 25 |
el.style.height = `${initialSize.height}px`; |
| 26 |
|
| 27 |
const handle = el.querySelector('[data-extendify-agent-resize]'); |
| 28 |
if (!(handle instanceof HTMLElement)) return; |
| 29 |
|
| 30 |
const onPointerDown = (e) => { |
| 31 |
e.preventDefault(); |
| 32 |
e.stopPropagation(); |
| 33 |
bg.style.pointerEvents = 'none'; |
| 34 |
if (pointerIdRef.current !== null) { |
| 35 |
return; |
| 36 |
} |
| 37 |
pointerIdRef.current = e.pointerId; |
| 38 |
handle.setPointerCapture(e.pointerId); |
| 39 |
start.current = { |
| 40 |
x: e.clientX, |
| 41 |
y: e.clientY, |
| 42 |
width: el.offsetWidth, |
| 43 |
height: el.offsetHeight, |
| 44 |
}; |
| 45 |
document.addEventListener('pointermove', onPointerMove); |
| 46 |
document.addEventListener('pointerup', onPointerUp); |
| 47 |
}; |
| 48 |
|
| 49 |
const onPointerMove = (e) => { |
| 50 |
const rect = el.getBoundingClientRect(); |
| 51 |
const maxAllowedWidth = Math.min(maxWidth, window.innerWidth - rect.left); |
| 52 |
const maxAllowedHeight = Math.min( |
| 53 |
maxHeight, |
| 54 |
window.innerHeight - rect.top, |
| 55 |
); |
| 56 |
const width = clamp( |
| 57 |
start.current.width + (e.clientX - start.current.x), |
| 58 |
minWidth, |
| 59 |
maxAllowedWidth, |
| 60 |
); |
| 61 |
const height = clamp( |
| 62 |
start.current.height + (e.clientY - start.current.y), |
| 63 |
minHeight, |
| 64 |
maxAllowedHeight, |
| 65 |
); |
| 66 |
el.style.width = `${width}px`; |
| 67 |
el.style.height = `${height}px`; |
| 68 |
}; |
| 69 |
|
| 70 |
const onPointerUp = (e) => { |
| 71 |
bg.style.pointerEvents = 'auto'; |
| 72 |
if (pointerIdRef.current !== e.pointerId) { |
| 73 |
return; |
| 74 |
} |
| 75 |
pointerIdRef.current = null; |
| 76 |
handle.releasePointerCapture(e.pointerId); |
| 77 |
document.removeEventListener('pointermove', onPointerMove); |
| 78 |
document.removeEventListener('pointerup', onPointerUp); |
| 79 |
|
| 80 |
const rect = el.getBoundingClientRect(); |
| 81 |
const maxAllowedWidth = Math.min(maxWidth, window.innerWidth - rect.left); |
| 82 |
const maxAllowedHeight = Math.min( |
| 83 |
maxHeight, |
| 84 |
window.innerHeight - rect.top, |
| 85 |
); |
| 86 |
|
| 87 |
const width = clamp( |
| 88 |
start.current.width + (e.clientX - start.current.x), |
| 89 |
minWidth, |
| 90 |
maxAllowedWidth, |
| 91 |
); |
| 92 |
const height = clamp( |
| 93 |
start.current.height + (e.clientY - start.current.y), |
| 94 |
minHeight, |
| 95 |
maxAllowedHeight, |
| 96 |
); |
| 97 |
onResizeEnd(width, height); |
| 98 |
}; |
| 99 |
|
| 100 |
handle.addEventListener('pointerdown', onPointerDown); |
| 101 |
|
| 102 |
return () => { |
| 103 |
handle.removeEventListener('pointerdown', onPointerDown); |
| 104 |
document.removeEventListener('pointermove', onPointerMove); |
| 105 |
document.removeEventListener('pointerup', onPointerUp); |
| 106 |
bg.style.pointerEvents = 'auto'; |
| 107 |
if (pointerIdRef.current !== null) { |
| 108 |
handle.releasePointerCapture(pointerIdRef.current); |
| 109 |
pointerIdRef.current = null; |
| 110 |
} |
| 111 |
}; |
| 112 |
}, [ |
| 113 |
el, |
| 114 |
open, |
| 115 |
initialSize.width, |
| 116 |
initialSize.height, |
| 117 |
onResizeEnd, |
| 118 |
minWidth, |
| 119 |
minHeight, |
| 120 |
maxWidth, |
| 121 |
maxHeight, |
| 122 |
]); |
| 123 |
}; |
| 124 |
|