Skip to content

Commit 723bb59

Browse files
committed
fixed undo/redo functionality
1 parent 78a73cf commit 723bb59

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed

Diff for: gradle/project-info.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// -----------------------------------------------------------------------------
44
ext.publishing.artifactId = project.name.toLowerCase()
55
ext.publishing.groupId = 'eu.mihosoft.monacofx'
6-
ext.publishing.versionId = '0.0.5'
6+
ext.publishing.versionId = '0.0.6'
77

88
ext.publishing.developerName = 'Michael Hoffer'
99
ext.publishing.developerAlias = 'miho'

Diff for: src/main/java/eu/mihosoft/monacofx/Document.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public class Document {
3434
private JSObject editorGlobal;
3535
private JSObject window;
3636

37+
private boolean updatingText;
38+
3739
private final StringProperty textProperty = new SimpleStringProperty();
3840
private final StringProperty languageProperty = new SimpleStringProperty();
3941
private final IntegerProperty numberOfLinesProperty = new SimpleIntegerProperty();
@@ -50,14 +52,19 @@ void setEditor(WebEngine engine, JSObject window, JSObject editor) {
5052

5153
// text changes -> js
5254
textProperty.addListener((ov) -> {
53-
editor.call("setValue", getText());
55+
if(!updatingText) editor.call("setValue", getText());
5456
});
5557

5658
// keep a global reference because it's garbage collected otherwise
5759
jsfListener = new JFunction( args -> {
5860
String text = (String) editor.call("getValue");
5961
if(text!=null) {
60-
setText(text);
62+
try {
63+
updatingText = true;
64+
setText(text);
65+
}finally {
66+
updatingText=false;
67+
}
6168
numberOfLinesProperty.setValue(text.split("\\R").length);
6269
}
6370
return null;

Diff for: src/main/java/eu/mihosoft/monacofx/ViewController.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,40 @@
3030
public final class ViewController {
3131

3232
private final Editor editor;
33+
private JSObject window;
3334

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

38+
private JFunction scrollChangeListener;
39+
3740
public ViewController(Editor editor) {
3841
this.editor = editor;
3942
}
4043

4144
void setEditor(JSObject window, JSObject editor) {
45+
this.window = window;
4246
// initial scroll
4347
editor.call("setScrollPosition", getScrollPosition());
4448
// scroll changes -> js
4549
scrollPositionProperty().addListener((ov) -> {
4650
editor.call("setScrollPosition", getScrollPosition());
4751
});
4852
// scroll changes <- js
49-
window.setMember("scrollChangeListener", new JFunction( args -> {
53+
scrollChangeListener = new JFunction( args -> {
5054
int pos = (int) editor.call("getScrollTop");
5155
setScrollPosition(pos);
5256
return null;
53-
}));
57+
});
58+
window.setMember("scrollChangeListener", scrollChangeListener);
59+
}
60+
61+
public void undo() {
62+
window.call("undo");
63+
}
64+
65+
public void redo() {
66+
window.call("redo");
5467
}
5568

5669
public void setScrollPosition(int posIdx) {

Diff for: src/main/resources/eu/mihosoft/monacofx/monaco-editor-0.20.0/index.html

+47
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,55 @@
126126
}
127127
});
128128

129+
// UNDO-/REDO functionality
130+
const initialVersion = editorView.getModel().getAlternativeVersionId();
131+
let currentVersion = initialVersion;
132+
let lastVersion = initialVersion;
133+
editorView.onDidChangeModelContent(e => {
134+
const versionId = editorView.getModel().getAlternativeVersionId();
135+
// undoing
136+
if (versionId < currentVersion) {
137+
// REDO AVAILABLE
138+
// enableRedoButton();
139+
140+
// no more undo possible
141+
if (versionId === initialVersion) {
142+
// UNDO NOT AVAILABLE
143+
// disableUndoButton();
144+
}
145+
} else {
146+
// redoing
147+
if (versionId <= lastVersion) {
148+
// redoing the last change
149+
if (versionId == lastVersion) {
150+
// REDO NOT AVAILABLE
151+
// disableRedoButton();
152+
}
153+
} else { // adding new change, disable redo when adding new changes
154+
// REDO NOT AVAILABLE
155+
// disableRedoButton();
156+
if (currentVersion > lastVersion) {
157+
lastVersion = currentVersion;
158+
}
159+
}
160+
161+
// UNDO AVAILABLE
162+
// enableUndoButton();
163+
}
164+
currentVersion = versionId;
165+
});
166+
129167
});
130168

169+
function undo() {
170+
editorView.trigger('aaaa', 'undo', 'aaaa');
171+
editorView.focus();
172+
}
173+
174+
function redo() {
175+
editorView.trigger('aaaa', 'redo', 'aaaa');
176+
editorView.focus();
177+
}
131178

132179
function getCode() {
133180

0 commit comments

Comments
 (0)