1
1
import { DOCUMENT } from '@angular/common' ;
2
2
import { DomSanitizer , SafeUrl } from '@angular/platform-browser' ;
3
- import { Component , ElementRef , Inject , Input , OnInit , ViewEncapsulation } from '@angular/core' ;
3
+ import { ChangeDetectorRef , Component , ElementRef , Inject , Input , OnDestroy , OnInit , ViewEncapsulation } from '@angular/core' ;
4
4
import { Router , ActivatedRoute } from '@angular/router' ;
5
5
import { environment } from '../../../environments/environment' ;
6
6
import sdk from '@stackblitz/sdk' ;
7
+ import { AppService , DemoCode } from '../../app.service' ;
8
+ import { Observable , Subject , takeUntil , tap } from 'rxjs' ;
7
9
8
10
@Component ( {
9
11
selector : 'nz-code-box' ,
10
12
encapsulation : ViewEncapsulation . None ,
11
13
templateUrl : './nz-codebox.component.html' ,
12
14
styleUrls : [ './nz-codebox.less' ]
13
15
} )
14
- export class NzCodeBoxComponent implements OnInit {
16
+ export class NzCodeBoxComponent implements OnInit , OnDestroy {
15
17
_code : string ;
16
18
_copied = false ;
17
19
_commandCopied = false ;
20
+ highlightCode ?: string ;
21
+ copyLoading = false ;
22
+ codeLoaded = false ;
18
23
showIframe : boolean ;
19
24
simulateIFrame : boolean ;
20
25
iframe : SafeUrl ;
21
- nzWidth = window . innerWidth * 0.8 ;
26
+ destroy$ = new Subject < void > ( ) ;
22
27
@Input ( ) nzTitle : string ;
23
28
@Input ( ) nzExpanded = false ;
24
29
@Input ( ) nzSelected = false ;
@@ -46,35 +51,6 @@ export class NzCodeBoxComponent implements OnInit {
46
51
this . _code = value ;
47
52
}
48
53
49
- navigateToFragment ( ) {
50
- // window.location.hash = this.nzLink;
51
- }
52
-
53
- copyCode ( code ) {
54
- this . copy ( code ) . then ( ( ) => {
55
- this . _copied = true ;
56
- setTimeout ( ( ) => {
57
- this . _copied = false ;
58
- } , 1000 ) ;
59
- } ) ;
60
- }
61
-
62
- handleCancel ( ) : void {
63
- this . nzExpanded = false ;
64
- }
65
-
66
- handleOk ( ) : void {
67
- this . nzExpanded = false ;
68
- }
69
-
70
- nzOkText ( ) {
71
- if ( window . location . href . split ( '/' ) . splice ( - 1 ) [ 0 ] === 'zh' ) {
72
- return '返回' ;
73
- } else {
74
- return 'Back' ;
75
- }
76
- }
77
-
78
54
copyGenerateCommand ( command ) {
79
55
this . copy ( command ) . then ( ( ) => {
80
56
this . _commandCopied = true ;
@@ -85,7 +61,8 @@ export class NzCodeBoxComponent implements OnInit {
85
61
}
86
62
87
63
copy ( value : string ) : Promise < string > {
88
- const promise = new Promise < string > ( ( resolve , reject ) : void => {
64
+ return new Promise < string > ( ( resolve ) : void => {
65
+ // @ts -ignore
89
66
let copyTextArea = null as HTMLTextAreaElement ;
90
67
try {
91
68
copyTextArea = this . dom . createElement ( 'textarea' ) ;
@@ -103,10 +80,50 @@ export class NzCodeBoxComponent implements OnInit {
103
80
}
104
81
}
105
82
} ) ;
83
+ }
106
84
107
- return promise ;
85
+ copyCode ( ) : void {
86
+ setTimeout ( ( ) => {
87
+ this . copyLoading = ! this . codeLoaded ;
88
+ this . cdr . markForCheck ( ) ;
89
+ } , 120 ) ;
90
+ this . getDemoCode ( ) . subscribe ( data => {
91
+ this . copyLoading = false ;
92
+ this . cdr . markForCheck ( ) ;
93
+ this . copy ( data . rawCode ) . then ( ( ) => {
94
+ this . _copied = true ;
95
+ setTimeout ( ( ) => {
96
+ this . _copied = false ;
97
+ this . cdr . markForCheck ( ) ;
98
+ } , 1000 ) ;
99
+ } ) ;
100
+ } ) ;
108
101
}
109
102
103
+
104
+ getDemoCode ( ) : Observable < DemoCode > {
105
+ return this . appService . getCode ( this . nzId ) . pipe (
106
+ takeUntil ( this . destroy$ ) ,
107
+ tap ( data => {
108
+ if ( data ) {
109
+ this . highlightCode = data . highlightCode ;
110
+ this . codeLoaded = true ;
111
+ this . cdr . markForCheck ( ) ;
112
+ }
113
+ } )
114
+ ) ;
115
+ }
116
+
117
+ expandCode ( expanded : boolean ) : void {
118
+ this . nzExpanded = expanded ;
119
+ if ( expanded ) {
120
+ this . getDemoCode ( ) . subscribe ( ) ;
121
+ }
122
+ this . cdr . markForCheck ( ) ;
123
+ }
124
+
125
+
126
+
110
127
/** bug here https://github.com/stackblitz/core/issues/311 **/
111
128
openOnStackBlitz ( ) {
112
129
sdk . openProject ( {
@@ -361,10 +378,17 @@ export class AppModule { }
361
378
constructor (
362
379
@Inject ( DOCUMENT ) private dom : Document ,
363
380
private sanitizer : DomSanitizer ,
381
+ private appService : AppService ,
382
+ private cdr : ChangeDetectorRef ,
364
383
private _el : ElementRef ,
365
384
private activatedRoute : ActivatedRoute ,
366
385
private router : Router
367
386
) { }
368
387
369
388
ngOnInit ( ) { }
370
- }
389
+
390
+ ngOnDestroy ( ) : void {
391
+ this . destroy$ . next ( ) ;
392
+ this . destroy$ . complete ( ) ;
393
+ }
394
+ }
0 commit comments