1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-28 17:27:50 +01: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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 21 deletions

2
jslib

@ -1 +1 @@
Subproject commit a72c8a60c1b7a6980bceee456c53a9ea7b9b3451
Subproject commit 8244971026ffefb962e235a79c5cb219163bead9

View File

@ -12,8 +12,6 @@ import { UserService } from 'jslib/abstractions/user.service';
import { AddEditComponent as BaseAddEditComponent } from 'jslib/angular/components/send/add-edit.component';
import { SendType } from 'jslib/enums/sendType';
@Component({
selector: 'app-send-add-edit',
templateUrl: 'add-edit.component.html',
@ -27,18 +25,11 @@ export class AddEditComponent extends BaseAddEditComponent {
messagingService, policyService);
}
async showSuccessMessage(inactive: boolean) {
if (inactive && this.copyLink && this.send.type === SendType.File) {
await this.platformUtilsService.showDialog(this.i18nService.t('createdSend'), null,
this.i18nService.t('ok'), null, 'success', null);
} else {
await super.showSuccessMessage(inactive);
}
}
copyLinkToClipboard(link: string) {
async copyLinkToClipboard(link: string): Promise<void | boolean> {
// Copy function on web depends on the modal being open or not. Since this event occurs during a transition
// of the modal closing we need to add a small delay to make sure state of the DOM is consistent.
window.setTimeout(() => super.copyLinkToClipboard(link), 500);
return new Promise(resolve => {
window.setTimeout(() => resolve(super.copyLinkToClipboard(link)), 500);
});
}
}

View File

@ -91,12 +91,12 @@ const i18nService = new I18nService(window.navigator.language, 'locales');
const stateService = new StateService();
const broadcasterService = new BroadcasterService();
const messagingService = new BroadcasterMessagingService(broadcasterService);
const platformUtilsService = new WebPlatformUtilsService(i18nService, messagingService);
const consoleLogService = new ConsoleLogService(false);
const platformUtilsService = new WebPlatformUtilsService(i18nService, messagingService, consoleLogService);
const storageService: StorageServiceAbstraction = new HtmlStorageService(platformUtilsService);
const secureStorageService: StorageServiceAbstraction = new MemoryStorageService();
const cryptoFunctionService: CryptoFunctionServiceAbstraction = new WebCryptoFunctionService(window,
platformUtilsService);
const consoleLogService = new ConsoleLogService(false);
const cryptoService = new CryptoService(storageService,
platformUtilsService.isDev() ? storageService : secureStorageService, cryptoFunctionService, platformUtilsService,
consoleLogService);

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;
}
}