diff --git a/src/angular/directives/flex-copy.directive.ts b/src/angular/directives/flex-copy.directive.ts new file mode 100644 index 0000000000..5b8636f0ab --- /dev/null +++ b/src/angular/directives/flex-copy.directive.ts @@ -0,0 +1,28 @@ +import { + Directive, + ElementRef, + HostListener, +} from '@angular/core'; + +import { PlatformUtilsService } from '../../abstractions/platformUtils.service'; + +@Directive({ + selector: '[appFlexCopy]', +}) +export class FlexCopyDirective { + constructor(private el: ElementRef, private platformUtilsService: PlatformUtilsService) { } + + @HostListener('copy') onCopy() { + if (window == null) { + return; + } + let copyText = ''; + const selection = window.getSelection(); + for (let i = 0; i < selection.rangeCount; i++) { + const range = selection.getRangeAt(i); + const text = range.toString(); + copyText += text; + } + this.platformUtilsService.copyToClipboard(copyText, { window: window }); + } +}