RichTextFX icon indicating copy to clipboard operation
RichTextFX copied to clipboard

Any efficient way to setStyle for multiple positions?

Open cyfung1031 opened this issue 1 year ago • 2 comments

I am using setStyleClass of StyleClassedTextArea to make colors in a long text paragraph. I have different colors to apply to different words.

Say classNames style-1 is red, style-2 is blue, style-3 is green.

If i want to apply style-1 to multiple sections of the textarea, I need to run them as a loop.

area.setStyleClass(2, 5, "style-1");
area.setStyleClass(6, 11, "style-1");
area.setStyleClass(23, 24, "style-1");
area.setStyleClass(52, 65, "style-1");
area.setStyleClass(76, 121, "style-1");
area.setStyleClass(150, 154, "style-1");
...

I find it is slow. I guess it is due to it renders every time I applied the styleclass. I tried to set it invisible first but in vain. Any way to make the rendering more efficient?

or any batchEditStart(), batchEditEnd() for styling? So it will not immediately render before I set all style-1, style-2, style-3

cyfung1031 avatar Mar 09 '23 23:03 cyfung1031

I think I have found the suitable demo for this question.

https://github.com/FXMisc/RichTextFX/blob/master/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/JavaKeywordsDemo.java

https://github.com/FXMisc/RichTextFX/blob/master/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/SpellCheckingDemo.java

I should have set a kind of observer to observe the changes before setting the text to the textarea.

I would suggest that it is better to add some explanation for StyleSpans how it works. I did not know that it requires reactfx (org.reactfx.collection.ListModification). As a beginner to this library, I thought org.fxmisc.richtext is just enough.

And from the word StyleSpans suggested, I did not know it can work with style name using Collections.singleton(styleClass), and even it is for a continuous segment of text, we can use Collections.emptyList() to by pass those segments we don't need to apply any formatting.


However, I do not understand the differences between these two implementations.

        textArea.multiPlainChanges()
                .successionEnds(Duration.ofMillis(500))
                .subscribe(change -> {
                    textArea.setStyleSpans(0, computeHighlighting(textArea.getText()));
                });
    codeArea.getVisibleParagraphs().addModificationObserver
        (
            new VisibleParagraphStyler<>( codeArea, this::computeHighlighting )
        );

Any pros & cons for these two implementations? How to choose?

cyfung1031 avatar Mar 10 '23 09:03 cyfung1031

When I was looking at StyleSpans this morning I also thought: this needs better documentation. :-)

With regards to the two implementations: The first implementation passes the textArea's entire text to computeHighlighting, but only 500ms after the last edit. The second implementation does an an immediate computeHighlighting, but only on any changed visible paragraphs.

Jugen avatar Mar 10 '23 13:03 Jugen