2009-08-22 2 views
15

Impossibile trovare nulla su questo con una Ricerca Google.Come copiare negli appunti con GWT?

Qualcuno sa come copiare un testo negli Appunti tramite il codice Java GWT? Vorrei evitare la soluzione di iniezione javascript non elaborata.

Qualsiasi aiuto o suggerimento apprezzato.

+0

GWT in realtà compila il codice Java fuori a javascript, così quando viene eseguito, è in realtà JavaScript in esecuzione. – helloandre

+0

Conosco l'uomo - ma se non lo scrivo non mi devo preoccupare di lavorare su tutti i browser – JohnIdol

+0

Intendevo l'iniezione javascript raw dal codice GWT – JohnIdol

risposta

3

Per il momento non sembra che ci siano alcune librerie GWT che forniscono questa funzionalità. In ogni caso, è impossibile sostenerlo in tutti i browser in quanto è necessario Flash. Una libreria piuttosto bella di quella che include la funzionalità è ZeroClipboard.

+0

obsoleto, vedi sotto. – ArcTanH

5

Il seguente codice ha funzionato bene per me in cromo:

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

Ecco una soluzione senza JS nativi, ma GWT elementare invece, ancora ispirato da @SushmithaShenoy, lasciando questo qui per riferimento futuro.

precondizione:

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

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

ora il codice di 'reale', forse collocato in un 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 non supporta nativamente il comando $doc.execCommand('copy');, ma è super facile.

Prima mettere a fuoco l'elemento, selezionare il testo, quindi copiarlo.

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

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

Basta avvolgere la risposta fornita https://stackoverflow.com/a/30810322/106261.

Quindi, si passa in qualsiasi testo a una funzione/metodo nativo javascript, la funzione js crea un nuovo elemento e copia negli appunti e rimuove l'elemento dopo la copia.

Nessuna necessità di librerie con nuovi browser.

così:

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