@@ -8,13 +8,14 @@ import {
8
8
QueryList ,
9
9
Type ,
10
10
ViewChildren ,
11
- ɵNgModuleFactory
11
+ ɵNgModuleFactory ,
12
12
} from '@angular/core' ;
13
13
import { MatSnackBar } from '@angular/material/snack-bar' ;
14
14
import { Clipboard } from '@angular/cdk/clipboard' ;
15
15
16
16
import { EXAMPLE_COMPONENTS , LiveExample } from '@angular/components-examples' ;
17
17
import { CodeSnippet } from './code-snippet' ;
18
+ import { normalizePath } from '../normalize-path' ;
18
19
19
20
export type Views = 'snippet' | 'full' | 'demo' ;
20
21
@@ -27,7 +28,7 @@ const preferredExampleFileOrder = ['HTML', 'TS', 'CSS'];
27
28
@Component ( {
28
29
selector : 'example-viewer' ,
29
30
templateUrl : './example-viewer.html' ,
30
- styleUrls : [ './example-viewer.scss' ]
31
+ styleUrls : [ './example-viewer.scss' ] ,
31
32
} )
32
33
export class ExampleViewer implements OnInit {
33
34
@ViewChildren ( CodeSnippet ) readonly snippet ! : QueryList < CodeSnippet > ;
@@ -39,16 +40,16 @@ export class ExampleViewer implements OnInit {
39
40
exampleTabs : { [ tabName : string ] : string } = { } ;
40
41
41
42
/** Data for the currently selected example. */
42
- exampleData : LiveExample | null = null ;
43
+ exampleData : LiveExample | null = null ;
43
44
44
45
/** URL to fetch code snippet for snippet view. */
45
46
fileUrl : string | undefined ;
46
47
47
48
/** Component type for the current example. */
48
- _exampleComponentType : Type < any > | null = null ;
49
+ _exampleComponentType : Type < any > | null = null ;
49
50
50
51
/** Module factory that declares the example component. */
51
- _exampleModuleFactory : NgModuleFactory < any > | null = null ;
52
+ _exampleModuleFactory : NgModuleFactory < any > | null = null ;
52
53
53
54
/** View of the example component. */
54
55
@Input ( ) view : Views | undefined ;
@@ -67,8 +68,9 @@ export class ExampleViewer implements OnInit {
67
68
this . _example = exampleName ;
68
69
this . exampleData = EXAMPLE_COMPONENTS [ exampleName ] ;
69
70
this . _generateExampleTabs ( ) ;
70
- this . _loadExampleComponent ( ) . catch ( ( error ) =>
71
- console . error ( `Could not load example '${ exampleName } ': ${ error } ` ) ) ;
71
+ this . _loadExampleComponent ( ) . catch ( error =>
72
+ console . error ( `Could not load example '${ exampleName } ': ${ error } ` )
73
+ ) ;
72
74
} else {
73
75
console . error ( `Could not find example: ${ exampleName } ` ) ;
74
76
}
@@ -84,7 +86,8 @@ export class ExampleViewer implements OnInit {
84
86
constructor (
85
87
private readonly snackbar : MatSnackBar ,
86
88
private readonly clipboard : Clipboard ,
87
- private readonly elementRef : ElementRef < HTMLElement > ) { }
89
+ private readonly elementRef : ElementRef < HTMLElement >
90
+ ) { }
88
91
89
92
ngOnInit ( ) {
90
93
if ( this . file ) {
@@ -146,26 +149,29 @@ export class ExampleViewer implements OnInit {
146
149
fileName = `${ contentBeforeDot } -${ contentAfterDot } .html` ;
147
150
}
148
151
149
- return this . exampleData ?
150
- `/docs-content/examples-highlighted/${ this . exampleData . packagePath } /${ fileName } ` : '' ;
152
+ return this . exampleData
153
+ ? `/docs-content/examples-highlighted/${ this . exampleData . packagePath } /${ fileName } `
154
+ : '' ;
151
155
}
152
156
153
157
_getExampleTabNames ( ) {
154
- return this . exampleTabs ? Object . keys ( this . exampleTabs ) . sort ( ( a , b ) => {
155
- let indexA = preferredExampleFileOrder . indexOf ( a ) ;
156
- let indexB = preferredExampleFileOrder . indexOf ( b ) ;
157
- // Files which are not part of the preferred example file order should be
158
- // moved after all items with a preferred index.
159
- if ( indexA === - 1 ) {
160
- indexA = preferredExampleFileOrder . length ;
161
- }
162
-
163
- if ( indexB === - 1 ) {
164
- indexB = preferredExampleFileOrder . length ;
165
- }
166
-
167
- return ( indexA - indexB ) || 1 ;
168
- } ) : [ ] ;
158
+ return this . exampleTabs
159
+ ? Object . keys ( this . exampleTabs ) . sort ( ( a , b ) => {
160
+ let indexA = preferredExampleFileOrder . indexOf ( a ) ;
161
+ let indexB = preferredExampleFileOrder . indexOf ( b ) ;
162
+ // Files which are not part of the preferred example file order should be
163
+ // moved after all items with a preferred index.
164
+ if ( indexA === - 1 ) {
165
+ indexA = preferredExampleFileOrder . length ;
166
+ }
167
+
168
+ if ( indexB === - 1 ) {
169
+ indexB = preferredExampleFileOrder . length ;
170
+ }
171
+
172
+ return indexA - indexB || 1 ;
173
+ } )
174
+ : [ ] ;
169
175
}
170
176
171
177
_copyLink ( ) {
@@ -191,7 +197,8 @@ export class ExampleViewer implements OnInit {
191
197
// files. More details: https://webpack.js.org/api/module-methods/#magic-comments.
192
198
const moduleExports : any = await import (
193
199
/* webpackExclude: /\.map$/ */
194
- '@angular/components-examples/fesm2020/' + module . importSpecifier ) ;
200
+ '@angular/components-examples/fesm2020/' + module . importSpecifier
201
+ ) ;
195
202
this . _exampleComponentType = moduleExports [ componentName ] ;
196
203
// The components examples package is built with Ivy. This means that no factory files are
197
204
// generated. To retrieve the factory of the AOT compiled module, we simply pass the module
@@ -217,18 +224,25 @@ export class ExampleViewer implements OnInit {
217
224
const exampleBaseFileName = `${ this . example } -example` ;
218
225
const docsContentPath = `/docs-content/examples-highlighted/${ this . exampleData . packagePath } ` ;
219
226
227
+ const tsPath = normalizePath ( `${ exampleBaseFileName } .ts` ) ;
228
+ const cssPath = normalizePath ( `${ exampleBaseFileName } .css` ) ;
229
+ const htmlPath = normalizePath ( `${ exampleBaseFileName } .html` ) ;
220
230
221
- for ( const fileName of this . exampleData . files ) {
231
+ for ( let fileName of this . exampleData . files ) {
222
232
// Since the additional files refer to the original file name, we need to transform
223
233
// the file name to match the highlighted HTML file that displays the source.
224
234
const fileSourceName = fileName . replace ( fileExtensionRegex , '$1-$2.html' ) ;
225
235
const importPath = `${ docsContentPath } /${ fileSourceName } ` ;
226
236
227
- if ( fileName === `${ exampleBaseFileName } .ts` ) {
237
+ // Normalize the path to allow for more consistent displaying in the tabs,
238
+ // and to make comparisons below more reliable.
239
+ fileName = normalizePath ( fileName ) ;
240
+
241
+ if ( fileName === tsPath ) {
228
242
this . exampleTabs [ 'TS' ] = importPath ;
229
- } else if ( fileName === ` ${ exampleBaseFileName } .css` ) {
243
+ } else if ( fileName === cssPath ) {
230
244
this . exampleTabs [ 'CSS' ] = importPath ;
231
- } else if ( fileName === ` ${ exampleBaseFileName } .html` ) {
245
+ } else if ( fileName === htmlPath ) {
232
246
this . exampleTabs [ 'HTML' ] = importPath ;
233
247
} else {
234
248
this . exampleTabs [ fileName ] = importPath ;
0 commit comments