[AAE-8648] Fix deprecated copy to clipboard api (#7610)

This commit is contained in:
Pablo Martinez Garcia
2022-05-11 11:23:47 +02:00
committed by GitHub
parent 0b2e218b88
commit 24e5893a56
2 changed files with 28 additions and 10 deletions

View File

@@ -54,7 +54,7 @@ describe('ClipboardService', () => {
}); });
it('should copy text to clipboard', () => { it('should copy text to clipboard', () => {
spyOn(document, 'execCommand'); spyOn(navigator.clipboard, 'writeText');
spyOn(inputElement, 'select'); spyOn(inputElement, 'select');
spyOn(inputElement, 'setSelectionRange'); spyOn(inputElement, 'setSelectionRange');
@@ -65,7 +65,7 @@ describe('ClipboardService', () => {
expect(inputElement.select).toHaveBeenCalledWith(); expect(inputElement.select).toHaveBeenCalledWith();
expect(inputElement.setSelectionRange) expect(inputElement.setSelectionRange)
.toHaveBeenCalledWith(0, inputElement.value.length); .toHaveBeenCalledWith(0, inputElement.value.length);
expect(document.execCommand).toHaveBeenCalledWith('copy'); expect(navigator.clipboard.writeText).toHaveBeenCalledWith('some text');
}); });
it('should notify copy to clipboard with message', () => { it('should notify copy to clipboard with message', () => {
@@ -76,4 +76,14 @@ describe('ClipboardService', () => {
expect(notificationService.openSnackMessage).toHaveBeenCalledWith('success'); expect(notificationService.openSnackMessage).toHaveBeenCalledWith('success');
}); });
it('should copy content to clipboard', () => {
spyOn(navigator.clipboard, 'writeText');
spyOn(notificationService, 'openSnackMessage');
clipboardService.copyContentToClipboard('some text', 'some message');
expect(navigator.clipboard.writeText).toHaveBeenCalledWith('some text');
expect(notificationService.openSnackMessage).toHaveBeenCalledWith('some message');
});
}); });

View File

@@ -26,7 +26,7 @@ export class ClipboardService {
constructor( constructor(
@Inject(DOCUMENT) private document: any, @Inject(DOCUMENT) private document: any,
private logService: LogService, private logService: LogService,
private notificationService: NotificationService) {} private notificationService: NotificationService) { }
/** /**
* Checks if the target element can have its text copied. * Checks if the target element can have its text copied.
@@ -52,7 +52,11 @@ export class ClipboardService {
try { try {
target.select(); target.select();
target.setSelectionRange(0, target.value.length); target.setSelectionRange(0, target.value.length);
this.document.execCommand('copy'); if (navigator.clipboard) {
navigator.clipboard.writeText(target.value);
} else {
this.document.execCommand('copy');
}
this.notify(message); this.notify(message);
} catch (error) { } catch (error) {
this.logService.error(error); this.logService.error(error);
@@ -68,12 +72,16 @@ export class ClipboardService {
*/ */
copyContentToClipboard(content: string, message: string) { copyContentToClipboard(content: string, message: string) {
try { try {
document.addEventListener('copy', (e: ClipboardEvent) => { if (navigator.clipboard) {
e.clipboardData.setData('text/plain', (content)); navigator.clipboard.writeText(content);
e.preventDefault(); } else {
document.removeEventListener('copy', null); document.addEventListener('copy', (e: ClipboardEvent) => {
}); e.clipboardData.setData('text/plain', (content));
document.execCommand('copy'); e.preventDefault();
document.removeEventListener('copy', null);
});
document.execCommand('copy');
}
this.notify(message); this.notify(message);
} catch (error) { } catch (error) {
this.logService.error(error); this.logService.error(error);