Para copiar datos al portapapeles en javascript Clipboard API (clipboard.write()) se debe usar en lugar del obsoleto Document.execCommand('copy') .
Quiero copiar rtf
al portapapeles, que parece no ser compatible con clipboard.write()
hasta ahora en Microsoft Edge Versión 98.0.1108.62:
Uncaught (in promise) DOMException: Type text/rtf not supported on write.
Así que intenté usar el comando obsoleto execCommand('copy')
, donde puedo copiar al portapapeles, pero no es posible pegar el contenido RTF, por ejemplo, en Microsoft Word u otro editor RTF. Probé con text/rtf
y application/rtf
.
<!DOCTYPE html> <html> <head> <script> window.onload = function () { document.querySelector('button').onclick = (e) => { const rtf = '{\\rtf1\\ansi\\deff0' + '\\trowd' + '\\cellx1000' + '\\cellx2000' + '\\cellx3000' + '\\intbl cell 1\\cell' + '\\intbl cell 2\\cell' + '\\intbl cell 3\\cell' + '\\row' + '}' document.oncopy = e => { e.preventDefault(); const clipboardData = e.clipboardData; clipboardData.setData('application/rtf', rtf); } document.execCommand('copy'); document.oncopy = null; }; document.querySelector('div[contenteditable]').onpaste = e => { e.preventDefault(); const clipboardData = e.clipboardData; console.log('retrieved "%s"', clipboardData.getData('application/rtf')); }; }; </script> </head> <body> <button>copy to clipboard</button> <div contenteditable>paste here</div> </body> </html>
Consulte también , solo hay una solución para html pero no para rtf.
Con text/html
y con contenido html, hay algunos problemas de diseño con una tabla, por lo que realmente tengo que usar rtf
en lugar de html
.