From 7b41373eb06dc890a87b18534e0e87feee1956ec Mon Sep 17 00:00:00 2001 From: Saleh Yusefnejad Date: Wed, 29 Nov 2023 19:03:23 +0330 Subject: [PATCH] feat(butil): add beforeUnload support to Butil #6169 (#6170) --- .../Bit.Butil.Demo/Pages/WindowPage.razor | 39 +++++++++++++++---- .../JsInterops/ConsoleJsInterop.cs | 0 .../JsInterops/EventsJsInterop.cs | 0 .../JsInterops/KeyboardJsInterop.cs | 0 .../Internals/JsInterops/WindowJsInterop.cs | 17 ++++++++ src/Butil/Bit.Butil/Publics/Window.cs | 10 +++++ src/Butil/Bit.Butil/Scripts/window.ts | 20 ++++++++++ 7 files changed, 78 insertions(+), 8 deletions(-) rename src/Butil/Bit.Butil/{ => Internals}/JsInterops/ConsoleJsInterop.cs (100%) rename src/Butil/Bit.Butil/{ => Internals}/JsInterops/EventsJsInterop.cs (100%) rename src/Butil/Bit.Butil/{ => Internals}/JsInterops/KeyboardJsInterop.cs (100%) create mode 100644 src/Butil/Bit.Butil/Internals/JsInterops/WindowJsInterop.cs create mode 100644 src/Butil/Bit.Butil/Scripts/window.ts 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