Skip to content

A Swiss Army knife for developers.

Add a copy button to the MarkdownPreview tool to copy the HTML output (#260)

Praneet Loke056b2c1c5b5e
diff --git a/src/dev/impl/DevToys/ViewModels/Tools/Text/MarkdownPreview/MarkdownPreviewToolViewModel.cs b/src/dev/impl/DevToys/ViewModels/Tools/Text/MarkdownPreview/MarkdownPreviewToolViewModel.cs
index f0970ced0..fc24b77f2 100644
--- a/src/dev/impl/DevToys/ViewModels/Tools/Text/MarkdownPreview/MarkdownPreviewToolViewModel.cs
+++ b/src/dev/impl/DevToys/ViewModels/Tools/Text/MarkdownPreview/MarkdownPreviewToolViewModel.cs
@@ -18,6 +18,10 @@ using Microsoft.Toolkit.Mvvm.Messaging;
using Windows.Storage;
using Windows.UI.Xaml;
using DevToys.Shared.Core;
+using Microsoft.Toolkit.Mvvm.Input;
+using Windows.ApplicationModel.DataTransfer;
+using DevToys.Core;
+using Clipboard = Windows.ApplicationModel.DataTransfer.Clipboard;
namespace DevToys.ViewModels.Tools.MarkdownPreview
{
@@ -88,6 +92,8 @@ namespace DevToys.ViewModels.Tools.MarkdownPreview
_themeListener = themeListener;
SettingsProvider = settingsProvider;
+ CopyHtmlCommand = new RelayCommand(ExecuteCopyHtmlCommand);
+
// Activate the view model's messenger.
IsActive = true;
@@ -153,5 +159,39 @@ namespace DevToys.ViewModels.Tools.MarkdownPreview
return htmlDocument;
}
+
+ #region CopyHtmlCommand
+
+ internal IRelayCommand CopyHtmlCommand { get; }
+
+ private void ExecuteCopyHtmlCommand()
+ {
+ if (InputValue == null)
+ {
+ return;
+ }
+
+ try
+ {
+ MarkdownPipeline pipeline = new MarkdownPipelineBuilder().UseEmojiAndSmiley().UseSmartyPants().UseAdvancedExtensions().Build();
+ string? htmlBody = Markdown.ToHtml(InputValue, pipeline);
+
+ var data = new DataPackage
+ {
+ RequestedOperation = DataPackageOperation.Copy
+ };
+
+ data.SetText(htmlBody ?? string.Empty);
+
+ Clipboard.SetContentWithOptions(data, new ClipboardContentOptions() { IsAllowedInHistory = true, IsRoamable = true });
+ Clipboard.Flush();
+ }
+ catch (Exception ex)
+ {
+ Logger.LogFault("Failed to copy from webview", ex);
+ }
+ }
+
+ #endregion
}
}
diff --git a/src/dev/impl/DevToys/Views/Tools/Text/MarkdownPreview/MarkdownPreviewToolPage.xaml b/src/dev/impl/DevToys/Views/Tools/Text/MarkdownPreview/MarkdownPreviewToolPage.xaml
index 917dc8eae..417fcb233 100644
--- a/src/dev/impl/DevToys/Views/Tools/Text/MarkdownPreview/MarkdownPreviewToolPage.xaml
+++ b/src/dev/impl/DevToys/Views/Tools/Text/MarkdownPreview/MarkdownPreviewToolPage.xaml
@@ -76,11 +76,26 @@
<toolkit:HeaderedContentControl
Grid.Column="2"
- Header="{x:Bind ViewModel.Strings.Output}"
- Margin="0,16,0,0"
+ Margin="0,0,0,0"
MinHeight="150"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch">
+ <toolkit:HeaderedContentControl.Header>
+ <toolkit:DockPanel>
+ <TextBlock VerticalAlignment="Bottom" Text="{x:Bind ViewModel.Strings.Output}"></TextBlock>
+ <Button
+ x:Name="CopyButton"
+ TabIndex="2"
+ HorizontalAlignment="Right"
+ AutomationProperties.Name="{Binding Instance.Common.Copy, Mode=OneTime, Source={StaticResource LanguageManager}}"
+ Command="{x:Bind ViewModel.CopyHtmlCommand}">
+ <StackPanel Orientation="Horizontal" Spacing="4">
+ <FontIcon Glyph="&#xF32B;"/>
+ <TextBlock VerticalAlignment="Center" Text="{Binding Instance.Common.Copy, Mode=OneTime, Source={StaticResource LanguageManager}}"/>
+ </StackPanel>
+ </Button>
+ </toolkit:DockPanel>
+ </toolkit:HeaderedContentControl.Header>
<Border
CornerRadius="4"
Margin="0,8,0,0"