Skip to content

Commit

Permalink
chore(demo): stackblitz upgrade (example with many files) (#673)
Browse files Browse the repository at this point in the history
* chore(demo): stackblitz upgrade (example with many files)

* chore(demo): new classes to parse typescipt files raw content

* chore(demo): `stackblitz` fix dialog component example

* chore(demo): fix stackblitz examples with angular localize

* feat(addon-doc): `TuiDocExampleComponent` change `tui-tabs` to `tui-tabs-with-more`

* chore(addon-doc): `Example` component small style refactor

* chore(demo): fix after resolving conflicts

* chore(demo): refactor after review

* chore(demo): `isMainComponentFile` => `isPrimaryComponentFile`

* chore(demo): `TsFileModuleParser` code style change
  • Loading branch information
nsbarsukov authored Oct 6, 2021
1 parent f467919 commit fdb1c69
Show file tree
Hide file tree
Showing 25 changed files with 362 additions and 149 deletions.
6 changes: 5 additions & 1 deletion projects/addon-doc/src/components/example/example.style.less
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
}
}

.t-tabs {
.t-tabs-wrapper {
padding: 0 2rem;
box-shadow: inset 0 -1px var(--tui-base-03);
display: flex;
Expand All @@ -57,6 +57,10 @@
}
}

.t-tabs {
flex-grow: 1;
}

