@@ -10,7 +10,11 @@ import { ExecutionCancelledError } from '../utils/examples/errors.ts';
1010import { exampleControlsAtom } from '../utils/examples/exampleControlAtom.ts' ;
1111import { executeExample } from '../utils/examples/exampleRunner.ts' ;
1212import type { ExampleState } from '../utils/examples/exampleState.ts' ;
13- import type { Example } from '../utils/examples/types.ts' ;
13+ import type {
14+ Example ,
15+ ExampleCommonFile ,
16+ ExampleSrcFile ,
17+ } from '../utils/examples/types.ts' ;
1418import { isGPUSupported } from '../utils/isGPUSupported.ts' ;
1519import { HtmlCodeEditor , TsCodeEditor } from './CodeEditor.tsx' ;
1620import { ControlPanel } from './ControlPanel.tsx' ;
@@ -20,6 +24,7 @@ import { openInStackBlitz } from './stackblitz/openInStackBlitz.ts';
2024
2125type Props = {
2226 example : Example ;
27+ common : ExampleCommonFile [ ] ;
2328 isPlayground ?: boolean ;
2429} ;
2530
@@ -66,8 +71,8 @@ function useExample(
6671 } , [ setSnackbarText , setExampleControlParams ] ) ;
6772}
6873
69- export function ExampleView ( { example } : Props ) {
70- const { tsFiles, tsImport, htmlFile } = example ;
74+ export function ExampleView ( { example, common } : Props ) {
75+ const { tsFiles : srcFiles , tsImport, htmlFile } = example ;
7176
7277 const [ snackbarText , setSnackbarText ] = useAtom ( currentSnackbarAtom ) ;
7378 const [ currentFilePath , setCurrentFilePath ] = useState < string > ( 'index.ts' ) ;
@@ -76,6 +81,7 @@ export function ExampleView({ example }: Props) {
7681 const codeEditorMobileShowing = useAtomValue ( codeEditorShownMobileAtom ) ;
7782 const exampleHtmlRef = useRef < HTMLDivElement > ( null ) ;
7883
84+ const tsFiles = filterRelevantTsFiles ( srcFiles , common ) ;
7985 const filePaths = tsFiles . map ( ( file ) => file . path ) ;
8086 const editorTabsList = [
8187 'index.ts' ,
@@ -171,7 +177,7 @@ export function ExampleView({ example }: Props) {
171177 </ div >
172178
173179 < div className = 'absolute right-0 z-5 md:top-15 md:right-8' >
174- < Button onClick = { ( ) => openInStackBlitz ( example ) } >
180+ < Button onClick = { ( ) => openInStackBlitz ( example , common ) } >
175181 < span className = 'font-bold' > Edit on</ span >
176182 < img
177183 src = 'https://developer.stackblitz.com/img/logo/stackblitz-logo-black_blue.svg'
@@ -290,3 +296,27 @@ function useResizableCanvas(exampleHtmlRef: RefObject<HTMLDivElement | null>) {
290296 } ;
291297 } , [ exampleHtmlRef ] ) ;
292298}
299+
300+ /**
301+ * NOTE: this function only filters common files used in src files.
302+ * Common files used in other common files will not be included.
303+ */
304+ function filterRelevantTsFiles (
305+ srcFiles : ExampleSrcFile [ ] ,
306+ commonFiles : ExampleCommonFile [ ] ,
307+ ) {
308+ const tsFiles : ( ExampleSrcFile | ExampleCommonFile ) [ ] = [
309+ ...srcFiles ,
310+ ] ;
311+
312+ for ( const common of commonFiles ) {
313+ for ( const src of srcFiles ) {
314+ if ( src . content . includes ( `common/${ common . path } ` ) ) {
315+ tsFiles . push ( common ) ;
316+ break ;
317+ }
318+ }
319+ }
320+
321+ return tsFiles ;
322+ }
0 commit comments