diff options
author | 2025-01-17 17:01:17 +0800 | |
---|---|---|
committer | 2025-01-20 11:35:33 +0800 | |
commit | 6f8694edd30c021bea991ce5a7adf85359f9a6d8 (patch) | |
tree | 6ee41c9ab98a413771dd7729dce97e14137f5ece /usecaseui-portal/src/app/views/maas/use/code-block.directive.ts | |
parent | d67b4b25e73bba60cc72a5c1c68e178d9ad93b3c (diff) |
Optimize your code and the Q&A assistant page has a shortcut to send questions.15.0.1
Issue-ID: USECASEUI-844
Change-Id: I652edab1945e3c2366fea388d6e3d8e5360d2922
Signed-off-by: kaixiliu <liukaixi@chinamobile.com>
Diffstat (limited to 'usecaseui-portal/src/app/views/maas/use/code-block.directive.ts')
-rw-r--r-- | usecaseui-portal/src/app/views/maas/use/code-block.directive.ts | 51 |
1 files changed, 27 insertions, 24 deletions
diff --git a/usecaseui-portal/src/app/views/maas/use/code-block.directive.ts b/usecaseui-portal/src/app/views/maas/use/code-block.directive.ts index 31247184..08efe92c 100644 --- a/usecaseui-portal/src/app/views/maas/use/code-block.directive.ts +++ b/usecaseui-portal/src/app/views/maas/use/code-block.directive.ts @@ -1,4 +1,4 @@ -import { AfterViewChecked, AfterViewInit, Directive, ElementRef, Renderer2 } from '@angular/core'; +import { Directive, ElementRef, Renderer2, Input } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; import { NzMessageService } from 'ng-zorro-antd'; import { ClipboardService } from 'ngx-clipboard'; @@ -6,33 +6,36 @@ import { ClipboardService } from 'ngx-clipboard'; @Directive({ selector: '[appCodeBlock]' }) -export class CodeBlockDirective implements AfterViewChecked { +export class CodeBlockDirective { + @Input() set appCodeBlock(status: string) { + if (status === 'finished') { + setTimeout(() => { + this.setCopyButton(); + }, 0); + + } + } constructor(private el: ElementRef, private renderer: Renderer2, private clipboardService: ClipboardService, private message: NzMessageService, private translate: TranslateService ) { } - ngAfterViewChecked() { - this.setCopyButton(); - } - -setCopyButton() { - const preElements = this.el.nativeElement.querySelectorAll('pre'); - - preElements.forEach(pre => { - const codeElement = pre.querySelector('code'); - const copyButtonExists = pre.querySelector('button.copy-button'); - if (codeElement && !copyButtonExists) { - const copyButton = this.renderer.createElement('button'); - this.renderer.addClass(copyButton, 'copy-button'); - this.renderer.setProperty(copyButton, 'innerHTML', 'Copy'); - this.renderer.listen(copyButton, 'click', () => { - this.clipboardService.copyFromContent(codeElement.innerText); - this.message.success(this.translate.instant('maas.copy_to_clipboard')); - }); - this.renderer.insertBefore(pre, copyButton, codeElement); - } - }); -} + setCopyButton() { + const preElements = this.el.nativeElement.querySelectorAll('pre'); + preElements.forEach(pre => { + const codeElement = pre.querySelector('code'); + const copyButtonExists = pre.querySelector('button.copy-button'); + if (codeElement && !copyButtonExists) { + const copyButton = this.renderer.createElement('button'); + this.renderer.addClass(copyButton, 'copy-button'); + this.renderer.setProperty(copyButton, 'innerHTML', 'Copy'); + this.renderer.listen(copyButton, 'click', () => { + this.clipboardService.copyFromContent(codeElement.innerText); + this.message.success(this.translate.instant('maas.copy_to_clipboard')); + }); + this.renderer.insertBefore(pre, copyButton, codeElement); + } + }); + } } |