refactor: separate globalEventHandlers from cell-specific event handlers

This commit is contained in:
ayo 2026-07-02 21:10:53 +02:00
parent 749b47fe40
commit a2d8d975ec

View file

@ -101,6 +101,7 @@ const Minesweeper = function(appId, version, hooks = undefined) {
appElement.innerHTML = '' appElement.innerHTML = ''
appElement.append(headingElement, gameBoard) appElement.append(headingElement, gameBoard)
} }
initializeGlobalEventHandlers()
generateGrid({ initial: true }) generateGrid({ initial: true })
} }
@ -277,14 +278,12 @@ const Minesweeper = function(appId, version, hooks = undefined) {
} }
function initializeEventHandlers(_cell) { /**
* Wire the document/window/grid-level input handlers. These don't depend on
let cell = _cell * any individual cell, so they're set up once instead of being reassigned
skip = false * for every cell during grid generation.
skipCondition = false */
function initializeGlobalEventHandlers() {
resetMouseEventFlags()
document.onkeydown = function(e) { document.onkeydown = function(e) {
if (e.keyCode == 32 || e.keyCode == 113) { if (e.keyCode == 32 || e.keyCode == 113) {
generateGrid() generateGrid()
@ -304,10 +303,13 @@ const Minesweeper = function(appId, version, hooks = undefined) {
grid.onmouseleave = function() { grid.onmouseleave = function() {
removeHighlights() removeHighlights()
} }
document.oncontextmenu = () => false document.oncontextmenu = () => false
document.onmouseup = function() { document.onmouseup = function() {
resetMouseEventFlags() resetMouseEventFlags()
} }
document.onmousedown = function(e) { document.onmousedown = function(e) {
isMobile = false isMobile = false
switch (e.button) { switch (e.button) {
@ -316,6 +318,15 @@ const Minesweeper = function(appId, version, hooks = undefined) {
case 2: isRight = true; break case 2: isRight = true; break
} }
} }
}
function initializeEventHandlers(_cell) {
let cell = _cell
skip = false
skipCondition = false
resetMouseEventFlags()
// Set grid status to active on first click // Set grid status to active on first click
cell.onmouseup = function(e) { cell.onmouseup = function(e) {