Skip to content

Commit 56e89d6

Browse files
committed
tut_spa: code editor / handle size and scroll
1 parent 775b026 commit 56e89d6

2 files changed

Lines changed: 37 additions & 17 deletions

File tree

tutorial/single_page_book_app/resources_singlepage/code_editor.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

7068
async 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-
144154
function _setupCodeEditorTabs(containers) {
145155
containers.forEach((container) => {
146156
const buttons = container.querySelectorAll(".code-editor-tab-button");

tutorial/single_page_book_app/resources_singlepage/style.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,16 @@ body {
254254
border-radius: 4px;
255255
}
256256

257+
/* Container which handles scrolling for code editor */
258+
.codemirror-wrapper {
259+
border: 1px solid #ccc;
260+
border-radius: 4px;
261+
width: 100%;
262+
/* initial height is set by js, so no need for height or max-height here*/
263+
min-height: 100px;
264+
overflow-x: auto;
265+
box-sizing: border-box;
266+
}
257267

258268
/* ==========================
259269
Code Editor Tabs

0 commit comments

Comments
 (0)