@@ -66,6 +66,7 @@ function registerKidCodeLanguage() {
6666require . config ( {
6767 paths : { vs : "https://cdn.jsdelivr.net/npm/monaco-editor@0.34.1/min/vs" } ,
6868} ) ;
69+
6970require ( [ "vs/editor/editor.main" ] , function ( ) {
7071 registerKidCodeLanguage ( ) ;
7172
@@ -103,33 +104,8 @@ require(["vs/editor/editor.main"], function () {
103104 minimap : { enabled : false } ,
104105 } ) ;
105106
106- // ✅ Dynamically populate dropdown safely
107- const selector = document . getElementById ( "exampleSelector" ) ;
108-
109- if ( ! window . examples ) {
110- console . error ( "examples.js failed to load or window.examples is undefined" ) ;
111- logToOutput (
112- "⚠️ examples.js not loaded — please check your file setup." ,
113- "error"
114- ) ;
115- return ;
116- }
117-
118- Object . keys ( window . examples ) . forEach ( ( exampleName ) => {
119- const option = document . createElement ( "option" ) ;
120- option . value = exampleName ;
121- option . textContent = exampleName ;
122- selector . appendChild ( option ) ;
123- } ) ;
124-
125- // ✅ NEW: Load example into editor when selected
126- selector . addEventListener ( "change" , ( ) => {
127- const selected = selector . value ;
128- if ( window . examples && window . examples [ selected ] ) {
129- editor . setValue ( window . examples [ selected ] ) ;
130- logToOutput ( `✅ Loaded example: ${ selected } ` ) ;
131- }
132- } ) ;
107+ // ✅ Safely initialize examples dropdown without breaking editor setup
108+ initializeExamples ( ) ;
133109
134110 // Add an editor action / keybinding so Ctrl/Cmd+Enter triggers the Run button
135111 editor . addAction ( {
@@ -150,6 +126,37 @@ require(["vs/editor/editor.main"], function () {
150126 validateCode ( ) ;
151127} ) ;
152128
129+ // --- Initialize example dropdown (gracefully degrades if examples.js missing) ---
130+ function initializeExamples ( ) {
131+ const selector = document . getElementById ( "exampleSelector" ) ;
132+ if ( ! selector ) {
133+ console . error ( "Example selector element not found" ) ;
134+ return ;
135+ }
136+
137+ if ( ! window . examples ) {
138+ console . warn ( "examples.js not loaded - example selector disabled" ) ;
139+ selector . disabled = true ;
140+ selector . innerHTML = '<option value="">Examples unavailable</option>' ;
141+ return ;
142+ }
143+
144+ Object . keys ( window . examples ) . forEach ( ( exampleName ) => {
145+ const option = document . createElement ( "option" ) ;
146+ option . value = exampleName ;
147+ option . textContent = exampleName ;
148+ selector . appendChild ( option ) ;
149+ } ) ;
150+
151+ selector . addEventListener ( "change" , ( ) => {
152+ const selected = selector . value ;
153+ if ( window . examples [ selected ] ) {
154+ editor . setValue ( window . examples [ selected ] ) ;
155+ logToOutput ( `✅ Loaded example: ${ selected } ` ) ;
156+ }
157+ } ) ;
158+ }
159+
153160// --- 2. ADD EVENT LISTENER TO THE RUN BUTTON ---
154161runButton . addEventListener ( "click" , async ( ) => {
155162 const code = editor . getValue ( ) ;
0 commit comments