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

Rich Text Editor in avalonia #4625

Open
fengyinyong opened this issue Sep 8, 2020 · 25 comments
Open

Rich Text Editor in avalonia #4625

fengyinyong opened this issue Sep 8, 2020 · 25 comments

Comments

@fengyinyong
Copy link

I need a rich text editor in my avalonia project,just like the rich text editor in WPF, witch can add words、images.....
I have read the Issue #4410 and https://github.com/AvaloniaUI/AvaloniaEdit/.
But they do not fulfill my needs

@kekekeks
Copy link
Member

kekekeks commented Sep 8, 2020

FlowDocument text model is being worked on

@fengyinyong
Copy link
Author

When work it out,please tell me in this issue. Thank you very much.

@dragonman117
Copy link

FlowDocument text model is being worked on

Is there any chance we can get an indication of where this work is going on? I don't really see it on any of the roadmaps I've been able to find, and I don't see any branches that appear to be showing any active work for this feature.

@danwalmsley
Copy link
Member

@Gillibald is actively working on it

@Gillibald
Copy link
Contributor

#5461

@warappa
Copy link

warappa commented Nov 10, 2022

Is there any news on this topic as PR #5461 was abandoned?

And as demand was mentioned in the PR: What's your preferred way of expressing demand? Via thumbs-up on this ticket? Somewhere in discussion section?

Anyway, thanks for your efforts!

@maxkatz6
Copy link
Member

Rich TextBlock with inlines from #5461 was already added after multiple other PRs. As well as selectable TextBlock.
RichTextBox specifically wasn't yet done. Current text rendering with IME support is in higher priority. After that work on RichTextBox can be started. @Gillibald might know better estimates.
So, since it is already planned to be added earlier or sooner, I don't think there is a point in voting.

Meanwhile, AvaloniaEdit is still a working alternative for some use cases.

@JesseDietrichson
Copy link

@Gillibald @maxkatz6 Is there any way we could discuss bumping the priority of RichTextBox + Flowdocument. Root is interested in funding this effort.

Would love to chat about this more. My email is [email protected]

@Gillibald
Copy link
Contributor

Please contact us at [email protected] and we can setup a meeting to discuss your requirements and what we can do to achieve them.

@JesseDietrichson
Copy link

@Gillibald FYI, I reached out to the above email.

Thanks!

@parentelement
Copy link

parentelement commented Apr 29, 2023

Not sure if people are still waiting on this but I finally decided to just build my own. I explored MAUI, which doesn't have one and doesn't have native Linux support, Uno doesn't have one, and Avalonia doesn't have one, so I started building my own based on my modified version of the RichTextKit library. I'm still working on features and working through some bugs and I still to add some hooks and scroll bars but currently I support:

  • Keybaord navigation and mouse interaction.
  • Text Styles (Bold, Italic, Underline, Subscript, Superscript, etc... everything RichTextKit does)
  • Merging styles (Applying a style to a selection merges that style with all runs in the selection - RichTextKit doesn't do this, it's a modification).
  • Mouse wheel scrolling - code based scrolling.
  • Copy/Paste/Undo/Redo
  • Fonts, Font Sizes
  • X and Y Scaling (zoom in/out)

There's a lot of work still to be done and I'm building on top of my own abstraction that'll allow me to build the same control for other platforms as well, but I'm starting with Avalonia. Here's a brief rough video of it working.

https://youtu.be/AKlW6Cl5SuU

@timunie
Copy link
Contributor

timunie commented May 2, 2023

@parentelement that looks really awsome. If the control will be open source, consider to add it to awsome avalonia 👍

@parentelement
Copy link

@timunie Thanks! Since I recorded that video I've fixed a few bugs, started breaking into a proper component structure, and got scrolling working with scroll bars. There are still quite a few bugs and some odd behavior, and also a memory leak issue due to how RTK handles Style caching which doesn't account for rendering/UI happening on different threads (the StyleManager is a ThreadLocal on a static object). I also have a lot of work to do in finalizing the API so I can provide a simple way to control the document while still allowing low level access if someone wants to build something custom on top of it.

The contents of the document are stored in a structure close to that of Flowdocument but not exactly, so I need to enhance RichTextKit to provide enumerable access to the content and styles so getting the text out is easier. By default it only exposes the text as a plain text block.

