From 8e2b1ab34d21fa0890d5d69756081fb72e1595dd Mon Sep 17 00:00:00 2001 From: Wim Deblauwe Date: Thu, 27 Jun 2019 15:28:35 +0200 Subject: [PATCH 1/2] Drive the right-click action from JavaFX instead of relying on JavaScript as it does not seem to work on macOS Fixes #283 --- .../editor/javafx/JavaFxHtmlPanel.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/asciidoc/intellij/editor/javafx/JavaFxHtmlPanel.java b/src/main/java/org/asciidoc/intellij/editor/javafx/JavaFxHtmlPanel.java index 39bc67fe6..eda078edf 100644 --- a/src/main/java/org/asciidoc/intellij/editor/javafx/JavaFxHtmlPanel.java +++ b/src/main/java/org/asciidoc/intellij/editor/javafx/JavaFxHtmlPanel.java @@ -51,6 +51,7 @@ import org.asciidoc.intellij.settings.AsciiDocApplicationSettings; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.w3c.dom.html.HTMLImageElement; import javax.swing.*; import java.awt.*; @@ -201,9 +202,7 @@ public void run() { myWebView = new WebView(); updateFontSmoothingType(myWebView, false); - // will be disabled via JavaScript inside processImages.js as it is not generally helpful here, - // but processImages.js will use right-click to export the images. - myWebView.setContextMenuEnabled(true); + registerContextMenu(JavaFxHtmlPanel.this.myWebView); myWebView.setZoom(JBUI.scale(1.f)); myWebView.getEngine().loadContent(prepareHtml("Initializing...")); @@ -246,6 +245,25 @@ public void run() { } + private void registerContextMenu(WebView webView) { + webView.setOnMousePressed(e -> { + if (e.getButton() == MouseButton.SECONDARY) { + JSObject object = getJavaScriptObjectAtLocation(webView, e); + if (object instanceof HTMLImageElement) { + String src = ((HTMLImageElement) object).getAttribute("src"); + ApplicationManager.getApplication().invokeLater(() -> runFX(() -> { + bridge.saveImage(src); + })); + } + } + }); + } + + private JSObject getJavaScriptObjectAtLocation(WebView webView, MouseEvent e) { + String script = String.format("document.elementFromPoint(%s,%s);", e.getX(), e.getY()); + return (JSObject) webView.getEngine().executeScript(script); + } + private static void runFX(@NotNull Runnable r) { IdeEventQueue.unsafeNonblockingExecute(r); } From d99990c6615fd8205c326f34db28c6905d76a855 Mon Sep 17 00:00:00 2001 From: Alexander Schwartz Date: Thu, 27 Jun 2019 16:52:39 +0200 Subject: [PATCH 2/2] removing preview JavaScript code after testing on Windows 10 #283 #285 --- .../editor/javafx/JavaFxHtmlPanel.java | 128 +++++++++--------- .../intellij/editor/javafx/processImages.js | 30 ---- 2 files changed, 63 insertions(+), 95 deletions(-) delete mode 100644 src/main/resources/org/asciidoc/intellij/editor/javafx/processImages.js diff --git a/src/main/java/org/asciidoc/intellij/editor/javafx/JavaFxHtmlPanel.java b/src/main/java/org/asciidoc/intellij/editor/javafx/JavaFxHtmlPanel.java index eda078edf..a4a0c2d87 100644 --- a/src/main/java/org/asciidoc/intellij/editor/javafx/JavaFxHtmlPanel.java +++ b/src/main/java/org/asciidoc/intellij/editor/javafx/JavaFxHtmlPanel.java @@ -86,7 +86,6 @@ protected String compute() { return new StringBuilder() .append("\n") .append("\n") - .append("\n") .append("\n") .append("