4
4
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
*/
6
6
7
- import { AfterViewChecked , AfterViewInit , ChangeDetectionStrategy , Component , ElementRef , Input , NgZone , ViewChild } from "@angular/core" ;
7
+ import { AfterViewInit , ChangeDetectionStrategy , Component , ElementRef , Input , NgZone , OnChanges , signal , ViewChild } from "@angular/core" ;
8
8
9
9
import Prism from "prismjs" ;
10
10
import { initCustomScrollbars } from "typedb-web-common/lib" ;
11
+ import { MatTooltipModule } from "@angular/material/tooltip" ;
11
12
12
13
const DEFAULT_MIN_LINES = { desktop : 33 , mobile : 13 } ;
13
14
@@ -17,12 +18,16 @@ const DEFAULT_MIN_LINES = { desktop: 33, mobile: 13 };
17
18
styleUrls : [ "code-snippet.component.scss" ] ,
18
19
changeDetection : ChangeDetectionStrategy . OnPush ,
19
20
standalone : true ,
20
- imports : [ ] ,
21
+ imports : [ MatTooltipModule ] ,
21
22
} )
22
- export class CodeSnippetComponent implements AfterViewInit , AfterViewChecked {
23
+ export class CodeSnippetComponent implements AfterViewInit , OnChanges {
23
24
@Input ( { required : true } ) snippet ! : { language ?: string , code : string } ;
24
25
@ViewChild ( "scrollbarX" ) scrollbarX ! : ElementRef < HTMLElement > ;
25
26
@ViewChild ( "scrollbarY" ) scrollbarY ! : ElementRef < HTMLElement > ;
27
+ @ViewChild ( "rootElement" ) rootElement ! : ElementRef < HTMLElement > ;
28
+
29
+ showOverlay = signal ( false ) ;
30
+ copied = signal ( false ) ;
26
31
27
32
get lineNumbers ( ) {
28
33
return [ ...Array ( Math . max (
@@ -34,10 +39,30 @@ export class CodeSnippetComponent implements AfterViewInit, AfterViewChecked {
34
39
constructor ( private ngZone : NgZone , private elementRef : ElementRef ) { }
35
40
36
41
ngAfterViewInit ( ) {
37
- this . ngZone . runOutsideAngular ( ( ) => initCustomScrollbars ( this . elementRef . nativeElement ) ) ;
42
+ this . maybeInitScrollbarsAndHighlighting ( ) ;
43
+ }
44
+
45
+ ngOnChanges ( ) {
46
+ this . maybeInitScrollbarsAndHighlighting ( ) ;
47
+ }
48
+
49
+ maybeInitScrollbarsAndHighlighting ( ) {
50
+ if ( this . snippet && this . rootElement ) {
51
+ Prism . highlightAllUnder ( this . elementRef . nativeElement ) ;
52
+ }
38
53
}
39
54
40
- ngAfterViewChecked ( ) {
41
- Prism . highlightAll ( ) ;
55
+ async copyCode ( ) {
56
+ try {
57
+ await navigator . clipboard . writeText ( this . snippet . code ) ;
58
+ this . copied . set ( true ) ;
59
+
60
+ // Reset copied state after 2 seconds
61
+ setTimeout ( ( ) => {
62
+ this . copied . set ( false ) ;
63
+ } , 2000 ) ;
64
+ } catch ( err ) {
65
+ console . error ( 'Failed to copy code:' , err ) ;
66
+ }
42
67
}
43
68
}
0 commit comments