ng-ytl-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
101 lines (88 loc) • 3.3 kB
text/typescript
import { Input, Component, OnInit, ViewEncapsulation, ElementRef, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';
export class NzCodeBoxComponent implements OnInit {
_code: string;
_copied = false;
nzTitle: string;
nzExpanded = false;
get nzCode(): string {
return this._code;
}
set nzCode(value: string) {
this._code = value;
}
copyCode(code) {
this.copy(code).then(() => {
this._copied = true;
setTimeout(() => {
this._copied = false;
}, 1000);
});
}
copy(value: string): Promise<string> {
const promise = new Promise<string>(
(resolve, reject): void => {
let copyTextArea = null as HTMLTextAreaElement;
try {
copyTextArea = this.dom.createElement('textarea');
copyTextArea.style.height = '0px';
copyTextArea.style.opacity = '0';
copyTextArea.style.width = '0px';
this.dom.body.appendChild(copyTextArea);
copyTextArea.value = value;
copyTextArea.select();
this.dom.execCommand('copy');
resolve(value);
} finally {
if (copyTextArea && copyTextArea.parentNode) {
copyTextArea.parentNode.removeChild(copyTextArea);
}
}
}
);
return ( promise );
}
constructor( private dom: Document, private _el: ElementRef) {
}
ngOnInit() {
}
}