Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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}}"
Click="CopyButton_Click">
<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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#nullable enable

using System;
using DevToys.Api.Core.Navigation;
using DevToys.Core;
using DevToys.Messages;
using DevToys.Shared.Core;
using DevToys.ViewModels.Tools.MarkdownPreview;
using Microsoft.Toolkit.Mvvm.Messaging;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Clipboard = Windows.ApplicationModel.DataTransfer.Clipboard;

namespace DevToys.Views.Tools.MarkdownPreview
{
Expand Down Expand Up @@ -65,5 +69,25 @@ public void Receive(NavigateToMarkdownPreviewHtmlMessage message)
Arguments.NotNull(message, nameof(message));
PreviewWebView.NavigateToString(message.Html);
}

private async void CopyButton_Click(object sender, RoutedEventArgs e)
praneetloke marked this conversation as resolved.
Show resolved Hide resolved
{
try
{
var data = new DataPackage
{
RequestedOperation = DataPackageOperation.Copy
};
string markdownBodyEl = "document.getElementsByClassName('markdown-body')[0].innerHTML";
veler marked this conversation as resolved.
Show resolved Hide resolved
data.SetText(await PreviewWebView.InvokeScriptAsync("eval", new string[] { markdownBodyEl }) ?? string.Empty);

Clipboard.SetContentWithOptions(data, new ClipboardContentOptions() { IsAllowedInHistory = true, IsRoamable = true });
Clipboard.Flush();
}
catch (Exception ex)
{
Logger.LogFault("Failed to copy from webview", ex);
}
}
}
}