miho / MonacoFX

JavaFX editor node based on the powerful Monaco editor that drives VS Code
MIT License
75 stars 25 forks source link

text property doesn't change #30

Closed DerekL0576 closed 4 months ago

DerekL0576 commented 4 months ago

When you type in the editor, it doesn't update the text property. So getEditor().getDocument().getText() either returns null or whatever text you manually set with setText().

It makes this whole thing pretty much useless.

DerekL0576 commented 4 months ago

To work around this issue I made these changes to src/main/java/eu/mihosoft/monacofx/Document.java

void setEditor(WebEngine engine, JSObject window, JSObject editor) {
....
    // initial text
    editor.call("setValue", textProperty.get());

    // text changes -> js
    textProperty.addListener((ov) -> {
        if (!updatingText) editor.call("setValue", textProperty.get());
    });
....
}
public String getText() {
    String text = (String) editor.call("getValue");
    try {
        updatingText = true;
        textProperty().set(text);
    } finally {
        updatingText = false;
    }
    numberOfLinesProperty.setValue(text.split("\\R").length);
    return text;
}

It's not perfect, but I can at least set the text and get the edited changes to save to file. The contentChangeListener is never triggered so I still can't get changed notifications.

EasyG0ing1 commented 4 months ago

@DerekL0576


MonacoFX mfx = new  MonacoFX();
mfx.getEditor().getDocument().textProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null)
                mfx.setText(newValue);
        });
DerekL0576 commented 4 months ago

Thanks @EasyG0ing1.

I can see now that we need to set a listener on the textProperty for the contentChangeListener to get triggered. The listener doesn't actually have to do anything, it works fine with an empty lambda function.

monacoFX.getEditor().getDocument().textProperty().addListener((observable, oldValue, newValue) -> {});

Weird, but it works. 👍

DerekL0576 commented 4 months ago

I actually found the real cause of my problem. I had to add exports eu.mihosoft.monacofx; to my module-info. I added it after my initial workaround and inadvertently fixed the original problem without realising. My bad.

EasyG0ing1 commented 4 months ago

Thanks @EasyG0ing1.

I can see now that we need to set a listener on the textProperty for the contentChangeListener to get triggered. The listener doesn't actually have to do anything, it works fine with an empty lambda function.

monacoFX.getEditor().getDocument().textProperty().addListener((observable, oldValue, newValue) -> {});

Weird, but it works. 👍

Thats Interesting ... never tried it with an empty listener before.

EasyG0ing1 commented 4 months ago

I actually found the real cause of my problem. I had to add exports eu.mihosoft.monacofx; to my module-info. I added it after my initial workaround and inadvertently fixed the original problem without realising. My bad.

@DerekL0576 That kinda makes sense ... though it shouldn't be required.