1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 00:03:56 +00:00

Refactor Send 'copy link' functionality (#960)

* Refactor Send 'copy link' functionality

* bump jslib

* Print debug message if copyToClipboard fails

* fix linting
This commit is contained in:
Thomas Rittson
2021-05-12 10:51:12 +10:00
committed by GitHub
parent 97e1c7a2ea
commit 3ac2ce079a
4 changed files with 16 additions and 21 deletions

View File

@@ -3,6 +3,7 @@ import Swal, { SweetAlertIcon } from 'sweetalert2';
import { DeviceType } from 'jslib/enums/deviceType';
import { I18nService } from 'jslib/abstractions/i18n.service';
import { LogService } from 'jslib/abstractions/log.service';
import { MessagingService } from 'jslib/abstractions/messaging.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
@@ -11,7 +12,8 @@ export class WebPlatformUtilsService implements PlatformUtilsService {
private browserCache: DeviceType = null;
constructor(private i18nService: I18nService, private messagingService: MessagingService) { }
constructor(private i18nService: I18nService, private messagingService: MessagingService,
private logService: LogService) { }
getDevice(): DeviceType {
if (this.browserCache != null) {
@@ -245,7 +247,7 @@ export class WebPlatformUtilsService implements PlatformUtilsService {
return process.env.SELF_HOST.toString() === 'true';
}
copyToClipboard(text: string, options?: any): void {
copyToClipboard(text: string, options?: any): void | boolean {
let win = window;
let doc = window.document;
if (options && (options.window || options.win)) {
@@ -269,11 +271,12 @@ export class WebPlatformUtilsService implements PlatformUtilsService {
}
copyEl.appendChild(textarea);
textarea.select();
let success = false;
try {
// Security exception may be thrown by some browsers.
const copyEnabled = doc.execCommand('copy');
if (!copyEnabled) {
throw new Error('Command unsupported or disabled');
success = doc.execCommand('copy');
if (!success) {
this.logService.debug('Copy command unsupported or disabled.');
}
} catch (e) {
// tslint:disable-next-line
@@ -281,6 +284,7 @@ export class WebPlatformUtilsService implements PlatformUtilsService {
} finally {
copyEl.removeChild(textarea);
}
return success;
}
}