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

feat(plnkr): Add initial version of openPlunker #26

Merged
merged 8 commits into from
Dec 21, 2016

Conversation

tinayuangao
Copy link
Contributor


TAGS: string[] = ['angular', 'material', 'example'];

description: string = 'Example for Angular Material';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add all of these to some kind of ExampleDescriptor class?

import { PlunkerUtil } from './plnkr-util';

@Component({
selector: 'app-plnkr',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "plunker-example"? (also be consistent with plnkr vs plunker, my vote is for everything to be "plunker")

@@ -0,0 +1,104 @@
<div class="demo-button">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know these will eventually go in a separate npm package, but maybe for now put them in assets/examples?

@@ -0,0 +1,36 @@
<!DOCTYPE html>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can go in assets/plunker

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still advocate moving the template files to assets, they're not really part of the application source

selector: 'app-plnkr',
templateUrl: 'plnkr.html',
})
export class Plunker implements OnInit {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about calling this PlunkerButton with selector plunker-button?

can be found in the LICENSE file at http://angular.io/license`;

TEMPLATE_PATH = '/app/shared/plnkr/template/';
TEMPLATE_FILES = ['index.html', 'systemjs.config.js', 'main.ts'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can move these constants outside the class since they're effectively private. That way we don't have to refer to them with this

@@ -0,0 +1,109 @@
import { Http } from '@angular/http';

export class PlunkerUtil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about something like PlunkerWriter, PlunkerExporter, or PlunkerConnector?

cc @mmalerba

exampleFiles: string[] = ['button-demo.html', 'button-demo.scss', 'button-demo.ts'];
selectorName: string = 'button-demo';
indexFilename: string = 'button-demo';
componentName: string = 'ButtonDemo';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to have a separate PlunkerData class that you construct and pass around rather than storing all of these properties directly on the service?

@@ -0,0 +1,109 @@
import { Http } from '@angular/http';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a top-level comment here that gives an explanation of the Plunker API since it doesn't seem to be documented anywhere on their site?

return form;
}

_generateInput(name: string, value: string, isFile = false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd call this _createFormInput

if (filename.indexOf('.ts') > -1 || filename.indexOf('.scss') > -1) {
content = content + '/** ' + this.COPYRIGHT + ' */';
} else if (filename.indexOf('.html') > -1) {
content = content + '<!-- ' + this.COPYRIGHT + ' -->';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a template string for these:

`${content}\n\n/**  ${COPYRIGHT} */`
`${content}\n\n<!-- ${COPYRIGHT} -->`

}
}

if (filename.indexOf('.ts') > -1 || filename.indexOf('.scss') > -1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would break this out into its own method _appendCopyright

}

_processContent(filename: string, path: string, content: string): string {
if (path == this.TEMPLATE_PATH) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite follow what these replacements are for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comments

(response) => {
let content = response.text();
content = this._processContent(filename, path, content);
this._generateInput(filename, content, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd break these out into separate handler functions:

 this._http.get(path + filename).subscribe(
  response => this._addFileToForm(response.text()),
  error => console.log(error));

Where _addFileToForm massages the file content and adds the input. Then you could remove the isFile argument from _createFormInput and pass the files[${name}] as the name (since you know specifically you're processing a file).

@tinayuangao
Copy link
Contributor Author

Added PlunkerData and changed PlunkerUtil to PlunkerWriter. PTAL. Thanks for review!

* private: true
* }
*/
export class PlunkerWriter {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add the @Injectable decorator to this to turn it into a service and add it to the providers list of the app's NgModule. Then PlunkerButton can inject this (instead of http) and this service can inject Http directly.

TEMPLATE_PATH = '/app/shared/plnkr/template/';
TEMPLATE_FILES = ['index.html', 'systemjs.config.js', 'main.ts'];

TAGS: string[] = ['angular', 'material', 'example'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you missed moving these constants out of the class; they can go right above it (example)

content = this._processTemplateFile(filename, path, content);
}
this._createFormInput(`files[${filename}]`, this._appendCopyright(filename, content));
this._submitForm();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to move this _submitForm call to the end of openPlunker?

// For example, <material-docs-example></material-docs-exmaple> will be replaced as
// <button-demo></button-demo>
content = content.replace(new RegExp('material-docs-example', 'g'),
this.exampleData.selectorName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use a regex literal:

content = content.replace(/material-docs-example/g, this.exampleData.selectorName),

this._submitForm();
}

_processTemplateFile(filename: string, path: string, content: string): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

/**
 * The Plunker template assets contain placeholder names for the examples:
 * "<material-docs-example>" and "MaterialDocsExample".
 * This will replace those placeholders with the names from the example metadata, 
 * e.g. "<basic-button-example>" and "BasicButtonExample"
 */
_replaceExamplePlaceholderNames(fileName: string, fileContent: string) { ... }

I also noticed that the path argument seems to not be used in this function.

@tinayuangao
Copy link
Contributor Author

Comments addressed. Please take another look. Thanks!

@@ -0,0 +1 @@
export * from './plunker-button';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the directory name to plunker

* Plunker data
* with information about Component name, selector, files used in example, and path to examples
*/
export class PlunkerData {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine for this CL, but this stuff isn't actually related to plunker at all, it should be called ExampleData and should probably live in the material2 repo eventually.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the name to ExampleData. I think we can extract information (selector, componentName) from example file. I will work on that in another PR.

@@ -0,0 +1,36 @@
<!DOCTYPE html>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still advocate moving the template files to assets, they're not really part of the application source

Copy link
Contributor Author

@tinayuangao tinayuangao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review. Moved template files to assets/plunker.

* Plunker data
* with information about Component name, selector, files used in example, and path to examples
*/
export class PlunkerData {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the name to ExampleData. I think we can extract information (selector, componentName) from example file. I will work on that in another PR.

@tinayuangao tinayuangao force-pushed the plunker branch 2 times, most recently from 5884c5b to 0f81773 Compare December 19, 2016 22:15
@tinayuangao
Copy link
Contributor Author

Added test for plunker-writer

@jelbourn
Copy link
Member

LGTM

@jelbourn
Copy link
Member

Actually, I just noticed that NavBar is now declared in two modules; it should be removed from AppModule

@tinayuangao
Copy link
Contributor Author

Updated - fixed NavBar and integrated with example view. Moved example-data to a better place

@jelbourn
Copy link
Member

LGTM, we can update to use the real examples in a follow-up PR.

@jelbourn jelbourn merged commit 592d8ec into angular:master Dec 21, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants