用谷歌搜索找不到任何相关结果。
有人知道如何通过GWT Java代码将一些文本复制到剪贴板吗?我想避免使用原始JavaScript注入解决方法。
感谢任何帮助或提示。
用谷歌搜索找不到任何相关结果。
有人知道如何通过GWT Java代码将一些文本复制到剪贴板吗?我想避免使用原始JavaScript注入解决方法。
感谢任何帮助或提示。
只需使用提供的答案https://dev59.com/fHRC5IYBdhLWcg3wJNcN#30810322即可。
因此,您可以将任何文本传递给JavaScript本地函数/方法,js函数会创建一个新元素并将其复制到剪贴板中,在复制后删除该元素。
在新版浏览器中不需要使用任何库。
所以:
public static native void copyTextToClipboard(String text) /*-{
var textArea = document.createElement("textarea");
//
// *** This styling is an extra step which is likely not required. ***
//
// Why is it here? To ensure:
// 1. the element is able to have focus and selection.
// 2. if element was to flash render it has minimal visual impact.
// 3. less flakyness with selection and copying which **might** occur if
// the textarea element is not visible.
//
// The likelihood is the element won't even render, not even a flash,
// so some of these are just precautions. However in IE the element
// is visible whilst the popup box asking the user for permission for
// the web page to copy to the clipboard.
//
// Place in top-left corner of screen regardless of scroll position.
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
// Ensure it has a small width and height. Setting to 1px / 1em
// doesn't work as this gives a negative w/h on some browsers.
textArea.style.width = '2em';
textArea.style.height = '2em';
// We don't need padding, reducing the size if it does flash render.
textArea.style.padding = 0;
// Clean up any borders.
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
// Avoid flash of white box if rendered for any reason.
textArea.style.background = 'transparent';
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
try {
var successful = document.execCommand('copy');
} catch (err) {
console.log('Unable to copy');
}
document.body.removeChild(textArea);
}-*/;
我曾经使用过ZeroClipboard和GWT结合使用(正如Alexander所建议的),但这并不是一件简单的事情。
请参考http://blog.dandoy.org/2011/09/using-zeroclipboard-with-gwt.html
以下代码在Chrome中对我很有效:
```public static native void copyToClipboard() /*-{
var selection = $wnd.getSelection();
var text = $doc.getElementById("myElement");
var range = $doc.createRange();
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
$doc.execCommand('copy');
selection.removeAllRanges();
}-*/;
GWT并不直接支持$doc.execCommand('copy');
命令,但是解决这个问题还是很容易的。
首先将焦点放在该项上,选择文本,然后复制即可。
myTextBox.setFocus(true);
myTextBox.selectAll();
boolean success = copyToClipboard();
private static native boolean copyToClipboard() /*-{
return $doc.execCommand('copy');
}-*/;
目前似乎没有任何GWT库提供此功能。无论如何,由于需要使用Flash,因此不可能在所有浏览器中支持此功能。一个非常好的库可以包装这个功能是ZeroClipboard。
我这里提供一种无需使用本地JS,而是使用gwt elemental的解决方案,灵感来自@SushmithaShenoy,将其留在此处以备将来参考。
前提条件:
import elemental.client.Browser;
import elemental.html.Selection;
import elemental.ranges.Range;
Label.getElement().setAttribute("id","your_element_id"); //unique ID!
现在是“真正”的代码,可能放置在点击处理程序中:
final Selection selection = Browser.getWindow().getSelection();
final Range range = Browser.getDocument().createRange();
range.selectNodeContents(Browser.getDocument().getElementById(""you_elements_id"));
selection.removeAllRanges();
selection.addRange(range);
Browser.getWindow().getDocument().execCommand("copy", false, "");
selection.removeAllRanges();