89 lines
2.4 KiB
Svelte
89 lines
2.4 KiB
Svelte
<script lang="ts">
|
|
import { onMount } from 'svelte';
|
|
import { Chessground } from '@lichess-org/chessground';
|
|
import type { Api } from '@lichess-org/chessground/api';
|
|
import type { Key } from '@lichess-org/chessground/types';
|
|
|
|
let {
|
|
fen,
|
|
lastMove,
|
|
check = false,
|
|
orientation = 'white',
|
|
arrow = null,
|
|
movable = null,
|
|
onmove
|
|
}: {
|
|
fen: string;
|
|
lastMove?: [string, string];
|
|
check?: boolean;
|
|
orientation?: 'white' | 'black';
|
|
arrow?: [string, string] | null;
|
|
/** when set, the given color may move: dests = legal destinations per square */
|
|
movable?: { color: 'white' | 'black'; dests: Map<string, string[]> } | null;
|
|
onmove?: (from: string, to: string) => void;
|
|
} = $props();
|
|
|
|
let el: HTMLDivElement;
|
|
let api: Api | undefined;
|
|
|
|
onMount(() => {
|
|
api = Chessground(el, {
|
|
fen,
|
|
orientation,
|
|
viewOnly: false,
|
|
coordinates: true,
|
|
animation: { enabled: true, duration: 220 },
|
|
highlight: { lastMove: true, check: true },
|
|
movable: { free: false, color: undefined, showDests: true, events: { after: (o, d) => onmove?.(o, d) } },
|
|
premovable: { enabled: false },
|
|
draggable: { enabled: true, showGhost: true },
|
|
drawable: {
|
|
enabled: true,
|
|
visible: true,
|
|
brushes: {
|
|
best: { key: 'best', color: '#15781b', opacity: 0.8, lineWidth: 10 },
|
|
green: { key: 'g', color: '#2d9a4a', opacity: 0.8, lineWidth: 10 },
|
|
red: { key: 'r', color: '#d1231c', opacity: 0.8, lineWidth: 10 },
|
|
blue: { key: 'b', color: '#1f5fae', opacity: 0.8, lineWidth: 10 },
|
|
yellow: { key: 'y', color: '#e6b800', opacity: 0.8, lineWidth: 10 }
|
|
} as any
|
|
}
|
|
});
|
|
return () => api?.destroy();
|
|
});
|
|
|
|
$effect(() => {
|
|
if (!api) return;
|
|
api.set({
|
|
fen,
|
|
orientation,
|
|
lastMove: lastMove as Key[] | undefined,
|
|
check,
|
|
turnColor: (fen.split(' ')[1] === 'w' ? 'white' : 'black') as any,
|
|
movable: movable
|
|
? { color: movable.color, dests: movable.dests as unknown as Map<Key, Key[]>, free: false }
|
|
: { color: undefined, dests: new Map() },
|
|
drawable: { autoShapes: arrow ? [{ orig: arrow[0] as Key, dest: arrow[1] as Key, brush: 'best' }] : [] }
|
|
});
|
|
});
|
|
</script>
|
|
|
|
<div class="board-frame">
|
|
<div bind:this={el} class="board"></div>
|
|
</div>
|
|
|
|
<style>
|
|
.board-frame {
|
|
border: 3px solid var(--board-frame);
|
|
box-shadow: var(--shadow);
|
|
background: var(--board-frame);
|
|
border-radius: var(--radius);
|
|
line-height: 0;
|
|
overflow: hidden;
|
|
}
|
|
.board {
|
|
width: 100%;
|
|
aspect-ratio: 1;
|
|
}
|
|
</style>
|