Skip to content

Commit d73596e

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

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ interface ImageEditorControlsProps {
5050
imagePath: string
5151
params: ImageEditorState
5252
selectedLayerId: string | null
53+
editingContext: string | null
5354
openSections: EditorOpenSections
5455
onOpenSectionsChange: (sections: EditorOpenSections) => void
5556
onUpdateParams: (updates: Partial<ImageEditorState>) => void
@@ -129,6 +130,7 @@ export function ImageEditorControls({
129130
imagePath,
130131
params,
131132
selectedLayerId,
133+
editingContext,
132134
openSections,
133135
onOpenSectionsChange,
134136
onUpdateParams,
@@ -141,9 +143,6 @@ export function ImageEditorControls({
141143
// Track the active dragged section for DragOverlay
142144
const [activeId, setActiveId] = useState<SectionKey | null>(null)
143145

144-
// Track editing context (which layer is being edited)
145-
const editingContext = imageEditor.getEditingContext()
146-
147146
const sensors = useSensors(
148147
useSensor(PointerSensor, {
149148
activationConstraint: {
@@ -254,6 +253,7 @@ export function ImageEditorControls({
254253
imageEditor={imageEditor}
255254
imagePath={imagePath}
256255
selectedLayerId={selectedLayerId}
256+
editingContext={editingContext}
257257
visualCropEnabled={isVisualCropEnabled}
258258
/>
259259
),
@@ -264,6 +264,7 @@ export function ImageEditorControls({
264264
imagePath,
265265
params,
266266
selectedLayerId,
267+
editingContext,
267268
onUpdateParams,
268269
onVisualCropToggle,
269270
isVisualCropEnabled,

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ interface LayerPanelProps {
3434
imageEditor: ImageEditor
3535
imagePath: string
3636
selectedLayerId: string | null
37+
editingContext: string | null
3738
visualCropEnabled?: boolean
3839
}
3940

@@ -177,13 +178,11 @@ export function LayerPanel({
177178
imageEditor,
178179
imagePath,
179180
selectedLayerId,
181+
editingContext,
180182
visualCropEnabled = false,
181183
}: LayerPanelProps) {
182184
const { t } = useTranslation()
183185
const layers = imageEditor.getLayers()
184-
const [editingContext, setEditingContext] = useState<string | null>(
185-
imageEditor.getEditingContext(),
186-
)
187186
const [activeId, setActiveId] = useState<string | null>(null)
188187
const [filePickerOpen, setFilePickerOpen] = useState(false)
189188
const [isAddingLayer, setIsAddingLayer] = useState(false)
@@ -236,7 +235,6 @@ export function LayerPanel({
236235
// Exit edit mode if deleting the editing layer
237236
if (editingContext === layerId) {
238237
imageEditor.switchContext(null)
239-
setEditingContext(null)
240238
}
241239
// removeLayer will automatically clear selection if needed
242240
imageEditor.removeLayer(layerId)
@@ -255,25 +253,22 @@ export function LayerPanel({
255253

256254
const handleEditLayer = useCallback(
257255
(layerId: string) => {
258-
// Enter edit mode for this layer (switchContext will auto-select)
256+
// Enter edit mode for this layer (switchContext will auto-select and notify via callback)
259257
imageEditor.switchContext(layerId)
260-
setEditingContext(layerId)
261258
},
262259
[imageEditor],
263260
)
264261

265262
const handleExitEditMode = useCallback(() => {
266-
// Exit edit mode, return to base
263+
// Exit edit mode and return to base
267264
imageEditor.switchContext(null)
268-
setEditingContext(null)
269265
}, [imageEditor])
270266

271267
const handleSelectBase = useCallback(() => {
272268
// Deselect all layers and return to base
273269
imageEditor.setSelectedLayerId(null)
274270
if (editingContext !== null) {
275271
imageEditor.switchContext(null)
276-
setEditingContext(null)
277272
}
278273
}, [editingContext, imageEditor])
279274

web/src/lib/image-editor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export interface ImageEditorCallbacks {
117117
onLoadingChange?: (isLoading: boolean) => void
118118
onHistoryChange?: () => void
119119
onSelectedLayerChange?: (layerId: string | null) => void
120+
onEditingContextChange?: (layerId: string | null) => void
120121
}
121122

122123
/**
@@ -1280,6 +1281,9 @@ export class ImageEditor {
12801281
// Switch context
12811282
this.editingContext = layerId
12821283

1284+
// Notify editing context change
1285+
this.callbacks.onEditingContextChange?.(layerId)
1286+
12831287
// Auto-select the layer when entering edit mode
12841288
if (layerId !== null) {
12851289
this.setSelectedLayerId(layerId)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export function ImageEditorPage({ galleryKey, imageKey, loaderData }: ImageEdito
9393
const [canUndo, setCanUndo] = useState(false)
9494
const [canRedo, setCanRedo] = useState(false)
9595
const [selectedLayerId, setSelectedLayerId] = useState<string | null>(null)
96+
const [editingContext, setEditingContext] = useState<string | null>(null)
9697

9798
// Unsaved changes warning hook
9899
const { showDialog, handleConfirm, handleCancel } = useUnsavedChangesWarning(canUndo)
@@ -118,6 +119,7 @@ export function ImageEditorPage({ galleryKey, imageKey, loaderData }: ImageEdito
118119
updateLocationState(encoded)
119120
},
120121
onSelectedLayerChange: setSelectedLayerId,
122+
onEditingContextChange: setEditingContext,
121123
})
122124

123125
// restore state from URL, after callbacks are set
@@ -349,6 +351,7 @@ export function ImageEditorPage({ galleryKey, imageKey, loaderData }: ImageEdito
349351
imagePath={imagePath}
350352
params={params}
351353
selectedLayerId={selectedLayerId}
354+
editingContext={editingContext}
352355
openSections={editorOpenSections}
353356
onOpenSectionsChange={handleOpenSectionsChange}
354357
onUpdateParams={updateParams}
@@ -411,6 +414,7 @@ export function ImageEditorPage({ galleryKey, imageKey, loaderData }: ImageEdito
411414
imagePath={imagePath}
412415
params={params}
413416
selectedLayerId={selectedLayerId}
417+
editingContext={editingContext}
414418
openSections={editorOpenSections}
415419
onOpenSectionsChange={handleOpenSectionsChange}
416420
onUpdateParams={updateParams}

0 commit comments

Comments
 (0)