11import { useState , useEffect , useRef } from "preact/hooks" ;
2- import { useLiveRegion } from ' ../hooks/useLiveRegion' ;
2+ import { useLiveRegion } from " ../hooks/useLiveRegion" ;
33import CodeMirror , { EditorView } from "@uiw/react-codemirror" ;
44import { javascript } from "@codemirror/lang-javascript" ;
55import { cdnLibraryUrl , cdnSoundUrl } from "@/src/globals/globals" ;
@@ -38,20 +38,34 @@ export const CodeEmbed = (props) => {
3838 ) ;
3939
4040 let { previewWidth, previewHeight } = props ;
41- const canvasMatch = / c r e a t e C a n v a s \( \s * ( \d + ) , \s * ( \d + ) \s * (?: , \s * (?: P 2 D | W E B G L ) \s * ) ? \) / m. exec ( initialCode ) ;
41+ const canvasMatch =
42+ / c r e a t e C a n v a s \( \s * ( \d + ) , \s * ( \d + ) \s * (?: , \s * (?: P 2 D | W E B G L ) \s * ) ? \) / m. exec (
43+ initialCode ,
44+ ) ;
4245 if ( canvasMatch ) {
4346 previewWidth = previewWidth || parseFloat ( canvasMatch [ 1 ] ) ;
4447 previewHeight = previewHeight || parseFloat ( canvasMatch [ 2 ] ) ;
4548 }
4649
47- const largeSketch = previewWidth && previewWidth > 770 - 60 ;
48-
4950 // Quick hack to make room for DOM that gets added below the canvas by default
50- const domMatch = / c r e a t e ( B u t t o n | S e l e c t | P | D i v | I n p u t | C o l o r P i c k e r ) / . exec ( initialCode ) ;
51+ const domMatch = / c r e a t e ( B u t t o n | S e l e c t | P | D i v | I n p u t | C o l o r P i c k e r ) / . exec (
52+ initialCode ,
53+ ) ;
5154 if ( domMatch && previewHeight ) {
5255 previewHeight += 100 ;
5356 }
5457
58+ // Fallback preview size when no usable canvas dimensions are detected.
59+ // Ensures DOM-based examples (e.g., createCapture with noCanvas) are visible.
60+ const DEFAULT_PREVIEW_WIDTH = 400 ;
61+ const DEFAULT_PREVIEW_HEIGHT = 300 ;
62+ if ( previewWidth === undefined && previewHeight === undefined ) {
63+ previewWidth = DEFAULT_PREVIEW_WIDTH ;
64+ previewHeight = DEFAULT_PREVIEW_HEIGHT ;
65+ }
66+
67+ const largeSketch = previewWidth && previewWidth > 770 - 60 ;
68+
5569 const codeFrameRef = useRef ( null ) ;
5670
5771 const updateOrReRun = ( ) => {
@@ -86,7 +100,7 @@ export const CodeEmbed = (props) => {
86100 >
87101 { props . previewable ? (
88102 < div
89- className = { `ml-0 flex w-fit gap-[20px] ${ largeSketch ? "flex-col" : ( props . allowSideBySide ? "" : "flex-col lg:flex-row" ) } ` }
103+ className = { `ml-0 flex w-fit gap-[20px] ${ largeSketch ? "flex-col" : props . allowSideBySide ? "" : "flex-col lg:flex-row" } ` }
90104 >
91105 < div >
92106 < CodeFrame
@@ -96,10 +110,16 @@ export const CodeEmbed = (props) => {
96110 base = { props . base }
97111 frameRef = { codeFrameRef }
98112 lazyLoad = { props . lazyLoad }
99- scripts = { props . includeSound ? [ cdnLibraryUrl , cdnSoundUrl ] :[ cdnLibraryUrl ] }
113+ scripts = {
114+ props . includeSound
115+ ? [ cdnLibraryUrl , cdnSoundUrl ]
116+ : [ cdnLibraryUrl ]
117+ }
100118 />
101119 </ div >
102- < div className = { `flex gap-2.5 ${ largeSketch ? "flex-row" : "md:flex-row lg:flex-col" } ` } >
120+ < div
121+ className = { `flex gap-2.5 ${ largeSketch ? "flex-row" : "md:flex-row lg:flex-col" } ` }
122+ >
103123 < CircleButton
104124 className = "bg-bg-gray-40"
105125 onClick = { updateOrReRun }
0 commit comments