Skip to content

Editor area

Giorgio Garofalo edited this page Feb 5, 2021 · 8 revisions

Disclaimer: This page is not going to explain every feature of the editor area since it would take too long as it inherits methods of RichTextFX's CodeArea. However, you can see a full list of its methods on its Javadoc page.

In Chorus, the editor area is the most important part of the UI, as it's where the user interacts the most, and it's built by paragraphs (lines).
From the API, the active area is accessible by calling either getArea() or getActiveTab().area.

Here are some examples:

area.getText();
area.getParagraphs();
area.getParagraph(index);
area.insertText(position, text);
area.replaceText(start, end, text);
area.deleteText(start, end);
area.saveFile();
area.refresh();

Custom highlighting

The API allows to highlight several parts of the area in just one line of code.
Inside the onHighlightUpdate(area), it is possible to call the area.highlight(regex, styleClass) method, which, given a RegEx, applies the style class to the matches.

IMPORTANT! The style class must be declared in an external stylesheet and assigned to the area.

NOTE! The method uses the Java RegEx engine, so that you'll be able to use features natively unsupported in JavaScript.

Example:

function onTabOpen(tab) {
    loadStylesheet('style.css', tab.getArea());
}

function onHighlightingUpdate(area) {
    area.highlight('(?<=abc)def', 'my-class')
}

style.css:

.my-class {
    -fx-fill: red;
}

Result:
Result
Magic!

Area events

It is possible to create listeners for every time the text is modified inside the onAreaTextChange(change, area) event.

change has some useful properties:

  • getPosition() (int) - index where the change happened at
  • getInserted() (string) - added text
  • getInsertionEnd() (int) - index where the insertion stopped at
  • getRemoved() (string) - deleted text
  • getRemovalEnd() (int) - index where the removal stopped at