Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
84 changes: 43 additions & 41 deletions AvRichTextBox/FlowDocument/FlowDocument_EditingFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,54 @@ namespace AvRichTextBox;

public partial class FlowDocument
{
internal void SetRangeToText(TextRange tRange, string newText)
{ //The delete range and SetRangeToText should constitute one Undo operation

Paragraph startPar = tRange.StartParagraph;
int rangeStart = tRange.Start;
int rangeEnd = tRange.End;
int deleteRangeLength = tRange.Length;
int parIndex = Blocks.IndexOf(startPar);
bool firstParEmpty = startPar.Inlines[0] is EditableRun erun && erun.Text == "";
bool firstParWasDeleted = startPar.StartInDoc == rangeStart && startPar.EndInDoc <= rangeEnd && !firstParEmpty;

//Delete any selected text first
if (tRange.Length > 0)
{
DeleteRange(tRange, false, false); // no undo, handled by PasteUndo
tRange.CollapseToStart();
SelectionExtendMode = ExtendMode.ExtendModeNone;
}

//Debug.WriteLine("first par deleted: " + firstParWasDeleted);
internal void SetRangeToText(TextRange tRange, string newText, bool copyFormatting = true)
{ //The delete range and SetRangeToText should constitute one Undo operation

Paragraph startPar = tRange.StartParagraph;
int rangeStart = tRange.Start;
int rangeEnd = tRange.End;
int deleteRangeLength = tRange.Length;
int parIndex = Blocks.IndexOf(startPar);
bool firstParEmpty = startPar.Inlines[0] is EditableRun erun && erun.Text == "";
bool firstParWasDeleted = startPar.StartInDoc == rangeStart && startPar.EndInDoc <= rangeEnd && !firstParEmpty;

//Delete any selected text first
if (tRange.Length > 0)
{
DeleteRange(tRange, false, false); // no undo, handled by PasteUndo
tRange.CollapseToStart();
SelectionExtendMode = ExtendMode.ExtendModeNone;
}

//Debug.WriteLine("first par deleted: " + firstParWasDeleted);

if (tRange.StartInline is not IEditable startInline) return;

List<IEditable> splitInlines = SplitRunAtPos(tRange.Start, startInline, GetCharPosInInline(startInline, tRange.Start));
List<IEditable> splitInlines = SplitRunAtPos(tRange.Start, startInline, GetCharPosInInline(startInline, tRange.Start));

int startInlineIndex = startPar.Inlines.IndexOf(splitInlines[0]) + 1;
int startInlineIndex = startPar.Inlines.IndexOf(splitInlines[0]) + 1;

if (splitInlines[0] is EditableRun sRun)
{
EditableRun newEditableRun = new(newText)
{
FontFamily = sRun.FontFamily,
FontWeight = sRun.FontWeight,
FontStyle = sRun.FontStyle,
FontSize = sRun.FontSize,
TextDecorations = sRun.TextDecorations,
Background = sRun.Background,
BaselineAlignment = sRun.BaselineAlignment,
Foreground = sRun.Foreground
};

startPar.Inlines.Insert(startInlineIndex, newEditableRun);

if (splitInlines[0].InlineText == "")
startPar.Inlines.Remove(splitInlines[0]);
}
if (splitInlines[0] is EditableRun sRun)
{
EditableRun newEditableRun = new(newText);

if (copyFormatting)
{
newEditableRun.FontFamily = sRun.FontFamily;
newEditableRun.FontWeight = sRun.FontWeight;
newEditableRun.FontStyle = sRun.FontStyle;
newEditableRun.FontSize = sRun.FontSize;
newEditableRun.TextDecorations = sRun.TextDecorations;
newEditableRun.Background = sRun.Background;
newEditableRun.BaselineAlignment = sRun.BaselineAlignment;
newEditableRun.Foreground = sRun.Foreground;
}

startPar.Inlines.Insert(startInlineIndex, newEditableRun);

if (splitInlines[0].InlineText == "")
startPar.Inlines.Remove(splitInlines[0]);
}

startPar.CallRequestInvalidateVisual();
startPar.CallRequestTextLayoutInfoStart();
Expand Down
1 change: 1 addition & 0 deletions AvRichTextBox/RichTextBox/RichTextBox.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<ContextMenu x:Name="DocContextMenu" Opening="DocContextMenu_Opening">
<MenuItem Header="Copy" Click="CopySelectionMenuItem_Click" IsEnabled="{Binding HasSelectedText}"/>
<MenuItem Header="Paste" Click="PasteSelectionMenuItem_Click" />
<MenuItem Header="Paste without formatting" Click="PasteSelectionAsPlainTextMenuItem_Click" />
<MenuItem Header="Cut" Click="CutSelectionMenuItem_Click" IsEnabled="{Binding HasSelectedText}"/>
<MenuItem Header="Delete" Click="DeleteSelectionMenuItem_Click" IsEnabled="{Binding HasSelectedText}"/>
<Separator/>
Expand Down
6 changes: 6 additions & 0 deletions AvRichTextBox/RichTextBox/RichTextBox.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ private void PasteSelectionMenuItem_Click(object? sender, RoutedEventArgs e)
if (IsReadOnly) return;
PasteFromClipboard();
}

private void PasteSelectionAsPlainTextMenuItem_Click(object? sender, RoutedEventArgs e)
{
if (IsReadOnly) return;
PasteFromClipboard(plainTextOnly: true);
}

private void CutSelectionMenuItem_Click(object? sender, RoutedEventArgs e)
{
Expand Down
Loading