From d7f9b5fa1de52e812b9738105d672f26ed03d0a9 Mon Sep 17 00:00:00 2001 From: Holger Stenger Date: Thu, 2 Jul 2015 23:01:11 +0200 Subject: [PATCH 1/5] Restore scroll position after content update. --- .../markdown/preview/ext/FXHtmlView.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java b/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java index 92782d1..3057ede 100644 --- a/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java +++ b/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java @@ -4,6 +4,7 @@ import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; +import javafx.concurrent.Worker; import javafx.embed.swing.JFXPanel; import javafx.event.EventHandler; import javafx.scene.Scene; @@ -14,6 +15,7 @@ import javax.swing.JComponent; public class FXHtmlView extends AbstractHtmlView { + private static final int PANEL_WIDTH_INT = 675; private static final int PANEL_HEIGHT_INT = 400; @@ -47,7 +49,22 @@ public void setContent(final String content) { public void run() { //currentContent = content.replace("", ""); currentContent = content; + + final Integer x = (Integer) webView.getEngine().executeScript("document.body.scrollLeft"); + final Integer y = (Integer) webView.getEngine().executeScript("document.body.scrollTop"); + webView.getEngine().loadContent(currentContent); + + final Worker loadWorker = webView.getEngine().getLoadWorker(); + loadWorker.stateProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, Worker.State oldValue, Worker.State newValue) { + if (newValue == Worker.State.SUCCEEDED) { + webView.getEngine().executeScript("window.scrollTo(" + x + "," + y + ");"); + loadWorker.stateProperty().removeListener(this); + } + } + }); } }); } From 1538733fde4c5037962bd217282b5013dd5d2b8f Mon Sep 17 00:00:00 2001 From: Holger Stenger Date: Thu, 2 Jul 2015 23:02:02 +0200 Subject: [PATCH 2/5] Removed commented out code. --- src/flow/netbeans/markdown/preview/ext/FXHtmlView.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java b/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java index 3057ede..1351803 100644 --- a/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java +++ b/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java @@ -47,7 +47,6 @@ public void setContent(final String content) { Platform.runLater(new Runnable() { @Override public void run() { - //currentContent = content.replace("", ""); currentContent = content; final Integer x = (Integer) webView.getEngine().executeScript("document.body.scrollLeft"); @@ -82,12 +81,6 @@ private void createScene() { webView.setMinSize(widthDouble, heightDouble); webView.setPrefSize(widthDouble, heightDouble); webView.setContextMenuEnabled(false); -// webView.getEngine().setCreatePopupHandler(new Callback() { -// @Override -// public WebEngine call(PopupFeatures p) { -// return null; -// } -// }); webView.getEngine().setConfirmHandler(new Callback() { @Override public Boolean call(String p) { From 2e0533414a7b507e960558425d1050e4ef30bfd3 Mon Sep 17 00:00:00 2001 From: Holger Stenger Date: Thu, 2 Jul 2015 23:48:06 +0200 Subject: [PATCH 3/5] Made code more robust. --- .../markdown/preview/ext/FXHtmlView.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java b/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java index 1351803..e5309ec 100644 --- a/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java +++ b/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java @@ -1,6 +1,7 @@ package flow.netbeans.markdown.preview.ext; import flow.netbeans.markdown.preview.AbstractHtmlView; +import java.text.MessageFormat; import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; @@ -49,18 +50,26 @@ public void setContent(final String content) { public void run() { currentContent = content; - final Integer x = (Integer) webView.getEngine().executeScript("document.body.scrollLeft"); - final Integer y = (Integer) webView.getEngine().executeScript("document.body.scrollTop"); + final Number x = (Integer) webView.getEngine().executeScript("document.body.scrollLeft"); + final Number y = (Integer) webView.getEngine().executeScript("document.body.scrollTop"); webView.getEngine().loadContent(currentContent); final Worker loadWorker = webView.getEngine().getLoadWorker(); loadWorker.stateProperty().addListener(new ChangeListener() { @Override - public void changed(ObservableValue observable, Worker.State oldValue, Worker.State newValue) { - if (newValue == Worker.State.SUCCEEDED) { - webView.getEngine().executeScript("window.scrollTo(" + x + "," + y + ");"); - loadWorker.stateProperty().removeListener(this); + public void changed(ObservableValue observable, + Worker.State oldValue, Worker.State newValue) { + switch (newValue) { + case SUCCEEDED: + loadWorker.stateProperty().removeListener(this); + String script = MessageFormat.format("window.scrollTo({0},{1});", x, y); + webView.getEngine().executeScript(script); + break; + case FAILED: + case CANCELLED: + loadWorker.stateProperty().removeListener(this); + break; } } }); From 468b822125047a64fa5a3886b962d7070e82fbe3 Mon Sep 17 00:00:00 2001 From: Holger Stenger Date: Thu, 2 Jul 2015 23:52:40 +0200 Subject: [PATCH 4/5] Fixed cast to Number. --- src/flow/netbeans/markdown/preview/ext/FXHtmlView.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java b/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java index e5309ec..84c5458 100644 --- a/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java +++ b/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java @@ -50,8 +50,8 @@ public void setContent(final String content) { public void run() { currentContent = content; - final Number x = (Integer) webView.getEngine().executeScript("document.body.scrollLeft"); - final Number y = (Integer) webView.getEngine().executeScript("document.body.scrollTop"); + final Number x = (Number) webView.getEngine().executeScript("document.body.scrollLeft"); + final Number y = (Number) webView.getEngine().executeScript("document.body.scrollTop"); webView.getEngine().loadContent(currentContent); From edff65985ec3c472685856280175a6eda7caca08 Mon Sep 17 00:00:00 2001 From: Holger Stenger Date: Mon, 13 Jul 2015 22:29:09 +0200 Subject: [PATCH 5/5] Fixed formatting of scroll position in generated JavaScript snippet. --- src/flow/netbeans/markdown/preview/ext/FXHtmlView.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java b/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java index 84c5458..6f1e924 100644 --- a/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java +++ b/src/flow/netbeans/markdown/preview/ext/FXHtmlView.java @@ -1,7 +1,6 @@ package flow.netbeans.markdown.preview.ext; import flow.netbeans.markdown.preview.AbstractHtmlView; -import java.text.MessageFormat; import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; @@ -63,7 +62,7 @@ public void changed(ObservableValue observable, switch (newValue) { case SUCCEEDED: loadWorker.stateProperty().removeListener(this); - String script = MessageFormat.format("window.scrollTo({0},{1});", x, y); + String script = String.format("window.scrollTo(%d,%d);", x, y); webView.getEngine().executeScript(script); break; case FAILED: