append copy textarea to modal if open

This commit is contained in:
Kyle Spearrin 2018-07-25 22:01:26 -04:00
parent f9e756402f
commit 87c72bd595
1 changed files with 8 additions and 4 deletions

View File

@ -208,7 +208,7 @@ export class WebPlatformUtilsService implements PlatformUtilsService {
} }
copyToClipboard(text: string, options?: any): void { copyToClipboard(text: string, options?: any): void {
const doc = options ? options.doc : window.document; const doc: Document = options ? options.doc : window.document;
if ((window as any).clipboardData && (window as any).clipboardData.setData) { if ((window as any).clipboardData && (window as any).clipboardData.setData) {
// IE specific code path to prevent textarea being shown while dialog is visible. // IE specific code path to prevent textarea being shown while dialog is visible.
(window as any).clipboardData.setData('Text', text); (window as any).clipboardData.setData('Text', text);
@ -217,9 +217,13 @@ export class WebPlatformUtilsService implements PlatformUtilsService {
textarea.textContent = text; textarea.textContent = text;
// Prevent scrolling to bottom of page in MS Edge. // Prevent scrolling to bottom of page in MS Edge.
textarea.style.position = 'fixed'; textarea.style.position = 'fixed';
doc.body.appendChild(textarea); let copyEl = doc.body;
// For some reason copy command won't work in Firefox when modal is open if appending to body
if (this.isFirefox() && doc.body.classList.contains('modal-open')) {
copyEl = doc.body.querySelector<HTMLElement>('.modal');
}
copyEl.appendChild(textarea);
textarea.select(); textarea.select();
try { try {
// Security exception may be thrown by some browsers. // Security exception may be thrown by some browsers.
doc.execCommand('copy'); doc.execCommand('copy');
@ -227,7 +231,7 @@ export class WebPlatformUtilsService implements PlatformUtilsService {
// tslint:disable-next-line // tslint:disable-next-line
console.warn('Copy to clipboard failed.', e); console.warn('Copy to clipboard failed.', e);
} finally { } finally {
doc.body.removeChild(textarea); copyEl.removeChild(textarea);
} }
} }
} }