Skip to content

Commit c1e08a4

Browse files
committed
更新为使用byte[]
1 parent 74bbc22 commit c1e08a4

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public partial class Term
2727
/// 获得/设置 收到数据回调
2828
/// </summary>
2929
[Parameter]
30-
public Func<string, Task>? OnData { get; set; }
30+
public Func<byte[], Task>? OnData { get; set; }
3131

3232
/// <summary>
3333
/// 获得/设置 终端 Resize 回调
@@ -132,7 +132,8 @@ private async Task ReadStreamAsync()
132132
{
133133
var read = await _stream.ReadAsync(buffer, _cancellationTokenSource.Token);
134134
if (read == 0) break;
135-
var data = System.Text.Encoding.UTF8.GetString(buffer, 0, read);
135+
var data = new byte[read];
136+
Array.Copy(buffer, data, read);
136137
await Write(data);
137138
}
138139
}
@@ -147,18 +148,27 @@ private async Task ReadStreamAsync()
147148
}
148149
}
149150

151+
/// <summary>
152+
/// 写入数据 (Byte[])
153+
/// </summary>
154+
/// <param name="data"></param>
155+
/// <returns></returns>
156+
public async Task Write(byte[] data)
157+
{
158+
await InvokeVoidAsync("write", Id, data);
159+
}
160+
150161
/// <summary>
151162
/// 收到数据 JSInvoke
152163
/// </summary>
153164
/// <param name="data"></param>
154165
/// <returns></returns>
155166
[JSInvokable]
156-
public async Task OnDataAsync(string data)
167+
public async Task OnDataAsync(byte[] data)
157168
{
158169
if (_stream != null && _stream.CanWrite)
159170
{
160-
var buffer = System.Text.Encoding.UTF8.GetBytes(data);
161-
await _stream.WriteAsync(buffer);
171+
await _stream.WriteAsync(data);
162172
await _stream.FlushAsync();
163173
}
164174

@@ -180,12 +190,12 @@ protected override async ValueTask DisposeAsync(bool disposing)
180190
}
181191

182192
/// <summary>
183-
/// 获得/设置 终端行数
193+
/// 获得 终端行数
184194
/// </summary>
185195
public int Rows { get; private set; }
186196

187197
/// <summary>
188-
/// 获得/设置 终端列数
198+
/// 获得 终端列数
189199
/// </summary>
190200
public int Columns { get; private set; }
191201

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export function init(id, invoke, options) {
2222
fontSize: options.fontSize || 14,
2323
cursorBlink: options.cursorBlink,
2424
lineHeight: options.lineHeight || 1.0,
25-
theme: options.theme || {}
25+
theme: options.theme || {},
26+
convertEol: options.convertEol
2627
});
2728

2829
const fitAddon = new FitAddon.FitAddon();
@@ -31,8 +32,9 @@ export function init(id, invoke, options) {
3132
term.open(el);
3233
fitAddon.fit();
3334

35+
const encoder = new TextEncoder();
3436
term.onData(data => {
35-
invoke.invokeMethodAsync("OnDataAsync", data);
37+
invoke.invokeMethodAsync("OnDataAsync", encoder.encode(data));
3638
});
3739

3840
term.onResize(size => {
@@ -50,7 +52,7 @@ export function init(id, invoke, options) {
5052
fitAddon.fit();
5153
const dims = fitAddon.proposeDimensions();
5254
if (dims) {
53-
invoke.invokeMethodAsync("OnResizeAsync", dims.rows, dims.cols);
55+
invoke.invokeMethodAsync("OnResizeAsync", dims.rows, dims.cols);
5456
}
5557
} catch (e) { }
5658
};

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public class TermOptions
3333
/// 获得/设置 行高 默认 1.0
3434
/// </summary>
3535
public double LineHeight { get; set; } = 1.0;
36+
37+
/// <summary>
38+
/// 获得/设置 是否将 \n 转换为 \r\n 默认 false
39+
/// </summary>
40+
public bool ConvertEol { get; set; }
3641
}
3742

3843
/// <summary>

0 commit comments

Comments
 (0)