JavaFX HTMLEditor - 在插入位置插入文本

时间:2017-10-08 10:14:39

标签: javascript javafx html-editor

我有以下JS函数在JavaFX-HTMLEditor的webview的Cursor位置插入HTML,但它绝对没有:

public static final String insertHtmlAtCursor = "function insertHtmlAtCursor(html) {\n"
        + "    var range, node;\n"
        + "    if (window.getSelection && window.getSelection().getRangeAt) {\n"
        + "        range = window.getSelection().getRangeAt(0);\n"
        + "        node = range.createContextualFragment(html);\n"
        + "        range.insertNode(node);\n"
        + "    } else if (document.selection && document.selection.createRange) {\n"
        + "        document.selection.createRange().pasteHTML(html);\n"
        + "    }\n"
        + "}insertHtmlAtCursor('####html####')";

插入HTML代码本身已经可以使用以下JS函数:

public static final String insertHTML
        = "(function getSelectionText() {\n"
        + "    var text = \"\";\n"
        + "    if (window.getSelection) {\n"
        + "        text = window.getSelection().toString();\n"
        + "    } else if (document.selection && document.selection.type != \"Control\") {\n"
        + "        text = document.selection.createRange().text;\n"
        + "    }\n"
        + "    if (window.getSelection) {\n"
        + "      if (window.getSelection().empty) {  // Chrome\n"
        + "        window.getSelection().empty();\n"
        + "      } else if (window.getSelection().removeAllRanges) {  // Firefox\n"
        + "        window.getSelection().removeAllRanges();\n"
        + "      }\n"
        + "    } else if (document.selection) {  // IE?\n"
        + "      document.selection.empty();\n"
        + "    }"
        + "    return text;\n"
        + "})()";

我的目标是在区域中放置文本或图像时在插入位置插入特定的HTML代码。我读到我应该在WebEngine实例上执行JS-Script(这里:从Dragboard插入图像)

Node toolNode = htmleditor.lookup(".top-toolbar");
Node webNode = htmleditor.lookup(".web-view");
    if (toolNode instanceof ToolBar && webNode instanceof WebView) {
        ToolBar bar = (ToolBar) toolNode;
        WebView webView = (WebView) webNode;
        engine = webView.getEngine();

        webView.setOnDragDropped(new EventHandler<DragEvent>() {
            @Override
            public void handle(DragEvent event) {
              try {
                    Object selection = engine.executeScript(insertHTML
                            .replace("####html####",
                                    escapeJavaStyleString(img, true, true)));
                   if (event.getDragboard().getUrl() != null) {
                   String text_old = htmleditor.getHtmlText();
                   htmleditor.setHtmlText(text_old + "<br><span><img src=' " + event.getDragboard().getUrl() + "'/></span>");

                   try {
                        engine.executeScript(insertHtmlAtCursor
                        .replace("####html####",
                                "testHTMLCode"));
                       } 
                   catch (JSException e) {
                       // A JavaScript Exception Occured
            }
          } catch (JSException e) {
                    // A JavaScript Exception Occured
            }
           }
        });
    }

不幸的是,我只通过事件得到光标位置,无论webview中的位置如何。

0 个答案:

没有答案