Skip to content

Commit 32723d1

Browse files
committed
feat: 增加 OnCompleteAsync 方法
1 parent 1e36561 commit 32723d1

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

src/BootstrapBlazor/Components/Typed/Typed.razor.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public partial class Typed
2222
[Parameter]
2323
public TypedOptions? Options { get; set; }
2424

25+
/// <summary>
26+
/// 获得/设置 打字结束回调方法 默认 null
27+
/// </summary>
28+
[Parameter]
29+
public Func<Task>? OnCompleteAsync { get; set; }
30+
2531
private string? _lastOptions;
2632

2733
private string? _text;
@@ -52,27 +58,20 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
5258
/// <returns></returns>
5359
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, Text, Options, new
5460
{
55-
TriggerStop = nameof(TriggerStop)
61+
TriggerComplete = nameof(TriggerComplete)
5662
});
5763

5864
/// <summary>
59-
///
65+
/// 打字结束方法 由 Javascript 触发
6066
/// </summary>
6167
/// <returns></returns>
6268
[JSInvokable]
63-
public Task TriggerStart()
69+
public async Task TriggerComplete()
6470
{
65-
return Task.CompletedTask;
66-
}
67-
68-
/// <summary>
69-
///
70-
/// </summary>
71-
/// <returns></returns>
72-
[JSInvokable]
73-
public Task TriggerStop()
74-
{
75-
return Task.CompletedTask;
71+
if (OnCompleteAsync != null)
72+
{
73+
await OnCompleteAsync();
74+
}
7675
}
7776

7877
private bool UpdateParameters()

src/BootstrapBlazor/Components/Typed/Typed.razor.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
import Data from "../../modules/data.js"
22
import Typed from '../../lib/typedjs/typed.module.js'
33

4-
export function init(id, invoke, text, options, callbacks) {
5-
const el = document.getElementById(id);
4+
const getOptions = (text, invoke, options, callbacks) => {
65
options ??= {};
6+
77
if (text) {
88
options.strings = [text];
99
}
1010
if (options.strings === void 0) {
1111
options.strings = [""];
1212
}
13+
options.onComplete = function () {
14+
if (options.loop) {
15+
return;
16+
}
17+
invoke.invokeMethodAsync(callbacks.triggerComplete);
18+
}
19+
return options;
20+
}
1321

14-
const typed = new Typed(el, options);
22+
export function init(id, invoke, text, options, callbacks) {
23+
const el = document.getElementById(id);
24+
const typed = new Typed(el, getOptions(text, invoke, options, callbacks));
1525
Data.set(id, { el, invoke, callbacks, typed });
1626
}
1727

1828
export function update(id, text, options) {
1929
const typedJs = Data.get(id);
2030

2131
if (typedJs) {
22-
const { el, typed } = typedJs;
32+
const { el, invoke, callbacks, typed } = typedJs;
2333
typed.destroy();
2434

25-
if (text) {
26-
options.strings = [text];
27-
}
28-
if (options.strings === void 0) {
29-
options.strings = [""];
30-
}
31-
typedJs.typed = new Typed(el, options);
35+
typedJs.typed = new Typed(el, getOptions(text, invoke, options, callbacks));
3236
}
3337
}
3438

0 commit comments

Comments
 (0)