Skip to content

Commit 058b54c

Browse files
committed
refactor(site): replace raw-loader with http
1 parent 73ca476 commit 058b54c

10 files changed

+176
-93
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@
107107
"prettier": "^3.1.1",
108108
"prismjs": "^1.29.0",
109109
"protractor": "^7.0.0",
110-
"raw-loader": "^4.0.2",
111110
"readline-sync": "^1.4.9",
112111
"remark": "^13.0.0",
113112
"rollup": "^1.10.1",
@@ -147,4 +146,4 @@
147146
"Android > 4.4"
148147
],
149148
"repository": "https://github.com/NG-ZORRO/ng-zorro-antd-mobile.git"
150-
}
149+
}
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,68 @@
11
<section class="code-box" [ngClass]="{'expand':nzExpanded, 'code-box-target':nzSelected}" [attr.id]="nzId">
22
<section class="code-box-meta markdown">
33
<div class="code-box-title">
4-
<a (click)="navigateToFragment()">{{ nzTitle }}
4+
<a>
5+
{{ nzTitle }}
56
<a class="edit-button" [attr.href]="nzHref" target="_blank">
67
<i nz-icon nzType="edit"></i>
78
</a>
89
</a>
910
</div>
1011
<ng-content select="[intro]"></ng-content>
11-
<span class="code-expand-icon" nz-tooltip [nzTooltipTitle]="nzExpanded?'Hide Code':'Show Code'" (click)="nzExpanded=!nzExpanded">
12-
<img alt="expand code"
13-
class="code-expand-icon-show"
14-
src="https://gw.alipayobjects.com/zos/rmsportal/OpROPHYqWmrMDBFMZtKF.svg"
15-
/>
16-
<nz-modal
17-
[(nzVisible)]="nzExpanded"
18-
[nzWidth]="nzWidth"
19-
[nzTitle]="nzTitle"
20-
[nzCancelText]="null"
21-
[nzOkText]="nzOkText()"
22-
(nzOnCancel)="handleCancel()"
23-
(nzOnOk)="handleOk()"
24-
>
25-
<nz-highlight *nzModalContent [nzCode]="_code" [nzLanguage]="'typescript'"></nz-highlight>
26-
</nz-modal>
27-
</span>
2812
</section>
2913
<div class="code-box-actions" style="position: relative; top: 0">
30-
<i nz-tooltip [nzTooltipTitle]="'Edit On StackBlitz'" style="opacity: 1;" class="code-box-code-copy" nz-icon nzType="form" (click)="openOnStackBlitz()"></i>
31-
<i nz-tooltip [nzTooltipTitle]="'Copy Code'" nz-icon [nzType]="_copied ? 'check' : 'copy'" class="code-box-code-copy" style="opacity: 1;" (click)="copyCode(nzRawCode.default)"></i>
32-
<i *ngIf="nzGenerateCommand" nz-tooltip [nzTooltipTitle]="'Copy Generate Command'" nz-icon [nzType]="_commandCopied ? 'check' : 'code'" class="code-box-code-copy" style="opacity: 1;" (click)="copyGenerateCommand(nzGenerateCommand)"></i>
14+
<span
15+
class="code-box-code-copy"
16+
style="opacity: 1;"
17+
nz-icon
18+
nz-tooltip
19+
[nzTooltipTitle]="'Edit On StackBlitz'"
20+
nzType="form"
21+
(click)="openOnStackBlitz()"
22+
></span>
23+
<span
24+
class="code-box-code-copy"
25+
style="opacity: 1;"
26+
nz-icon
27+
nz-tooltip
28+
[nzTooltipTitle]="'Copy Code'"
29+
[nzType]="_copied ? 'check' : 'copy'"
30+
(click)="copyCode()"
31+
></span>
32+
<span
33+
*ngIf="nzGenerateCommand"
34+
class="code-box-code-copy"
35+
style="opacity: 1;"
36+
nz-icon
37+
nz-tooltip
38+
[nzTooltipTitle]="'Copy Generate Command'"
39+
[nzType]="_commandCopied ? 'check' : 'code'"
40+
(click)="copyGenerateCommand(nzGenerateCommand)"
41+
></span>
42+
<span
43+
class="code-expand-icon"
44+
nz-tooltip
45+
[nzTooltipTitle]="nzExpanded ? 'Hide Code' : 'Show Code'"
46+
(click)="expandCode(!nzExpanded)"
47+
>
48+
<img
49+
alt="expand code"
50+
src="https://gw.alipayobjects.com/zos/rmsportal/wSAkBuJFbdxsosKKpqyq.svg"
51+
[class.code-expand-icon-show]="nzExpanded"
52+
[class.code-expand-icon-hide]="!nzExpanded"
53+
/>
54+
<img
55+
alt="expand code"
56+
src="https://gw.alipayobjects.com/zos/rmsportal/OpROPHYqWmrMDBFMZtKF.svg"
57+
[class.code-expand-icon-show]="!nzExpanded"
58+
[class.code-expand-icon-hide]="nzExpanded"
59+
/>
60+
</span>
3361
</div>
34-
<section class="highlight-wrapper" [ngClass]="{'highlight-wrapper-expand':true}">
62+
<section class="highlight-wrapper" [class.highlight-wrapper-expand]="nzExpanded">
3563
<div class="highlight">
3664
<ng-content select="[code]"></ng-content>
37-
<nz-highlight [nzCode]="_code" [nzLanguage]="'typescript'"></nz-highlight>
65+
<nz-highlight [nzCode]="codeLoaded ? _code : 'Loading'" [nzLanguage]="'typescript'"></nz-highlight>
3866
</div>
3967
</section>
4068
</section>

