Commit 6e64cc7e authored by Eduardo Sanz García's avatar Eduardo Sanz García Committed by Eduardo

Fix clipboard copy on iOS

On iOS the element must be an `input` (or `textarea`)  and with attribute `'contenteditable'`

Closes #858
parent d7c316ea
...@@ -10,10 +10,13 @@ ...@@ -10,10 +10,13 @@
* @param {string} text * @param {string} text
*/ */
export function copyText(text) { export function copyText(text) {
const temp = document.createElement('pre'); const temp = document.createElement('input');
temp.className = 'copy-text'; temp.value = text;
temp.textContent = text; temp.setAttribute('data-testid', 'copy-text');
// Recipe from https://stackoverflow.com/a/34046084/14463679
temp.contentEditable = 'true';
document.body.appendChild(temp); document.body.appendChild(temp);
temp.focus();
try { try {
const range = document.createRange(); const range = document.createRange();
...@@ -22,6 +25,7 @@ export function copyText(text) { ...@@ -22,6 +25,7 @@ export function copyText(text) {
selection.removeAllRanges(); selection.removeAllRanges();
range.selectNodeContents(temp); range.selectNodeContents(temp);
selection.addRange(range); selection.addRange(range);
temp.setSelectionRange(0, temp.value.length);
document.execCommand('copy'); document.execCommand('copy');
} finally { } finally {
temp.remove(); temp.remove();
......
...@@ -14,7 +14,7 @@ describe('copy-to-clipboard', () => { ...@@ -14,7 +14,7 @@ describe('copy-to-clipboard', () => {
* Returns the temporary element used to hold text being copied. * Returns the temporary element used to hold text being copied.
*/ */
function tempSpan() { function tempSpan() {
return document.querySelector('.copy-text'); return document.querySelector('[data-testid=copy-text]');
} }
beforeEach(() => { beforeEach(() => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment