Skip to content

Commit 9f9a224

Browse files
committed
feat: 增加 OnEvent 回调方法
1 parent d46fd46 commit 9f9a224

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

src/components/BootstrapBlazor.Player/Player.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
@if (Mode == PlayerMode.Audio)
66
{
7-
<audio id="@Id" playsinline controls crossorigin>
7+
<audio id="@Id" playsinline controls crossorigin data-bb-event="@EventString">
88
</audio>
99
}
1010
else {
11-
<video id="@Id" playsinline controls crossorigin>
11+
<video id="@Id" playsinline controls crossorigin data-bb-event="@EventString">
1212
</video>
1313
}

src/components/BootstrapBlazor.Player/Player.razor.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ public partial class Player
2525
[EditorRequired]
2626
public PlayerOptions? Options { get; set; }
2727

28-
private string? ClassString => CssBuilder.Default("bb-video-player")
29-
.AddClassFromAttributes(AdditionalAttributes)
30-
.Build();
28+
/// <summary>
29+
/// Gets or sets the client event callback. Default is null.
30+
/// </summary>
31+
[Parameter]
32+
public Func<string, Task>? OnEvent { get; set; }
33+
34+
private string? EventString => OnEvent == null ? "true" : null;
3135

3236
/// <summary>
3337
/// <inheritdoc/>
@@ -39,7 +43,7 @@ protected override async Task InvokeInitAsync()
3943
{
4044
Options.Language ??= CultureInfo.CurrentUICulture.Name;
4145
}
42-
await InvokeVoidAsync("init", Id, Interop, "", Options);
46+
await InvokeVoidAsync("init", Id, Interop, nameof(TriggerEvent), Options);
4347
}
4448

4549
/// <summary>
@@ -48,4 +52,18 @@ protected override async Task InvokeInitAsync()
4852
/// <param name="option"></param>
4953
/// <returns></returns>
5054
public Task Reload(PlayerOptions option) => InvokeVoidAsync("reload", Id, option);
55+
56+
/// <summary>
57+
/// Trigger <see cref="OnEvent"/> event callback. Triggered by JSInterop.
58+
/// </summary>
59+
/// <param name="eventName"></param>
60+
/// <returns></returns>
61+
[JSInvokable]
62+
public async Task TriggerEvent(string eventName)
63+
{
64+
if (OnEvent != null)
65+
{
66+
await OnEvent(eventName);
67+
}
68+
}
5169
}

0 commit comments

Comments
 (0)