Other things I'm working on:

  • Configurable and collapsible paragraph margins to allow enabling double space between paragraphs
  • Configurable paragraph indent (which isn't currently supported at all) so first lines can be indented
  • Enumerable access to the document content (likely a yield/return that will transform into a common format)
  • A way to load content so it can be populated either directly in the control or via code
  • Copying and Pasting styles (right now copy/paste is plain text only)
  • Image support (This likely won't make my first release when I do it)
  • Bug fixing - scrolling via keyboard / caret navigation and extending selection ranges, fixing some style merging issues
  • A machanism for notifying consumers where a style property has more than one value in a selection range (such as more than one font in a rage so a font combobox can react accordingly).

It already supports page margins, left, right, centered text. Looking into Justified text and possibly assigning margins at the paragraph level.

Also, as a separate project I'd like to provide some import/export support for things like HTML/PDF/RTF/OpenXML.

The intent for sure is to at least publish and open source my modifications to RichTextKit as well as the core layer that provides all of the interaction, shortcut handling, etc. This would allow anyone to easily build a UI control on top of it. I haven't decided yet whether I'll open source the final editor control. I likely will for Avalonia, but may keep it closed for Uno and Maui.

@parentelement
Copy link

Couple more points to add... I stress tested it with a couple of documents, one with 200,000 words and the other with 2.4 Million words. Of course memory consumption gets fairly high at that point but still under 1 Gig total for the whole application and the operation was still buttery smooth. One thing I want to figure out though, and I'm not super familiar with Avalonia, but right now it uses the feature lease to get access to the Skia canvas.

The problems there is that, for one, Skia isn't guaranteed to always be the renderer that Avalonia uses. And secondly, I have no control over the individual rendered unit because everything is being don't together, so I don't have a good way to do caching, meaning I'm having to do some recomputation and render the document within the visible bounds every frame.

While I'm happy with the performance, I think I'm going to change my control to use a WriteableBitmap and construct my own Skia canvas to draw on it. This will allow me to continue to use Skia even if it's not the Avalonia renderer. It will also allow me to cache the document bitmap and just re-render it instead of redrawing it. And finally, the caret then becomes a separate layer that's just composited on top of the document (again allowing me to cache the document bitmap because it doesn't matter if the caret is visible or not).

@Gillibald
Copy link
Contributor

In general Avalonia is just lacking a proper document model. If you were using the TextFormatter you would get richer features with optional virtualization support.

@parentelement
Copy link

@Gillibald 100%. If I was developing purely for Avalonia I'd probably just go the route of porting the WPF controls directly, but I want to support Maui and Uno as well. Fortunately, RichTextKit already essentially provides what I need because I'm only actually rendering what's within the visible bounds so rendering isn't an issue. To add, though, Skia by itself isn't always fantastic for text. RTK uses Harfbuzz for text shaping and ligature support which makes supporting the whole set of Unicode (and even emojis) possible more richly than what TextFormatter can provide.

@Gillibald
Copy link
Contributor

TextFormatter supports Unicode 13 and is optimized for memory consumption. Its API is designed to be used to support arbitrary sources of text and supports random access so virtualization is no issue. I just wanted to inform you.

@parentelement
Copy link

parentelement commented May 2, 2023 via email

@parentelement
Copy link

Looks like TextFormatter is only ported to Avalonia, but I'm pretty happy with RTK. Making some pretty decent progress.

https://youtu.be/Mutb0SWRjwQ

@doublecount
Copy link

@parentelement
saw your great work on RichTextBox. Is there already any plan for release, what kind of license do you consider?

@parentelement
Copy link

@parentelement saw your great work on RichTextBox. Is there already any plan for release, what kind of license do you consider?

It's in progress. My focus has been cleaning up some bugs. Tentatively, the plan is to release it as two separate packages. One will be open source for sure. That have the RTK fork along with the core interface library and I'll have adapters for each target platform starting with Avalonia.

@hematec
Copy link

hematec commented Jan 17, 2024

@parentelement
you've done an impressive job on this!
Are there any news for a planned release date?

@parentelement
Copy link

@parentelement you've done an impressive job on this! Are there any news for a planned release date?

Thanks, appreciate the kind words! There's no specific time frame yet. I'm still working on it but it's somewhat sporadic as I'm the only contributor currently.

@mateli
Copy link

mateli commented Oct 11, 2024

@parentelement I suggest that you decide what to open source asap so that others can contribute. Also I do not think that a fully functioning rich text editor is required for making a first release. Instead deciding on a minimal viable functionality and making sure that future updates do not regress the controls behavior is useful.

On another note it may be useful to considering other rendering engines than Skia. Vello for example seems really fast and it uses wgpu for rendering so it should work on even more platforms than Skia. Using wgpu for headless rendering and then putting the result into the Skia pipeline could be fast if it can be done within the GPU.

Although most of that would be premature. The first release should be something that can display and edit simple rich text.

@alexandrehtrb
Copy link

To be honest, I just need a TextBox that can show coloured parts of text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests