@@ -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 ) {
0 commit comments