2009-08-22 15 views
15

Google खोज के साथ इस पर कुछ भी नहीं मिला।जीडब्ल्यूटी के साथ क्लिपबोर्ड पर कैसे कॉपी करें?

क्या कोई जानता है कि कुछ पाठ को क्लिपबोर्ड पर जीडब्ल्यूटी जावा कोड के माध्यम से कैसे कॉपी किया जाए? मैं कच्चे जावास्क्रिप्ट इंजेक्शन समाधान से बचना चाहता हूं।

किसी भी मदद या पॉइंटर्स की सराहना की।

+0

gwt वास्तव में अपने जावा कोड जावास्क्रिप्ट के लिए बाहर संकलित है, इसलिए जब इसे चलाता है, यह वास्तव जावास्क्रिप्ट चल रहा है। – helloandre

+0

मुझे आदमी पता है - लेकिन अगर मैं इसे नहीं लिखता तो मुझे इसके बारे में चिंता करने की ज़रूरत नहीं है कि सभी ब्राउज़रों पर काम कर रहे हैं – JohnIdol

+0

मेरा मतलब है जीडब्ल्यूटी कोड – JohnIdol

उत्तर

3

इस पल के लिए ऐसा लगता है कि कोई भी जीडब्ल्यूटी पुस्तकालय नहीं है जो इस कार्यक्षमता प्रदान करता है। किसी भी मामले में, फ्लैश की आवश्यकता होने पर सभी ब्राउज़रों में इसका समर्थन करना असंभव है। कार्यक्षमता को लपेटने की तुलना में एक अच्छी लाइब्रेरी ZeroClipboard है।

+0

पुराना, नीचे देखें। ब्लॉग पोस्ट के लिए – ArcTanH

5

मैंने जीडब्ल्यूटी के साथ ZeroClipboard का उपयोग किया है (जैसा कि अलेक्जेंडर द्वारा सुझाया गया है) लेकिन यह सीधा नहीं था।

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

+0

+1। –

+0

ज़ीरोक्लिबोर्ड की आधिकारिक साइट http://zeroclipboard.org/ है और स्रोत कोड भंडार https://github.com/zeroclipboard/zeroclipboard है –

5

निम्नलिखित कोड क्रोम में मेरे लिए ठीक काम किया देखें:

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(); 
}-*/; 
1

यहाँ देशी जे एस के बिना एक समाधान है, लेकिन मौलिक 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(); 
2

GWT देशी रूप $doc.execCommand('copy'); आदेश का समर्थन नहीं करता है, लेकिन यह सुपर आसान है।

पहले आइटम पर ध्यान केंद्रित करें, टेक्स्ट का चयन करें, फिर इसे कॉपी करें।

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

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

बस प्रदान किए गए उत्तर https://stackoverflow.com/a/30810322/106261 को लपेटें।

तो, आप जावास्क्रिप्ट मूल कार्य/विधि में किसी भी पाठ में पास करते हैं, जेएस फ़ंक्शन क्लिपबोर्ड पर एक नया तत्व और प्रतियां बनाता है, और कॉपी करने के बाद तत्व को हटा देता है।

नए ब्राउज़र के साथ किसी भी libs के लिए कोई ज़रूरत नहीं है।

तो:

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); 
    }-*/; 
संबंधित मुद्दे