Copy copy of TinyMCE from notepad (clipboard) with javafx WebView on java 1.8

I have embedded the tinymce editor in javafx WebWiew. I load the editor using the webEngine.load method. Problems arise when copying paste. When I copy some content from notepad to tinymce, it is pasted. Then, when I copy some content from tinymce to tinymce, it is pasted. No problems. But as soon as I inserted some content from tinyme to tinymce, I can no longer copy tinymce (webView) from abroad. For example, when I copy text from a notepad and paste, the value copied from the notepad is ignored, and the previous value copied from tinymce is pasted again.

I added a listener to the webView and checked the clipboard values, they are correct in all cases:

webView.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() { @Override public void handle(KeyEvent arg0) { if (arg0.isControlDown() && arg0.getCode() == KeyCode.V) { final ClipBoard clipBoard = ClipBoard.getSystemClipBoard(); System.out.println(clipBoard.getContent(DataFormat.PLAIN_TEXT)); .. 

And on the editor side, by initializing the TinyMCE editor: As you can see in the third attempt, args.content is erroneous and does not contain a value on the clipboard.

 tinymce.init { paste_preprocess : function(plugin,args) { debug(args.content); 

Step 1: [OK]

value copied from notepad ABCDE

java system out for clipboard: ABCDE

editor html debug says: ABCDE

Step 2: [OK]

value copied from tinymce XYZQ editor

java system out for clipboard: XYZQ

html debug editor says: XYZQ

Step 3: [FAIL]

value copied from notepad ASDFG

java system out for clipboard: ASDFG

html debug editor says: XYZQ

Properties of the system:

TinyMCE 4.2.2

Windows 7

The problem occurs in java 1.8.65 and 1.8.66

The problem does not occur in java 1.7.40

Solution: I can manually send content from java to javafx (editor) using executable scripts, etc. And override the value in the paste_preprocess function for tinymce. But why is this happening? (The case does not occur on java 1.7). There must be a better solution.

+5
source share
1 answer

I had the same problem, but I still needed a solution to embed HTML or Word XML in TinyMCE, and not just from plain text.

The solution was to basically ignore the original paste event, and then use the pasteHtml() paste plugin to paste the clipboard data from Java. The reason for this is that at the point where paste_preprocess is paste_preprocess , the contents of the clipboard are already formatted in HTML from TinyMCE, so I cannot just set args.content to what is in the Java clipboard.

There are both AWT and FX implementations for the clipboards that I use. The FX implementation provides some useful methods for getting HTML from the clipboard.

JavaScript code

 var regularPaste = false; tinyMCE.init({ ... paste_preprocess : function(plugin, args) { if(!regularPaste) { regularPaste = true; var clipboardData = window.java.getClipboardData(); plugin.clipboard.pasteHtml(clipboardData); // This will call paste_preprocess again args.content = ""; // Ignore what TinyMCE think it should insert } regularPaste = false; }, ... }); 

Java code

 public class Bridge { public String getClipboardData() { javafx.scene.input.Clipboard clipboardFx = javafx.scene.input.Clipboard.getSystemClipboard(); java.awt.datatransfer.Clipboard clipboardAwt = Toolkit.getDefaultToolkit().getSystemClipboard(); String data = ""; try { if (clipboardFx.hasHtml()) { data = clipboardFx.getHtml(); } else { // We use the AWT clipboard if we want to retreive text because the FX implementation delivers funky characters // when pasting from eg Command Prompt data = (String) clipboardAwt.getData(DataFlavor.stringFlavor); data = data.replaceAll("(\n|\r|\n\r|\r\n)", "<br />"); } } catch (Exception e) { System.out.println("Failed getting clipboard data"); } return data; } } 

Where is your WebView you need to register an instance of the Bridge class.

 JSObject window = (JSObject) webView.getEngine().executeScript("window"); window.setMember("java", new Bridge()); 
0
source

Source: https://habr.com/ru/post/1236970/


All Articles