From e5fec1fc9a64b5b35849bc5f5c43a099c82e8c1e Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 6 Nov 2017 21:55:52 -0500 Subject: [PATCH] move copy to clipboard to static --- src/services/autofill.service.ts | 2 +- src/services/utils.service.ts | 49 +++++++++++++++++--------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/services/autofill.service.ts b/src/services/autofill.service.ts index 23198983f6..671e8c32f2 100644 --- a/src/services/autofill.service.ts +++ b/src/services/autofill.service.ts @@ -174,7 +174,7 @@ export default class AutofillService { return null; }).then((code: string) => { if (code) { - this.utilsService.copyToClipboard(code); + UtilsService.copyToClipboard(code); } return code; diff --git a/src/services/utils.service.ts b/src/services/utils.service.ts index 389b82f7c6..20b7be18b0 100644 --- a/src/services/utils.service.ts +++ b/src/services/utils.service.ts @@ -8,6 +8,31 @@ const AnalyticsIds = { }; export default class UtilsService { + static copyToClipboard(text: string, doc?: Document): void { + doc = doc || document; + if ((window as any).clipboardData && (window as any).clipboardData.setData) { + // IE specific code path to prevent textarea being shown while dialog is visible. + (window as any).clipboardData.setData('Text', text); + } else if (doc.queryCommandSupported && doc.queryCommandSupported('copy')) { + const textarea = doc.createElement('textarea'); + textarea.textContent = text; + // Prevent scrolling to bottom of page in MS Edge. + textarea.style.position = 'fixed'; + doc.body.appendChild(textarea); + textarea.select(); + + try { + // Security exception may be thrown by some browsers. + doc.execCommand('copy'); + } catch (e) { + // tslint:disable-next-line + console.warn('Copy to clipboard failed.', e); + } finally { + doc.body.removeChild(textarea); + } + } + } + static urlBase64Decode(str: string): string { let output = str.replace(/-/g, '+').replace(/_/g, '/'); switch (output.length % 4) { @@ -326,29 +351,7 @@ export default class UtilsService { } copyToClipboard(text: string, doc?: Document) { - doc = doc || document; - if ((window as any).clipboardData && (window as any).clipboardData.setData) { - // IE specific code path to prevent textarea being shown while dialog is visible. - return (window as any).clipboardData.setData('Text', text); - } else if (doc.queryCommandSupported && doc.queryCommandSupported('copy')) { - const textarea = doc.createElement('textarea'); - textarea.textContent = text; - // Prevent scrolling to bottom of page in MS Edge. - textarea.style.position = 'fixed'; - doc.body.appendChild(textarea); - textarea.select(); - - try { - // Security exception may be thrown by some browsers. - return doc.execCommand('copy'); - } catch (ex) { - // tslint:disable-next-line - console.warn('Copy to clipboard failed.', ex); - return false; - } finally { - doc.body.removeChild(textarea); - } - } + UtilsService.copyToClipboard(text, doc); } inSidebar(theWindow: Window) {