Skip to content

Commit 1acfcab

Browse files
committed
Homepage: lazy CodeMirror init, editor hidden until arrow clicked
1 parent 4206071 commit 1acfcab

1 file changed

Lines changed: 11 additions & 15 deletions

File tree

index.html

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -596,15 +596,8 @@ <h3>New to C++ Mode?</h3>
596596
}
597597
}`;
598598

599-
const heroEditor = CodeMirror.fromTextArea(document.getElementById('hero-editor'), {
600-
mode: 'cppmode', theme: 'cppmode', lineNumbers: true,
601-
indentUnit: 2, tabSize: 2, autofocus: false, lineWrapping: false,
602-
});
603-
heroEditor.setValue(HERO_DEFAULT);
604-
heroEditor.setSize('100%', '300px');
605-
599+
let heroEditor = null;
606600
let heroEditorOpen = false;
607-
let heroEditorMoved = false;
608601

609602
function toggleHeroEditor() {
610603
heroEditorOpen = !heroEditorOpen;
@@ -616,14 +609,17 @@ <h3>New to C++ Mode?</h3>
616609
overlay.style.display = 'flex';
617610
arrow.style.transform = 'rotate(180deg)';
618611
runBtn.style.display = 'block';
619-
// Move CodeMirror into the overlay on first open
620-
if (!heroEditorMoved) {
621-
document.getElementById('hero-cm-wrap').appendChild(heroEditor.getWrapperElement());
622-
heroEditorMoved = true;
612+
document.body.style.overflow = 'hidden';
613+
// Init CodeMirror lazily on first open
614+
if (!heroEditor) {
615+
heroEditor = CodeMirror(document.getElementById('hero-cm-wrap'), {
616+
mode: 'cppmode', theme: 'cppmode', lineNumbers: true,
617+
indentUnit: 2, tabSize: 2, lineWrapping: false,
618+
value: HERO_DEFAULT,
619+
});
620+
heroEditor.setSize('100%', '100%');
623621
}
624-
heroEditor.setSize('100%', '100%');
625622
setTimeout(() => heroEditor.refresh(), 50);
626-
document.body.style.overflow = 'hidden';
627623
} else {
628624
overlay.style.display = 'none';
629625
arrow.style.transform = '';
@@ -650,7 +646,7 @@ <h3>New to C++ Mode?</h3>
650646
try {
651647
const resp = await fetch(HERO_API + '/compile', {
652648
method: 'POST', headers: {'Content-Type':'application/json'},
653-
body: JSON.stringify({code: heroEditor.getValue()}),
649+
body: JSON.stringify({code: heroEditor ? heroEditor.getValue() : HERO_DEFAULT}),
654650
});
655651
const data = await resp.json();
656652
if (data.error) { alert('Error:\n' + data.error.substring(0,300)); }

0 commit comments

Comments
 (0)