Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve the stackblitz export impl
- Loading branch information
1 parent
07899f4
commit ad2a353
Showing
4 changed files
with
85 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { XlayersNgxEditorModel } from '../code-editor/editor-container/codegen/codegen.service'; | ||
import { StackBlitzProjectPayload } from './stackblitz.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class ExportStackblitzAngularService { | ||
constructor() {} | ||
prepare(content: XlayersNgxEditorModel[]): StackBlitzProjectPayload { | ||
const files = {}; | ||
for (let i = 0; i < content.length; i++) { | ||
for (const prop in content[i]) { | ||
if (prop === 'uri') { | ||
files[`src/app/xlayers/` + content[i].uri] = content[i].value; | ||
} | ||
} | ||
} | ||
|
||
files['src/app/app.component.ts'] = ` | ||
import { Component } from '@angular/core'; | ||
@Component({ | ||
selector: 'my-app', | ||
template: '<app-xlayers></app-xlayers>', | ||
}) | ||
export class AppComponent {} | ||
`; | ||
files['src/app/app.module.ts'] = ` | ||
import { NgModule } from '@angular/core'; | ||
import { BrowserModule } from '@angular/platform-browser'; | ||
import { AppComponent } from './app.component'; | ||
import { XlayersModule } from './xlayers/xlayers.module'; | ||
@NgModule({ | ||
imports: [ BrowserModule, XlayersModule ], | ||
declarations: [ AppComponent ], | ||
bootstrap: [ AppComponent ] | ||
}) | ||
export class AppModule { } | ||
`; | ||
files['src/main.ts'] = ` | ||
import 'core-js/es6/reflect'; | ||
import 'core-js/es7/reflect'; | ||
import 'zone.js/dist/zone'; | ||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; | ||
import { AppModule } from './app/app.module'; | ||
platformBrowserDynamic().bootstrapModule(AppModule) | ||
`; | ||
files['src/index.html'] = '<my-app>loading</my-app>'; | ||
|
||
return { | ||
files, | ||
title: 'xlayers', | ||
description: 'xLayers generated project', | ||
template: 'angular-cli', | ||
tags: ['angular', 'xlayers'], | ||
dependencies: {} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,34 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { XlayersNgxEditorModel } from '../code-editor/editor-container/codegen/codegen.service'; | ||
import sdk from '@stackblitz/sdk'; | ||
import { XlayersNgxEditorModel } from '../code-editor/editor-container/codegen/codegen.service'; | ||
import { ExportStackblitzAngularService } from './stackblitz.angular.service'; | ||
|
||
export interface StackBlitzProjectPayload { | ||
files: { [path: string]: string }; | ||
title: string; | ||
description: string; | ||
template: 'angular-cli' | 'create-react-app' | 'typescript' | 'javascript'; | ||
tags?: string[]; | ||
dependencies?: { [name: string]: string }; | ||
settings?: { | ||
compile?: { | ||
trigger?: 'auto' | 'keystroke' | 'save'; | ||
action?: 'hmr' | 'refresh'; | ||
clearConsole?: boolean; | ||
}; | ||
}; | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class ExportStackblitzService { | ||
|
||
constructor() { } | ||
constructor(private angularExport: ExportStackblitzAngularService) {} | ||
|
||
async export(content: Array<XlayersNgxEditorModel>) { | ||
|
||
const files = {}; | ||
for (let i = 0; i < content.length; i++) { | ||
for (let prop in content[i]) { | ||
if (prop === 'uri') { | ||
files[`src/app/xlayers/`+content[i].uri] = content[i].value; | ||
} | ||
} | ||
} | ||
|
||
files['src/app/app.component.ts'] = ` | ||
import { Component } from '@angular/core'; | ||
@Component({ | ||
selector: 'my-app', | ||
template: '<app-xlayers></app-xlayers>', | ||
}) | ||
export class AppComponent {} | ||
`; | ||
files['src/app/app.module.ts'] = ` | ||
import { NgModule } from '@angular/core'; | ||
import { BrowserModule } from '@angular/platform-browser'; | ||
import { AppComponent } from './app.component'; | ||
import { XlayersModule } from './xlayers/xlayers.module'; | ||
@NgModule({ | ||
imports: [ BrowserModule, XlayersModule ], | ||
declarations: [ AppComponent ], | ||
bootstrap: [ AppComponent ] | ||
}) | ||
export class AppModule { } | ||
`; | ||
files['src/main.ts'] = ` | ||
import 'core-js/es6/reflect'; | ||
import 'core-js/es7/reflect'; | ||
import 'zone.js/dist/zone'; | ||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; | ||
import { AppModule } from './app/app.module'; | ||
platformBrowserDynamic().bootstrapModule(AppModule) | ||
`; | ||
files['src/index.html'] = '<my-app>loading</my-app>'; | ||
|
||
const project = { | ||
files, | ||
title: 'xlayers', | ||
description: 'xLayers generated project', | ||
template: 'angular-cli', | ||
tags: ['angular', 'xlayers'], | ||
dependencies: {} | ||
}; | ||
|
||
|
||
const project: StackBlitzProjectPayload = this.angularExport.prepare( | ||
content | ||
); | ||
sdk.openProject(project); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters