11import { useEffect , useRef , useState , type PointerEvent as ReactPointerEvent } from "react" ;
22import type { DomEditSelection } from "./domEditing" ;
3- import type { OverlayRect } from "./domEditOverlayGeometry" ;
3+ import { type OverlayRect , RESIZE_HANDLE_HIT_PX } from "./domEditOverlayGeometry" ;
44import {
55 type CropEdge ,
66 cropRectFromInsets ,
@@ -37,47 +37,53 @@ interface DomEditCropHandlesProps {
3737// and centered inside the strip.
3838const EDGE_HIT_THICKNESS = 12 ;
3939const EDGE_HIT_LENGTH = 32 ;
40+ const EDGE_PILL_LENGTH = 24 ;
41+ const REPOSITION_HANDLE_PX = 22 ;
4042
41- /** Place an edge handle's hit strip just OUTSIDE the given crop edge
42- * (translate pushes it fully past the boundary). Keeps the element body free
43- * for moving. Corners stay free for the selection's own resize handles. */
44- function edgeHandlePlacement (
45- edge : CropEdge ,
46- rect : { left : number ; top : number ; width : number ; height : number } ,
47- ) {
48- const cx = rect . left + rect . width / 2 ;
49- const cy = rect . top + rect . height / 2 ;
50- if ( edge === "top" ) {
51- return { left : cx , top : rect . top , transform : "translate(-50%, -100%)" } ;
52- }
53- if ( edge === "bottom" ) {
54- return { left : cx , top : rect . top + rect . height , transform : "translate(-50%, 0)" } ;
55- }
56- if ( edge === "left" ) {
57- return { left : rect . left , top : cy , transform : "translate(-100%, -50%)" } ;
58- }
59- return { left : rect . left + rect . width , top : cy , transform : "translate(0, -50%)" } ;
60- }
43+ type Rect = { left : number ; top : number ; width : number ; height : number } ;
6144
62- const EDGES : CropEdge [ ] = [ "top" , "right" , "bottom" , "left" ] ;
63-
64- /** Hit-strip + pill dimensions for an edge handle, keyed on its orientation. */
65- function edgeHandleMetrics ( vertical : boolean ) : {
66- hitWidth : number ;
67- hitHeight : number ;
68- cursor : string ;
69- pillWidth : number ;
70- pillHeight : number ;
71- } {
45+ /** An edge handle's hit strip, just OUTSIDE the crop edge so the body stays free
46+ * for moving, and short of the corner squares the resize dots own, with its
47+ * cursor and pill. Null when the edge is too short to leave any room. */
48+ function edgeHandleLayout ( edge : CropEdge , rect : Rect ) {
49+ const vertical = edge === "left" || edge === "right" ;
50+ const length = Math . min (
51+ EDGE_HIT_LENGTH ,
52+ ( vertical ? rect . height : rect . width ) - RESIZE_HANDLE_HIT_PX ,
53+ ) ;
54+ if ( length <= 0 ) return null ;
55+ const pill = Math . min ( EDGE_PILL_LENGTH , length ) ;
56+ if ( vertical ) {
57+ return {
58+ hit : {
59+ left : edge === "left" ? rect . left - EDGE_HIT_THICKNESS : rect . left + rect . width ,
60+ top : rect . top + ( rect . height - length ) / 2 ,
61+ width : EDGE_HIT_THICKNESS ,
62+ height : length ,
63+ } ,
64+ cursor : "ew-resize" ,
65+ pill : { width : 4 , height : pill } ,
66+ } ;
67+ }
7268 return {
73- hitWidth : vertical ? EDGE_HIT_THICKNESS : EDGE_HIT_LENGTH ,
74- hitHeight : vertical ? EDGE_HIT_LENGTH : EDGE_HIT_THICKNESS ,
75- cursor : vertical ? "ew-resize" : "ns-resize" ,
76- pillWidth : vertical ? 4 : 24 ,
77- pillHeight : vertical ? 24 : 4 ,
69+ hit : {
70+ left : rect . left + ( rect . width - length ) / 2 ,
71+ top : edge === "top" ? rect . top - EDGE_HIT_THICKNESS : rect . top + rect . height ,
72+ width : length ,
73+ height : EDGE_HIT_THICKNESS ,
74+ } ,
75+ cursor : "ns-resize" ,
76+ pill : { width : pill , height : 4 } ,
7877 } ;
7978}
8079
80+ /** The reposition handle's size: shrunk until its box clears the corner squares. */
81+ function repositionHandleSize ( rect : Rect ) : number {
82+ return Math . min ( REPOSITION_HANDLE_PX , Math . max ( rect . width , rect . height ) - RESIZE_HANDLE_HIT_PX ) ;
83+ }
84+
85+ const EDGES : CropEdge [ ] = [ "top" , "right" , "bottom" , "left" ] ;
86+
8187/**
8288 * Always-on crop, integrated with the selection (no crop "mode"): while a
8389 * croppable element is selected its clip is lifted so the FULL content shows and
@@ -167,6 +173,7 @@ export function DomEditCropHandles({
167173 frame . scaleX ,
168174 frame . scaleY ,
169175 ) ;
176+ const repositionSize = repositionHandleSize ( cropRect ) ;
170177
171178 const startCropGesture = ( edge : CropEdge | "move" , event : ReactPointerEvent < HTMLElement > ) => {
172179 if ( ! onStyleCommit ) return ;
@@ -214,13 +221,19 @@ export function DomEditCropHandles({
214221 setState ( ( prev ) => ( { ...prev , insets : nextInsets } ) ) ;
215222 } ;
216223
217- const finishCropGesture = ( event : ReactPointerEvent < HTMLElement > ) => {
224+ const endCropGesture = ( event : ReactPointerEvent < HTMLElement > ) => {
218225 const gesture = gestureRef . current ;
219- if ( ! gesture || gesture . pointerId !== event . pointerId ) return ;
226+ if ( ! gesture || gesture . pointerId !== event . pointerId ) return null ;
220227 event . preventDefault ( ) ;
221228 event . stopPropagation ( ) ;
222229 gestureRef . current = null ;
223230 setDragging ( false ) ;
231+ return gesture ;
232+ } ;
233+
234+ const finishCropGesture = ( event : ReactPointerEvent < HTMLElement > ) => {
235+ const gesture = endCropGesture ( event ) ;
236+ if ( ! gesture ) return ;
224237 if ( ! gesture . didMove ) return ;
225238 // Commit to the file. The commit path re-applies the value to the live
226239 // element synchronously, so re-lift in the same turn to keep showing the full
@@ -250,12 +263,8 @@ export function DomEditCropHandles({
250263 } ;
251264
252265 const cancelCropGesture = ( event : ReactPointerEvent < HTMLElement > ) => {
253- const gesture = gestureRef . current ;
254- if ( ! gesture || gesture . pointerId !== event . pointerId ) return ;
255- event . preventDefault ( ) ;
256- event . stopPropagation ( ) ;
257- gestureRef . current = null ;
258- setDragging ( false ) ;
266+ const gesture = endCropGesture ( event ) ;
267+ if ( ! gesture ) return ;
259268 // Clip stays lifted; the dim follows the reset insets.
260269 setState ( ( prev ) => ( { ...prev , insets : gesture . startInsets } ) ) ;
261270 } ;
@@ -317,19 +326,18 @@ export function DomEditCropHandles({
317326 { /* Reposition handle — a center circle shown only once cropped. Drag it to
318327 pan the crop window (which part of the element shows) without resizing
319328 the crop. It's a small, discrete target, so a body drag still MOVES. */ }
320- { hasCrop && (
329+ { hasCrop && repositionSize > 0 && (
321330 < button
322331 type = "button"
323332 aria-label = "Reposition crop"
324333 title = "Reposition crop"
325334 data-dom-edit-crop-handle = "true"
326335 className = "pointer-events-auto absolute rounded-full border-2 border-studio-accent bg-studio-accent/30 shadow-[0_0_0_1px_rgba(0,0,0,0.4)]"
327336 style = { {
328- left : cropRect . left + cropRect . width / 2 ,
329- top : cropRect . top + cropRect . height / 2 ,
330- width : 22 ,
331- height : 22 ,
332- transform : "translate(-50%, -50%)" ,
337+ left : cropRect . left + ( cropRect . width - repositionSize ) / 2 ,
338+ top : cropRect . top + ( cropRect . height - repositionSize ) / 2 ,
339+ width : repositionSize ,
340+ height : repositionSize ,
333341 cursor : "move" ,
334342 touchAction : "none" ,
335343 } }
@@ -340,16 +348,15 @@ export function DomEditCropHandles({
340348 />
341349 ) }
342350 { /* Edge handles — drag a side to crop it. Positioned just OUTSIDE the crop
343- edge (via edgeHandlePlacement ) so they never overlap the element body:
351+ edge (via edgeHandleLayout ) so they never overlap the element body:
344352 dragging the body always MOVES, only a handle crops. The pill is
345353 hover-revealed (or shown while dragging / once a crop exists) so the
346354 resting selection chrome stays uncluttered; the hit strip is always
347355 live, and the title names the affordance. */ }
348356 { EDGES . map ( ( edge ) => {
349- const vertical = edge === "left" || edge === "right" ;
350- const place = edgeHandlePlacement ( edge , cropRect ) ;
357+ const layout = edgeHandleLayout ( edge , cropRect ) ;
358+ if ( ! layout ) return null ;
351359 const revealed = dragging || hasCrop || hotEdge === edge ;
352- const m = edgeHandleMetrics ( vertical ) ;
353360 return (
354361 < button
355362 key = { edge }
@@ -359,12 +366,8 @@ export function DomEditCropHandles({
359366 data-dom-edit-crop-handle = "true"
360367 className = "pointer-events-auto absolute flex items-center justify-center border-0 bg-transparent p-0"
361368 style = { {
362- left : place . left ,
363- top : place . top ,
364- width : m . hitWidth ,
365- height : m . hitHeight ,
366- transform : place . transform ,
367- cursor : m . cursor ,
369+ ...layout . hit ,
370+ cursor : layout . cursor ,
368371 touchAction : "none" ,
369372 } }
370373 onPointerEnter = { ( ) => setHotEdge ( edge ) }
@@ -377,8 +380,7 @@ export function DomEditCropHandles({
377380 < span
378381 className = "pointer-events-none rounded-full bg-studio-accent/90 shadow-[0_0_0_1px_rgba(0,0,0,0.4)] transition-opacity duration-100"
379382 style = { {
380- width : m . pillWidth ,
381- height : m . pillHeight ,
383+ ...layout . pill ,
382384 opacity : revealed ? 1 : 0 ,
383385 } }
384386 />
0 commit comments