Search code examples
webviewjavafx-8clipboardpasteace-editor

Cannot paste into ACE editor on JavaFX WebView


I'm developing a small JavaFX application. I would like to embed a Lua editor. I'm using ACE Editor in my application through a WebView node. The editor loads perfectly; however, I cannot paste any text through mouse events and keyboard events, regardless from where the text was copied. Is this a known bug?

HTML file:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Lua Editor</title>
        <style type="text/css" media="screen">
            #editor { 
                position: absolute;
                top: 0;
                right: 0;
                bottom: 0;
                left: 0;
            }
        </style>
    </head>
    <body>
        <div id="editor"></div>
        <script src="ace.js" type="text/javascript" charset="utf-8"></script>
        <script>
            var editor = ace.edit("editor");
            editor.getSession().setMode("ace/mode/lua");
            editor.setOptions({
                fontSize: "11pt"
            });
        </script>
    </body>
</html>

Java code snippet:

@FXML
private WebView scriptEditor;

private void initializeCardScript() {
    scriptEditor.getEngine().load("path to HTML file");
}

Solution

  • With José Pereda suggestion, I was able to paste text through key events with ctrl+v. But I couldn't paste with the mouse click Copy/Cut/Paste dialog.

    I was able to solve my problem entirely, by creating a communication bridge from Javascript to java. I call a java method on the paste event at ACE editor.

    Java code snippet:

    private void initializeCardScript() {
        scriptEditor.getEngine().load("path to HTML file");
        ((JSObject) scriptEditor.getEngine().executeScript("window")).setMember("java", new Object() {
            public void paste() {
                String content = (String) Clipboard.getSystemClipboard().getContent(DataFormat.PLAIN_TEXT);
                if (content != null) {
                    scriptEditor.getEngine().executeScript("editor.onPaste(\"" + content.replace("\n", "\\n") + "\");");
                }
            }
        });
    }
    

    ace.js:

    var onPaste = function(e) {
        java.paste();
    //  var data = handleClipboardData(e);
    //  if (typeof data == "string") {
    //      if (data)
    //          host.onPaste(data, e);
    //      if (useragent.isIE)
    //          setTimeout(resetSelection);
    //      event.preventDefault(e);
    //  }
    //      else {
    //          text.value = "";
    //          pasted = true;
    //      }
    };
    

    Sources:

    https://stackoverflow.com/a/25676561/3956070

    https://blogs.oracle.com/javafx/entry/communicating_between_javascript_and_javafx