Skip to content

Commit

Permalink
chore(demo): add Stackblitz integration for documentation examples (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nsbarsukov authored Mar 1, 2023
1 parent 7c79212 commit 0815b8e
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 37 deletions.
11 changes: 11 additions & 0 deletions projects/demo/src/app/app.providers.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import {isPlatformBrowser, LocationStrategy, PathLocationStrategy} from '@angular/common';
import {inject, PLATFORM_ID, Provider} from '@angular/core';
import {
TUI_DOC_CODE_EDITOR,
TUI_DOC_DEFAULT_TABS,
TUI_DOC_LOGO,
TUI_DOC_PAGES,
TUI_DOC_SOURCE_CODE,
TUI_DOC_TITLE,
tuiDocExampleOptionsProvider,
TuiDocSourceCodePathOptions,
} from '@taiga-ui/addon-doc';
import {HIGHLIGHT_OPTIONS} from 'ngx-highlightjs';

import {DEMO_PAGES} from '../pages/pages';
import {StackblitzService} from '../pages/stackblitz/stackblitz.service';
import {LOGO_CONTENT} from './modules/logo/logo.component';

export const APP_PROVIDERS: Provider[] = [
Expand Down Expand Up @@ -52,6 +55,14 @@ export const APP_PROVIDERS: Provider[] = [
).replace(/[A-Z]/g, m => `-${m.toLowerCase()}`)}`;
},
},
{
provide: TUI_DOC_CODE_EDITOR,
useClass: StackblitzService,
},
tuiDocExampleOptionsProvider({
codeEditorVisibilityHandler: files =>
Object.keys(files).length === 1 && Boolean(files['MaskitoOptions']),
}),
{
provide: HIGHLIGHT_OPTIONS,
useFactory: () => {
Expand Down
2 changes: 1 addition & 1 deletion projects/demo/src/app/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export const appRoutes: Routes = [
{
path: DemoPath.Stackblitz,
loadChildren: async () =>
import(`../pages/stackblitz-starter/stackblitz-starter.module`).then(
import(`../pages/stackblitz/stackblitz-starter.module`).then(
m => m.StackblitzStarterModule,
),
data: {
Expand Down

This file was deleted.

28 changes: 0 additions & 28 deletions projects/demo/src/pages/stackblitz-starter/stackblitz.service.ts

This file was deleted.

13 changes: 13 additions & 0 deletions projects/demo/src/pages/stackblitz/files/example.ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
```ts
import './styles.css';
import {Maskito} from '@maskito/core';
import mask from './mask';

const element: HTMLInputElement | HTMLTextAreaElement | null = document.querySelector('input, textarea');

if (element) {
const maskedElement = new Maskito(element, mask);

console.info('Call this function when the element is detached from DOM', maskedElement.destroy);
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ export class StackblitzStarterComponent implements OnInit {
}

async openStackblitz(): Promise<void> {
const [html, ts, css] = await Promise.all(
[
import('./files/index.html?raw'),
import('./files/index.ts?raw'),
import('./files/styles.css?raw'),
].map(tuiRawLoad),
const [ts, css] = await Promise.all(
[import('./files/starter.ts?raw'), import('./files/styles.css?raw')].map(
tuiRawLoad,
),
);

return this.stackblitz.openStarter(
Expand All @@ -39,7 +37,7 @@ export class StackblitzStarterComponent implements OnInit {
description:
'A starter with Maskito library\nDocumentation: https://tinkoff.github.io/maskito',
files: {
'index.html': html,
'index.html': `<input />`,
'index.ts': ts,
'styles.css': css,
},
Expand Down
64 changes: 64 additions & 0 deletions projects/demo/src/pages/stackblitz/stackblitz.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {Injectable} from '@angular/core';
import stackblitz, {OpenOptions, Project} from '@stackblitz/sdk';
import {
TuiCodeEditor,
tuiRawLoad,
tuiTryParseMarkdownCodeBlock,
} from '@taiga-ui/addon-doc';

@Injectable()
export class StackblitzService implements TuiCodeEditor {
private readonly baseProjectConfigs: Pick<Project, 'dependencies' | 'template'> = {
template: `typescript`,
dependencies: {
'@maskito/core': '*',
'@maskito/kit': '*',
},
};

readonly name = 'Stackblitz';

async edit(
component: string,
id: string,
files: Record<string, string>,
): Promise<void> {
const [tsMd, css] = await Promise.all(
[import('./files/example.ts.md?raw'), import('./files/styles.css?raw')].map(
tuiRawLoad,
),
);

return stackblitz.openProject(
{
...this.baseProjectConfigs,
title: `maskito/${component}/${id}`,
description: `Maskito example of the component ${component}`,
files: {
'index.html': component.includes('textarea')
? `<textarea></textarea>`
: `<input />`,
'styles.css': css,
'index.ts': tuiTryParseMarkdownCodeBlock(tsMd)[0],
'mask.ts': files['MaskitoOptions'],
},
},
{openFile: 'index.ts,mask.ts'},
);
}

openStarter(
{title, description, files}: Pick<Project, 'description' | 'files' | 'title'>,
openOptions?: OpenOptions,
): void {
return stackblitz.openProject(
{
...this.baseProjectConfigs,
title,
description,
files,
},
openOptions,
);
}
}

0 comments on commit 0815b8e

Please sign in to comment.