| Author | Karl Tauber <email> |
|---|---|
| Date | 2015-08-05 12:05:49 GMT+0200 |
| Commit | 669613d35c62b054383d1343e5603a21b15febe2 |
| Parent | 371f7be |
| Delta | 39 lines added, 11 lines removed, 28-line increase |
| package org.markdownwriterfx.preview; | ||
| +import java.nio.file.Path; | ||
| import javafx.scene.Node; | ||
| import javafx.scene.control.ScrollBar; | ||
| @Override | ||
| - public void update(RootNode astRoot) { | ||
| + public void update(RootNode astRoot, Path path) { | ||
| double scrollTop = textArea.getScrollTop(); | ||
| double scrollLeft = textArea.getScrollLeft(); | ||
| package org.markdownwriterfx.preview; | ||
| +import java.nio.file.Path; | ||
| import javafx.scene.Node; | ||
| import javafx.scene.control.ScrollBar; | ||
| @Override | ||
| - public void update(RootNode astRoot) { | ||
| + public void update(RootNode astRoot, Path path) { | ||
| double scrollTop = textArea.getScrollTop(); | ||
| package org.markdownwriterfx.preview; | ||
| +import java.nio.file.Path; | ||
| import javafx.application.Platform; | ||
| import javafx.beans.property.DoubleProperty; | ||
| interface Preview { | ||
| - void update(RootNode astRoot); | ||
| + void update(RootNode astRoot, Path path); | ||
| void scrollY(double value); | ||
| } | ||
| tabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldTab, newTab) -> { | ||
| - getActivePreview().update(getMarkdownAST()); | ||
| + update(); | ||
| scrollY(); | ||
| + }); | ||
| + | ||
| + path.addListener((observable, oldValue, newValue) -> { | ||
| + update(); | ||
| }); | ||
| markdownAST.addListener((observable, oldValue, newValue) -> { | ||
| - getActivePreview().update(newValue); | ||
| + update(); | ||
| }); | ||
| private Preview getActivePreview() { | ||
| return (Preview) tabPane.getSelectionModel().getSelectedItem().getUserData(); | ||
| + } | ||
| + | ||
| + private void update() { | ||
| + getActivePreview().update(getMarkdownAST(), getPath()); | ||
| } | ||
| }); | ||
| } | ||
| + | ||
| + // 'path' property | ||
| + private final ObjectProperty<Path> path = new SimpleObjectProperty<>(); | ||
| + public Path getPath() { return path.get(); } | ||
| + public void setPath(Path path) { this.path.set(path); } | ||
| + public ObjectProperty<Path> pathProperty() { return path; } | ||
| // 'markdownAST' property | ||
| package org.markdownwriterfx.preview; | ||
| +import java.nio.file.Path; | ||
| import java.util.Collections; | ||
| import javafx.scene.Node; | ||
| static String toHtml(RootNode astRoot) { | ||
| + if (astRoot == null) | ||
| + return ""; | ||
| return new ToHtmlSerializer(new LinkRenderer(), | ||
| Collections.<String, VerbatimSerializer>emptyMap(), | ||
| PegDownPlugins.NONE.getHtmlSerializerPlugins()) | ||
| .toHtml(astRoot); | ||
| } | ||
| @Override | ||
| - public void update(RootNode astRoot) { | ||
| + public void update(RootNode astRoot, Path path) { | ||
| if (!webView.getEngine().getLoadWorker().isRunning()) { | ||
| // get window.scrollX and window.scrollY from web engine, | ||
| // but only no worker is running (in this case the result would be zero) | ||
| Object scrollXobj = webView.getEngine().executeScript("window.scrollX"); | ||
| Object scrollYobj = webView.getEngine().executeScript("window.scrollY"); | ||
| lastScrollX = (scrollXobj instanceof Number) ? ((Number)scrollXobj).intValue() : 0; | ||
| lastScrollY = (scrollYobj instanceof Number) ? ((Number)scrollYobj).intValue() : 0; | ||
| } | ||
| + String base = (path != null) | ||
| + ? ("<base href=\"" + path.getParent().toUri().toString() + "\">\n") | ||
| + : ""; | ||
| String scrollScript = (lastScrollX > 0 || lastScrollY > 0) | ||
| ? (" onload='window.scrollTo("+lastScrollX+", "+lastScrollY+");'") | ||
| - : null; | ||
| + : ""; | ||
| webView.getEngine().loadContent( | ||
| - "<!DOCTYPE html><html><head><link rel=\"stylesheet\" href=\"" | ||
| - + getClass().getResource("markdownpad-github.css") | ||
| - + "\"></head><body" + scrollScript + ">" | ||
| + "<!DOCTYPE html>\n" | ||
| + + "<html>\n" | ||
| + + "<head>\n" | ||
| + + "<link rel=\"stylesheet\" href=\"" + getClass().getResource("markdownpad-github.css") + "\">\n" | ||
| + + base | ||
| + + "</head>\n" | ||
| + + "<body" + scrollScript + ">\n" | ||
| + toHtml(astRoot) | ||
| - + "</body></html>"); | ||
| + + "</body>\n" | ||
| + + "</html>"); | ||
| } | ||