diff --git a/src/Squidex/app/features/content/pages/content/content-page.component.ts b/src/Squidex/app/features/content/pages/content/content-page.component.ts index 0b4f99e85..ae4909c9c 100644 --- a/src/Squidex/app/features/content/pages/content/content-page.component.ts +++ b/src/Squidex/app/features/content/pages/content/content-page.component.ts @@ -120,7 +120,7 @@ export class ContentPageComponent extends ResourceOwner implements CanComponentD this.loadContent(this.content.dataDraft, true); } - if (autosaved) { + if (autosaved && this.isOtherContent(content) && this.contentForm.hasChanges(autosaved)) { this.dialogs.confirm('Unsaved changes', 'You have unsaved changes. Do you want to load them now?') .subscribe(shouldLoad => { if (shouldLoad) { @@ -148,6 +148,10 @@ export class ContentPageComponent extends ResourceOwner implements CanComponentD })); } + private isOtherContent(content: ContentDto | null | undefined) { + return !this.content || !content || content.id !== this.content.id; + } + public canDeactivate(): Observable { if (!this.contentForm.hasChanged()) { return of(true); diff --git a/src/Squidex/app/shared/state/contents.forms.spec.ts b/src/Squidex/app/shared/state/contents.forms.spec.ts index 2c4695159..882caa758 100644 --- a/src/Squidex/app/shared/state/contents.forms.spec.ts +++ b/src/Squidex/app/shared/state/contents.forms.spec.ts @@ -671,6 +671,26 @@ describe('ContentForm', () => { } }); + it('should return true if new value is not equal to current value', () => { + const simpleForm = createForm([ + createField({ id: 1, properties: createProperties('String'), partitioning: 'invariant' }) + ]); + + const hasChanged = simpleForm.hasChanges({ field1: { iv: 'other' }}); + + expect(hasChanged).toBeTruthy(); + }); + + it('should return false if new value is same as current value', () => { + const simpleForm = createForm([ + createField({ id: 1, properties: createProperties('String'), partitioning: 'invariant' }) + ]); + + const hasChanged = simpleForm.hasChanges({ field1: { iv: null }}); + + expect(hasChanged).toBeFalsy(); + }); + describe('for new content', () => { let simpleForm: EditContentForm; diff --git a/src/Squidex/app/shared/state/contents.forms.ts b/src/Squidex/app/shared/state/contents.forms.ts index ff54cefe0..4fa104e40 100644 --- a/src/Squidex/app/shared/state/contents.forms.ts +++ b/src/Squidex/app/shared/state/contents.forms.ts @@ -462,6 +462,12 @@ export class EditContentForm extends Form { return !Types.jsJsonEquals(this.initialData, currentValue); } + public hasChanges(changes: any) { + const currentValue = this.form.getRawValue(); + + return !Types.jsJsonEquals(changes, currentValue); + } + public arrayItemRemove(field: RootFieldDto, language: AppLanguageDto, index: number) { const partitionForm = this.findArrayItemForm(field, language);