@@ -4,13 +4,17 @@ import { type OverlayRect, RESIZE_HANDLE_HIT_PX } from "./domEditOverlayGeometry
44import {
55 type CropEdge ,
66 cropRectFromInsets ,
7+ dropElementCropLift ,
8+ hasCropInsets ,
9+ liftElementCrop ,
710 readElementCropFrame ,
811 readElementCropInsets ,
912 resolveCropInsetFromEdgeDrag ,
1013 resolveCropInsetFromMoveDrag ,
1114 rotateDeltaIntoFrame ,
1215} from "./domEditOverlayCrop" ;
1316import { buildInsetClipPathSides , type ClipPathInsetSides } from "./clipPathHelpers" ;
17+ import { readCropFollowingResize } from "./cropResize" ;
1418
1519interface CropGestureState {
1620 edge : CropEdge | "move" ;
@@ -19,6 +23,7 @@ interface CropGestureState {
1923 startY : number ;
2024 startInsets : ClipPathInsetSides ;
2125 insets : ClipPathInsetSides ;
26+ radius : number ;
2227 /** Element frame captured at gesture start: pointer deltas rotate into it. */
2328 angleDeg : number ;
2429 scaleX : number ;
@@ -83,6 +88,7 @@ function repositionHandleSize(rect: Rect): number {
8388}
8489
8590const EDGES : CropEdge [ ] = [ "top" , "right" , "bottom" , "left" ] ;
91+ const NO_CROP = { top : 0 , right : 0 , bottom : 0 , left : 0 , radius : 0 } ;
8692
8793/**
8894 * Always-on crop, integrated with the selection (no crop "mode"): while a
@@ -92,7 +98,7 @@ const EDGES: CropEdge[] = ["top", "right", "bottom", "left"];
9298 * grid guides framing); release commits `clip-path: inset(...)` through the
9399 * normal style-commit path (one undo step per drag). When cropped, a center
94100 * handle pans the crop window. Corners stay free for the selection's own resize
95- * handle. Leaving the selection restores the committed crop . The clip-path model
101+ * handle. Leaving the selection drops the lift . The element's clip-path
96102 * is the source of truth — nothing here mutates layout.
97103 */
98104export function DomEditCropHandles ( {
@@ -109,54 +115,29 @@ export function DomEditCropHandles({
109115 // clip with an inset (or deletes it).
110116 const cropStateFor = ( element : HTMLElement ) => {
111117 const parsed = readElementCropInsets ( element ) ;
112- const { radius , ... insets } = parsed ?? { top : 0 , right : 0 , bottom : 0 , left : 0 , radius : 0 } ;
113- return { element, croppable : parsed !== null , insets, radius } ;
118+ const { top, right, bottom, left } = parsed ?? NO_CROP ;
119+ return { element, croppable : parsed !== null , insets : { top , right , bottom , left } } ;
114120 } ;
115121 const [ state , setState ] = useState ( ( ) => cropStateFor ( selection . element ) ) ;
116122
117123 // Re-sync when the selection targets a different element (reselect, or an
118- // undo/redo that re-keys the node): read its committed crop before the lift
119- // effect runs. Read inside the guard so a drag's per-frame setState doesn't
120- // re-run getComputedStyle every frame.
124+ // undo/redo that re-keys the node).
121125 if ( state . element !== selection . element ) {
122126 setState ( cropStateFor ( selection . element ) ) ;
123127 }
124128
125- const hasCrop =
126- state . insets . top > 0 ||
127- state . insets . right > 0 ||
128- state . insets . bottom > 0 ||
129- state . insets . left > 0 ;
129+ // The element's clip-path is the crop; state only holds a crop drag's draft.
130+ const committed = readCropFollowingResize ( selection . element ) ?? NO_CROP ;
131+ const insets = dragging ? state . insets : committed ;
132+ const hasCrop = hasCropInsets ( insets ) ;
130133
131134 // Lift the clip while the element is selected so the full content shows and the
132- // cropped-away area can be dimmed; restore on deselect. Keyed on the element so
133- // switching selections restores the previous one. Runs after render, so the
134- // state re-sync above still reads the element's real committed clip. Restore
135- // prefers the pre-lift inline value VERBATIM — the rebuilt inset only replaces
136- // it after a crop gesture actually commits, so a mere select+deselect can
137- // never reformat (or drop) what the author wrote. Both refs are written only
138- // by THIS element's lift effect and crop gestures — never derived from render
139- // state, which by cleanup time already describes the NEXT selection (a direct
140- // A→B switch re-syncs state to B before A's cleanup runs).
141- const liftedRef = useRef ( false ) ;
142- const preLiftInlineClipRef = useRef ( "" ) ;
143- // null = no crop gesture committed this selection; "" = committed a crop
144- // removal; anything else = the exact committed clip-path value.
145- const committedClipRef = useRef < string | null > ( null ) ;
135+ // cropped-away area can be dimmed. Keyed on the element so a direct A→B switch drops A's lift.
146136 useEffect ( ( ) => {
147137 const el = selection . element ;
148138 if ( readElementCropInsets ( el ) === null ) return ;
149- preLiftInlineClipRef . current = el . style . getPropertyValue ( "clip-path" ) ;
150- committedClipRef . current = null ;
151- el . style . setProperty ( "clip-path" , "none" ) ;
152- liftedRef . current = true ;
153- return ( ) => {
154- liftedRef . current = false ;
155- const committed = committedClipRef . current ;
156- const restore = committed !== null ? committed || null : preLiftInlineClipRef . current || null ;
157- if ( restore ) el . style . setProperty ( "clip-path" , restore ) ;
158- else el . style . removeProperty ( "clip-path" ) ;
159- } ;
139+ liftElementCrop ( el ) ;
140+ return ( ) => dropElementCropLift ( el ) ;
160141 } , [ selection . element ] ) ;
161142
162143 // The crop applies in the element's LOCAL frame (clip-path precedes the
@@ -169,7 +150,7 @@ export function DomEditCropHandles({
169150 // Crop rect in FRAME-LOCAL coordinates (origin = frame top-left).
170151 const cropRect = cropRectFromInsets (
171152 { left : 0 , top : 0 , width : frame . width , height : frame . height } ,
172- state . insets ,
153+ insets ,
173154 frame . scaleX ,
174155 frame . scaleY ,
175156 ) ;
@@ -180,19 +161,23 @@ export function DomEditCropHandles({
180161 event . preventDefault ( ) ;
181162 event . stopPropagation ( ) ;
182163 event . currentTarget . setPointerCapture ( event . pointerId ) ;
164+ // Read at press: a resize may have rescaled the crop since the last render.
165+ const pressed = readCropFollowingResize ( selection . element ) ?? NO_CROP ;
183166 gestureRef . current = {
184167 edge,
185168 pointerId : event . pointerId ,
186169 startX : event . clientX ,
187170 startY : event . clientY ,
188- startInsets : state . insets ,
189- insets : state . insets ,
171+ startInsets : pressed ,
172+ insets : pressed ,
173+ radius : pressed . radius ,
190174 angleDeg : frame . angleDeg ,
191175 scaleX : frame . scaleX ,
192176 scaleY : frame . scaleY ,
193177 } ;
194178 // Clip is already lifted by the selection effect; just flag the drag so the
195179 // rule-of-thirds grid shows.
180+ setState ( ( prev ) => ( { ...prev , insets : pressed } ) ) ;
196181 setDragging ( true ) ;
197182 } ;
198183
@@ -234,29 +219,12 @@ export function DomEditCropHandles({
234219 const finishCropGesture = ( event : ReactPointerEvent < HTMLElement > ) => {
235220 const gesture = endCropGesture ( event ) ;
236221 if ( ! gesture ) return ;
237- // Commit to the file. The commit path re-applies the value to the live
238- // element synchronously, so re-lift in the same turn to keep showing the full
239- // content + dim while selected. Re-lift again on rejection so a failed commit
240- // still restores crop-mode presentation without an unhandled rejection.
241- const el = selection . element ;
242- const reLift = ( ) => {
243- if ( liftedRef . current ) el . style . setProperty ( "clip-path" , "none" ) ;
244- } ;
245- const { insets } = gesture ;
246- const committedValue = buildInsetClipPathSides ( insets , state . radius ) ;
247- if ( committedValue === buildInsetClipPathSides ( gesture . startInsets , state . radius ) ) return ;
248- const cropped = insets . top > 0 || insets . right > 0 || insets . bottom > 0 || insets . left > 0 ;
249- const commit = onStyleCommit ?.( "clip-path" , committedValue ) ;
250- // handleDomStyleCommit applies the persisted value to the live element
251- // synchronously before its first await. Restore the crop-mode lift in this
252- // same turn so the browser never paints that intermediate cropped state.
253- reLift ( ) ;
254- void Promise . resolve ( commit ) . then ( ( ) => {
255- // Only a landed commit makes the rebuilt inset the restore value; a
256- // failed one keeps restoring the pre-lift clip. Store the value itself —
257- // by deselect time, render state describes the next selection.
258- committedClipRef . current = cropped ? committedValue : "" ;
259- } , reLift ) ;
222+ // The commit writes the element's clip-path (and puts it back if the save fails);
223+ // the lift keeps it hidden while selected. A drag that ends where it started saves nothing.
224+ const value = buildInsetClipPathSides ( gesture . insets , gesture . radius ) ;
225+ if ( value === buildInsetClipPathSides ( gesture . startInsets , gesture . radius ) ) return ;
226+ const commit = onStyleCommit ?.( "clip-path" , value ) ;
227+ void Promise . resolve ( commit ) . catch ( ( ) => undefined ) ;
260228 } ;
261229
262230 const cancelCropGesture = ( event : ReactPointerEvent < HTMLElement > ) => {
0 commit comments