From 1edd0310bde436ee286c60b1dc934f3724d1bfd6 Mon Sep 17 00:00:00 2001 From: Jiaqi Liu Date: Sun, 12 Jan 2020 15:53:42 -0800 Subject: [PATCH] Respect line ending when copy selected text to clipboard Respect line ending when copy selected text to clipboard --- .../Controls/TextEditor/ITextEditor.cs | 3 ++ .../Controls/TextEditor/TextEditor.xaml.cs | 29 +++++++++++++++++++ .../TextEditor/TextEditorContextFlyout.cs | 2 +- .../Controls/TextEditor/TextEditorCore.cs | 24 ++------------- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/Notepads/Controls/TextEditor/ITextEditor.cs b/src/Notepads/Controls/TextEditor/ITextEditor.cs index 44aa347cd..627d81655 100644 --- a/src/Notepads/Controls/TextEditor/ITextEditor.cs +++ b/src/Notepads/Controls/TextEditor/ITextEditor.cs @@ -7,6 +7,7 @@ using Notepads.Utilities; using Windows.Storage; using Windows.UI.Xaml; + using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Input; public interface ITextEditor @@ -73,6 +74,8 @@ void Init(TextFile textFile, Encoding GetEncoding(); + void CopyPlainTextToWindowsClipboard(TextControlCopyingToClipboardEventArgs args); + void RevertAllChanges(); bool TryChangeEncoding(Encoding encoding); diff --git a/src/Notepads/Controls/TextEditor/TextEditor.xaml.cs b/src/Notepads/Controls/TextEditor/TextEditor.xaml.cs index 000131e27..264657006 100644 --- a/src/Notepads/Controls/TextEditor/TextEditor.xaml.cs +++ b/src/Notepads/Controls/TextEditor/TextEditor.xaml.cs @@ -14,6 +14,7 @@ using Notepads.Services; using Notepads.Utilities; using Windows.ApplicationModel.Core; + using Windows.ApplicationModel.DataTransfer; using Windows.ApplicationModel.Resources; using Windows.Storage; using Windows.System; @@ -174,6 +175,7 @@ public TextEditor() TextEditorCore.TextChanging += TextEditorCore_OnTextChanging; TextEditorCore.SelectionChanged += TextEditorCore_OnSelectionChanged; TextEditorCore.KeyDown += TextEditorCore_OnKeyDown; + TextEditorCore.CopyPlainTextToWindowsClipboardRequested += TextEditorCore_CopyPlainTextToWindowsClipboardRequested; TextEditorCore.ContextFlyout = new TextEditorContextFlyout(this, TextEditorCore); // Init shortcuts @@ -199,6 +201,7 @@ public void Dispose() TextEditorCore.TextChanging -= TextEditorCore_OnTextChanging; TextEditorCore.SelectionChanged -= TextEditorCore_OnSelectionChanged; TextEditorCore.KeyDown -= TextEditorCore_OnKeyDown; + TextEditorCore.CopyPlainTextToWindowsClipboardRequested -= TextEditorCore_CopyPlainTextToWindowsClipboardRequested; if (TextEditorCore.ContextFlyout is TextEditorContextFlyout contextFlyout) { @@ -663,6 +666,27 @@ public void Focus() } } + public void CopyPlainTextToWindowsClipboard(TextControlCopyingToClipboardEventArgs args) + { + if (args != null) + { + args.Handled = true; + } + + try + { + DataPackage dataPackage = new DataPackage { RequestedOperation = DataPackageOperation.Copy }; + var text = LineEndingUtility.ApplyLineEnding(TextEditorCore.Document.Selection.Text, GetLineEnding()); + dataPackage.SetText(text); + Clipboard.SetContentWithOptions(dataPackage, new ClipboardContentOptions() { IsAllowedInHistory = true, IsRoamable = true }); + Clipboard.Flush(); + } + catch (Exception ex) + { + LoggingService.LogError($"Failed to copy plain text to Windows clipboard: {ex.Message}"); + } + } + public bool NoChangesSinceLastSaved(bool compareTextOnly = false) { if (!_loaded) return true; @@ -736,6 +760,11 @@ private void TextEditorCore_OnTextChanging(RichEditBox textEditor, RichEditBoxTe TextChanging?.Invoke(this, EventArgs.Empty); } + private void TextEditorCore_CopyPlainTextToWindowsClipboardRequested(object sender, TextControlCopyingToClipboardEventArgs e) + { + CopyPlainTextToWindowsClipboard(e); + } + public void ShowFindAndReplaceControl(bool showReplaceBar) { if (!TextEditorCore.IsEnabled || Mode != TextEditorMode.Editing) diff --git a/src/Notepads/Controls/TextEditor/TextEditorContextFlyout.cs b/src/Notepads/Controls/TextEditor/TextEditorContextFlyout.cs index 6313a89a2..8f104833d 100644 --- a/src/Notepads/Controls/TextEditor/TextEditorContextFlyout.cs +++ b/src/Notepads/Controls/TextEditor/TextEditorContextFlyout.cs @@ -118,7 +118,7 @@ public MenuFlyoutItem Copy Key = VirtualKey.C, IsEnabled = false, }); - _copy.Click += (sender, args) => _textEditorCore.CopyPlainTextToWindowsClipboard(null); + _copy.Click += (sender, args) => _textEditor.CopyPlainTextToWindowsClipboard(null); } return _copy; } diff --git a/src/Notepads/Controls/TextEditor/TextEditorCore.cs b/src/Notepads/Controls/TextEditor/TextEditorCore.cs index 9ceff6077..55dfc258a 100644 --- a/src/Notepads/Controls/TextEditor/TextEditorCore.cs +++ b/src/Notepads/Controls/TextEditor/TextEditorCore.cs @@ -26,6 +26,8 @@ public class TextEditorCore : RichEditBox public event EventHandler FontZoomFactorChanged; + public event EventHandler CopyPlainTextToWindowsClipboardRequested; + private const char RichEditBoxDefaultLineEnding = '\r'; private string[] _contentLinesCache; @@ -298,26 +300,6 @@ public void GetCurrentLineColumn(out int lineIndex, out int columnIndex, out int } } - public void CopyPlainTextToWindowsClipboard(TextControlCopyingToClipboardEventArgs args) - { - if (args != null) - { - args.Handled = true; - } - - try - { - DataPackage dataPackage = new DataPackage { RequestedOperation = DataPackageOperation.Copy }; - dataPackage.SetText(Document.Selection.Text); - Clipboard.SetContentWithOptions(dataPackage, new ClipboardContentOptions() { IsAllowedInHistory = true, IsRoamable = true }); - Clipboard.Flush(); - } - catch (Exception ex) - { - LoggingService.LogError($"Failed to copy plain text to Windows clipboard: {ex.Message}"); - } - } - public async Task PastePlainTextFromWindowsClipboard(TextControlPasteEventArgs args) { if (args != null) @@ -486,7 +468,7 @@ private async void TextEditorCore_Paste(object sender, TextControlPasteEventArgs private void TextEditorCore_CopyingToClipboard(RichEditBox sender, TextControlCopyingToClipboardEventArgs args) { - CopyPlainTextToWindowsClipboard(args); + CopyPlainTextToWindowsClipboardRequested?.Invoke(sender, args); } private void SetDefaultTabStopAndLineSpacing(FontFamily font, double fontSize)