Skip to content

Commit 0fc92b5

Browse files
committed
refactor: 重构代码
1 parent 8291492 commit 0fc92b5

2 files changed

Lines changed: 42 additions & 41 deletions

File tree

src/components/BootstrapBlazor.Term/Components/Term.razor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ namespace BootstrapBlazor.Components;
1515
public partial class Term
1616
{
1717
/// <summary>
18-
/// <para lang="zh">获得/设置 <see cref="TermOptions"/> 实例</para>
19-
/// <para lang="en">Gets or sets the <see cref="TermOptions"/></para>
18+
/// <para lang="zh">获得/设置 <see cref="TermOptions"/> 实例 默认 null</para>
19+
/// <para lang="en">Gets or sets the <see cref="TermOptions"/> instance Default value is null.</para>
2020
/// </summary>
2121
[Parameter]
22-
public TermOptions Options { get; set; } = new TermOptions();
22+
public TermOptions? Options { get; set; }
2323

2424
/// <summary>
2525
/// <para lang="zh">获得/设置 收到数据回调</para>

src/components/BootstrapBlazor.Term/Components/Term.razor.js

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ export async function init(id, invoke, options) {
1111
return;
1212
}
1313

14-
const term = new Terminal({
15-
fontFamily: options.fontFamily || "Consolas, 'Courier New', monospace",
16-
fontSize: options.fontSize || 14,
17-
cursorBlink: options.cursorBlink,
18-
lineHeight: options.lineHeight || 1.0,
19-
theme: options.theme || {},
20-
convertEol: options.convertEol
21-
});
14+
options = {
15+
... {
16+
fontFamily: "Consolas, 'Courier New', monospace",
17+
fontSize: 14,
18+
lineHeight: 1.0,
19+
},
20+
...options
21+
};
2222

23+
const term = new Terminal(options);
2324
const fitAddon = new FitAddon.FitAddon();
2425
term.loadAddon(fitAddon);
2526

@@ -28,65 +29,65 @@ export async function init(id, invoke, options) {
2829

2930
const encoder = new TextEncoder();
3031
term.onData(data => {
31-
el.term.write(data);
32-
console.log(data);
33-
//invoke.invokeMethodAsync("TriggerReceiveDataAsync", encoder.encode(data));
32+
invoke.invokeMethodAsync("TriggerReceiveDataAsync", encoder.encode(data));
3433
});
3534

3635
term.onResize(size => {
3736
invoke.invokeMethodAsync("TriggerResizeAsync", size.rows, size.cols);
3837
});
3938

40-
el.term = term;
41-
el.fitAddon = fitAddon;
42-
el.invoke = invoke;
43-
4439
const resizeHandler = () => {
4540
try {
4641
fitAddon.fit();
4742
const dims = fitAddon.proposeDimensions();
4843
if (dims) {
4944
invoke.invokeMethodAsync("TriggerResizeAsync", dims.rows, dims.cols);
5045
}
51-
} catch (e) { }
46+
} catch (e) {
47+
console.warn(e);
48+
}
5249
};
5350
window.addEventListener('resize', resizeHandler);
54-
el.resizeHandler = resizeHandler;
51+
52+
Data.set(id, {
53+
term,
54+
resizeHandler
55+
});
5556
}
5657

5758
export function write(id, data) {
58-
const el = document.getElementById(id);
59-
if (el && el.term) {
60-
el.term.write(data);
59+
const terminal = Data.get(id);
60+
const { term } = terminal;
61+
if (term) {
62+
term.write(data);
6163
}
6264
}
6365

6466
export function writeln(id, data) {
65-
const el = document.getElementById(id);
66-
if (el && el.term) {
67-
el.term.writeln(data);
67+
const terminal = Data.get(id);
68+
const { term } = terminal;
69+
if (term) {
70+
term.writeln(data);
6871
}
6972
}
7073

7174
export function clear(id) {
72-
const el = document.getElementById(id);
73-
if (el && el.term) {
74-
el.term.clear();
75+
const terminal = Data.get(id);
76+
const { term } = terminal;
77+
if (term) {
78+
term.clear();
7579
}
7680
}
7781

7882
export function dispose(id) {
79-
const el = document.getElementById(id);
80-
if (el) {
81-
if (el.resizeHandler) {
82-
window.removeEventListener('resize', el.resizeHandler);
83-
}
84-
if (el.term) {
85-
el.term.dispose();
86-
delete el.term;
87-
}
88-
if (el.fitAddon) {
89-
delete el.fitAddon;
90-
}
83+
const terminal = Data.get(id);
84+
Data.remove(id);
85+
86+
const { term, resizeHandler } = terminal;
87+
if (resizeHandler) {
88+
window.removeEventListener('resize', resizeHandler);
89+
}
90+
if (term) {
91+
term.dispose();
9192
}
9293
}

0 commit comments

Comments
 (0)