diff --git a/src/Butil/Bit.Butil.Demo/Pages/WindowPage.razor b/src/Butil/Bit.Butil.Demo/Pages/WindowPage.razor
index fadbba241d..549ccaa3a5 100644
--- a/src/Butil/Bit.Butil.Demo/Pages/WindowPage.razor
+++ b/src/Butil/Bit.Butil.Demo/Pages/WindowPage.razor
@@ -16,18 +16,29 @@
}
-
Open the DevTools and start pressing keys on your keyboard
+
+
+
-
-
-
+Open the DevTools and start pressing keys on your keyboard
+
+
+
+
+Is KeyDown Registered? @isKeyDownRegistered
+
+
-Is Registered: @isRegistered
+
+
+
+Is BeforeUnload Registered? @isBeforeUnloadRegistered
@code {
- private bool isRegistered = false;
+ private bool isKeyDownRegistered;
+ private bool isBeforeUnloadRegistered;
private Action _handler = default!;
@@ -41,12 +52,24 @@
private void AddEventListener()
{
_ = window.AddEventListener(ButilEvents.KeyDown, _handler);
- isRegistered = true;
+ isKeyDownRegistered = true;
}
private void RemoveEventListener()
{
_ = window.RemoveEventListener(ButilEvents.KeyDown, _handler);
- isRegistered = false;
+ isKeyDownRegistered = false;
+ }
+
+ private void AddBeforeUnload()
+ {
+ _ = window.AddBeforeUnload();
+ isBeforeUnloadRegistered = true;
+ }
+
+ private void RemoveBeforeUnload()
+ {
+ _ = window.RemoveBeforeUnload();
+ isBeforeUnloadRegistered = false;
}
}
\ No newline at end of file
diff --git a/src/Butil/Bit.Butil/JsInterops/ConsoleJsInterop.cs b/src/Butil/Bit.Butil/Internals/JsInterops/ConsoleJsInterop.cs
similarity index 100%
rename from src/Butil/Bit.Butil/JsInterops/ConsoleJsInterop.cs
rename to src/Butil/Bit.Butil/Internals/JsInterops/ConsoleJsInterop.cs
diff --git a/src/Butil/Bit.Butil/JsInterops/EventsJsInterop.cs b/src/Butil/Bit.Butil/Internals/JsInterops/EventsJsInterop.cs
similarity index 100%
rename from src/Butil/Bit.Butil/JsInterops/EventsJsInterop.cs
rename to src/Butil/Bit.Butil/Internals/JsInterops/EventsJsInterop.cs
diff --git a/src/Butil/Bit.Butil/JsInterops/KeyboardJsInterop.cs b/src/Butil/Bit.Butil/Internals/JsInterops/KeyboardJsInterop.cs
similarity index 100%
rename from src/Butil/Bit.Butil/JsInterops/KeyboardJsInterop.cs
rename to src/Butil/Bit.Butil/Internals/JsInterops/KeyboardJsInterop.cs
diff --git a/src/Butil/Bit.Butil/Internals/JsInterops/WindowJsInterop.cs b/src/Butil/Bit.Butil/Internals/JsInterops/WindowJsInterop.cs
new file mode 100644
index 0000000000..845b1d8248
--- /dev/null
+++ b/src/Butil/Bit.Butil/Internals/JsInterops/WindowJsInterop.cs
@@ -0,0 +1,17 @@
+using System.Threading.Tasks;
+using Microsoft.JSInterop;
+
+namespace Bit.Butil;
+
+internal static class WindowJsInterop
+{
+ internal static async Task AddBeforeUnload(this IJSRuntime js)
+ {
+ await js.InvokeVoidAsync("BitButil.window.addBeforeUnload");
+ }
+
+ internal static async Task RemoveBeforeUnload(this IJSRuntime js)
+ {
+ await js.InvokeVoidAsync("BitButil.window.removeBeforeUnload");
+ }
+}
diff --git a/src/Butil/Bit.Butil/Publics/Window.cs b/src/Butil/Bit.Butil/Publics/Window.cs
index 1a00713736..a3530b629e 100644
--- a/src/Butil/Bit.Butil/Publics/Window.cs
+++ b/src/Butil/Bit.Butil/Publics/Window.cs
@@ -17,4 +17,14 @@ public async Task RemoveEventListener(string domEvent, Action listener, bo
{
await DomEventDispatcher.RemoveEventListener(js, ElementName, domEvent, listener, useCapture);
}
+
+ public async Task AddBeforeUnload()
+ {
+ await js.AddBeforeUnload();
+ }
+
+ public async Task RemoveBeforeUnload()
+ {
+ await js.RemoveBeforeUnload();
+ }
}
diff --git a/src/Butil/Bit.Butil/Scripts/window.ts b/src/Butil/Bit.Butil/Scripts/window.ts
new file mode 100644
index 0000000000..be4dc4c7d9
--- /dev/null
+++ b/src/Butil/Bit.Butil/Scripts/window.ts
@@ -0,0 +1,20 @@
+var BitButil = BitButil || {};
+
+(function (butil: any) {
+ butil.window = {
+ addBeforeUnload,
+ removeBeforeUnload
+ };
+
+ function addBeforeUnload() {
+ window.onbeforeunload = e => {
+ e.preventDefault();
+ e.returnValue = true;
+ return true;
+ };
+ }
+
+ function removeBeforeUnload() {
+ window.onbeforeunload = null;
+ }
+}(BitButil));
\ No newline at end of file