diff --git a/lib/core/clipboard/clipboard.service.spec.ts b/lib/core/clipboard/clipboard.service.spec.ts index 92e0085c48..3dd80dbd03 100644 --- a/lib/core/clipboard/clipboard.service.spec.ts +++ b/lib/core/clipboard/clipboard.service.spec.ts @@ -54,7 +54,7 @@ describe('ClipboardService', () => { }); it('should copy text to clipboard', () => { - spyOn(document, 'execCommand'); + spyOn(navigator.clipboard, 'writeText'); spyOn(inputElement, 'select'); spyOn(inputElement, 'setSelectionRange'); @@ -65,7 +65,7 @@ describe('ClipboardService', () => { expect(inputElement.select).toHaveBeenCalledWith(); expect(inputElement.setSelectionRange) .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', () => { @@ -76,4 +76,14 @@ describe('ClipboardService', () => { 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'); + }); }); diff --git a/lib/core/clipboard/clipboard.service.ts b/lib/core/clipboard/clipboard.service.ts index 54f50c75b2..712d57477e 100644 --- a/lib/core/clipboard/clipboard.service.ts +++ b/lib/core/clipboard/clipboard.service.ts @@ -26,7 +26,7 @@ export class ClipboardService { constructor( @Inject(DOCUMENT) private document: any, private logService: LogService, - private notificationService: NotificationService) {} + private notificationService: NotificationService) { } /** * Checks if the target element can have its text copied. @@ -52,7 +52,11 @@ export class ClipboardService { try { target.select(); 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); } catch (error) { this.logService.error(error); @@ -68,12 +72,16 @@ export class ClipboardService { */ copyContentToClipboard(content: string, message: string) { try { - document.addEventListener('copy', (e: ClipboardEvent) => { - e.clipboardData.setData('text/plain', (content)); - e.preventDefault(); - document.removeEventListener('copy', null); - }); - document.execCommand('copy'); + if (navigator.clipboard) { + navigator.clipboard.writeText(content); + } else { + document.addEventListener('copy', (e: ClipboardEvent) => { + e.clipboardData.setData('text/plain', (content)); + e.preventDefault(); + document.removeEventListener('copy', null); + }); + document.execCommand('copy'); + } this.notify(message); } catch (error) { this.logService.error(error);