如何复制与GWT到剪贴板?(How to copy to clipboard with GWT?)

2019-07-03 12:03发布

找不到与谷歌搜索上这样的东西。

有谁知道如何一些文本复制到剪贴板通过GWT Java代码复制? 我想,以避免原料的JavaScript注射液。

任何帮助或指针赞赏。

Answer 1:

眼下它似乎并不像有提供此功能的任何GWT库。 在任何情况下,这是不可能的,因为需要有Flash支持这个在所有浏览器。 一个相当不错的库比包装的功能是ZeroClipboard 。



Answer 2:

我已经使用ZeroClipboard与GWT(亚历山大的建议),但它并不简单。

见http://blog.dandoy.org/2011/09/using-zeroclipboard-with-gwt.html



Answer 3:

下面的代码工作得很好,我在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();
}-*/;


Answer 4:

GWT本身不支持$doc.execCommand('copy'); 命令,但它的超级容易。

首先将重点项目,选择文本,然后复制它。

myTextBox.setFocus(true);
myTextBox.selectAll();
boolean success = copyToClipboard();

private static native boolean copyToClipboard() /*-{
    return $doc.execCommand('copy');
}-*/;


Answer 5:

这里没有原生JS的解决方案,但GWT元素替代,仍然@SushmithaShenoy启发,这里离开这个以供将来参考。

前提:

import elemental.client.Browser;
import elemental.html.Selection;
import elemental.ranges.Range;

Label.getElement().setAttribute("id","your_element_id"); //unique ID!

现在“真正的”代码,也许放在函数clickhandler:

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();


Answer 6:

只是包装所提供的答案https://stackoverflow.com/a/30810322/106261 。

所以,你在任何文本到一个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);
    }-*/;


文章来源: How to copy to clipboard with GWT?
标签: gwt clipboard