mirror of
https://github.com/lichess-org/chessground.git
synced 2026-05-26 13:50:53 +00:00
move resize observer to bindDocument to allow unbind on destroy - closes #386
This commit is contained in:
+1
-1
@@ -44,7 +44,7 @@ export function Chessground(element: HTMLElement, config?: Config): Api {
|
|||||||
state.drawable.prevSvgHash = '';
|
state.drawable.prevSvgHash = '';
|
||||||
updateBounds(state);
|
updateBounds(state);
|
||||||
redrawNow(false);
|
redrawNow(false);
|
||||||
events.bindBoard(state, onResize);
|
events.bindBoard(state);
|
||||||
if (!prevUnbind) state.dom.unbind = events.bindDocument(state, onResize);
|
if (!prevUnbind) state.dom.unbind = events.bindDocument(state, onResize);
|
||||||
state.events.insert?.(elements);
|
state.events.insert?.(elements);
|
||||||
return state;
|
return state;
|
||||||
|
|||||||
+13
-5
@@ -8,11 +8,12 @@ import { isRightButton } from './util.js';
|
|||||||
type MouchBind = (e: cg.MouchEvent) => void;
|
type MouchBind = (e: cg.MouchEvent) => void;
|
||||||
type StateMouchBind = (d: State, e: cg.MouchEvent) => void;
|
type StateMouchBind = (d: State, e: cg.MouchEvent) => void;
|
||||||
|
|
||||||
export function bindBoard(s: State, onResize: () => void): void {
|
// binds events to managed nodes,
|
||||||
|
// i.e. nodes that can be created and deleted by chessground.
|
||||||
|
// Unbinding these is not necessary, as they will be garbage collected with the nodes.
|
||||||
|
export function bindBoard(s: State): void {
|
||||||
const boardEl = s.dom.elements.board;
|
const boardEl = s.dom.elements.board;
|
||||||
|
|
||||||
if ('ResizeObserver' in window) new ResizeObserver(onResize).observe(s.dom.elements.wrap);
|
|
||||||
|
|
||||||
if (s.disableContextMenu || s.drawable.enabled) {
|
if (s.disableContextMenu || s.drawable.enabled) {
|
||||||
boardEl.addEventListener('contextmenu', e => e.preventDefault());
|
boardEl.addEventListener('contextmenu', e => e.preventDefault());
|
||||||
}
|
}
|
||||||
@@ -30,13 +31,20 @@ export function bindBoard(s: State, onResize: () => void): void {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns the unbind function
|
// binds events to unmanaged nodes, i.e. the document around chessground,
|
||||||
|
// and the wrap element on which chessground was applied.
|
||||||
|
// returns the unbind function so chessground can clean up on destroy.
|
||||||
export function bindDocument(s: State, onResize: () => void): cg.Unbind {
|
export function bindDocument(s: State, onResize: () => void): cg.Unbind {
|
||||||
const unbinds: cg.Unbind[] = [];
|
const unbinds: cg.Unbind[] = [];
|
||||||
|
|
||||||
|
if ('ResizeObserver' in window) {
|
||||||
|
const ro = new ResizeObserver(onResize);
|
||||||
|
ro.observe(s.dom.elements.wrap);
|
||||||
|
unbinds.push(() => ro.disconnect());
|
||||||
|
}
|
||||||
// Old versions of Edge and Safari do not support ResizeObserver. Send
|
// Old versions of Edge and Safari do not support ResizeObserver. Send
|
||||||
// chessground.resize if a user action has changed the bounds of the board.
|
// chessground.resize if a user action has changed the bounds of the board.
|
||||||
if (!('ResizeObserver' in window)) unbinds.push(unbindable(document.body, 'chessground.resize', onResize));
|
else unbinds.push(unbindable(document.body, 'chessground.resize', onResize));
|
||||||
|
|
||||||
if (!s.viewOnly) {
|
if (!s.viewOnly) {
|
||||||
const onmove = dragOrDraw(s, drag.move, draw.move);
|
const onmove = dragOrDraw(s, drag.move, draw.move);
|
||||||
|
|||||||
Reference in New Issue
Block a user