Skip to content

Commit 08e0a36

Browse files
committed
Copy selected text as highlighted HTML
1 parent 5e25a8b commit 08e0a36

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/CodeSnip/MainWindow.xaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@
395395
Width="16" Height="16" Stretch="Uniform" />
396396
</MenuItem.Icon>
397397
</MenuItem>
398+
<MenuItem Header="Colored HTML" Click="CopyAsHtmlColored_Click">
399+
<MenuItem.Icon>
400+
<Path Data="{StaticResource FileHtml}" Fill="{DynamicResource MahApps.Brushes.AccentBase}"
401+
Width="16" Height="16" Stretch="Uniform" />
402+
</MenuItem.Icon>
403+
</MenuItem>
398404
<MenuItem Header="BBCode" Click="CopyAsBBCode_Click">
399405
<MenuItem.Icon>
400406
<Path Data="{StaticResource CodeBrackets}" Fill="{DynamicResource MahApps.Brushes.AccentBase}"

src/CodeSnip/MainWindow.xaml.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,30 @@ private void CopyAsHtml_Click(object sender, RoutedEventArgs e)
557557
}
558558
}
559559

560+
private void CopyAsHtmlColored_Click(object sender, RoutedEventArgs e)
561+
{
562+
if (string.IsNullOrEmpty(textEditor.SelectedText))
563+
return;
564+
565+
var selection = textEditor.TextArea.Selection;
566+
if (selection.IsEmpty)
567+
return;
568+
569+
var htmlOptions = new ICSharpCode.AvalonEdit.Highlighting.HtmlOptions();
570+
string fragment = selection.CreateHtmlFragment(htmlOptions);
571+
572+
string wrappedHtml = $"<div>{fragment}</div>";
573+
574+
try
575+
{
576+
Clipboard.SetText(wrappedHtml);
577+
}
578+
catch (Exception ex)
579+
{
580+
_ = MessageBox.Show($"Failed to copy to clipboard: {ex.Message}");
581+
}
582+
}
583+
560584
private void CopyAsBBCode_Click(object sender, RoutedEventArgs e)
561585
{
562586
if (string.IsNullOrEmpty(textEditor.SelectedText))

0 commit comments

Comments
 (0)