-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathindex.html
More file actions
682 lines (583 loc) · 20.9 KB
/
index.html
File metadata and controls
682 lines (583 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>jChess Examples</title>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: system-ui, sans-serif;
background: #1a1a1a;
color: #e0e0e0;
height: 100dvh;
display: flex;
flex-direction: column;
overflow: hidden;
}
header {
padding: 0.75rem 1.25rem;
border-bottom: 1px solid #2e2e2e;
flex-shrink: 0;
}
header h1 {
font-size: 1.25rem;
font-weight: 700;
color: #f0d9b5;
}
.layout {
display: flex;
flex: 1;
min-height: 0;
}
/* ── Sidebar ── */
.sidebar {
width: 220px;
flex-shrink: 0;
border-right: 1px solid #2e2e2e;
overflow-y: auto;
padding: 0.5rem 0;
}
.sidebar-item {
padding: 0.6rem 1rem;
cursor: pointer;
font-size: 0.875rem;
color: #aaa;
border-left: 3px solid transparent;
transition: background 100ms, color 100ms;
line-height: 1.3;
}
.sidebar-item:hover {
background: #242424;
color: #e0e0e0;
}
.sidebar-item.active {
border-left-color: #b58863;
background: #242424;
color: #f0d9b5;
}
/* ── Main area ── */
.main {
--jchess-size: min(480px, 75dvh);
flex: 1;
display: flex;
align-items: center;
justify-content: center;
padding: 1.5rem;
min-width: 0;
overflow-y: auto;
}
/* ── Move list ── */
.move-list-panel {
width: 160px;
flex-shrink: 0;
}
.move-list {
height: var(--jchess-size, min(480px, 75dvh));
overflow-y: auto;
overflow-x: hidden;
font-family: monospace;
font-size: 0.8rem;
}
.move-list .move-row {
display: flex;
align-items: baseline;
}
.move-list .move-num {
color: #555;
text-align: right;
padding: 0.2rem 0.3rem 0.2rem 0.2rem;
min-width: 2rem;
user-select: none;
flex-shrink: 0;
}
.move-list .move {
color: #bbb;
border-radius: 3px;
cursor: pointer;
padding: 0.2rem 0.3rem;
min-width: 3.5rem;
}
.move-list .move:hover { background: #2a2a2a; color: #e0e0e0; }
.move-list .move.current {
background: #b58863;
color: #fff;
}
.move-list .variation-block {
padding: 0.05rem 0.3rem 0.1rem 0.5rem;
font-size: 0.75rem;
color: #666;
line-height: 1.8;
white-space: normal;
overflow-wrap: break-word;
}
.move-list .variation-block .move-num {
color: #555;
min-width: unset;
padding: 0.05rem 0.1rem;
}
.move-list .variation-block .move {
color: #888;
min-width: unset;
padding: 0.05rem 0.2rem;
}
.move-list .variation-block .move:hover { color: #e0e0e0; background: #2a2a2a; }
.board-column {
display: flex;
flex-direction: column;
align-items: center;
gap: 0.75rem;
}
.board-row {
display: flex;
gap: 1rem;
align-items: flex-start;
}
.board-wrap {
flex-shrink: 0;
}
/* ── Controls ── */
.controls {
display: flex;
flex-direction: column;
align-items: center;
gap: 0.5rem;
width: 100%;
}
.btn-row {
display: flex;
gap: 0.5rem;
}
button {
padding: 0.4rem 0.9rem;
border: 1px solid #444;
background: #2a2a2a;
color: #e0e0e0;
border-radius: 4px;
cursor: pointer;
font-size: 0.875rem;
transition: background 100ms;
}
button:hover:not(:disabled) { background: #3a3a3a; }
button:disabled { opacity: 0.35; cursor: default; }
.move-display {
font-family: monospace;
font-size: 0.9rem;
color: #f0d9b5;
min-height: 1.2em;
}
.halfmove-display {
font-size: 0.75rem;
color: #555;
}
/* ── Annotation panel ── */
.annotation-panel {
width: 220px;
flex-shrink: 0;
}
.annotation-panel {
position: relative;
}
.annotation-panel.has-annotation::before {
content: '\201C';
position: absolute;
top: -0.1em;
left: -0.15em;
font-size: 5rem;
line-height: 1;
color: #2e2e2e;
font-style: italic;
pointer-events: none;
}
.annotation {
height: var(--jchess-size, min(480px, 75dvh));
font-size: 0.95rem;
line-height: 1.7;
color: #888;
font-style: italic;
white-space: normal;
overflow-y: auto;
padding-top: 1.5rem;
}
/* ── PGN paste input ── */
.pgn-input-wrap {
display: flex;
flex-direction: column;
gap: 0.5rem;
flex: 1;
}
.pgn-input-wrap textarea {
flex: 1;
background: #111;
border: 1px solid #333;
border-radius: 4px;
color: #ccc;
font-family: monospace;
font-size: 0.75rem;
padding: 0.5rem;
resize: none;
outline: none;
}
.pgn-input-wrap textarea:focus { border-color: #555; }
.no-controls {
font-size: 0.8rem;
color: #555;
font-style: italic;
}
.source-details {
width: 100%;
max-width: min(480px, 75dvh);
}
.source-details summary {
font-size: 0.75rem;
color: #555;
cursor: pointer;
user-select: none;
padding: 0.25rem 0;
}
.source-details summary:hover { color: #888; }
.source-details pre {
margin-top: 0.5rem;
padding: 0.75rem;
background: #111;
border: 1px solid #2e2e2e;
border-radius: 4px;
font-size: 0.75rem;
color: #aaa;
white-space: pre-wrap;
word-break: break-all;
max-height: 200px;
overflow-y: auto;
}
.game-title {
font-size: 0.95rem;
font-weight: 600;
color: #f0d9b5;
margin-bottom: 0.25rem;
}
</style>
</head>
<body>
<header>
<h1>jChess</h1>
</header>
<div class="layout">
<nav class="sidebar" id="sidebar"></nav>
<div class="main" id="main"></div>
</div>
<script type="module">
import { ChessViewer } from './src/chess.ts'
const DEMOS = [
{ title: 'Fischer vs. Spassky (1992)', pgn: 'fischer-spassky.pgn' },
{ title: 'justdoeet vs. Zornhau', pgn: 'justdoeet.pgn' },
{ title: 'Markowski vs. Piket (with queening)', pgn: 'with-queening.pgn' },
{ title: 'Unambiguous knight move', pgn: 'unambiguous-knight.pgn' },
{ title: 'Heavily annotated game', pgn: 'heavily-annotated.pgn' },
{ title: 'Middle game — jump to move 25', pgn: 'middle-game.pgn', jumpTo: 25 },
{ title: 'RAV: Sicilian with nested variations', pgn: 'with-rav.pgn' },
{ title: 'FEN: Sicilian after 1.e4 c5 2.Nf3', options: { fen: 'rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2' } },
{ title: 'FEN: Mid-game position', options: { fen: 'rq2r1k1/1b3pp1/p3p1n1/1p4BQ/8/7R/PP3PPP/4R1K1 w - - 0 0' } },
{ title: 'Blank board', options: {} },
{ title: 'Paste PGN', paste: true },
]
async function loadPGN(filename) {
return fetch(`./examples/${filename}`).then(r => r.text())
}
// ── Build static DOM ──────────────────────────────────────────────────────
const boardColumn = document.createElement('div')
boardColumn.className = 'board-column'
const gameTitle = document.createElement('div')
gameTitle.className = 'game-title'
const boardWrap = document.createElement('div')
boardWrap.className = 'board-wrap'
const controlsEl = document.createElement('div')
controlsEl.className = 'controls'
const sourceDetails = document.createElement('details')
sourceDetails.className = 'source-details'
const sourceSummary = document.createElement('summary')
const sourcePre = document.createElement('pre')
sourceDetails.append(sourceSummary, sourcePre)
// ── Shared viewer ─────────────────────────────────────────────────────────
let viewer = new ChessViewer(boardWrap, {})
// ── Controls (created once, updated per game) ─────────────────────────────
const btnRow = document.createElement('div')
const backBtn = document.createElement('button')
const nextBtn = document.createElement('button')
const flipBtn = document.createElement('button')
const exitVarBtn = document.createElement('button')
const moveDisplay = document.createElement('div')
const hmDisplay = document.createElement('div')
const annotation = document.createElement('div')
const noControls = document.createElement('div')
const pgnInputWrap = document.createElement('div')
pgnInputWrap.className = 'pgn-input-wrap'
const pgnTextarea = document.createElement('textarea')
pgnTextarea.placeholder = 'Paste PGN here…'
const pgnLoadBtn = document.createElement('button')
pgnLoadBtn.textContent = 'Load'
pgnInputWrap.append(pgnTextarea, pgnLoadBtn)
const moveListPanel = document.createElement('div')
moveListPanel.className = 'move-list-panel'
const moveListEl = document.createElement('div')
moveListEl.className = 'move-list'
moveListPanel.appendChild(moveListEl)
const annotationPanel = document.createElement('div')
annotationPanel.className = 'annotation-panel'
annotationPanel.appendChild(annotation)
const boardRow = document.createElement('div')
boardRow.className = 'board-row'
boardRow.append(moveListPanel, boardWrap, annotationPanel)
// Re-append boardColumn children in correct order now that boardRow exists
boardColumn.append(gameTitle, boardRow, controlsEl, sourceDetails)
document.getElementById('main').append(boardColumn)
btnRow.className = 'btn-row'
moveDisplay.className = 'move-display'
hmDisplay.className = 'halfmove-display'
annotation.className = 'annotation'
noControls.className = 'no-controls'
backBtn.textContent = '← Back'
nextBtn.textContent = 'Next →'
flipBtn.textContent = 'Flip'
exitVarBtn.textContent = '↑ Exit variation'
exitVarBtn.style.display = 'none'
noControls.textContent = 'Position only — no moves'
btnRow.append(backBtn, nextBtn, flipBtn, exitVarBtn)
controlsEl.append(btnRow, moveDisplay, hmDisplay, noControls)
function updateControls() {
const hm = viewer.getHalfmove()
const total = viewer.getTotalMoves()
const move = viewer.getCurrentMove()
const hasMoves = total > 0
btnRow.style.display = hasMoves ? '' : 'none'
moveDisplay.style.display = hasMoves ? '' : 'none'
hmDisplay.style.display = hasMoves ? '' : 'none'
noControls.style.display = hasMoves ? 'none' : ''
if (hasMoves) {
const fullMove = Math.ceil(hm / 2)
moveDisplay.textContent = move
? `${fullMove}. ${hm % 2 === 0 ? '...' : ''}${move}`
: '(start)'
hmDisplay.textContent = `Move ${hm} / ${total}`
const annotationText = (viewer.getAnnotation() ?? '').replace(/\s+/g, ' ').trim()
annotation.textContent = annotationText
annotationPanel.classList.toggle('has-annotation', annotationText.length > 0)
backBtn.disabled = hm === 0
nextBtn.disabled = hm >= total
}
exitVarBtn.style.display = viewer.isInVariation ? '' : 'none'
highlightCurrentMove()
}
backBtn.addEventListener('click', () => { viewer.prev(); updateControls() })
nextBtn.addEventListener('click', () => { viewer.next(); updateControls() })
exitVarBtn.addEventListener('click', () => { viewer.exitVariation(); updateControls() })
flipBtn.addEventListener('click', () => viewer.flip())
document.addEventListener('keydown', e => {
if (e.key === 'ArrowLeft') { viewer.prev(); updateControls() }
if (e.key === 'ArrowRight') { viewer.next(); updateControls() }
})
// ── Sidebar ───────────────────────────────────────────────────────────────
const sidebar = document.getElementById('sidebar')
let activeItem = null
/**
* Build a div-based move list from `transitions`.
* `startFull` / `startColor` set the starting full-move number and side.
* `navPath` is an array of { branchHalfmove, variationIndex } steps describing the full
* path from the main line to reach this variation. Empty array = main line.
* `depth` is nesting level (0 = main line), used only for CSS class.
* Returns a DocumentFragment containing the move rows and variation blocks.
*/
function buildMoveSequence(transitions, startFull, startColor, navPath, depth) {
const frag = document.createDocumentFragment()
let fullMove = startFull
let color = startColor
let currentRow = null
for (let i = 0; i < transitions.length; i++) {
const t = transitions[i]
const halfmoveInLine = i + 1
// Snapshot color/fullMove for this move's variations (before advancing)
const moveColor = color
const moveFull = fullMove
// Start a new row for every white move, or whenever there's no current row
if (color === 'w') {
currentRow = document.createElement('div')
currentRow.className = 'move-row'
const numSpan = document.createElement('span')
numSpan.className = 'move-num'
numSpan.textContent = `${fullMove}.`
currentRow.appendChild(numSpan)
frag.appendChild(currentRow)
} else if (!currentRow) {
// No current row (start of sequence or after a variation block) and it's black's move
currentRow = document.createElement('div')
currentRow.className = 'move-row'
const numSpan = document.createElement('span')
numSpan.className = 'move-num'
numSpan.textContent = `${fullMove}…`
currentRow.appendChild(numSpan)
const empty = document.createElement('span')
empty.className = 'move'
currentRow.appendChild(empty)
frag.appendChild(currentRow)
}
const moveSpan = document.createElement('span')
moveSpan.className = 'move'
moveSpan.textContent = t.san
moveSpan.dataset.lineDepth = String(depth)
moveSpan.dataset.halfmoveInLine = String(halfmoveInLine)
moveSpan.addEventListener('click', () => {
// Exit back to main line, then replay the full nav path to reach this variation
while (viewer.isInVariation) viewer.exitVariation()
for (const step of navPath) {
viewer.jumpTo(step.branchHalfmove)
viewer.enterVariation(step.variationIndex)
}
viewer.jumpTo(halfmoveInLine)
updateControls()
})
currentRow.appendChild(moveSpan)
// Advance
if (color === 'b') fullMove++
color = color === 'w' ? 'b' : 'w'
// Append variation blocks (they start from the position BEFORE this move)
if (t.variations && t.variations.length > 0) {
for (let vi = 0; vi < t.variations.length; vi++) {
const varT = t.variations[vi]
if (!varT || varT.length === 0) continue
const block = document.createElement('div')
block.className = 'variation-block'
const childNavPath = [...navPath, { branchHalfmove: i, variationIndex: vi }]
block.appendChild(buildVariationSpan(varT, moveFull, moveColor, childNavPath))
frag.appendChild(block)
currentRow = null
}
}
}
return frag
}
/**
* Build an inline ( move1 move2 (nested...) ) <span> for a variation.
* Nested variations are also inline spans so they wrap naturally within the text.
*/
function buildVariationSpan(transitions, startFull, startColor, navPath) {
const span = document.createElement('span')
span.append('(')
let fullMove = startFull
let color = startColor
let showNum = true // first move always needs a number
for (let i = 0; i < transitions.length; i++) {
const t = transitions[i]
const halfmoveInLine = i + 1
const moveColor = color
const moveFull = fullMove
if (showNum || color === 'w') {
const numSpan = document.createElement('span')
numSpan.className = 'move-num'
numSpan.textContent = color === 'w' ? `${fullMove}.` : `${fullMove}\u2026`
span.appendChild(numSpan)
showNum = false
}
const moveSpan = document.createElement('span')
moveSpan.className = 'move'
moveSpan.textContent = t.san
moveSpan.addEventListener('click', (e) => {
e.stopPropagation()
while (viewer.isInVariation) viewer.exitVariation()
for (const step of navPath) {
viewer.jumpTo(step.branchHalfmove)
viewer.enterVariation(step.variationIndex)
}
viewer.jumpTo(halfmoveInLine)
updateControls()
})
span.appendChild(moveSpan)
if (color === 'b') fullMove++
color = color === 'w' ? 'b' : 'w'
if (t.variations && t.variations.length > 0) {
for (let vi = 0; vi < t.variations.length; vi++) {
const varT = t.variations[vi]
if (!varT || varT.length === 0) continue
const childNavPath = [...navPath, { branchHalfmove: i, variationIndex: vi }]
span.appendChild(buildVariationSpan(varT, moveFull, moveColor, childNavPath))
}
showNum = true // after an inline variation, next move needs its number
}
}
span.append(')')
return span
}
function buildMoveList() {
const transitions = viewer.getTransitions()
if (transitions.length === 0) { moveListEl.innerHTML = ''; return }
moveListEl.replaceChildren(buildMoveSequence(transitions, 1, 'w', [], 0))
}
function highlightCurrentMove() {
const hm = viewer.getHalfmove()
const inVar = viewer.isInVariation
moveListEl.querySelectorAll('.move').forEach(el => {
const depth = Number(el.dataset.lineDepth ?? 0)
// Highlight main-line moves when not in a variation; variation moves when in one
const isMain = depth === 0 && !inVar
if (isMain) {
el.classList.toggle('current', el.dataset.halfmoveInLine === String(hm))
} else {
el.classList.remove('current')
}
})
const current = moveListEl.querySelector('.move.current')
current?.scrollIntoView({ block: 'nearest' })
}
function loadPGNText(pgn) {
viewer.loadPGN(pgn)
buildMoveList()
updateControls()
}
pgnLoadBtn.addEventListener('click', () => loadPGNText(pgnTextarea.value))
pgnTextarea.addEventListener('keydown', e => {
if (e.key === 'Enter' && (e.metaKey || e.ctrlKey)) loadPGNText(pgnTextarea.value)
})
async function loadDemo(demo, itemEl) {
if (activeItem) activeItem.classList.remove('active')
activeItem = itemEl
itemEl.classList.add('active')
gameTitle.textContent = demo.title
if (demo.paste) {
annotationPanel.replaceChildren(pgnInputWrap)
sourceDetails.style.display = 'none'
viewer.loadPGN('')
moveListEl.innerHTML = ''
updateControls()
pgnTextarea.focus()
return
}
annotationPanel.replaceChildren(annotation)
let options = demo.options ?? {}
if (demo.pgn) options = { ...options, pgn: await loadPGN(demo.pgn) }
sourceDetails.removeAttribute('open')
if (options.pgn) {
sourceSummary.textContent = 'Show PGN'
sourcePre.textContent = options.pgn.trim()
sourceDetails.style.display = ''
} else if (options.fen) {
sourceSummary.textContent = 'Show FEN'
sourcePre.textContent = options.fen
sourceDetails.style.display = ''
} else {
sourceDetails.style.display = 'none'
}
viewer.loadPGN(options.pgn ?? '', options.fen)
if (demo.jumpTo) viewer.jumpTo(demo.jumpTo)
buildMoveList()
updateControls()
}
DEMOS.forEach((demo, i) => {
const item = document.createElement('div')
item.className = 'sidebar-item'
item.textContent = demo.title
item.addEventListener('click', () => loadDemo(demo, item))
sidebar.appendChild(item)
// Load the first demo automatically
if (i === 0) loadDemo(demo, item)
})
</script>
</body>
</html>