Fixed Clear Line issue
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
@using IOModuleTestBlazor.Services
|
||||
@inject ISerialPortService SerialService
|
||||
@inject IJSRuntime JS
|
||||
|
||||
<PageTitle>Serial Terminal</PageTitle>
|
||||
|
||||
@@ -122,6 +123,8 @@
|
||||
private string? _connectError;
|
||||
private List<string> _availablePorts = new();
|
||||
private List<string> _displayLines = new();
|
||||
private IJSObjectReference? _jsModule;
|
||||
private bool _scrollPending = false;
|
||||
|
||||
private static readonly (string Label, string Cmd)[] _quickCommands =
|
||||
[
|
||||
@@ -188,15 +191,30 @@
|
||||
InvokeAsync(() =>
|
||||
{
|
||||
RefreshLines();
|
||||
_scrollPending = true;
|
||||
StateHasChanged();
|
||||
});
|
||||
}
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (firstRender)
|
||||
_jsModule = await JS.InvokeAsync<IJSObjectReference>(
|
||||
"import", "./Components/Pages/SerialTerminal.razor.js");
|
||||
|
||||
if (_scrollPending && _jsModule is not null)
|
||||
{
|
||||
_scrollPending = false;
|
||||
await _jsModule.InvokeVoidAsync("scrollToBottom", "terminal-output");
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshLines()
|
||||
=> _displayLines = SerialService.GetLines().ToList();
|
||||
|
||||
private void ClearTerminal()
|
||||
{
|
||||
SerialService.ClearLines();
|
||||
_displayLines.Clear();
|
||||
StateHasChanged();
|
||||
}
|
||||
@@ -205,5 +223,8 @@
|
||||
=> string.Join("\n", _displayLines);
|
||||
|
||||
public void Dispose()
|
||||
=> SerialService.DataReceived -= OnDataReceived;
|
||||
{
|
||||
SerialService.DataReceived -= OnDataReceived;
|
||||
_jsModule?.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
export function scrollToBottom(elementId) {
|
||||
const el = document.getElementById(elementId);
|
||||
if (el) el.scrollTop = el.scrollHeight;
|
||||
}
|
||||
@@ -21,6 +21,9 @@ public interface ISerialPortService
|
||||
/// Each line is prefixed with "> " (sent) or "< " (received).</summary>
|
||||
IReadOnlyList<string> GetLines();
|
||||
|
||||
/// <summary>Clears the line buffer.</summary>
|
||||
void ClearLines();
|
||||
|
||||
/// <summary>Fired on the SerialPort receive thread when new lines arrive.</summary>
|
||||
event Action? DataReceived;
|
||||
}
|
||||
|
||||
@@ -97,6 +97,12 @@ public sealed class SerialPortService : ISerialPortService, IDisposable
|
||||
return _lines.ToList();
|
||||
}
|
||||
|
||||
public void ClearLines()
|
||||
{
|
||||
lock (_lock)
|
||||
_lines.Clear();
|
||||
}
|
||||
|
||||
private void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
|
||||
{
|
||||
SerialPort? port;
|
||||
|
||||
Reference in New Issue
Block a user