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

Commit d94e2a8

Browse files
devversionjelbourn
authored andcommitted
fixup! refactor: update stackblitz examples write to use webcontainers for v13 compatibility
Update version in package.json file
1 parent d08aed6 commit d94e2a8

File tree

5 files changed

+56
-44
lines changed

5 files changed

+56
-44
lines changed
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
<button mat-icon-button type="button" (click)="openStackBlitz()" [disabled]="isDisabled"
2-
[attr.aria-label]="isDisabled ? 'Building StackBlitz example...' : 'Edit this example in StackBlitz'"
3-
[matTooltip]="isDisabled ? 'Building ' + exampleData?.description + ' StackBlitz example...' :
4-
'Edit ' + exampleData?.description + ' example in StackBlitz'">
1+
<button mat-icon-button type="button" (click)="openStackBlitz()"
2+
aria-label="Edit this example in StackBlitz"
3+
[matTooltip]="'Edit ' + exampleData?.description + ' example in StackBlitz'">
54
<mat-icon>open_in_new</mat-icon>
65
</button>

src/app/shared/stack-blitz/stack-blitz-button.ts

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, HostListener, Input, NgModule} from '@angular/core';
1+
import {Component, Input, NgModule, NgZone} from '@angular/core';
22
import {ExampleData} from '@angular/components-examples';
33
import {MatButtonModule} from '@angular/material/button';
44
import {MatIconModule} from '@angular/material/icon';
@@ -10,15 +10,6 @@ import {StackBlitzWriter} from './stack-blitz-writer';
1010
templateUrl: './stack-blitz-button.html',
1111
})
1212
export class StackBlitzButton {
13-
/**
14-
* The button becomes disabled if the user hovers over the button before the StackBlitz
15-
* is ready for opening. After the StackBlitz is ready, the button becomes enabled again.
16-
*
17-
* The StackBlitz preparation usually happens extremely quickly, but we handle the case of the
18-
* StackBlitz not yet being ready for people with poor network connections or slow devices.
19-
*/
20-
isDisabled = false;
21-
2213
exampleData: ExampleData | undefined;
2314

2415
/**
@@ -31,30 +22,33 @@ export class StackBlitzButton {
3122
*/
3223
private _openStackBlitzFn: (() => void) | null = null;
3324

34-
@HostListener('mouseover')
35-
onMouseOver() {
36-
this.isDisabled = this._openStackBlitzFn === null;
37-
}
38-
3925
@Input()
40-
set example(example: string | undefined) {
41-
if (example) {
42-
const isTest = example.includes('harness');
43-
this.exampleData = new ExampleData(example);
44-
this.stackBlitzWriter.createStackBlitzForExample(example, this.exampleData, isTest)
45-
.then(openFn => {
46-
this._openStackBlitzFn = openFn;
47-
this.isDisabled = false;
48-
});
26+
set example(exampleId: string | undefined) {
27+
if (exampleId) {
28+
this.exampleData = new ExampleData(exampleId);
29+
this._prepareStackBlitzForExample(exampleId, this.exampleData);
4930
} else {
50-
this.isDisabled = true;
31+
this.exampleData = undefined;
32+
this._openStackBlitzFn = null;
5133
}
5234
}
5335

54-
constructor(private stackBlitzWriter: StackBlitzWriter) {}
36+
constructor(private stackBlitzWriter: StackBlitzWriter, private ngZone: NgZone) {}
37+
38+
openStackBlitz(): void {
39+
if (this._openStackBlitzFn === null) {
40+
alert('StackBlitz is not ready yet. Please try again in a few seconds.');
41+
} else {
42+
this._openStackBlitzFn?.();
43+
}
44+
}
5545

56-
async openStackBlitz(): Promise<void> {
57-
this._openStackBlitzFn?.();
46+
private _prepareStackBlitzForExample(exampleId: string, data: ExampleData): void {
47+
this.ngZone.runOutsideAngular(async () => {
48+
const isTest = exampleId.includes('harness');
49+
this._openStackBlitzFn = await this.stackBlitzWriter
50+
.createStackBlitzForExample(exampleId, data, isTest);
51+
});
5852
}
5953
}
6054

src/app/shared/stack-blitz/stack-blitz-writer.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,32 @@ export class StackBlitzWriter {
6666
constructor(private _http: HttpClient, private _ngZone: NgZone) {}
6767

6868
/** Opens a StackBlitz for the specified example. */
69-
async createStackBlitzForExample(exampleId: string, data: ExampleData,
70-
isTest: boolean): Promise<() => void> {
71-
// Run outside the zone since this form doesn't interact with Angular
69+
createStackBlitzForExample(exampleId: string, data: ExampleData,
70+
isTest: boolean): Promise<() => void> {
71+
// Run outside the zone since the creation doesn't interact with Angular
7272
// and the file requests can cause excessive change detections.
73-
return await this._ngZone.runOutsideAngular(async () => {
73+
return this._ngZone.runOutsideAngular(async () => {
7474
const files = await this._buildInMemoryFileDictionary(data, exampleId, isTest);
7575
const exampleMainFile = `src/app/${data.indexFilename}`;
7676

7777
return () => {
78-
this._openStackBlitz(`Angular Components - ${exampleId} example`, exampleMainFile, files);
78+
this._openStackBlitz({
79+
files,
80+
title: `Angular Components - ${data.description}`,
81+
description: `${data.description}\n\nAuto-generated from: https://material.angular.io`,
82+
openFile: exampleMainFile,
83+
});
7984
};
8085
});
8186
}
8287

83-
/** Opens a new WebContainer-based StackBlitz with the given files. */
84-
private _openStackBlitz(title: string, openFile: string, files: FileDictionary): void {
88+
/** Opens a new WebContainer-based StackBlitz for the given files. */
89+
private _openStackBlitz({title, description, openFile, files}:
90+
{title: string, description: string, openFile: string, files: FileDictionary}): void {
8591
stackblitz.openProject({
8692
title,
8793
files,
88-
description: title,
94+
description,
8995
template: PROJECT_TEMPLATE,
9096
tags: PROJECT_TAGS,
9197
}, {openFile});
@@ -147,15 +153,22 @@ export class StackBlitzWriter {
147153
*/
148154
private _replaceExamplePlaceholders(data: ExampleData, fileName: string,
149155
fileContent: string, isTest: boolean): string {
156+
// Replaces the version placeholder in the `index.html` and `package.json` file.
157+
// Technically we invalidate the `package-lock.json` file for the StackBlitz boilerplate
158+
// by dynamically changing the version in the `package.json`, but the Turbo package manager
159+
// seems to be able to partially re-use the lock file to speed up the module tree computation,
160+
// so providing a lock file is still reasonable while modifying the `package.json`.
161+
if (fileName === 'src/index.html' || fileName === 'package.json') {
162+
fileContent = fileContent.replace(/\${version}/g, MAT_VERSION.full);
163+
}
150164

151165
if (fileName === 'src/index.html') {
152166
// Replace the component selector in `index,html`.
153167
// For example, <material-docs-example></material-docs-example> will be replaced as
154168
// <button-demo></button-demo>
155169
fileContent = fileContent
156170
.replace(/material-docs-example/g, data.selectorName)
157-
.replace(/\${title}/g, data.description)
158-
.replace(/\${version}/g, MAT_VERSION.full);
171+
.replace(/\${title}/g, data.description);
159172
} else if (fileName === '.stackblitzrc') {
160173
fileContent = fileContent
161174
.replace(/\${startCommand}/, isTest ? 'turbo test' : 'turbo start');

src/assets/stack-blitz/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
"private": true,
1212
"dependencies": {
1313
"@angular/animations": "13.0.0-rc.0",
14-
"@angular/cdk": "13.0.0-rc.0",
14+
"@angular/cdk": "${version}",
1515
"@angular/common": "13.0.0-rc.0",
1616
"@angular/compiler": "13.0.0-rc.0",
1717
"@angular/core": "13.0.0-rc.0",
1818
"@angular/forms": "13.0.0-rc.0",
19-
"@angular/material": "13.0.0-rc.0",
19+
"@angular/material": "${version}",
2020
"@angular/platform-browser": "13.0.0-rc.0",
2121
"@angular/platform-browser-dynamic": "13.0.0-rc.0",
2222
"@angular/router": "13.0.0-rc.0",

src/assets/stack-blitz/src/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {VERSION as CDK_VERSION} from '@angular/cdk';
2+
import {VERSION as MAT_VERSION} from '@angular/material/core';
13
import {enableProdMode} from '@angular/core';
24
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
35

@@ -8,5 +10,9 @@ if (environment.production) {
810
enableProdMode();
911
}
1012

13+
/* eslint-disable no-console */
14+
console.info('Angular CDK version', CDK_VERSION.full);
15+
console.info('Angular Material version', MAT_VERSION.full);
16+
1117
platformBrowserDynamic().bootstrapModule(AppModule)
1218
.catch(err => console.error(err));

0 commit comments

Comments
 (0)