Api: Fix egw.copyTextToClipboard() to work in popups too

This commit is contained in:
nathan 2023-05-18 13:46:36 -06:00
parent 7579b07b42
commit 9535cd9801

View File

@ -154,6 +154,8 @@ egw.extend('utils', egw.MODULE_GLOBAL, function()
*/ */
function fallbackCopyTextToClipboard(event, target_element, text) function fallbackCopyTextToClipboard(event, target_element, text)
{ {
const win = target_element?.ownerDocument.defaultView ?? target_element.ownerDocument.parentWindow ?? window;
// Cancel any no-select css // Cancel any no-select css
if (target_element) if (target_element)
{ {
@ -162,17 +164,17 @@ egw.extend('utils', egw.MODULE_GLOBAL, function()
let range = document.createRange(); let range = document.createRange();
range.selectNode(target_element); range.selectNode(target_element);
window.getSelection().removeAllRanges(); win.getSelection().removeAllRanges();
window.getSelection().addRange(range); win.getSelection().addRange(range);
target_element.style.userSelect = old_select; target_element.style.userSelect = old_select;
// detect we are in IE via checking setActive, since it's // detect we are in IE via checking setActive, since it's
// only supported in IE, and make sure there's clipboardData object // only supported in IE, and make sure there's clipboardData object
if (event && typeof event.target.setActive != 'undefined' && window.clipboardData) if (event && typeof event.target.setActive != 'undefined' && win.clipboardData)
{ {
window.clipboardData.setData('Text', target_element.textContent.trim()); win.clipboardData.setData('Text', target_element.textContent.trim());
} }
if (event && event.clipboardData) if (event && event.clipboardData)
{ {
@ -181,7 +183,7 @@ egw.extend('utils', egw.MODULE_GLOBAL, function()
} }
} }
let textArea; let textArea;
if (!window.clipboardData) if (!win.clipboardData)
{ {
textArea = document.createElement("textarea"); textArea = document.createElement("textarea");
@ -192,7 +194,7 @@ egw.extend('utils', egw.MODULE_GLOBAL, function()
textArea.style.left = "0"; textArea.style.left = "0";
textArea.style.position = "fixed"; textArea.style.position = "fixed";
document.body.appendChild(textArea); win.document.body.appendChild(textArea);
textArea.focus(); textArea.focus();
textArea.select(); textArea.select();
} }
@ -200,7 +202,7 @@ egw.extend('utils', egw.MODULE_GLOBAL, function()
let successful = false; let successful = false;
try try
{ {
successful = document.execCommand('copy'); successful = win.document.execCommand('copy');
const msg = successful ? 'successful' : 'unsuccessful'; const msg = successful ? 'successful' : 'unsuccessful';
console.log('Fallback: Copying text command was ' + msg); console.log('Fallback: Copying text command was ' + msg);
} }
@ -209,7 +211,7 @@ egw.extend('utils', egw.MODULE_GLOBAL, function()
successful = false; successful = false;
} }
document.body.removeChild(textArea); win.document.body.removeChild(textArea);
return successful; return successful;
} }
@ -514,7 +516,8 @@ egw.extend('utils', egw.MODULE_GLOBAL, function()
return Promise.resolve(success ? undefined : false); return Promise.resolve(success ? undefined : false);
} }
// Use Clipboard API // Use Clipboard API
return navigator.clipboard.writeText(text); const win = target_element?.ownerDocument.defaultView ?? target_element.ownerDocument.parentWindow ?? window;
return win.navigator.clipboard.writeText(text);
} }
}; };