You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import { Directive, Input, Output, EventEmitter, HostListener } from '@angular/core';
|
|
|
|
@Directive({
|
|
selector: '[rtlClipboard]'
|
|
})
|
|
export class ClipboardDirective {
|
|
|
|
@Input() payload: string;
|
|
|
|
@Output()
|
|
public readonly copied: EventEmitter<string> = new EventEmitter<string>();
|
|
|
|
@HostListener('click', ['$event'])
|
|
public onClick(event: MouseEvent): void {
|
|
event.preventDefault();
|
|
|
|
if (!this.payload) { return; }
|
|
|
|
if (navigator.clipboard) {
|
|
this.copyUsingClipboardAPI();
|
|
} else {
|
|
this.copyUsingFallbackMethod();
|
|
}
|
|
}
|
|
|
|
private copyUsingFallbackMethod(): void {
|
|
const input = document.createElement('textarea');
|
|
input.innerText = this.payload;
|
|
document.body.appendChild(input);
|
|
input.select();
|
|
|
|
try {
|
|
// Allowing deprecated command for older browsers where navigator is not available.
|
|
// For newer browsers where execCommand is deprecated, navigator should be available and this fallback will not be called.
|
|
// eslint-disable-next-line deprecation/deprecation
|
|
const result = document.execCommand('copy');
|
|
if (result) {
|
|
this.copied.emit(this.payload.toString());
|
|
} else {
|
|
this.copied.emit('Error could not copy text.');
|
|
}
|
|
} finally {
|
|
document.body.removeChild(input);
|
|
}
|
|
}
|
|
|
|
private copyUsingClipboardAPI(): void {
|
|
navigator.clipboard.writeText(this.payload.toString()).then(() => {
|
|
this.copied.emit(this.payload.toString());
|
|
}).catch((err) => {
|
|
this.copied.emit('Error could not copy text: ' + JSON.stringify(err));
|
|
});
|
|
}
|
|
|
|
}
|