Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions ui/analyse/src/view/nvuiView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ export function renderNvui(ctx: AnalyseNvuiContext): VNode {
hl('h2', i18n.site.board),
hl(
'div.board',
{ hook: { insert: el => boardEventsHook(ctx, el.elm as HTMLElement) } },
{
hook: {
insert: el => boardEventsHook(ctx, el.elm as HTMLElement),
update: (_, vnode) => boardEventsHook(ctx, vnode.elm as HTMLElement),
},
},
renderBoard(
ctrl.chessground.state.pieces,
ctrl.data.game.variant.key === 'racingKings' ? 'white' : ctrl.bottomColor(),
Expand Down Expand Up @@ -224,13 +229,18 @@ function boardEventsHook(
el: HTMLElement,
): void {
const $board = $(el);
const $buttons = $board.find('button');
// Remove old handlers before rebinding (important on re-render)
$board.off('.nvui');
const steps = () => ctrl.tree.getNodeList(ctrl.path);
const fenSteps = () => steps().map(step => step.fen);
const opponentColor = () => (ctrl.node.ply % 2 === 0 ? 'black' : 'white');
$buttons.on('blur', leaveSquareHandler($buttons));
$buttons.on('click', selectionHandler(opponentColor));
$buttons.on('keydown', (e: KeyboardEvent) => {
$board.on('blur', 'button', e => {
leaveSquareHandler($board.find('button'))(e);
});
$board.on('click', 'button', e => {
selectionHandler(opponentColor)(e);
});
$board.on('keydown', 'button', (e: KeyboardEvent) => {
if (e.shiftKey && e.key.match(/^[ad]$/i)) jumpMoveOrLine(ctrl)(e);
else if (e.key.match(/^x$/i))
scanDirectionsHandler(ctrl.bottomColor(), ctrl.chessground.state.pieces, moveStyle.get())(e);
Expand Down
26 changes: 19 additions & 7 deletions ui/puzzle/src/view/nvuiView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ export function renderNvui({
ground,
el.elm as HTMLElement,
),
update: (_, vnode) =>
boardEventsHook(
{
ctrl,
notify,
moveStyle,
pieceStyle,
prefixStyle,
positionStyle,
boardStyle,
},
ground,
vnode.elm as HTMLElement,
),
},
},

Expand Down Expand Up @@ -171,16 +185,14 @@ export function renderNvui({
function boardEventsHook(ctx: PuzzleNvuiContext, ground: Api, el: HTMLElement): void {
const { ctrl, moveStyle, pieceStyle, prefixStyle, notify } = ctx;
const $board = $(el);
const $buttons = $board.find('button');
// Remove old handlers before rebinding (important on re-render)
$board.off('.nvui');
const steps = ctrl.tree.getNodeList(ctrl.path);
const fenSteps = () => steps.map(step => step.fen);

$buttons.on('blur', nv.leaveSquareHandler($buttons));
$buttons.on(
'click',
nv.selectionHandler(() => opposite(ctrl.pov)),
);
$buttons.on('keydown', (e: KeyboardEvent) => {
$board.on('blur', 'button', e => nv.leaveSquareHandler($board.find('button'))(e));
$board.on('click', 'button', e => nv.selectionHandler(() => opposite(ctrl.pov))(e));
$board.on('keydown', 'button', (e: KeyboardEvent) => {
if (e.shiftKey && e.key.match(/^[ad]$/i)) nextOrPrev(ctrl)(e);
else if (e.key.match(/^x$/i))
scanDirectionsHandler(
Expand Down
Loading