1
0
mirror of https://github.com/bitwarden/desktop.git synced 2024-11-02 08:30:14 +01:00

move copy to clipboard to static

This commit is contained in:
Kyle Spearrin 2017-11-06 21:55:52 -05:00
parent 9c5a9da9f5
commit e5fec1fc9a
2 changed files with 27 additions and 24 deletions

View File

@ -174,7 +174,7 @@ export default class AutofillService {
return null;
}).then((code: string) => {
if (code) {
this.utilsService.copyToClipboard(code);
UtilsService.copyToClipboard(code);
}
return code;

View File

@ -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) {