From 723bb591198933c2baafbd8bd3f441b2e67e167a Mon Sep 17 00:00:00 2001 From: miho Date: Fri, 5 Mar 2021 14:23:54 +0100 Subject: [PATCH] fixed undo/redo functionality --- gradle/project-info.gradle | 2 +- .../java/eu/mihosoft/monacofx/Document.java | 11 ++++- .../eu/mihosoft/monacofx/ViewController.java | 17 ++++++- .../monacofx/monaco-editor-0.20.0/index.html | 47 +++++++++++++++++++ 4 files changed, 72 insertions(+), 5 deletions(-) diff --git a/gradle/project-info.gradle b/gradle/project-info.gradle index 5ec173c..fc806dc 100644 --- a/gradle/project-info.gradle +++ b/gradle/project-info.gradle @@ -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' diff --git a/src/main/java/eu/mihosoft/monacofx/Document.java b/src/main/java/eu/mihosoft/monacofx/Document.java index e4f7d85..f7f84d2 100644 --- a/src/main/java/eu/mihosoft/monacofx/Document.java +++ b/src/main/java/eu/mihosoft/monacofx/Document.java @@ -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(); @@ -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; diff --git a/src/main/java/eu/mihosoft/monacofx/ViewController.java b/src/main/java/eu/mihosoft/monacofx/ViewController.java index 9fd522e..76ffae7 100644 --- a/src/main/java/eu/mihosoft/monacofx/ViewController.java +++ b/src/main/java/eu/mihosoft/monacofx/ViewController.java @@ -30,15 +30,19 @@ public final class ViewController { private final Editor editor; + private JSObject window; //private final ObjectProperty 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 @@ -46,11 +50,20 @@ void setEditor(JSObject window, JSObject editor) { 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) { diff --git a/src/main/resources/eu/mihosoft/monacofx/monaco-editor-0.20.0/index.html b/src/main/resources/eu/mihosoft/monacofx/monaco-editor-0.20.0/index.html index 8b21a9a..60ef1bb 100644 --- a/src/main/resources/eu/mihosoft/monacofx/monaco-editor-0.20.0/index.html +++ b/src/main/resources/eu/mihosoft/monacofx/monaco-editor-0.20.0/index.html @@ -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() {