@@ -4,13 +4,21 @@ import type { OverlayRect } from "./domEditOverlayGeometry";
44import {
55 type CropEdge ,
66 cropRectFromInsets ,
7+ hasCropInsets ,
78 readElementCropFrame ,
89 readElementCropInsets ,
910 resolveCropInsetFromEdgeDrag ,
1011 resolveCropInsetFromMoveDrag ,
1112 rotateDeltaIntoFrame ,
1213} from "./domEditOverlayCrop" ;
1314import { buildInsetClipPathSides , type ClipPathInsetSides } from "./clipPathHelpers" ;
15+ import {
16+ dropElementCropLift ,
17+ isElementCropLifted ,
18+ liftElementCrop ,
19+ readLiftedCrop ,
20+ setLiftedCrop ,
21+ } from "./liftedCrop" ;
1422
1523interface CropGestureState {
1624 edge : CropEdge | "move" ;
@@ -116,41 +124,18 @@ export function DomEditCropHandles({
116124 setState ( cropStateFor ( selection . element ) ) ;
117125 }
118126
119- const hasCrop =
120- state . insets . top > 0 ||
121- state . insets . right > 0 ||
122- state . insets . bottom > 0 ||
123- state . insets . left > 0 ;
127+ // Once lifted, the committed crop is the lift's (a resize rescales it there); a crop drag draws its draft.
128+ const insets = ( ! dragging && readLiftedCrop ( selection . element ) ) || state . insets ;
129+ const hasCrop = hasCropInsets ( insets ) ;
124130
125131 // Lift the clip while the element is selected so the full content shows and the
126- // cropped-away area can be dimmed; restore on deselect. Keyed on the element so
127- // switching selections restores the previous one. Runs after render, so the
128- // state re-sync above still reads the element's real committed clip. Restore
129- // prefers the pre-lift inline value VERBATIM — the rebuilt inset only replaces
130- // it after a crop gesture actually commits, so a mere select+deselect can
131- // never reformat (or drop) what the author wrote. Both refs are written only
132- // by THIS element's lift effect and crop gestures — never derived from render
133- // state, which by cleanup time already describes the NEXT selection (a direct
134- // A→B switch re-syncs state to B before A's cleanup runs).
135- const liftedRef = useRef ( false ) ;
136- const preLiftInlineClipRef = useRef ( "" ) ;
137- // null = no crop gesture committed this selection; "" = committed a crop
138- // removal; anything else = the exact committed clip-path value.
139- const committedClipRef = useRef < string | null > ( null ) ;
132+ // cropped-away area can be dimmed; deselect restores it. Keyed on the element so
133+ // a direct A→B switch restores A's own crop, not the re-synced state's.
140134 useEffect ( ( ) => {
141135 const el = selection . element ;
142136 if ( readElementCropInsets ( el ) === null ) return ;
143- preLiftInlineClipRef . current = el . style . getPropertyValue ( "clip-path" ) ;
144- committedClipRef . current = null ;
145- el . style . setProperty ( "clip-path" , "none" ) ;
146- liftedRef . current = true ;
147- return ( ) => {
148- liftedRef . current = false ;
149- const committed = committedClipRef . current ;
150- const restore = committed !== null ? committed || null : preLiftInlineClipRef . current || null ;
151- if ( restore ) el . style . setProperty ( "clip-path" , restore ) ;
152- else el . style . removeProperty ( "clip-path" ) ;
153- } ;
137+ liftElementCrop ( el ) ;
138+ return ( ) => dropElementCropLift ( el ) ;
154139 } , [ selection . element ] ) ;
155140
156141 // The crop applies in the element's LOCAL frame (clip-path precedes the
@@ -163,7 +148,7 @@ export function DomEditCropHandles({
163148 // Crop rect in FRAME-LOCAL coordinates (origin = frame top-left).
164149 const cropRect = cropRectFromInsets (
165150 { left : 0 , top : 0 , width : frame . width , height : frame . height } ,
166- state . insets ,
151+ insets ,
167152 frame . scaleX ,
168153 frame . scaleY ,
169154 ) ;
@@ -178,14 +163,15 @@ export function DomEditCropHandles({
178163 pointerId : event . pointerId ,
179164 startX : event . clientX ,
180165 startY : event . clientY ,
181- startInsets : state . insets ,
166+ startInsets : insets ,
182167 didMove : false ,
183168 angleDeg : frame . angleDeg ,
184169 scaleX : frame . scaleX ,
185170 scaleY : frame . scaleY ,
186171 } ;
187172 // Clip is already lifted by the selection effect; just flag the drag so the
188173 // rule-of-thirds grid shows.
174+ setState ( ( prev ) => ( { ...prev , insets } ) ) ;
189175 setDragging ( true ) ;
190176 } ;
191177
@@ -228,25 +214,19 @@ export function DomEditCropHandles({
228214 // still restores crop-mode presentation without an unhandled rejection.
229215 const el = selection . element ;
230216 const reLift = ( ) => {
231- if ( liftedRef . current ) el . style . setProperty ( "clip-path" , "none" ) ;
217+ if ( isElementCropLifted ( el ) ) el . style . setProperty ( "clip-path" , "none" ) ;
232218 } ;
233219 const committedValue = buildInsetClipPathSides ( state . insets , state . radius ) ;
234- const cropped =
235- state . insets . top > 0 ||
236- state . insets . right > 0 ||
237- state . insets . bottom > 0 ||
238- state . insets . left > 0 ;
239220 const commit = onStyleCommit ?.( "clip-path" , committedValue ) ;
240221 // handleDomStyleCommit applies the persisted value to the live element
241222 // synchronously before its first await. Restore the crop-mode lift in this
242223 // same turn so the browser never paints that intermediate cropped state.
243224 reLift ( ) ;
244- void Promise . resolve ( commit ) . then ( ( ) => {
245- // Only a landed commit makes the rebuilt inset the restore value; a
246- // failed one keeps restoring the pre-lift clip. Store the value itself —
247- // by deselect time, render state describes the next selection.
248- committedClipRef . current = cropped ? committedValue : "" ;
249- } , reLift ) ;
225+ const revert = setLiftedCrop ( el , hasCropInsets ( state . insets ) ? committedValue : "" ) ;
226+ void Promise . resolve ( commit ) . then ( undefined , ( ) => {
227+ revert ( ) ;
228+ reLift ( ) ;
229+ } ) ;
250230 } ;
251231
252232 const cancelCropGesture = ( event : ReactPointerEvent < HTMLElement > ) => {
0 commit comments