scripts/site/_site/src/app/share/nz-codebox/nz-codebox.component.ts

+59-35
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
import { DOCUMENT } from '@angular/common';
22
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';
44
import { Router, ActivatedRoute } from '@angular/router';
55
import { environment } from '../../../environments/environment';
66
import sdk from '@stackblitz/sdk';
7+
import { AppService, DemoCode } from '../../app.service';
8+
import { Observable, Subject, takeUntil, tap } from 'rxjs';
79

810
@Component({
911
selector: 'nz-code-box',
1012
encapsulation: ViewEncapsulation.None,
1113
templateUrl: './nz-codebox.component.html',
1214
styleUrls: ['./nz-codebox.less']
1315
})
14-
export class NzCodeBoxComponent implements OnInit {
16+
export class NzCodeBoxComponent implements OnInit, OnDestroy {
1517
_code: string;
1618
_copied = false;
1719
_commandCopied = false;
20+
highlightCode?: string;
21+
copyLoading = false;
22+
codeLoaded = false;
1823
showIframe: boolean;
1924
simulateIFrame: boolean;
2025
iframe: SafeUrl;
21-
nzWidth = window.innerWidth * 0.8;
26+
destroy$ = new Subject<void>();
2227
@Input() nzTitle: string;
2328
@Input() nzExpanded = false;
2429
@Input() nzSelected = false;
@@ -46,35 +51,6 @@ export class NzCodeBoxComponent implements OnInit {
4651
this._code = value;
4752
}
4853

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-
7854
copyGenerateCommand(command) {
7955
this.copy(command).then(() => {
8056
this._commandCopied = true;
@@ -85,7 +61,8 @@ export class NzCodeBoxComponent implements OnInit {
8561
}
8662

8763
copy(value: string): Promise<string> {
88-
const promise = new Promise<string>((resolve, reject): void => {
64+
return new Promise<string>((resolve): void => {
65+
// @ts-ignore
8966
let copyTextArea = null as HTMLTextAreaElement;
9067
try {
9168
copyTextArea = this.dom.createElement('textarea');
@@ -103,10 +80,50 @@ export class NzCodeBoxComponent implements OnInit {
10380
}
10481
}
10582
});
83+
}
10684

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+
});
108101
}
109102

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+
110127
/** bug here https://github.com/stackblitz/core/issues/311 **/
111128
openOnStackBlitz() {
112129
sdk.openProject({
@@ -361,10 +378,17 @@ export class AppModule { }
361378
constructor(
362379
@Inject(DOCUMENT) private dom: Document,
363380
private sanitizer: DomSanitizer,
381+
private appService: AppService,
382+
private cdr: ChangeDetectorRef,
364383
private _el: ElementRef,
365384
private activatedRoute: ActivatedRoute,
366385
private router: Router
367386
) {}
368387

369388
ngOnInit() {}
370-
}
389+
390+
ngOnDestroy(): void {
391+
this.destroy$.next();
392+
this.destroy$.complete();
393+
}
394+
}

scripts/site/_site/src/style/demo.less

+37-21
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,7 @@
128128
}
129129

130130
.code-expand-icon {
131-
position: absolute;
132-
right: 16px;
133-
bottom: 23px;
134131
cursor: pointer;
135-
width: 16px;
136-
height: 16px;
137-
line-height: 16px;
138-
text-align: center;
139-
font-size: 14px;
140-
font-weight: bold;
141-
padding: 8px;
142-
background-color: rgba(256, 256, 256, 0.6);
143-
z-index: 1000;
144132
}
145133

146134
.code-expand-icon-show,
@@ -174,7 +162,7 @@
174162
}
175163

176164
.highlight-wrapper {
177-
display: block;
165+
display: none;
178166
overflow: auto;
179167
border-radius: 0 0 @border-radius-sm @border-radius-sm;
180168
max-height: 150px;
@@ -197,14 +185,42 @@
197185
}
198186

199187
&-actions {
200-
position: absolute;
201-
top: 10px;
202-
right: 12px;
203-
text-align: right;
204-
> i,
205-
> form {
188+
display: flex;
189+
justify-content: center;
190+
padding: 12px 0;
191+
border-top: 1px dashed @site-border-color-split;
192+
opacity: 0.7;
193+
transition: opacity 0.3s;
194+
195+
&:hover {
196+
opacity: 1;
197+
}
198+
199+
> form,
200+
> span {
201+
position: relative;
206202
display: inline-block;
207-
margin-left: 8px;
203+
width: 16px;
204+
height: 16px;
205+
margin-left: 16px;
206+
vertical-align: top;
207+
208+
.ant-row-rtl & {
209+
margin-right: 16px;
210+
margin-left: 0;
211+
}
212+
213+
&:first-child {
214+
margin-left: 0;
215+
216+
.ant-row-rtl & {
217+
margin-right: 0;
218+
}
219+
}
220+
}
221+
222+
> form {
223+
top: -2px;
208224
}
209225
}
210226

@@ -269,4 +285,4 @@
269285
background: #fff;
270286
}
271287
}
272-
}
288+
}

