|
| 1 | +import sdk from '@stackblitz/sdk'; |
| 2 | + |
| 3 | +// The default title to use for Stackblitz examples (when not overwritten) |
| 4 | +const DEFAULT_EDITOR_TITLE = 'Ionic Docs Example'; |
| 5 | +// The default description to use for Stackblitz examples (when not overwritten) |
| 6 | +const DEFAULT_EDITOR_DESCRIPTION = ''; |
| 7 | +// Default package version to use for all @ionic/* packages. |
| 8 | +const DEFAULT_IONIC_VERSION = '^6.0.0'; |
| 9 | + |
| 10 | +export interface EditorOptions { |
| 11 | + /** |
| 12 | + * The title of the Stackblitz example. |
| 13 | + */ |
| 14 | + title?: string; |
| 15 | + /** |
| 16 | + * The description of the Stackblitz example. |
| 17 | + */ |
| 18 | + description?: string; |
| 19 | +} |
| 20 | + |
| 21 | +const loadSourceFiles = async (files: string[]) => { |
| 22 | + const sourceFiles = await Promise.all(files.map(f => fetch(`/docs/code/stackblitz/${f}`))); |
| 23 | + return (await Promise.all(sourceFiles.map(res => res.text()))); |
| 24 | +} |
| 25 | + |
| 26 | +const openHtmlEditor = async (code: string, options?: EditorOptions) => { |
| 27 | + const [index_ts, index_html] = await loadSourceFiles([ |
| 28 | + 'html/index.ts', |
| 29 | + 'html/index.html', |
| 30 | + ]); |
| 31 | + |
| 32 | + sdk.openProject({ |
| 33 | + template: 'typescript', |
| 34 | + title: options?.title ?? DEFAULT_EDITOR_TITLE, |
| 35 | + description: options?.description ?? DEFAULT_EDITOR_DESCRIPTION, |
| 36 | + files: { |
| 37 | + // Injects our code sample into the body of the HTML document |
| 38 | + 'index.html': index_html.replace(/<body><\/body>/g, `<body>\n` + code + '</body>'), |
| 39 | + 'index.ts': index_ts, |
| 40 | + }, |
| 41 | + dependencies: { |
| 42 | + '@ionic/core': DEFAULT_IONIC_VERSION, |
| 43 | + }, |
| 44 | + }) |
| 45 | +} |
| 46 | + |
| 47 | +const openAngularEditor = async (code: string, options?: EditorOptions) => { |
| 48 | + const [main_ts, app_module_ts, app_component_ts, styles_css, angular_json] = await loadSourceFiles([ |
| 49 | + 'angular/main.ts', |
| 50 | + 'angular/app.module.ts', |
| 51 | + 'angular/app.component.ts', |
| 52 | + 'angular/styles.css', |
| 53 | + 'angular/angular.json' |
| 54 | + ]) |
| 55 | + |
| 56 | + sdk.openProject({ |
| 57 | + template: 'angular-cli', |
| 58 | + title: options?.title ?? DEFAULT_EDITOR_TITLE, |
| 59 | + description: options?.description ?? DEFAULT_EDITOR_DESCRIPTION, |
| 60 | + files: { |
| 61 | + 'src/main.ts': main_ts, |
| 62 | + 'src/polyfills.ts': `import 'zone.js/dist/zone';`, |
| 63 | + 'src/app/app.module.ts': app_module_ts, |
| 64 | + 'src/app/app.component.ts': app_component_ts, |
| 65 | + 'src/app/app.component.html': code, |
| 66 | + 'src/index.html': '<app-root></app-root>', |
| 67 | + 'src/styles.css': styles_css, |
| 68 | + 'angular.json': angular_json, |
| 69 | + }, |
| 70 | + dependencies: { |
| 71 | + '@ionic/angular': DEFAULT_IONIC_VERSION, |
| 72 | + }, |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +const openReactEditor = async (code: string, options?: EditorOptions) => { |
| 77 | + // Matches the name after `export default` to use as the component tag. |
| 78 | + let componentTagName; |
| 79 | + try { |
| 80 | + componentTagName = new RegExp(/function([\S\s]*?)\(/g).exec(code)[1].trim(); |
| 81 | + } catch (e) { |
| 82 | + console.error('Error parsing the component tag name from the React code snippet. Please make sure that the code snippet for React ends with export default ComponentName;'); |
| 83 | + } |
| 84 | + |
| 85 | + if (!componentTagName) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + const [index_js, app_tsx] = await loadSourceFiles([ |
| 90 | + 'react/index.js', |
| 91 | + 'react/app.tsx' |
| 92 | + ]); |
| 93 | + |
| 94 | + const app_tsx_renamed = app_tsx |
| 95 | + // Inserts the component name from the sample into the <IonApp> tag. |
| 96 | + .replace(/<IonApp><\/IonApp>/g, `<IonApp><${componentTagName} /></IonApp>`) |
| 97 | + // Imports the component from our `main` example file. |
| 98 | + .replace(/setupIonicReact\(\);/g, `import ${componentTagName} from "./main";\n\n` + 'setupIonicReact();'); |
| 99 | + |
| 100 | + sdk.openProject({ |
| 101 | + template: 'create-react-app', |
| 102 | + title: options?.title ?? DEFAULT_EDITOR_TITLE, |
| 103 | + description: options?.description ?? DEFAULT_EDITOR_DESCRIPTION, |
| 104 | + files: { |
| 105 | + 'index.html': `<div id="root"></div>`, |
| 106 | + 'index.js': index_js, |
| 107 | + 'App.js': app_tsx_renamed, |
| 108 | + 'main.js': code, |
| 109 | + }, |
| 110 | + dependencies: { |
| 111 | + react: 'latest', |
| 112 | + 'react-dom': 'latest', |
| 113 | + '@ionic/react': DEFAULT_IONIC_VERSION, |
| 114 | + // Stackblitz requires this dependency to run |
| 115 | + '@stencil/core': '^2.13.0', |
| 116 | + }, |
| 117 | + }) |
| 118 | +} |
| 119 | + |
| 120 | +const openVueEditor = async (code: string, options?: EditorOptions) => { |
| 121 | + const [package_json, index_html, vite_config_js, main_js, app_vue] = await loadSourceFiles([ |
| 122 | + 'vue/package.json', |
| 123 | + 'vue/index.html', |
| 124 | + 'vue/vite.config.js', |
| 125 | + 'vue/main.js', |
| 126 | + 'vue/App.vue' |
| 127 | + ]); |
| 128 | + /** |
| 129 | + * We have to use Stackblitz web containers here (node template), due |
| 130 | + * to multiple issues with Vite, Vue/Vue Router and Vue 3's script setup. |
| 131 | + * |
| 132 | + * https://github.com/stackblitz/core/issues/1308 |
| 133 | + */ |
| 134 | + sdk.openProject({ |
| 135 | + template: 'node', |
| 136 | + title: options?.title ?? DEFAULT_EDITOR_TITLE, |
| 137 | + description: options?.description ?? DEFAULT_EDITOR_DESCRIPTION, |
| 138 | + files: { |
| 139 | + 'src/App.vue': app_vue, |
| 140 | + 'src/components/Example.vue': code, |
| 141 | + 'src/main.js': main_js, |
| 142 | + 'index.html': index_html, |
| 143 | + 'vite.config.js': vite_config_js, |
| 144 | + 'package.json': package_json, |
| 145 | + '.stackblitzrc': `{ |
| 146 | + "startCommand": "yarn run dev" |
| 147 | + }` |
| 148 | + } |
| 149 | + }); |
| 150 | +} |
| 151 | + |
| 152 | +export { openAngularEditor, openHtmlEditor, openReactEditor, openVueEditor }; |
0 commit comments