Skip to content

Commit

Permalink
feat(butil): add beforeUnload support to Butil #6169 (#6170)
Browse files Browse the repository at this point in the history
  • Loading branch information
msynk authored Nov 29, 2023
1 parent 6c96f4b commit 7b41373
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/Butil/Bit.Butil.Demo/Pages/WindowPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,29 @@
}
</pre>

<h3>Open the DevTools and start pressing keys on your keyboard</h3>
<br />
<hr />
<br />

<button @onclick=AddEventListener>AddEventListener</button>
&nbsp;
<button @onclick=RemoveEventListener>RemoveEventListener</button>
<h4>Open the DevTools and start pressing keys on your keyboard</h4>
<br />
<button @onclick=AddEventListener>AddEventListener</button>&nbsp;<button @onclick=RemoveEventListener>RemoveEventListener</button>
<br />
<br />
<div>Is KeyDown Registered? @isKeyDownRegistered</div>

<br />
<hr />
<br />

<div>Is Registered: @isRegistered</div>
<button @onclick=AddBeforeUnload>AddBeforeUnload</button>&nbsp;<button @onclick=RemoveBeforeUnload>RemoveBeforeUnload</button>
<br />
<br />
<div>Is BeforeUnload Registered? @isBeforeUnloadRegistered</div>

@code {
private bool isRegistered = false;
private bool isKeyDownRegistered;
private bool isBeforeUnloadRegistered;

private Action<ButilKeyboardEventArgs> _handler = default!;

Expand All @@ -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;
}
}
17 changes: 17 additions & 0 deletions src/Butil/Bit.Butil/Internals/JsInterops/WindowJsInterop.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
10 changes: 10 additions & 0 deletions src/Butil/Bit.Butil/Publics/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ public async Task RemoveEventListener<T>(string domEvent, Action<T> listener, bo
{
await DomEventDispatcher.RemoveEventListener(js, ElementName, domEvent, listener, useCapture);
}

public async Task AddBeforeUnload()
{
await js.AddBeforeUnload();
}

public async Task RemoveBeforeUnload()
{
await js.RemoveBeforeUnload();
}
}
20 changes: 20 additions & 0 deletions src/Butil/Bit.Butil/Scripts/window.ts
Original file line number Diff line number Diff line change
@@ -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));

0 comments on commit 7b41373

Please sign in to comment.