Skip to content

Commit 809e9b0

Browse files
committed
refactor: image editor layer context state
1 parent d73596e commit 809e9b0

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

web/src/components/image-editor/controls/layer-controls.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import type { BlendMode, ImageLayer } from '@/lib/image-editor'
3030
interface LayerControlsProps {
3131
layer: ImageLayer
3232
isEditing: boolean
33+
aspectRatioLocked: boolean
34+
onAspectRatioLockChange: (locked: boolean) => void
3335
visualCropEnabled?: boolean
3436
onUpdate: (updates: Partial<ImageLayer>) => void
3537
onEditLayer: () => void
@@ -50,6 +52,8 @@ const BLEND_MODES: BlendMode[] = [
5052
export function LayerControls({
5153
layer,
5254
isEditing,
55+
aspectRatioLocked,
56+
onAspectRatioLockChange,
5357
visualCropEnabled = false,
5458
onUpdate,
5559
onEditLayer,
@@ -62,9 +66,6 @@ export function LayerControls({
6266
return layer.originalDimensions.width / layer.originalDimensions.height
6367
})
6468

65-
// Default to locked
66-
const [aspectRatioLocked, setAspectRatioLocked] = useState(true)
67-
6869
// Get current width/height from transforms or use original dimensions
6970
const currentWidth = layer.transforms?.width || layer.originalDimensions.width
7071
const currentHeight = layer.transforms?.height || layer.originalDimensions.height
@@ -420,7 +421,7 @@ export function LayerControls({
420421
<Button
421422
variant='outline'
422423
size='sm'
423-
onClick={() => setAspectRatioLocked(!aspectRatioLocked)}
424+
onClick={() => onAspectRatioLockChange(!aspectRatioLocked)}
424425
className='h-8 w-8 p-0'
425426
title={
426427
aspectRatioLocked

web/src/components/image-editor/imagor-editor-controls.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ interface ImageEditorControlsProps {
5151
params: ImageEditorState
5252
selectedLayerId: string | null
5353
editingContext: string | null
54+
layerAspectRatioLocked: boolean
55+
onLayerAspectRatioLockChange: (locked: boolean) => void
5456
openSections: EditorOpenSections
5557
onOpenSectionsChange: (sections: EditorOpenSections) => void
5658
onUpdateParams: (updates: Partial<ImageEditorState>) => void
@@ -131,6 +133,8 @@ export function ImageEditorControls({
131133
params,
132134
selectedLayerId,
133135
editingContext,
136+
layerAspectRatioLocked,
137+
onLayerAspectRatioLockChange,
134138
openSections,
135139
onOpenSectionsChange,
136140
onUpdateParams,
@@ -254,6 +258,8 @@ export function ImageEditorControls({
254258
imagePath={imagePath}
255259
selectedLayerId={selectedLayerId}
256260
editingContext={editingContext}
261+
layerAspectRatioLocked={layerAspectRatioLocked}
262+
onLayerAspectRatioLockChange={onLayerAspectRatioLockChange}
257263
visualCropEnabled={isVisualCropEnabled}
258264
/>
259265
),
@@ -265,6 +271,8 @@ export function ImageEditorControls({
265271
params,
266272
selectedLayerId,
267273
editingContext,
274+
layerAspectRatioLocked,
275+
onLayerAspectRatioLockChange,
268276
onUpdateParams,
269277
onVisualCropToggle,
270278
isVisualCropEnabled,

web/src/components/image-editor/layer-panel.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ interface LayerPanelProps {
3535
imagePath: string
3636
selectedLayerId: string | null
3737
editingContext: string | null
38+
layerAspectRatioLocked: boolean
39+
onLayerAspectRatioLockChange: (locked: boolean) => void
3840
visualCropEnabled?: boolean
3941
}
4042

@@ -179,6 +181,8 @@ export function LayerPanel({
179181
imagePath,
180182
selectedLayerId,
181183
editingContext,
184+
layerAspectRatioLocked,
185+
onLayerAspectRatioLockChange,
182186
visualCropEnabled = false,
183187
}: LayerPanelProps) {
184188
const { t } = useTranslation()
@@ -450,6 +454,8 @@ export function LayerPanel({
450454
<LayerControls
451455
layer={selectedLayer}
452456
isEditing={editingContext === selectedLayer.id}
457+
aspectRatioLocked={layerAspectRatioLocked}
458+
onAspectRatioLockChange={onLayerAspectRatioLockChange}
453459
visualCropEnabled={visualCropEnabled}
454460
onUpdate={(updates) => handleUpdateLayer(selectedLayer.id, updates)}
455461
onEditLayer={() => handleEditLayer(selectedLayer.id)}

web/src/pages/image-editor-page.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,19 @@ export function ImageEditorPage({ galleryKey, imageKey, loaderData }: ImageEdito
9494
const [canRedo, setCanRedo] = useState(false)
9595
const [selectedLayerId, setSelectedLayerId] = useState<string | null>(null)
9696
const [editingContext, setEditingContext] = useState<string | null>(null)
97+
const [layerAspectRatioLocked, setLayerAspectRatioLocked] = useState(true)
9798

9899
// Unsaved changes warning hook
99100
const { showDialog, handleConfirm, handleCancel } = useUnsavedChangesWarning(canUndo)
100101

101102
// Derive visualCropEnabled from params state (single source of truth)
102103
const visualCropEnabled = params.visualCropEnabled ?? false
103104

105+
// Reset aspect ratio lock when switching layers
106+
useEffect(() => {
107+
setLayerAspectRatioLocked(true)
108+
}, [selectedLayerId])
109+
104110
useEffect(() => {
105111
// Initialize editor FIRST (this resets state to defaults)
106112
imageEditor.initialize({
@@ -352,6 +358,8 @@ export function ImageEditorPage({ galleryKey, imageKey, loaderData }: ImageEdito
352358
params={params}
353359
selectedLayerId={selectedLayerId}
354360
editingContext={editingContext}
361+
layerAspectRatioLocked={layerAspectRatioLocked}
362+
onLayerAspectRatioLockChange={setLayerAspectRatioLocked}
355363
openSections={editorOpenSections}
356364
onOpenSectionsChange={handleOpenSectionsChange}
357365
onUpdateParams={updateParams}
@@ -415,6 +423,8 @@ export function ImageEditorPage({ galleryKey, imageKey, loaderData }: ImageEdito
415423
params={params}
416424
selectedLayerId={selectedLayerId}
417425
editingContext={editingContext}
426+
layerAspectRatioLocked={layerAspectRatioLocked}
427+
onLayerAspectRatioLockChange={setLayerAspectRatioLocked}
418428
openSections={editorOpenSections}
419429
onOpenSectionsChange={handleOpenSectionsChange}
420430
onUpdateParams={updateParams}

0 commit comments

Comments
 (0)