Skip to content

Commit

Permalink
Respect line ending when copy selected text to clipboard
Browse files Browse the repository at this point in the history
Respect line ending when copy selected text to clipboard
  • Loading branch information
0x7c13 committed Jan 12, 2020
1 parent b6325ab commit 1edd031
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
3 changes: 3 additions & 0 deletions src/Notepads/Controls/TextEditor/ITextEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,6 +74,8 @@ void Init(TextFile textFile,

Encoding GetEncoding();

void CopyPlainTextToWindowsClipboard(TextControlCopyingToClipboardEventArgs args);

void RevertAllChanges();

bool TryChangeEncoding(Encoding encoding);
Expand Down
29 changes: 29 additions & 0 deletions src/Notepads/Controls/TextEditor/TextEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
24 changes: 3 additions & 21 deletions src/Notepads/Controls/TextEditor/TextEditorCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class TextEditorCore : RichEditBox

public event EventHandler<double> FontZoomFactorChanged;

public event EventHandler<TextControlCopyingToClipboardEventArgs> CopyPlainTextToWindowsClipboardRequested;

private const char RichEditBoxDefaultLineEnding = '\r';

private string[] _contentLinesCache;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1edd031

Please sign in to comment.