Simplecto.Avalonia.RichTextBox
1.0.7
Please use the latest version 1.0.9
See the version list below for details.
dotnet add package Simplecto.Avalonia.RichTextBox --version 1.0.7
NuGet\Install-Package Simplecto.Avalonia.RichTextBox -Version 1.0.7
<PackageReference Include="Simplecto.Avalonia.RichTextBox" Version="1.0.7" />
paket add Simplecto.Avalonia.RichTextBox --version 1.0.7
#r "nuget: Simplecto.Avalonia.RichTextBox, 1.0.7"
// Install Simplecto.Avalonia.RichTextBox as a Cake Addin
#addin nuget:?package=Simplecto.Avalonia.RichTextBox&version=1.0.7
// Install Simplecto.Avalonia.RichTextBox as a Cake Tool
#tool nuget:?package=Simplecto.Avalonia.RichTextBox&version=1.0.7
A RichTextBox control for Avalonia
As of 2024, Avalonia doesn't yet come with a RichTextBox, and since I needed one I created a "poor-man's version" based on the existing control "SelectableTextBox".
Mirroring WPF, this RichTextBox control uses the concept of a FlowDocument (FlowDoc), which contains Blocks (at the current time, only "Paragraph" is available, although Section or Table could be added later). A "Paragraph" contains "IEditable" objects (EditableRun (from Avalonia.Run) and EditableInlineUIContainer (from Avalonia.InlineUIContainer)) and it is bound to an "EditableParagraph" (inheriting from SelectableTextBlock).
The FlowDoc is at heart merely an ObservableCollection of Blocks bound as the ItemsSource of an ItemsControl inside a ScrollViewer. Upon adding the appropriate key input handling, voila, a RichTextBox magically appeared.
(The hard part after that was implementing the selection logic, because Selection for the RichTextBox has to be able to move between and span multiple Paragraphs (SelectableTextBlocks), both with the keyboard and the mouse, and to allow editing functions that involve splitting or merging Paragraphs. And of course the Inline logic for spanning, inserting, splitting or deleting Inlines.
The RichTextBox has the usual key functions:<br> Ctrl-B for bold/unbold<br> Ctrl-I for italic/unitalic<br> Ctrl-U for underline/remove underline<br> Ctrl-Z for undo<br> Ctrl-A for select all<br>
The FlowDoc has a "Selection" property, with Start, End, Length, Select, Delete, Text, etc.
The RichTextBox also includes the concept of TextRange (of which Selection is merely a special case), which can be defined to format text from code independent from the current FlowDoc.Selection. A new TextRange is created with a Start and End (and its owning FlowDoc), whereby it is automatically added to the FlowDoc's TextRanges List so its Start and/or End can be updated whenever text changes in the FlowDoc require it. TextRange also has an ApplyFormatting property which allows any AvaloniaProperty to be applied that pertains to Inlines.
I've tried to add Undos for all editing possibilities, but Undo hasn't really been stress-tested to the max, yet. There is no particular limit set for number of undos at the current time. (Redo doesn't exist yet.)
The RichTextBox content can be saved/loaded either as straight Xaml or a XamlPackage (to preserve images), similar to the WPF RichTextBox.<br> It can also save and load the FlowDoc content as a Word document (.docx), though only with a subset of Word document features. This includes text, some common text/paragraph formatting, and most images, but not very much else at this time.<br>
Various future to-do improvements include:<br>
**Finish paragraph formatting (such as line spacing) <br> **RTF import/export (need to find a reasonably solid RTF parser)<br> **Save/Load Xaml (to/from a stream) for Selection and any given TextRange <br>
Adding Table and Section Block types<br> Allow the Undo limit to be set <br> Redo functionality (could be a headache) <br> More stress testing <br> A quirk or two at times when extending selection using PageUp or PageDown Key.<br>
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Avalonia (>= 11.0.10)
- Avalonia.Controls.ItemsRepeater (>= 11.0.10)
- Avalonia.Desktop (>= 11.0.10)
- Avalonia.ReactiveUI (>= 11.0.10)
- Avalonia.Skia (>= 11.0.10)
- Avalonia.Themes.Fluent (>= 11.0.10)
- DocumentFormat.OpenXml (>= 3.0.2)
- SixLabors.ImageSharp (>= 3.1.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.9 | 69 | 6/12/2024 |
In version 1.0.6, ability to load/save as Word file (.docx only) has been added. (Word file load error fixed)
Also, FontStyle and Underline attributes are properly saved/loaded now.