nise/nise-frontend/src/corelib/components/code-with-copy-button/code-with-copy-button.component.ts

33 lines
822 B
TypeScript

import {AfterViewInit, ChangeDetectorRef, Component, ElementRef, Input} from '@angular/core';
import {NgIf} from "@angular/common";
@Component({
selector: 'app-code-with-copy-button',
standalone: true,
imports: [
NgIf
],
templateUrl: './code-with-copy-button.component.html',
styleUrl: './code-with-copy-button.component.css'
})
export class CodeWithCopyButtonComponent implements AfterViewInit {
@Input() textToCopy!: string;
constructor(private elRef: ElementRef, private cdr: ChangeDetectorRef) {}
ngAfterViewInit() {
this.textToCopy = this.elRef.nativeElement.innerText.trim();
this.cdr.detectChanges();
}
copyToClipboard() {
navigator.clipboard.writeText(this.textToCopy).then(() => {
}).catch(err => {
console.error('could not copy text:', err);
});
}
}