From 254eac11a199cf334b1e12c83b4e7b278c4f6dc0 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 1 Jul 2024 21:02:01 +0800 Subject: [PATCH] enhance: paste text on subject * for single line text, all text will be pasted in subject text box * if paste in the middle of subject content, multi-line text will be transformed into single line text by `ReplaceLineEndings(" ")` * if paste in the end of subject content, multi-line text will be split into two parts. - first line will be pasted in the subject box - remains will be pasted in the start of description box --- src/Views/CommitMessageTextBox.axaml.cs | 37 ++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Views/CommitMessageTextBox.axaml.cs b/src/Views/CommitMessageTextBox.axaml.cs index 043c05aa..70f978b8 100644 --- a/src/Views/CommitMessageTextBox.axaml.cs +++ b/src/Views/CommitMessageTextBox.axaml.cs @@ -20,6 +20,11 @@ namespace SourceGit.Views protected override Type StyleKeyOverride => typeof(TextBox); + public void Paste(string text) + { + OnTextInput(new TextInputEventArgs() { Text = text }); + } + protected override void OnKeyDown(KeyEventArgs e) { var dump = new KeyEventArgs() @@ -112,7 +117,7 @@ namespace SourceGit.Views } } - private void OnSubjectTextBoxPreviewKeyDown(object sender, KeyEventArgs e) + private async void OnSubjectTextBoxPreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter || (e.Key == Key.Right && SubjectEditor.CaretIndex == Subject.Length)) { @@ -120,6 +125,36 @@ namespace SourceGit.Views DescriptionEditor.CaretIndex = 0; e.Handled = true; } + else if (e.Key == Key.V && ((OperatingSystem.IsMacOS() && e.KeyModifiers == KeyModifiers.Meta) || (!OperatingSystem.IsMacOS() && e.KeyModifiers == KeyModifiers.Control))) + { + var text = await App.GetClipboardTextAsync(); + text = text.Trim(); + + if (!string.IsNullOrEmpty(text)) + { + if (SubjectEditor.CaretIndex == Subject.Length) + { + var idx = text.IndexOf('\n'); + if (idx == -1) + { + SubjectEditor.Paste(text); + } + else + { + SubjectEditor.Paste(text.Substring(0, idx)); + DescriptionEditor.Focus(); + DescriptionEditor.CaretIndex = 0; + DescriptionEditor.Paste(text.Substring(idx + 1) + "\n"); + } + } + else + { + SubjectEditor.Paste(text.ReplaceLineEndings(" ")); + } + } + + e.Handled = true; + } } private void OnDescriptionTextBoxPreviewKeyDown(object sender, KeyEventArgs e)