From a2d8d975ec056f4191ce97c8b899f97afa0cb17c Mon Sep 17 00:00:00 2001 From: Ayo Date: Thu, 2 Jul 2026 21:10:53 +0200 Subject: [PATCH] refactor: separate globalEventHandlers from cell-specific event handlers --- lib/mnswpr.js | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/mnswpr.js b/lib/mnswpr.js index b1247c3..276c348 100644 --- a/lib/mnswpr.js +++ b/lib/mnswpr.js @@ -101,6 +101,7 @@ const Minesweeper = function(appId, version, hooks = undefined) { appElement.innerHTML = '' appElement.append(headingElement, gameBoard) } + initializeGlobalEventHandlers() generateGrid({ initial: true }) } @@ -277,14 +278,12 @@ const Minesweeper = function(appId, version, hooks = undefined) { } - function initializeEventHandlers(_cell) { - - let cell = _cell - skip = false - skipCondition = false - - resetMouseEventFlags() - + /** + * Wire the document/window/grid-level input handlers. These don't depend on + * any individual cell, so they're set up once instead of being reassigned + * for every cell during grid generation. + */ + function initializeGlobalEventHandlers() { document.onkeydown = function(e) { if (e.keyCode == 32 || e.keyCode == 113) { generateGrid() @@ -304,10 +303,13 @@ const Minesweeper = function(appId, version, hooks = undefined) { grid.onmouseleave = function() { removeHighlights() } + document.oncontextmenu = () => false + document.onmouseup = function() { resetMouseEventFlags() } + document.onmousedown = function(e) { isMobile = false switch (e.button) { @@ -316,6 +318,15 @@ const Minesweeper = function(appId, version, hooks = undefined) { case 2: isRight = true; break } } + } + + function initializeEventHandlers(_cell) { + + let cell = _cell + skip = false + skipCondition = false + + resetMouseEventFlags() // Set grid status to active on first click cell.onmouseup = function(e) {