Skip to content

Commit 973bfdd

Browse files
committed
fix: reduced the sensitivity of view modification checks
1 parent bb6e6b2 commit 973bfdd

3 files changed

Lines changed: 44 additions & 13 deletions

File tree

packages/studio-web/src/app/editor/editor.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ <h2 i18n="Title for upload box" class="title">
4242
Offline-HTML file included in the Web Bundle download format.
4343
</p>
4444
<input
45-
#textInputElement
45+
#rasFileUpload
4646
(change)="onRasFileSelected($event)"
4747
class="form-control"
4848
name="text"

packages/studio-web/src/app/editor/editor.component.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ import { WcStylingComponent } from "../shared/wc-styling/wc-styling.component";
3939
styleUrls: ["./editor.component.sass"],
4040
standalone: false,
4141
})
42-
export class EditorComponent implements OnDestroy, OnInit, AfterViewInit {
42+
export class EditorComponent implements OnDestroy, AfterViewInit {
4343
@ViewChild("wavesurferContainer") wavesurferContainer!: ElementRef;
4444
wavesurfer: WaveSurfer;
4545
@ViewChild("readalongContainer") readalongContainerElement: ElementRef;
4646
@ViewChild("handle") handleElement!: ElementRef;
4747
@ViewChild("styleWindow") styleElement!: WcStylingComponent;
48+
49+
@ViewChild("rasFileUpload")
50+
private rasFileUpload: ElementRef<HTMLInputElement>;
4851
readalong: Components.ReadAlong;
4952

5053
language: "eng" | "fra" | "spa" = "eng";
@@ -55,6 +58,9 @@ export class EditorComponent implements OnDestroy, OnInit, AfterViewInit {
5558

5659
unsubscribe$ = new Subject<void>();
5760
rasFileIsLoaded = false;
61+
62+
private beforeUnload: (e: Event) => void;
63+
5864
constructor(
5965
public b64Service: B64Service,
6066
private fileService: FileService,
@@ -78,6 +84,23 @@ export class EditorComponent implements OnDestroy, OnInit, AfterViewInit {
7884
console.log("[DEBUG] window resized");
7985
this.resetStyleWindowSize();
8086
});
87+
88+
this.beforeUnload = (e: Event) => {
89+
if (this.formIsDirty()) {
90+
e.preventDefault(); // chrome & edge >118, circa Oct 2023
91+
e.returnValue = true; // chrome & edge <=118
92+
}
93+
};
94+
95+
// User Browser's default messaging to warn the user when they're about to leave the page
96+
window.addEventListener("beforeunload", this.beforeUnload);
97+
}
98+
99+
private formIsDirty(): boolean {
100+
return (
101+
(this.rasFileUpload && this.rasFileUpload.nativeElement.value !== "") ||
102+
this.rasFileIsLoaded
103+
);
81104
}
82105

83106
async ngAfterViewInit(): Promise<void> {
@@ -177,7 +200,6 @@ export class EditorComponent implements OnDestroy, OnInit, AfterViewInit {
177200
});
178201
}
179202

180-
ngOnInit(): void {}
181203
async ngOnDestroy() {
182204
// Save translations, images and all other edits to a temporary blob before destroying component
183205
// We just re-use the download service method here for simplicity and reload from this when
@@ -196,6 +218,8 @@ export class EditorComponent implements OnDestroy, OnInit, AfterViewInit {
196218
);
197219
}
198220
this.rasFileIsLoaded = false;
221+
222+
window.removeEventListener("beforeunload", this.beforeUnload);
199223
}
200224

201225
download(download_type: SupportedOutputs) {

packages/studio-web/src/app/studio/studio.component.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import { StepperSelectionEvent } from "@angular/cdk/stepper";
4040
import { HttpErrorResponse } from "@angular/common/http";
4141
import { DownloadService } from "../shared/download/download.service";
4242
import { StudioService } from "./studio.service";
43-
import { environment } from "../../environments/environment";
4443

4544
@Component({
4645
selector: "studio-component",
@@ -54,6 +53,8 @@ export class StudioComponent implements OnDestroy, OnInit {
5453
@ViewChild("demo", { static: false }) demo?: DemoComponent;
5554
@ViewChild("stepper") private stepper: MatStepper;
5655
unsubscribe$ = new Subject<void>();
56+
57+
private beforeUnload: (e: Event) => void;
5758
private route: ActivatedRoute;
5859
constructor(
5960
private titleService: Title,
@@ -108,13 +109,15 @@ export class StudioComponent implements OnDestroy, OnInit {
108109
true,
109110
);
110111

112+
this.beforeUnload = (e: Event) => {
113+
if (this.formIsDirty()) {
114+
e.preventDefault(); // chrome & edge >118, circa Oct 2023
115+
e.returnValue = true; // chrome & edge <=118
116+
}
117+
};
118+
111119
// User Browser's default messaging to warn the user when they're about to leave the page
112-
if (environment.production) {
113-
window.addEventListener("beforeunload", (e) => {
114-
if (this.formIsDirty()) (e || window.event).returnValue = true;
115-
return true;
116-
});
117-
}
120+
window.addEventListener("beforeunload", this.beforeUnload);
118121

119122
// Catch and report a catastrophic failure as soon as possible
120123
this.ssjsService
@@ -137,6 +140,8 @@ export class StudioComponent implements OnDestroy, OnInit {
137140
this.studioService.lastStepperIndex = this.stepper?.selectedIndex;
138141
this.unsubscribe$.next();
139142
this.unsubscribe$.complete();
143+
144+
window.removeEventListener("beforeunload", this.beforeUnload);
140145
}
141146

142147
selectionChange(event: StepperSelectionEvent) {
@@ -155,9 +160,11 @@ export class StudioComponent implements OnDestroy, OnInit {
155160

156161
formIsDirty() {
157162
return (
158-
this.studioService.audioControl$.value !== null ||
159-
this.studioService.textControl$.value !== null ||
160-
this.studioService.$textInput
163+
this.studioService.audioControl$.value ||
164+
this.studioService.textControl$.value ||
165+
this.studioService.$textInput.value ||
166+
this.studioService.langMode$.value !== "generic" ||
167+
this.studioService.langControl$.value !== "und"
161168
);
162169
}
163170

0 commit comments

Comments
 (0)