Comments (15)
Do the following three steps ....
- Declare private field:
private SuspendableYes suspendUndo = new SuspendableYes();
- Set undo manager when creating area:
textArea.setUndoManager( UndoUtils.richTextSuspendableUndoManager( textArea, suspendUndo ) );
- Use suspendUndo when required:
suspendUndo.suspendWhile( () -> textArea.setStyleSpans( position, ssb.create() ) );
from richtextfx.
Would codeArea.getStyleRangeAtPosition( pos );
maybe help ?
from richtextfx.
@Jugen Thank you for your answer. This is my code:
System.out.println("The position:" + position);
var range = this.textArea.getStyleRangeAtPosition(position - 1);
System.out.println("The range:" + range);
And this is output:
The position:304
The range:16, 20
Firstly I couldn't understand how it is possible. But it can be possible if returned range is considered in specific paragraph. But, what if my span takes two paragraphs? Then how can I take the multiparagraph span at position X? Or I misunderstand something.
from richtextfx.
Yeah, I'm surprised by that as well !? Looking at the code reveals that the returned IndexRange is relative to the paragraph as you guessed.
I'd say this is a bug but I'm not going to change it since it's been like that for how long and may break somebody's code ? I will however add a note to the JavaDoc for those methods.
I'd suggest using getStyleRangeAtPosition( par, col )
instead where it makes more sense that the returned IndexRange is relative to the paragraph.
For style spans over multiple paragraphs you could try the following based on what is done in Paragraph:
// Must use this method of getStyleSpans or the one with parameter IndexRange and not any other !
var spans = codeArea.getStyleSpans( 0, codeArea.getLength() );
var offset = spans.offsetToPosition( targetPos, Bias.Backward );
var styleStart = offset.getMinor();
var styleEnd = styleStart + spans.getStyleSpan( offset.getMajor() ).getLength();
from richtextfx.
@Jugen Thank you for your answer. I couldn't make it work:
My code:
public IndexRange getRealStyleRangeAt(int position) {
// Must use this method of getStyleSpans or the one with parameter IndexRange and not any other !
var spans = this.getStyleSpans(0, this.getLength());
var offset = spans.offsetToPosition(position, Bias.Backward);
var styleStart = offset.getMinor();
var styleEnd = styleStart + spans.getStyleSpan( offset.getMajor() ).getLength();
return new IndexRange(styleStart, styleEnd);
}
Usage:
System.out.println("The position:" + position);
var range = this.textArea.getRealStyleRangeAt(position - 1);
System.out.println("The range:" + range);
Output:
The position:302
The range:2, 5
from richtextfx.
I can provide test code, if it's required. I think it is necessary to add to RichTextFX API something like this.
from richtextfx.
Please do provide test code, then I'll have go at it ....
from richtextfx.
This is code:
public class JavaFxTest7 extends Application {
private final CodeArea codeArea = new CodeArea();
@Override
public void start(Stage stage) {
var text = """
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.foo</groupId>
<artifactId>bar</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>Foo</name>
""";
this.codeArea.appendText(text);
VBox.setVgrow(codeArea, Priority.ALWAYS);
StyleSpansBuilder<Collection<String>> ssb = new StyleSpansBuilder<>();
ssb.add(Collections.singletonList("highlighted"), 3);
codeArea.setStyleSpans(300, ssb.create());
var button = new Button("Test");
button.setOnAction((e) -> {
var position = this.codeArea.getCaretPosition();
System.out.println("The position:" + position);
var range = this.getRealStyleRangeAt(position - 1);
System.out.println("The range:" + range);
});
var css= this.getClass().getResource("test7.css").toExternalForm();
Scene scene = new Scene(new VBox(codeArea, button), 600, 200);
scene.getStylesheets().add(css);
stage.setScene(scene);
stage.show();
}
public IndexRange getRealStyleRangeAt(int position) {
// Must use this method of getStyleSpans or the one with parameter IndexRange and not any other !
var spans = this.codeArea.getStyleSpans(0, this.codeArea.getLength());
var offset = spans.offsetToPosition(position, TwoDimensional.Bias.Backward);
var styleStart = offset.getMinor();
var styleEnd = styleStart + spans.getStyleSpan( offset.getMajor() ).getLength();
return new IndexRange(styleStart, styleEnd);
}
public static void main(String[] args) {
launch();
}
}
from richtextfx.
Try updating getRealStyleRangeAt
to the following:
public IndexRange getRealStyleRangeAt( int position )
{
var length = 0;
for ( var ss : codeArea.getStyleSpans( 0, codeArea.getLength() ) )
{
if ( (length += ss.getLength()) >= position )
{
var start = length - ss.getLength();
return new IndexRange( start, length );
}
}
return new IndexRange( 0,0 );
}
from richtextfx.
@Jugen This is what I wanted to avoid. As I understand RichTextFX has its own model for style span ranges and I wanted to use it instead if iterating all style spans. Or there is no chance to get REAL range for concrete position?
from richtextfx.
Yeah, I don't like the iteration either but I don't know of another way as I haven't been able to find it in the model.
The StyleSpans
interface extends TwoDimensional
which only seems to be able to give us relative ranges and not absolute ones.
The API allows us to get a StyleSpan
at a particular position and to tell us where in that StyleSpan
the position is but it has no knowledge of it's own position and StyleSpans
doesn't seem to know the absolute position either.
I can try and add a start field to StyleSpan
which StyleSpansBuilder
can update when creating StyleSpans
. Then integrate getRealStyleRangeAt
into StyleSpans
which can then be reduced down to the following I think:
var offset = spans.offsetToPosition( position, Bias.Backward );
var span = spans.getStyleSpan( offset.getMajor() );
var spanStart = span.getStart();
return new IndexRange( spanStart, spanStart + span.getLength() );
from richtextfx.
@Jugen The only thing I can suggest is to distinguish relative ranges
and absolute ones
in API. Maybe something like this getAbsoluteStyleRangeAt(...)
.
from richtextfx.
Please see the PR that I've submitted to try and address this issue for you.
from richtextfx.
@Jugen I've checked. It seems to work. At least, all my tests finally passed. Only one question - when I do:
textArea.setStyleSpans(position, ssb.create())
then this modification is saved to undo list as a separate action. But this is not right as it is not an user action. Could you say, how I can exclude my style modification from undo actions? By other words undo manager must ignore my style operation.
from richtextfx.
@Jugen Yes, it helped. Thank you very much!
from richtextfx.
Related Issues (20)
- Question: how I can use InlineCssTextField with multiple color and pattern text please HOT 3
- Question: codeArea.removeSelection delayed display HOT 1
- CodeArea seems to intercept the input method, preventing it from being passed to other TextFields.
- Remove null character at end of pasted string
- JDK8 project HOT 1
- Question: is it possible to disable scrolling to bottom on appending text? HOT 2
- Question: How to set background color for current line/paragraph using CSS? HOT 4
- Question: Is possible to detect when INSERT mode is used? HOT 16
- Question: How to make wave underline? HOT 4
- Question: What is the strategy to implement Highlight All for found matches for modifiable text? HOT 5
- Question: Getting font size of the CodeArea HOT 3
- Question: when to use StyleSpans? HOT 8
- Bug: Observer is not removed from richChanges() HOT 3
- Code building fails on Ubuntu HOT 2
- How to remove all styles from undo text? HOT 4
- How to convert absolute position to paragraph/column position? HOT 1
- Question: is textArea.getLength() equal to the number of Unicode code points HOT 5
- Bug: text area removes selection on text append HOT 10
- Selected color with alpha add extra colors HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from richtextfx.