diff --git a/src/Example/Program.cs b/src/Example/Program.cs index c712d32..c0c99a7 100644 --- a/src/Example/Program.cs +++ b/src/Example/Program.cs @@ -299,8 +299,9 @@ file sealed class EditorOverlay : IInputComponent Children = { new Text( - "Ctrl+A/E 行首行尾 Ctrl+Left/Right 按词移动\n" + + "Home/End 行首行尾 Ctrl+Left/Right 按词移动\n" + "Ctrl+Backspace/Delete 按词删除 Ctrl+Z/Y 撤销重做\n" + + "Ctrl+C 清空当前内容但保留历史记录\n" + "空内容时 Up/Down 浏览历史 Esc 提交并关闭"), new Text(), _editor, diff --git a/src/TinyTUI/Components/Editor.cs b/src/TinyTUI/Components/Editor.cs index 4ca28a1..2a36ca3 100644 --- a/src/TinyTUI/Components/Editor.cs +++ b/src/TinyTUI/Components/Editor.cs @@ -94,7 +94,7 @@ public sealed class Editor(ITextMeasurer? textMeasurer = null) : IInputComponent var line = _lines[lineIndex]; if (lineIndex == _cursorRow) { - if (line.Length == 0 && Placeholder.Length > 0) + if (IsEmpty && Placeholder.Length > 0) line = CursorMarker.Marker + Placeholder; else // Renderer 会提取这个 marker 并移动硬件光标 所以这里不渲染可见光标字符 @@ -124,11 +124,8 @@ public sealed class Editor(ITextMeasurer? textMeasurer = null) : IInputComponent case { Kind: TuiInputEventKind.Key, Value: var key } when key == KeyNames.Ctrl("y"): Redo(); break; - case { Kind: TuiInputEventKind.Key, Value: var key } when key == KeyNames.Ctrl("a"): - MoveToLineStart(); - break; - case { Kind: TuiInputEventKind.Key, Value: var key } when key == KeyNames.Ctrl("e"): - MoveToLineEnd(); + case { Kind: TuiInputEventKind.Key, Value: var key } when key == KeyNames.Ctrl("c"): + Edit(ClearText); break; case { Kind: TuiInputEventKind.Key, Value: var key } when key == KeyNames.Ctrl(KeyNames.Left): MoveWordLeft(); @@ -265,6 +262,19 @@ public sealed class Editor(ITextMeasurer? textMeasurer = null) : IInputComponent } + /// + /// 清空当前编辑文本但保留历史记录 + /// + private void ClearText() + { + _lines.Clear(); + _lines.Add(string.Empty); + _cursorRow = 0; + _cursorColumn = 0; + _historyIndex = -1; + _historyDraft = string.Empty; + } + /// /// 删除光标左侧一个 Rune 或在行首合并到上一行 /// @@ -454,6 +464,8 @@ public sealed class Editor(ITextMeasurer? textMeasurer = null) : IInputComponent private bool IsSingleEmptyLineOrBrowsing() => _historyIndex >= 0 || (_lines.Count == 1 && _lines[0].Length == 0); + private bool IsEmpty => _lines.Count == 1 && _lines[0].Length == 0; + /// /// 恢复上一个编辑快照 /// @@ -600,4 +612,5 @@ public sealed class Editor(ITextMeasurer? textMeasurer = null) : IInputComponent Word, Punctuation, } + }