复制并粘贴在javafx应用程序中嵌入的codemirror.js中 [英] Copy and paste in codemirror.js embeded in javafx application

查看:129
本文介绍了复制并粘贴在javafx应用程序中嵌入的codemirror.js中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用codemirror.js库在Java FX中创建简单的编辑器。
我使用javafx.scene.web.WebView组件在javafx中嵌入了codemirror编辑器,其中包含以下html / js代码:

I'm creating simple editor in Java FX using codemirror.js library. I embeded codemirror editor in javafx using javafx.scene.web.WebView component, with the folowing html/js code:

<body>
<form>
   <textarea id="code" name="code">
   </textarea>
</form>
<script>
   var editor = CodeMirror.fromTextArea(document.getElementById("code"), { lineNumbers: true});
</script>
</body>

Codemirror编辑器本身支持撤消,重做,剪切,复制和粘贴。

Codemirror editor itself supports undo, redo, cut, copy and paste.

我的应用程序中还有javafx主菜单,我想添加复制或粘贴等操作。我想以某种方式绑定这个菜单操作与我的代码镜像编辑器,所以如果用户点击例如从主菜单粘贴,剪贴板中的内容将添加到codemirror编辑器中。

I have also javafx main menue in my application and I want to add actions like copy or paste to it. I want to somehow "bind" this menue actions with my codemirror editor, so if user click e.g. paste from main menue, the content from clipboard will be added to the codemirror editor.

我解决了撤消和重做操作的这个问题:codemirror有两个js函数undo()和redo(),我可以通过javafx.scene从java级别调用它们。 web.WebView.executeScript方法。

I solved this problem for undo and redo operations: codemirror has two js functions undo() and redo() and I can invoke they from java level via javafx.scene.web.WebView.executeScript method.

我的问题是如何处理剪切,复制和粘贴操作?如何使用codemirror编辑器将此操作与主菜单绑定?我在codemirror.js中找不到任何可以处理此操作的js函数。

My question is how to handle cut, copy and paste operations? How to bind this operations from main menue with codemirror editor? I can't find any js functions in codemirror.js that can handle this oprations.

任何帮助表示感谢并提前致谢。

Any help appreciated and thanks in advance.

推荐答案

我'已找到解决方案:
Codmirror在API中没有cut,copy和past等函数,但它允许获取和替换所选文本,因此我可以自己编写这些操作。

I've found solution: Codmirror doesn't have functions like cut, copy and past in API, but it allow to get and replace selected text, so I can write those operations by myself.

public void cut() {
    String selectedText = (String) webview.getEngine().executeScript(
            "editor.getSelection();");
    webview.getEngine().executeScript("editor.replaceSelection(\"\");");
    final Clipboard clipboard = Clipboard.getSystemClipboard();
    final ClipboardContent content = new ClipboardContent();
    content.putString(selectedText);
    clipboard.setContent(content);

}

public void copy() {
    String selectedText = (String) webview.getEngine().executeScript(
            "editor.getSelection();");
    final Clipboard clipboard = Clipboard.getSystemClipboard();
    final ClipboardContent content = new ClipboardContent();
    content.putString(selectedText);
    clipboard.setContent(content);
}

public void paste() {
    final Clipboard clipboard = Clipboard.getSystemClipboard();
    String content = (String) clipboard.getContent(DataFormat.PLAIN_TEXT);
    webview.getEngine().executeScript(String.format("editor.replaceSelection(\"%s\");", content));
}

这篇关于复制并粘贴在javafx应用程序中嵌入的codemirror.js中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