Skip to content

Commit

Permalink
fixed undo/redo functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
miho committed Mar 5, 2021
1 parent 78a73cf commit 723bb59
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle/project-info.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// -----------------------------------------------------------------------------
ext.publishing.artifactId = project.name.toLowerCase()
ext.publishing.groupId = 'eu.mihosoft.monacofx'
ext.publishing.versionId = '0.0.5'
ext.publishing.versionId = '0.0.6'

ext.publishing.developerName = 'Michael Hoffer'
ext.publishing.developerAlias = 'miho'
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/eu/mihosoft/monacofx/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class Document {
private JSObject editorGlobal;
private JSObject window;

private boolean updatingText;

private final StringProperty textProperty = new SimpleStringProperty();
private final StringProperty languageProperty = new SimpleStringProperty();
private final IntegerProperty numberOfLinesProperty = new SimpleIntegerProperty();
Expand All @@ -50,14 +52,19 @@ void setEditor(WebEngine engine, JSObject window, JSObject editor) {

// text changes -> js
textProperty.addListener((ov) -> {
editor.call("setValue", getText());
if(!updatingText) editor.call("setValue", getText());
});

// keep a global reference because it's garbage collected otherwise
jsfListener = new JFunction( args -> {
String text = (String) editor.call("getValue");
if(text!=null) {
setText(text);
try {
updatingText = true;
setText(text);
}finally {
updatingText=false;
}
numberOfLinesProperty.setValue(text.split("\\R").length);
}
return null;
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/eu/mihosoft/monacofx/ViewController.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,40 @@
public final class ViewController {

private final Editor editor;
private JSObject window;

//private final ObjectProperty<Position> cursorPositionProperty = new SimpleObjectProperty<>();
private final IntegerProperty scrollPositionProperty = new SimpleIntegerProperty();

private JFunction scrollChangeListener;

public ViewController(Editor editor) {
this.editor = editor;
}

void setEditor(JSObject window, JSObject editor) {
this.window = window;
// initial scroll
editor.call("setScrollPosition", getScrollPosition());
// scroll changes -> js
scrollPositionProperty().addListener((ov) -> {
editor.call("setScrollPosition", getScrollPosition());
});
// scroll changes <- js
window.setMember("scrollChangeListener", new JFunction( args -> {
scrollChangeListener = new JFunction( args -> {
int pos = (int) editor.call("getScrollTop");
setScrollPosition(pos);
return null;
}));
});
window.setMember("scrollChangeListener", scrollChangeListener);
}

public void undo() {
window.call("undo");
}

public void redo() {
window.call("redo");
}

public void setScrollPosition(int posIdx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,55 @@
}
});

// UNDO-/REDO functionality
const initialVersion = editorView.getModel().getAlternativeVersionId();
let currentVersion = initialVersion;
let lastVersion = initialVersion;
editorView.onDidChangeModelContent(e => {
const versionId = editorView.getModel().getAlternativeVersionId();
// undoing
if (versionId < currentVersion) {
// REDO AVAILABLE
// enableRedoButton();

// no more undo possible
if (versionId === initialVersion) {
// UNDO NOT AVAILABLE
// disableUndoButton();
}
} else {
// redoing
if (versionId <= lastVersion) {
// redoing the last change
if (versionId == lastVersion) {
// REDO NOT AVAILABLE
// disableRedoButton();
}
} else { // adding new change, disable redo when adding new changes
// REDO NOT AVAILABLE
// disableRedoButton();
if (currentVersion > lastVersion) {
lastVersion = currentVersion;
}
}

// UNDO AVAILABLE
// enableUndoButton();
}
currentVersion = versionId;
});

});

function undo() {
editorView.trigger('aaaa', 'undo', 'aaaa');
editorView.focus();
}

function redo() {
editorView.trigger('aaaa', 'redo', 'aaaa');
editorView.focus();
}

function getCode() {

Expand Down

0 comments on commit 723bb59

Please sign in to comment.