Skip to content

Commit 1a04cae

Browse files
authored
fix: reset and unsaved change states in editor (#25588)
1 parent 3ace578 commit 1a04cae

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

web/src/lib/components/asset-viewer/asset-viewer.svelte

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,7 @@
194194
195195
const closeEditor = async () => {
196196
if (editManager.hasAppliedEdits) {
197-
console.log(asset);
198197
const refreshedAsset = await getAssetInfo({ id: asset.id });
199-
console.log(refreshedAsset);
200198
onAssetChange?.(refreshedAsset);
201199
assetViewingStore.setAsset(refreshedAsset);
202200
}

web/src/lib/components/asset-viewer/editor/editor-panel.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<Button
7676
variant="outline"
7777
onclick={() => editManager.resetAllChanges()}
78-
disabled={!editManager.hasChanges}
78+
disabled={!editManager.canReset}
7979
class="self-start"
8080
shape="round"
8181
size="small"

web/src/lib/managers/edit/edit-manager.svelte.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface EditToolManager {
1515
onDeactivate: () => void;
1616
resetAllChanges: () => Promise<void>;
1717
hasChanges: boolean;
18+
canReset: boolean;
1819
edits: EditAction[];
1920
}
2021

@@ -41,19 +42,22 @@ export class EditManager {
4142

4243
currentAsset = $state<AssetResponseDto | null>(null);
4344
selectedTool = $state<EditTool | null>(null);
44-
hasChanges = $derived(this.tools.some((t) => t.manager.hasChanges));
4545

4646
// used to disable multiple confirm dialogs and mouse events while one is open
4747
isShowingConfirmDialog = $state(false);
4848
isApplyingEdits = $state(false);
4949
hasAppliedEdits = $state(false);
5050

51+
hasUnsavedChanges = $derived(this.tools.some((t) => t.manager.hasChanges) && !this.hasAppliedEdits);
52+
canReset = $derived(this.tools.some((t) => t.manager.canReset));
53+
5154
async closeConfirm(): Promise<boolean> {
5255
// Prevent multiple dialogs (usually happens with rapid escape key presses)
5356
if (this.isShowingConfirmDialog) {
5457
return false;
5558
}
56-
if (!this.hasChanges || this.hasAppliedEdits) {
59+
60+
if (!this.hasUnsavedChanges) {
5761
return true;
5862
}
5963

web/src/lib/managers/edit/transform-manager.svelte.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ type RegionConvertParams = {
3838
};
3939

4040
class TransformManager implements EditToolManager {
41-
hasChanges: boolean = $derived.by(() => this.checkEdits());
41+
canReset: boolean = $derived.by(() => this.checkEdits());
42+
hasChanges: boolean = $state(false);
4243

4344
darkenLevel = $state(0.65);
4445
isInteracting = $state(false);
@@ -56,7 +57,7 @@ class TransformManager implements EditToolManager {
5657
cropAspectRatio = $state('free');
5758
originalImageSize = $state<ImageDimensions>({ width: 1000, height: 1000 });
5859
region = $state({ x: 0, y: 0, width: 100, height: 100 });
59-
preveiwImgSize = $derived({
60+
previewImageSize = $derived({
6061
width: this.cropImageSize.width * this.cropImageScale,
6162
height: this.cropImageSize.height * this.cropImageScale,
6263
});
@@ -73,6 +74,7 @@ class TransformManager implements EditToolManager {
7374
edits = $derived.by(() => this.getEdits());
7475

7576
setAspectRatio(aspectRatio: string) {
77+
this.hasChanges = true;
7678
this.cropAspectRatio = aspectRatio;
7779

7880
if (!this.imgElement || !this.cropAreaEl) {
@@ -88,8 +90,8 @@ class TransformManager implements EditToolManager {
8890

8991
checkEdits() {
9092
return (
91-
Math.abs(this.preveiwImgSize.width - this.region.width) > 2 ||
92-
Math.abs(this.preveiwImgSize.height - this.region.height) > 2 ||
93+
Math.abs(this.previewImageSize.width - this.region.width) > 2 ||
94+
Math.abs(this.previewImageSize.height - this.region.height) > 2 ||
9395
this.mirrorHorizontal ||
9496
this.mirrorVertical ||
9597
this.normalizedRotation !== 0
@@ -98,8 +100,8 @@ class TransformManager implements EditToolManager {
98100

99101
checkCropEdits() {
100102
return (
101-
Math.abs(this.preveiwImgSize.width - this.region.width) > 2 ||
102-
Math.abs(this.preveiwImgSize.height - this.region.height) > 2
103+
Math.abs(this.previewImageSize.width - this.region.width) > 2 ||
104+
Math.abs(this.previewImageSize.height - this.region.height) > 2
103105
);
104106
}
105107

@@ -232,9 +234,12 @@ class TransformManager implements EditToolManager {
232234
this.originalImageSize = { width: 1000, height: 1000 };
233235
this.cropImageScale = 1;
234236
this.cropAspectRatio = 'free';
237+
this.hasChanges = false;
235238
}
236239

237240
mirror(axis: 'horizontal' | 'vertical') {
241+
this.hasChanges = true;
242+
238243
if (this.imageRotation % 180 !== 0) {
239244
axis = axis === 'horizontal' ? 'vertical' : 'horizontal';
240245
}
@@ -247,6 +252,8 @@ class TransformManager implements EditToolManager {
247252
}
248253

249254
async rotate(angle: number) {
255+
this.hasChanges = true;
256+
250257
this.imageRotation += angle;
251258
await tick();
252259
this.onImageLoad();
@@ -760,6 +767,7 @@ class TransformManager implements EditToolManager {
760767
return;
761768
}
762769

770+
this.hasChanges = true;
763771
const newX = Math.max(0, Math.min(mouseX - this.dragOffset.x, cropArea.clientWidth - this.region.width));
764772
const newY = Math.max(0, Math.min(mouseY - this.dragOffset.y, cropArea.clientHeight - this.region.height));
765773

@@ -781,6 +789,7 @@ class TransformManager implements EditToolManager {
781789
}
782790
this.fadeOverlay(false);
783791

792+
this.hasChanges = true;
784793
const { x, y, width, height } = crop;
785794
const minSize = 50;
786795
let newRegion = { ...crop };

0 commit comments

Comments
 (0)