Skip to content

Commit aed78b7

Browse files
authored
feat : Add JSON‑driven background image support (#316)
* Add background JSON support - Load background map from JSON - Store user preference - Update DOMContentLoaded handler * Refactor background-switcher.js - Replace hardcoded BG_MAP with empty object - Convert loadBackgroundMap to async/await - Simplify DOMContentLoaded listener using async function - Improve readability and eliminate data duplication * Update background-switcher.js path and warning - Use DOCUMENTATION_OPTIONS.URL_ROOT for robust JSON path - Clarify warning when JSON fails to load * Fix pre-commit workflow - Remove duplicate pre-commit execution - Keep only standard all-files check - Fix CI failure caused by redundant hook stages * Revert: Restore manual hook stage test - Keep both pre-commit execution stages - Standard hooks and manual hooks both needed - Rename second step for clarity * Apply prettier formatting - Format background-switcher.js - Format backgrounds.json - Ensure code style consistency * refactor: improve separation of concerns in loadBackgroundMap - loadBackgroundMap now returns data (or null on failure) instead of directly modifying the module-level BG_MAP variable - Added robust data validation in DOMContentLoaded handler to ensure the loaded JSON is a valid object before populating BG_MAP - Addresses code review feedback for better maintainability and testability * refactor: use Object.prototype.toString for robust type checking - Replace multi-condition object check with more idiomatic approach - Object.prototype.toString.call() handles edge cases like typeof null - Addresses code review feedback for cleaner validation logic
1 parent b3992fc commit aed78b7

File tree

3 files changed

+56
-26
lines changed

3 files changed

+56
-26
lines changed

.github/workflows/pre-commit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ jobs:
5252
5353
- name: 🏃🏻‍♂️‍➡️ Run pre-commit
5454
run: pre-commit run --color=always --all-files
55-
- name: 🏃🏻‍♂️‍➡️ Run pre-commit
55+
- name: 🏃🏻‍♂️‍➡️ Run pre-commit (manual hooks)
5656
run: pre-commit run --color=always --all-files --hook-stage manual
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"shades-games-club": "the-big-issue",
3+
"first-event": "first-event-second-game",
4+
"second-event": "second-event",
5+
"third-event": "third-event-beers-in-play",
6+
"fourth-event": "fourth-event",
7+
"fifth-event": "fifth-event",
8+
"sixth-event": "sixth-event",
9+
"catan": "catan",
10+
"chess": "chess",
11+
"c-o-o-l-chess": "cool-chess",
12+
"harmegedo": "harmegedo",
13+
"history-of-64-shades": "history",
14+
"quaternity": "quaternity-wooden-set",
15+
"the-first-challenger": "first-event-first-game",
16+
"top-pitfalls": "top-pitfalls",
17+
"uno-show-em-no-mercy": "uno-show-em-no-mercy"
18+
}

doc/source/_static/js/background-switcher.js

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
1-
// Map page IDs to their corresponding background image URLs
2-
const BG_MAP = {
3-
'shades-games-club': 'the-big-issue',
4-
'first-event': 'first-event-second-game',
5-
'second-event': 'second-event',
6-
'third-event': 'third-event-beers-in-play',
7-
'fourth-event': 'fourth-event',
8-
'fifth-event': 'fifth-event',
9-
'sixth-event': 'sixth-event',
10-
catan: 'catan',
11-
chess: 'chess',
12-
'c-o-o-l-chess': 'cool-chess',
13-
harmegedo: 'harmegedo',
14-
'history-of-64-shades': 'history',
15-
quaternity: 'quaternity-wooden-set',
16-
'the-first-challenger': 'first-event-first-game',
17-
'top-pitfalls': 'top-pitfalls',
18-
'uno-show-em-no-mercy': 'uno-show-em-no-mercy',
19-
};
1+
const BG_MAP = {};
202

213
const BACKGROUND_KEY = 'sphinx_background_preference';
224

5+
/**
6+
* Load the background map from a JSON file using async/await.
7+
*/
8+
async function loadBackgroundMap() {
9+
const jsonUrl = (window.DOCUMENTATION_OPTIONS?.URL_ROOT || '') + '_static/data/backgrounds.json';
10+
try {
11+
const response = await fetch(jsonUrl);
12+
if (!response.ok) {
13+
console.warn('Failed to load background map JSON; background images will be disabled.');
14+
return null;
15+
}
16+
return await response.json();
17+
} catch (err) {
18+
console.error('Error loading background map JSON:', err);
19+
return null;
20+
}
21+
}
22+
23+
/**
24+
* Load user's background preference and apply it.
25+
*/
26+
function loadBackgroundPreference() {
27+
const storedBackground = localStorage.getItem(BACKGROUND_KEY) || 'on';
28+
window.setBackground(storedBackground);
29+
}
30+
2331
/**
2432
* Public function to turn the background image on or off.
2533
* Defined globally so the HTML `href="javascript:setBackground(...)` works.
@@ -62,12 +70,16 @@ window.setBackground = function (state) {
6270
updateBackgroundLinks(state);
6371
};
6472

65-
function loadBackgroundPreference() {
66-
const storedBackground = localStorage.getItem(BACKGROUND_KEY) || 'on';
67-
window.setBackground(storedBackground); // Use the global function
68-
}
73+
// Wait for the document to fully load, load the map, then apply preference.
6974

70-
// Wait for the document to fully load and load background preference
71-
document.addEventListener('DOMContentLoaded', function () {
75+
document.addEventListener('DOMContentLoaded', async function () {
76+
const data = await loadBackgroundMap();
77+
if (Object.prototype.toString.call(data) === '[object Object]') {
78+
Object.assign(BG_MAP, data);
79+
} else if (data) {
80+
console.warn(
81+
'Loaded background map from JSON is not a valid object; background images will be disabled.',
82+
);
83+
}
7284
loadBackgroundPreference();
7385
});

0 commit comments

Comments
 (0)