.t-demo {
.customize-scroll();
padding: 2rem;
Expand Down
13 changes: 9 additions & 4 deletions projects/addon-doc/src/components/example/example.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ <h3 *ngIf="heading" class="t-title">
[content]="description"
></h4>
<div class="t-example">
<div class="t-tabs">
<tui-tabs [(activeItemIndex)]="activeItemIndex">
<button *ngFor="let tab of tabs" tuiTab>{{tab}}</button>
</tui-tabs>
<div class="t-tabs-wrapper">
<tui-tabs-with-more
class="t-tabs"
[(activeItemIndex)]="activeItemIndex"
>
<ng-container *ngFor="let tab of tabs">
<button *tuiTab tuiTab>{{tab}}</button>
</ng-container>
</tui-tabs-with-more>

<button
*ngIf="codeEditor"
Expand Down
64 changes: 1 addition & 63 deletions projects/demo/src/modules/app/app.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ import {
} from '@taiga-ui/addon-doc';
import {TUI_IS_CYPRESS} from '@taiga-ui/cdk';
import {TUI_ANIMATIONS_DURATION, TUI_SANITIZER} from '@taiga-ui/core';
import {iconsPathFactory, TUI_ICONS_PATH} from '@taiga-ui/core';
import {NgDompurifySanitizer} from '@tinkoff/ng-dompurify';
import {HIGHLIGHT_OPTIONS} from 'ngx-highlightjs';
import {TUI_DOC_EXAMPLE_CONTENT_PROCESSOR} from '../../../../addon-doc/src/tokens/example-content-processor';
import {PROMPT_PROVIDER} from '../customization/dialogs/examples/prompt/prompt.component';
import {FrontEndExample} from '../interfaces/front-end-example';
import {SEE_ALSO_GROUPS} from './app.const';
import {LOGO_CONTENT} from './logo/logo.component';
import {pages} from './pages';
import {StackblitzService} from './stackblitz/stackblitz.service';
import {exampleContentProcessor} from './utils';

export const DEFAULT_TABS = [
$localize`Description and examples`,
Expand All @@ -42,63 +41,6 @@ export const HIGHLIGHT_OPTIONS_VALUE = {
},
};

export function exampleContentProcessor(content: FrontEndExample): FrontEndExample {
return processLess(processTs(content));
}

function processTs(content: FrontEndExample): FrontEndExample {
if (!content.TypeScript) {
return content;
}

const withChangeDetectionImport = addIntoExistingImport(
content.TypeScript,
'ChangeDetectionStrategy',
'@angular/core',
);

return {
...content,
TypeScript: withChangeDetectionImport
.replace(/import {encapsulation} from '..\/.*';\n/gm, '')
.replace(/import {changeDetection} from '..\/.*';\n/gm, '')
.replace(/\n +encapsulation,/gm, '')
.replace(
/changeDetection,/gm,
'changeDetection: ChangeDetectionStrategy.OnPush,',
),
};
}

function processLess(content: FrontEndExample): FrontEndExample {
return content.LESS
? {
...content,
LESS: content.LESS.replace(
"@import 'taiga-ui-local';",
"@import '~@taiga-ui/core/styles/taiga-ui-local';",
),
}
: content;
}

export function addIntoExistingImport(
data: string = '',
entity: string = '',
packageName: string = '',
): string {
const packageImportsRegex = new RegExp(
`(?:import\\s?\\{\\r?\\n?)(?:(?:.*),\\r?\\n?)*?(?:.*?)\\r?\\n?} from (?:'|")${packageName}(?:'|");`,
'gm',
);

return data.replace(packageImportsRegex, parsed => {
return parsed.replace('{', `{${entity}, `);
});
}

export const ICONS_PATH = iconsPathFactory('assets/taiga-ui/icons/');

export const APP_PROVIDERS = [
Title,
PROMPT_PROVIDER,
Expand All @@ -110,10 +52,6 @@ export const APP_PROVIDERS = [
provide: TUI_SANITIZER,
useClass: NgDompurifySanitizer,
},
{
provide: TUI_ICONS_PATH,
useValue: ICONS_PATH,
},
{
provide: TUI_DOC_SOURCE_CODE,
useValue: (context: TuiDocSourceCodePathOptions) => {
Expand Down
3 changes: 3 additions & 0 deletions projects/demo/src/modules/app/classes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './ts-file.parser';
export * from './ts-file-module.parser';
export * from './ts-file-component.parser';
22 changes: 22 additions & 0 deletions projects/demo/src/modules/app/classes/ts-file-component.parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {TsFileParser} from './ts-file.parser';

const SELECTOR_REGEXP = /(?<=selector:\s['"])(.*)(?=['"])/gi;
const TEMPLATE_URL_REGEXP = /(?<=templateUrl:\s['"])(.*)(?=['"])/gi;
const STYLE_URLS_REGEXP = /(?<=styleUrls:\s)(\[.*\])/gi;

export class TsFileComponentParser extends TsFileParser {
set selector(newSelector: string) {
this.rawFileContent = this.rawFileContent.replace(SELECTOR_REGEXP, newSelector);
}

set templateUrl(newUrl: string) {
this.rawFileContent = this.rawFileContent.replace(TEMPLATE_URL_REGEXP, newUrl);
}

set styleUrls(newUrls: string[] | readonly string[]) {
this.rawFileContent = this.rawFileContent.replace(
STYLE_URLS_REGEXP,
JSON.stringify(newUrls),
);
}
}
23 changes: 23 additions & 0 deletions projects/demo/src/modules/app/classes/ts-file-module.parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {TsFileParser} from './ts-file.parser';

const DECLARATIONS_REG_EXP = /(?<=declarations:\s\[)(.*)(?=\])/gi;

export class TsFileModuleParser extends TsFileParser {
addDeclaration(entity: string): void {
this.rawFileContent = this.rawFileContent.replace(
'declarations: [',
`declarations: [${entity}, `,
);
}

addModuleImport(entity: string): void {
this.rawFileContent = this.rawFileContent.replace(
'imports: [',
`imports: [\n${entity}, `,
);
}

hasDeclarationEntity(entity: string): boolean {
return (this.rawFileContent.match(DECLARATIONS_REG_EXP) || '').includes(entity);
}
}
55 changes: 55 additions & 0 deletions projects/demo/src/modules/app/classes/ts-file.parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const CLASS_NAME_REGEXP = /(?<=export class\s)(\w*)/gi;

export class TsFileParser {
get className(): string {
const [className] = this.rawFileContent.match(CLASS_NAME_REGEXP) || [''];

return className;
}

set className(newClassName: string) {
this.rawFileContent = this.rawFileContent.replace(
CLASS_NAME_REGEXP,
newClassName,
);
}

get hasNgModule(): boolean {
return this.rawFileContent.includes('@NgModule');
}

get hasNgComponent(): boolean {
return this.rawFileContent.includes('@Component');
}

constructor(protected rawFileContent: string) {
const classesInside = rawFileContent.match(CLASS_NAME_REGEXP) || [];

if (classesInside.length > 1) {
throw new Error('TsFileParser: 1 component/module per ts-file');
}
}

addImport(entity: string, packageOrPath: string): void {
const fromName = packageOrPath.replace('.ts', '');

this.rawFileContent = this.rawFileContent.includes(fromName)
? this.addIntoExistingImport(entity, fromName)
: `import {${entity}} from '${fromName}';\n${this.rawFileContent};`;
}

toString(): string {
return this.rawFileContent;
}

private addIntoExistingImport(entity: string, packageName: string): string {
const packageImportsRegex = new RegExp(
`(?:import\\s?\\{\\r?\\n?)(?:(?:.*),\\r?\\n?)*?(?:.*?)\\r?\\n?} from (?:'|")${packageName}(?:'|");`,
'gm',
);

return this.rawFileContent.replace(packageImportsRegex, parsed => {
return parsed.replace('{', `{${entity}, `);
});
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import '@angular/localize/init';
import 'zone.js/dist/zone';
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export const STACKBLITZ_DEPS: Record<string, string> = {
'@angular/cdk': '*',
'@angular/core': '*',
'@angular/common': '*',
'@angular/compiler': '*',
'@angular/forms': '*',
'@angular/localize': '*',
'@angular/platform-browser': '*',
'@angular/platform-browser-dynamic': '*',
'@angular/animations': '*',
'@angular/router': '*',
'@taiga-ui/cdk': '*',
'@taiga-ui/i18n': '*',
'@taiga-ui/core': '*',
'@taiga-ui/kit': '*',
'@taiga-ui/icons': '*',
'@taiga-ui/addon-charts': '*',
'@taiga-ui/addon-commerce': '*',
'@taiga-ui/addon-mobile': '*',
'@taiga-ui/addon-table': '*',
'@taiga-ui/addon-tablebars': '*',
'@taiga-ui/addon-editor': '*',
'@tinkoff/ng-dompurify': '*',
'@tinkoff/ng-polymorpheus': '3.1.8',
'@ng-web-apis/common': '*',
'@tinkoff/ng-event-plugins': '*',
'@ng-web-apis/intersection-observer': '*',
'@ng-web-apis/mutation-observer': '*',
'angular2-text-mask': '*',
dompurify: '*',
'@types/dompurify': '*',
'prosemirror-state': '*',
};
Loading

0 comments on commit fdb1c69

Please sign in to comment.