Skip to content

Commit

Permalink
Merge pull request #89 from stengerh/feature-preview-scroll-position-nb8
Browse files Browse the repository at this point in the history
Preserve scroll position in preview
  • Loading branch information
madflow committed Jul 14, 2015
2 parents 7bd78be + edff659 commit 18825e4
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/flow/netbeans/markdown/preview/ext/FXHtmlView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -45,9 +47,31 @@ public void setContent(final String content) {
Platform.runLater(new Runnable() {
@Override
public void run() {
//currentContent = content.replace("</head>", "<script type=\"text/javascript\">window.onload = function(){window.onbeforeunload = function() {return \"***navigation-hook***\";};};</script></head>");
currentContent = content;

final Number x = (Number) webView.getEngine().executeScript("document.body.scrollLeft");
final Number y = (Number) webView.getEngine().executeScript("document.body.scrollTop");

webView.getEngine().loadContent(currentContent);

final Worker<Void> loadWorker = webView.getEngine().getLoadWorker();
loadWorker.stateProperty().addListener(new ChangeListener<Worker.State>() {
@Override
public void changed(ObservableValue<? extends Worker.State> observable,
Worker.State oldValue, Worker.State newValue) {
switch (newValue) {
case SUCCEEDED:
loadWorker.stateProperty().removeListener(this);
String script = String.format("window.scrollTo(%d,%d);", x, y);
webView.getEngine().executeScript(script);
break;
case FAILED:
case CANCELLED:
loadWorker.stateProperty().removeListener(this);
break;
}
}
});
}
});
}
Expand All @@ -65,12 +89,6 @@ private void createScene() {
webView.setMinSize(widthDouble, heightDouble);
webView.setPrefSize(widthDouble, heightDouble);
webView.setContextMenuEnabled(false);
// webView.getEngine().setCreatePopupHandler(new Callback<PopupFeatures, WebEngine>() {
// @Override
// public WebEngine call(PopupFeatures p) {
// return null;
// }
// });
webView.getEngine().setConfirmHandler(new Callback<String, Boolean>() {
@Override
public Boolean call(String p) {
Expand Down

0 comments on commit 18825e4

Please sign in to comment.