@@ -59,47 +59,42 @@ function _createEditorRunPythonCodeButton(editorView) {
5959 runButton . classList . add ( "run-python-button" ) ;
6060
6161 runButton . addEventListener ( "click" , ( ) => {
62- // Get the current text from the editor
6362 const currentCode = editorView . state . doc . toString ( ) ;
6463 runPythonCode ( currentCode ) ;
6564 } ) ;
66-
6765 return runButton ;
6866}
6967
7068async function _initializeCodeMirrorEditors ( baseUrlPath ) {
7169 const containers = document . querySelectorAll ( ".code-editor-tab-container" ) ;
7270
73- // console.log("Found Code Editor Containers:", containers.length); // Debug log
74-
7571 for ( const container of containers ) {
7672 const tabPanes = container . querySelectorAll ( ".code-editor-tab-pane" ) ;
7773
78- // console.log("Found Tab Panes:", tabPanes.length); // Debug log
79-
8074 for ( const tabPane of tabPanes ) {
8175 const filePath = tabPane . dataset . file ;
8276 const language = tabPane . dataset . language . toLowerCase ( ) ;
8377
84- // console.log(`Initializing CodeMirror for file: ${filePath}, language: ${language}`); // Debug log
85-
8678 try {
87- // Correctly handle baseUrlPath and filePath to avoid duplication
88- // Relative path; combine with baseUrlPath
79+ // Combine baseUrlPath + filePath properly
8980 let fullPath = new URL ( filePath , `${ window . location . origin } /${ baseUrlPath } /` ) . toString ( ) ;
90- // console.log(`Full Path for Fetch: ${fullPath}`); // Debug log
91-
9281 const fileResponse = await fetch ( fullPath ) ;
9382 if ( ! fileResponse . ok ) {
9483 console . warn ( `File not found: ${ fullPath } ` ) ;
9584 continue ;
9685 }
9786
9887 const codeContent = await fileResponse . text ( ) ;
99- // console.log(`Fetched Code Content for ${filePath}:\n`, codeContent); // Debug log
10088
89+ // Create a wrapper for CodeMirror so we can set resize/height on it
90+ const wrapper = document . createElement ( "div" ) ;
91+ wrapper . classList . add ( "codemirror-wrapper" ) ;
92+
93+ // Append this wrapper to the pane’s placeholder
10194 const cmPlaceholder = tabPane . querySelector ( ".codemirror-placeholder" ) ;
95+ cmPlaceholder . appendChild ( wrapper ) ;
10296
97+ // Build up CodeMirror extensions
10398 const extensions = [
10499 lineNumbers ( ) ,
105100 syntaxHighlighting ( defaultHighlightStyle ) ,
@@ -116,15 +111,31 @@ async function _initializeCodeMirrorEditors(baseUrlPath) {
116111 extensions . push ( cpp ( ) ) ;
117112 }
118113
114+ // Create the editor in the wrapper
119115 const editorView = new EditorView ( {
120116 state : EditorState . create ( {
121117 doc : codeContent ,
122118 extensions,
123119 } ) ,
124- parent : cmPlaceholder ,
120+ parent : wrapper , // set the parent as our new wrapper
125121 } ) ;
126122
127- // If it's a Python editor, add a "Run" button below it
123+ // *** Adjust initial height based on line count ***
124+ const lineCount = codeContent . split ( "\n" ) . length ;
125+ const maxLines = 20 ; // limit to 20 lines
126+ const chosenLines = Math . min ( lineCount , maxLines ) ;
127+
128+ // We'll assume ~1.4em line height. Adjust as needed or measure programmatically
129+ const lineHeightPx = 22 ; // approx
130+ const initialHeightPx = chosenLines * lineHeightPx ;
131+
132+ // Now style the wrapper
133+ wrapper . style . resize = "vertical" ;
134+ wrapper . style . overflow = "auto" ;
135+ wrapper . style . maxHeight = "600px" ; // some overall maximum if you like
136+ wrapper . style . height = `${ initialHeightPx } px` ; // initial size
137+
138+ // If it's a Python editor, add a run button
128139 if ( language === "python" ) {
129140 const runButton = _createEditorRunPythonCodeButton ( editorView ) ;
130141 tabPane . appendChild ( runButton ) ;
@@ -137,10 +148,9 @@ async function _initializeCodeMirrorEditors(baseUrlPath) {
137148 }
138149 }
139150
140- _setupCodeEditorTabs ( containers ) ; // Ensure tab setup is called
151+ _setupCodeEditorTabs ( containers ) ;
141152}
142153
143-
144154function _setupCodeEditorTabs ( containers ) {
145155 containers . forEach ( ( container ) => {
146156 const buttons = container . querySelectorAll ( ".code-editor-tab-button" ) ;
0 commit comments