Skip to content

Commit 43345c2

Browse files
committed
tut_spa: improve code editor tabs style & fix code loading
1 parent 820f1fb commit 43345c2

3 files changed

Lines changed: 58 additions & 50 deletions

File tree

tutorial/justfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ gpt_files_content:
1515
tutorial-json_toc:
1616
python scripts/convert_toc.py
1717

18+
19+
tutorial_prepare: tutorial-json_toc
20+
cd single_page_book_app && npm install
21+
1822
tutorial_book:
1923
cd jbook && rm -rf _build && export PYTHONPATH=$(pwd)/sphinx_ext_imgui && jupyter-book build .
2024

tutorial/single_page_book_app/resources_singlepage/code_editor.js

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,10 @@ async function _processCodesIncludeInMarkdown(mdText, baseUrlPath) {
1818
if (line.trim().startsWith("```{codes_include}")) {
1919
const match = line.match(/```{codes_include}\s+(.*)/);
2020
if (match) {
21-
console.log("Found codes_include directive:", match[1]); // Log matched directive
21+
// console.log("Found codes_include directive:", match[1]); // Log matched directive
2222
const baseName = match[1].trim();
2323

2424
// Generate HTML as a string
25-
// const tabsHtml = `
26-
// <div class="code-editor-tab-container" data-base-name="${baseName}">
27-
// <div class="code-editor-tab-buttons">
28-
// <button class="code-editor-tab-button active" data-tab="Python">Python</button>
29-
// <button class="code-editor-tab-button" data-tab="C++">C++</button>
30-
// </div>
31-
// <div class="code-editor-tab-content">
32-
// <div class="code-editor-tab-pane" data-language="Python" data-file="${baseName}.py">
33-
// <div class="codemirror-placeholder" data-language="python"></div>
34-
// </div>
35-
// <div class="code-editor-tab-pane hidden" data-language="C++" data-file="${baseName}.cpp">
36-
// <div class="codemirror-placeholder" data-language="cpp"></div>
37-
// </div>
38-
// </div>
39-
// </div>`;
4025
const tabsHtml = `
4126
<div class="code-editor-tab-container" data-base-name="${baseName}">
4227
<div class="code-editor-tab-buttons">
@@ -52,9 +37,7 @@ async function _processCodesIncludeInMarkdown(mdText, baseUrlPath) {
5237
</div>
5338
</div>
5439
</div>`;
55-
56-
57-
console.log("Generated HTML for tabs:", tabsHtml); // Debug log
40+
// console.log("Generated HTML for tabs:", tabsHtml); // Debug log
5841
processedLines.push(tabsHtml);
5942
}
6043
insideCodesIncludeBlock = true;
@@ -68,31 +51,37 @@ async function _processCodesIncludeInMarkdown(mdText, baseUrlPath) {
6851
return processedLines.join("\n");
6952
}
7053

54+
7155
async function _initializeCodeMirrorEditors(baseUrlPath) {
7256
const containers = document.querySelectorAll(".code-editor-tab-container");
7357

74-
console.log("Found Code Editor Containers:", containers.length); // Debug log
58+
// console.log("Found Code Editor Containers:", containers.length); // Debug log
7559

7660
for (const container of containers) {
7761
const tabPanes = container.querySelectorAll(".code-editor-tab-pane");
7862

79-
console.log("Found Tab Panes:", tabPanes.length); // Debug log
63+
// console.log("Found Tab Panes:", tabPanes.length); // Debug log
8064

8165
for (const tabPane of tabPanes) {
8266
const filePath = tabPane.dataset.file;
8367
const language = tabPane.dataset.language.toLowerCase();
8468

85-
console.log(`Initializing CodeMirror for file: ${filePath}, language: ${language}`); // Debug log
69+
// console.log(`Initializing CodeMirror for file: ${filePath}, language: ${language}`); // Debug log
8670

8771
try {
88-
const fileResponse = await fetch(`${baseUrlPath}${filePath}`);
72+
// Correctly handle baseUrlPath and filePath to avoid duplication
73+
// Relative path; combine with baseUrlPath
74+
let fullPath = new URL(filePath, `${window.location.origin}/${baseUrlPath}/`).toString();
75+
// console.log(`Full Path for Fetch: ${fullPath}`); // Debug log
76+
77+
const fileResponse = await fetch(fullPath);
8978
if (!fileResponse.ok) {
90-
console.warn(`File not found: ${baseUrlPath}${filePath}`);
79+
console.warn(`File not found: ${fullPath}`);
9180
continue;
9281
}
9382

9483
const codeContent = await fileResponse.text();
95-
console.log(`Fetched Code Content for ${filePath}:\n`, codeContent); // Debug log
84+
// console.log(`Fetched Code Content for ${filePath}:\n`, codeContent); // Debug log
9685

9786
const cmPlaceholder = tabPane.querySelector(".codemirror-placeholder");
9887

@@ -129,16 +118,17 @@ async function _initializeCodeMirrorEditors(baseUrlPath) {
129118
_setupCodeEditorTabs(containers); // Ensure tab setup is called
130119
}
131120

121+
132122
function _setupCodeEditorTabs(containers) {
133123
containers.forEach((container) => {
134124
const buttons = container.querySelectorAll(".code-editor-tab-button");
135125
const panes = container.querySelectorAll(".code-editor-tab-pane");
136126

137127
buttons.forEach((button) => {
138-
console.log(`Attaching click event to button: ${button.dataset.tab}`); // Debugging log
128+
// console.log(`Attaching click event to button: ${button.dataset.tab}`); // Debugging log
139129
button.addEventListener("click", () => {
140130
const targetTab = button.dataset.tab;
141-
console.log(`Tab clicked: ${targetTab}`); // Debugging log
131+
// console.log(`Tab clicked: ${targetTab}`); // Debugging log
142132

143133
// Deactivate all buttons and panes
144134
buttons.forEach((btn) => btn.classList.remove("active"));
@@ -191,12 +181,13 @@ export async function prepareCodeEditors(mdText, baseUrlPath) {
191181
const renderedHtml = marked(processedMdText);
192182

193183
// Debug log: Final rendered HTML
194-
console.log("Final Rendered HTML:", renderedHtml);
184+
// console.log("Final Rendered HTML:", renderedHtml);
195185

196186
// Update the content area with the rendered HTML
197187
const contentArea = document.getElementById("content-area");
198188
contentArea.innerHTML = renderedHtml;
199189

200190
// Initialize CodeMirror editors for the placeholders
201-
await _initializeCodeMirrorEditors(baseUrlPath);
191+
//await _initializeCodeMirrorEditors(baseUrlPath);
192+
await _initializeCodeMirrorEditors("jbook");
202193
}

tutorial/single_page_book_app/resources_singlepage/style.css

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -248,60 +248,73 @@ body {
248248
/* ==========================
249249
Code Editor Tabs
250250
========================== */
251+
251252
.code-editor-tab-container {
252-
border: 1px solid #ccc;
253+
border: 1px solid #007bff; /* Blue border for the container */
253254
border-radius: 4px;
254255
overflow: hidden;
255256
margin-bottom: 1rem;
256-
background-color: #f9f9f9;
257+
background-color: #ffffff;
257258
}
258259

259260
.code-editor-tab-buttons {
260261
display: flex;
261-
background-color: #f9f9f9;
262+
justify-content: flex-start; /* Align tabs to the left */
263+
padding: 0.2rem; /* Small padding around tabs */
264+
background-color: #f9f9f9; /* Light gray for the tab bar */
262265
border-bottom: 1px solid #ccc;
266+
gap: 0.2rem; /* Add small gap between tabs */
263267
}
264268

265269
.code-editor-tab-button {
266-
flex: 1;
267-
padding: 0.5rem;
268-
text-align: center;
269-
background-color: #e9ecef;
270+
padding: 0.5rem 1rem; /* Adjust padding for a compact look */
270271
border: none;
272+
border-radius: 4px 4px 0 0; /* Rounded top corners */
273+
background-color: #e9ecef; /* Light gray background for inactive tabs */
271274
cursor: pointer;
272-
font-size: 1rem;
275+
font-size: 0.9rem;
273276
font-weight: bold;
277+
color: #333; /* Neutral dark text for inactive tabs */
278+
transition: background-color 0.3s, color 0.3s;
274279
}
275280

276281
.code-editor-tab-button.active {
282+
background-color: #007bff; /* Blue background for active tab */
283+
color: white; /* White text for active tab */
277284
font-weight: bold;
278-
background-color: #007bff;
279-
color: white;
280-
border-bottom: 2px solid white;
285+
border-bottom: 2px solid #ffffff; /* Seamless transition to the content area */
281286
}
282287

283288
.code-editor-tab-button:hover {
284-
background-color: #0056b3;
289+
background-color: #0056b3; /* Darker blue on hover */
285290
color: white;
286291
}
287292

288293
.code-editor-tab-content {
289-
padding: 0;
290-
background-color: white;
294+
padding: 1rem;
295+
background-color: #ffffff; /* White background for content area */
296+
border-top: none;
291297
}
292298

293299
.code-editor-tab-pane {
294-
display: none; /* All panes are hidden by default */
295-
padding: 1rem;
300+
display: block;
301+
animation: fadeIn 0.2s ease-in; /* Smooth fade-in animation for content */
296302
}
297303

298-
.code-editor-tab-pane:not(.hidden) {
299-
display: block; /* Only the active pane is shown */
304+
.code-editor-tab-pane.hidden {
305+
display: none;
300306
}
301307

302-
.code-editor-tab-button.active {
303-
background-color: #007bff;
304-
color: white;
308+
/* Subtle animation for tab switching */
309+
@keyframes fadeIn {
310+
from {
311+
opacity: 0;
312+
transform: translateY(5px);
313+
}
314+
to {
315+
opacity: 1;
316+
transform: translateY(0);
317+
}
305318
}
306319

307320
/* ==========================

0 commit comments

Comments
 (0)