Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit d7ca254

Browse files
authored
refactor: prepare for docs-content changes (#837)
We recently refactored how our docs-content example module is composed. We did this because the docs app assumes path in the docs-content package that are not necessarily always correct. i.e. breaking the chips drag and drop example. Additionally the docs-content example module is now more self-contained so that assumptions about files inside of an example are not required anymore. i.e. examples can now _not_ have CSS or HTML files. Previously we always had to display an empty CSS/HTML file (which is confusing).
1 parent 1ddebad commit d7ca254

File tree

5 files changed

+76
-45
lines changed

5 files changed

+76
-45
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"build:content": "yarn upgrade @angular/components-examples",
1919
"build:sm": "ng build --prod --source-map",
2020
"prod-build": "ng build --prod",
21-
"postinstall": "webdriver-manager update --gecko false && ngcc --async false --properties es2015 browser module main --first-only --create-ivy-entry-points",
21+
"postinstall": "webdriver-manager update --gecko false",
2222
"publish-prod": "bash ./tools/deploy.sh stable prod",
2323
"publish-dev": "bash ./tools/deploy.sh",
2424
"publish-beta": "bash ./tools/deploy.sh stable beta"
@@ -30,7 +30,7 @@
3030
"@angular/cdk-experimental": "^10.0.2",
3131
"@angular/common": "^10.0.1",
3232
"@angular/compiler": "^10.0.1",
33-
"@angular/components-examples": "angular/material2-docs-content#10.0.x",
33+
"@angular/components-examples": "https://github.com/angular/material2-docs-content.git#33b7ab32041e2845fb82497ff304c86690fded90",
3434
"@angular/core": "^10.0.1",
3535
"@angular/forms": "^10.0.1",
3636
"@angular/google-maps": "^10.0.2",

projects/sub/src/assets/hello.css

Whitespace-only changes.

src/app/shared/example-viewer/example-viewer.spec.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('ExampleViewer', () => {
7676

7777
it('should expand to TS tab', async(async () => {
7878
component.example = exampleKey;
79-
component.file = 'file.ts';
79+
component.file = EXAMPLE_COMPONENTS[exampleKey].primaryFile;
8080
component.view = 'snippet';
8181
component.toggleCompactView();
8282

@@ -112,18 +112,18 @@ describe('ExampleViewer', () => {
112112

113113
it('should print an error message about incorrect file type', async(() => {
114114
spyOn(console, 'error');
115+
component.example = exampleKey;
115116
component.file = 'file.bad';
116117
component.selectCorrectTab();
117118

118119
expect(console.error).toHaveBeenCalledWith(
119-
'Unexpected file type: bad. Expected html, ts, or css.');
120+
`Could not find tab for file extension: "bad".`);
120121
}));
121122

122123
it('should set and return example properly', async(() => {
123124
component.example = exampleKey;
124125
const data = component.exampleData;
125-
// TODO(jelbourn): remove `as any` once LiveExample is updated to have optional members.
126-
expect(data).toEqual(EXAMPLE_COMPONENTS[exampleKey] as any);
126+
expect(data).toEqual(EXAMPLE_COMPONENTS[exampleKey]);
127127
}));
128128

129129
it('should print an error message about missing example', async(() => {
@@ -159,14 +159,32 @@ describe('ExampleViewer', () => {
159159
it('should be able to render additional files', () => {
160160
EXAMPLE_COMPONENTS['additional-files'] = {
161161
...EXAMPLE_COMPONENTS[exampleKey],
162-
additionalFiles: ['some-additional-file.html'],
162+
files: [
163+
'additional-files-example.ts',
164+
'additional-files-example.html',
165+
'additional-files-example.css',
166+
'some-additional-file.html'
167+
],
163168
};
164169

165170
component.example = 'additional-files';
166171

167172
expect(component._getExampleTabNames())
168173
.toEqual(['HTML', 'TS', 'CSS', 'some-additional-file.html']);
169174
});
175+
176+
it('should be possible for example to not have CSS or HTML files', () => {
177+
EXAMPLE_COMPONENTS['additional-files'] = {
178+
...EXAMPLE_COMPONENTS[exampleKey],
179+
files: [
180+
'additional-files-example.ts',
181+
],
182+
};
183+
184+
component.example = 'additional-files';
185+
186+
expect(component._getExampleTabNames()).toEqual(['TS']);
187+
});
170188
});
171189

172190
describe('copy button', () => {

src/app/shared/example-viewer/example-viewer.ts

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export type Views = 'snippet' | 'full' | 'demo';
1717
/** Regular expression that matches a file name and its extension */
1818
const fileExtensionRegex = /(.*)\.(\w+)/;
1919

20+
/** Preferred order for files of an example displayed in the viewer. */
21+
const preferredExampleFileOrder = ['HTML', 'TS', 'CSS'];
22+
2023
@Component({
2124
selector: 'example-viewer',
2225
templateUrl: './example-viewer.html',
@@ -28,7 +31,7 @@ export class ExampleViewer implements OnInit {
2831
/** The tab to jump to when expanding from snippet view. */
2932
selectedTab: number = 0;
3033

31-
/** Map of example files that should be displayed in the view-source tab. */
34+
/** Map of example files that should be displayed in the view-source tab in order. */
3235
exampleTabs: {[tabName: string]: string};
3336

3437
/** Data for the currently selected example. */
@@ -79,21 +82,24 @@ export class ExampleViewer implements OnInit {
7982
}
8083
}
8184

85+
/** Selects a given tab based on the example file of the compact view. */
8286
selectCorrectTab() {
83-
const fileType = this.file?.split('.').slice(-1)[0];
84-
switch (fileType) {
85-
case 'html':
86-
this.selectedTab = 0;
87-
break;
88-
case 'ts':
89-
this.selectedTab = 1;
90-
break;
91-
case 'css':
92-
this.selectedTab = 2;
93-
break;
94-
default:
95-
console.error(`Unexpected file type: ${fileType}. Expected html, ts, or css.`);
87+
if (!this.file || !this.exampleTabs) {
88+
return;
9689
}
90+
91+
const extension = this.file.substring(this.file.lastIndexOf('.') + 1);
92+
const exampleTabNames = this._getExampleTabNames();
93+
94+
for (let i = 0; i < exampleTabNames.length; i++) {
95+
const tabName = exampleTabNames[i];
96+
if (tabName.toLowerCase() === extension || tabName.endsWith(`.${extension}`)) {
97+
this.selectedTab = i;
98+
return;
99+
}
100+
}
101+
102+
console.error(`Could not find tab for file extension: "${extension}".`);
97103
}
98104

99105
toggleCompactView() {
@@ -129,12 +135,19 @@ export class ExampleViewer implements OnInit {
129135
fileName = `${contentBeforeDot}-${contentAfterDot}.html`;
130136
}
131137

132-
const examplePath = `${this.exampleData.module.importSpecifier}/${this.example}`;
133-
return `/docs-content/examples-highlighted/${examplePath}/${fileName}`;
138+
return `/docs-content/examples-highlighted/${this.exampleData.packagePath}/${fileName}`;
134139
}
135140

136141
_getExampleTabNames() {
137-
return Object.keys(this.exampleTabs);
142+
return Object.keys(this.exampleTabs).sort((a, b) => {
143+
let indexA = preferredExampleFileOrder.indexOf(a);
144+
let indexB = preferredExampleFileOrder.indexOf(b);
145+
// Files which are not part of the preferred example file order should be
146+
// moved after all items with a preferred index.
147+
if (indexA === -1) indexA = preferredExampleFileOrder.length;
148+
if (indexB === -1) indexB = preferredExampleFileOrder.length;
149+
return (indexA - indexB) || 1;
150+
});
138151
}
139152

140153
/** Loads the component and module factory for the currently selected example. */
@@ -158,22 +171,28 @@ export class ExampleViewer implements OnInit {
158171
}
159172

160173
private _generateExampleTabs() {
161-
const examplePath = `${this.exampleData.module.importSpecifier}/${this.example}`;
162-
const docsContentPath = `/docs-content/examples-highlighted/${examplePath}`;
163-
164-
this.exampleTabs = {
165-
HTML: `${docsContentPath}/${this.example}-example-html.html`,
166-
TS: `${docsContentPath}/${this.example}-example-ts.html`,
167-
CSS: `${docsContentPath}/${this.example}-example-css.html`,
168-
};
174+
const docsContentPath = `/docs-content/examples-highlighted/${this.exampleData.packagePath}`;
175+
// Name of the default example files. If files with such name exist within the example,
176+
// we provide a shorthand for them within the example tabs (for less verbose tabs).
177+
const exampleBaseFileName = `${this.example}-example`;
169178

170-
const additionalFiles = this.exampleData.additionalFiles || [];
179+
this.exampleTabs = {};
171180

172-
additionalFiles.forEach((fileName: string) => {
181+
for (const fileName of this.exampleData.files) {
173182
// Since the additional files refer to the original file name, we need to transform
174183
// the file name to match the highlighted HTML file that displays the source.
175184
const fileSourceName = fileName.replace(fileExtensionRegex, '$1-$2.html');
176-
this.exampleTabs[fileName] = `${docsContentPath}/${fileSourceName}`;
177-
});
185+
const importPath = `${docsContentPath}/${fileSourceName}`;
186+
187+
if (fileName === `${exampleBaseFileName}.ts`) {
188+
this.exampleTabs['TS'] = importPath;
189+
} else if (fileName === `${exampleBaseFileName}.css`) {
190+
this.exampleTabs['CSS'] = importPath;
191+
} else if (fileName === `${exampleBaseFileName}.html`) {
192+
this.exampleTabs['HTML'] = importPath;
193+
} else {
194+
this.exampleTabs[fileName] = importPath;
195+
}
196+
}
178197
}
179198
}

yarn.lock

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,9 @@
202202
dependencies:
203203
tslib "^2.0.0"
204204

205-
"@angular/components-examples@angular/material2-docs-content#10.0.x":
206-
version "10.0.2-sha-ba5c2feb0"
207-
resolved "https://codeload.github.com/angular/material2-docs-content/tar.gz/00da8d7f03897721fd841b7829f37a9afc06ed7f"
208-
dependencies:
209-
tslib "^2.0.0"
210-
211-
"@angular/components-examples@https://github.com/angular/material2-docs-content.git#fd76eb77418b2193f243be0570b727e63728e3e3":
212-
version "10.0.1-sha-55d50750e"
213-
resolved "https://github.com/angular/material2-docs-content.git#fd76eb77418b2193f243be0570b727e63728e3e3"
205+
"@angular/components-examples@https://github.com/angular/material2-docs-content.git#33b7ab32041e2845fb82497ff304c86690fded90":
206+
version "10.1.0-next.0-sha-57f20bfb9"
207+
resolved "https://github.com/angular/material2-docs-content.git#33b7ab32041e2845fb82497ff304c86690fded90"
214208
dependencies:
215209
tslib "^2.0.0"
216210

0 commit comments

Comments
 (0)