scripts/site/_site/src/styles.less

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* You can add global styles to this file, and also import other style files */
2-
@import '~@angular/cdk/overlay-prebuilt.css';
2+
@import 'node_modules/@angular/cdk/overlay-prebuilt.css';
33
@import "../../node_modules/ng-zorro-antd/src/ng-zorro-antd.less";
44
@import "../../components/ng-zorro-antd-mobile.less";
55
@import "./style/index";
6-
@import "./theme";
6+
@import "./theme";

scripts/site/generate-site.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const nameWithoutSuffixUtil = require('./utils/name-without-suffix');
66
const generateCode = require('./utils/generate-code');
77
const generateCodeBox = require('./utils/generate-code-box');
88
const generateDemo = require('./utils/generate-demo');
9+
const generateDemoCodeFiles = require('./utils/generate-demo-code-files');
910
const generateDocs = require('./utils/generate-docs');
1011
const generateRoutes = require('./utils/generate-routes');
1112
const getMeta = require('./utils/get-meta');
@@ -93,6 +94,7 @@ rootDir.forEach(componentName => {
9394
};
9495
componentsMap[componentName] = result.docZh.meta;
9596
generateDemo(showCaseComponentPath, result);
97+
generateDemoCodeFiles(result, showCasePath);
9698
}
9799
});
98100

@@ -113,9 +115,7 @@ if (!isSyncSpecific) {
113115
en: getMeta(docsMap[name].en)
114116
}
115117
});
116-
118+
117119
generateDocs(showCaseTargetPath, docsMap);
118120
generateRoutes(showCaseTargetPath, componentsMap, docsMeta);
119121
}
120-
121-

scripts/site/template/code.template.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<nz-code-box nzTitle="{{title}}" [nzId]="'components-{{component}}-demo-{{key}}'" nzSelector="demo-{{component}}-{{key}}" [nzLink]="'components-{{component}}-demo-{{key}}'" nzGenerateCommand="{{nzGenerateCommand}}" nzComponentName="{{componentName}}" [nzCode]="{{code}}" [nzRawCode]="{{rawCode}}" nzIframeSource="{{iframeSource}}" nzIframeHeight="{{iframeHeight}}" [nzHref]="'https://github.com/NG-ZORRO/ng-zorro-antd-mobile/edit/master/components/{{component}}/demo/{{key}}.md'" (click)="selectedCard('components-{{component}}-demo-{{key}}')">
1+
<nz-code-box nzTitle="{{title}}" [nzId]="'components-{{component}}-demo-{{key}}'" nzSelector="demo-{{component}}-{{key}}" [nzLink]="'components-{{component}}-demo-{{key}}'" nzGenerateCommand="{{nzGenerateCommand}}" nzComponentName="{{componentName}}" [nzCode]="{{code}}" nzIframeSource="{{iframeSource}}" nzIframeHeight="{{iframeHeight}}" [nzHref]="'https://github.com/NG-ZORRO/ng-zorro-antd-mobile/edit/master/components/{{component}}/demo/{{key}}.md'" (click)="selectedCard('components-{{component}}-demo-{{key}}')">
22
<div intro ngNonBindable>
33
{{doc}}
44
</div>

0 commit comments

Comments
 (0)