1
1
// Import modules
2
2
import { runJavaFile } from './wasm-jvm.js' ;
3
+ // --- Add: Import Java source code for source view ---
4
+ import { javaFiles } from './source.js' ;
3
5
4
6
// --- State Machine for Terminal UI ---
5
7
const javaFilesList = [
@@ -17,21 +19,51 @@ function renderTerminal() {
17
19
let html = '' ;
18
20
if ( terminalState === 'menu' ) {
19
21
html += '<span style="color:#888">Use ↑/↓ to select, Enter to run, → to view code.</span><br>' ;
22
+ html += '<div class="mobile-menu-btns">' ;
23
+ javaFilesList . forEach ( ( file , idx ) => {
24
+ html += `<button class="mobile-menu-btn${ idx === selectedFileIdx ?' active' :'' } " data-idx="${ idx } ">${ file . label } </button><br>` ;
25
+ } ) ;
26
+ html += '</div>' ;
20
27
javaFilesList . forEach ( ( file , idx ) => {
21
28
html += ( idx === selectedFileIdx ? '<b style="color:#ff5555">▶ ' : ' ' ) + file . label + ( idx === selectedFileIdx ? '</b>' : '' ) + '<br>' ;
22
29
} ) ;
23
30
html += '<br><span style="color:#ffb86c">' + javaFilesList [ selectedFileIdx ] . brief + '</span>' ;
24
31
} else if ( terminalState === 'output' ) {
25
32
html += '<pre style="color:#ff5555;">' + lastOutputText + '</pre>' ;
26
33
html += '<br><span style="color:#888">[→] Check source code | [b] Back to menu</span>' ;
34
+ html += '<div class="mobile-output-btns"><button id="mobile-source-btn">Check Source Code</button> <button id="mobile-back-btn">Back to Menu</button></div>' ;
27
35
} else if ( terminalState === 'source' ) {
28
36
html += '<pre style="color:#ff5555;">' + javaFiles [ javaFilesList [ selectedFileIdx ] . key ] + '</pre>' ;
29
37
html += '<br><span style="color:#888">[←] Back to output | [b] Back to menu</span>' ;
38
+ html += '<div class="mobile-source-btns"><button id="mobile-back-output-btn">Back to Output</button> <button id="mobile-back-menu-btn">Back to Menu</button></div>' ;
30
39
} else if ( terminalState === 'running' ) {
31
40
html += '<span style="color:#888">Running Java...</span>' ;
32
41
}
33
42
terminal . innerHTML = html ;
34
- // Show/hide mobile source button
43
+
44
+ // Mobile menu button logic
45
+ document . querySelectorAll ( '.mobile-menu-btn' ) . forEach ( btn => {
46
+ btn . onclick = function ( ) {
47
+ selectedFileIdx = parseInt ( btn . getAttribute ( 'data-idx' ) ) ;
48
+ renderTerminal ( ) ;
49
+ } ;
50
+ btn . ondblclick = function ( ) {
51
+ selectedFileIdx = parseInt ( btn . getAttribute ( 'data-idx' ) ) ;
52
+ terminalState = 'running' ;
53
+ renderTerminal ( ) ;
54
+ runJavaFile ( javaFilesList [ selectedFileIdx ] . key , showOutput ) ;
55
+ } ;
56
+ } ) ;
57
+ // Mobile output/source navigation
58
+ const srcBtn = document . getElementById ( 'mobile-source-btn' ) ;
59
+ if ( srcBtn ) srcBtn . onclick = showSource ;
60
+ const backBtn = document . getElementById ( 'mobile-back-btn' ) ;
61
+ if ( backBtn ) backBtn . onclick = showMenu ;
62
+ const backOutputBtn = document . getElementById ( 'mobile-back-output-btn' ) ;
63
+ if ( backOutputBtn ) backOutputBtn . onclick = ( ) => showOutput ( lastOutputText ) ;
64
+ const backMenuBtn = document . getElementById ( 'mobile-back-menu-btn' ) ;
65
+ if ( backMenuBtn ) backMenuBtn . onclick = showMenu ;
66
+ // Show/hide desktop source button
35
67
const btn = document . getElementById ( 'show-source-btn' ) ;
36
68
if ( btn ) btn . style . display = ( terminalState === 'output' ) ? 'inline-block' : 'none' ;
37
69
}
0 commit comments