Skip to content

Commit d918c71

Browse files
Merge pull request #57 from mhakash/feature/persist-settings
Persist sync scrolling and theme states
2 parents 0611723 + e6680ff commit d918c71

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

script.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,30 @@ document.addEventListener("DOMContentLoaded", function () {
7373
const githubImportCancelBtn = document.getElementById("github-import-cancel");
7474
const githubImportSubmitBtn = document.getElementById("github-import-submit");
7575

76+
// ========================================
77+
// GLOBAL STATE (persisted across reloads)
78+
// ========================================
79+
const GLOBAL_STATE_KEY = 'markdownViewerGlobalState';
80+
81+
function loadGlobalState() {
82+
try { return JSON.parse(localStorage.getItem(GLOBAL_STATE_KEY)) || {}; }
83+
catch { return {}; }
84+
}
85+
86+
function saveGlobalState(patch) {
87+
localStorage.setItem(GLOBAL_STATE_KEY, JSON.stringify({ ...loadGlobalState(), ...patch }));
88+
}
89+
7690
// Check dark mode preference first for proper initialization
7791
const prefersDarkMode =
7892
window.matchMedia &&
7993
window.matchMedia("(prefers-color-scheme: dark)").matches;
80-
81-
document.documentElement.setAttribute(
82-
"data-theme",
83-
prefersDarkMode ? "dark" : "light"
84-
);
85-
86-
themeToggle.innerHTML = prefersDarkMode
94+
const savedTheme = loadGlobalState().theme;
95+
const initialTheme = savedTheme ?? (prefersDarkMode ? "dark" : "light");
96+
97+
document.documentElement.setAttribute("data-theme", initialTheme);
98+
99+
themeToggle.innerHTML = initialTheme === "dark"
87100
? '<i class="bi bi-sun"></i>'
88101
: '<i class="bi bi-moon"></i>';
89102

@@ -1508,6 +1521,7 @@ This is a fully client-side application. Your content never leaves your browser
15081521
toggleSyncButton.classList.remove("sync-disabled");
15091522
toggleSyncButton.classList.remove("border-primary");
15101523
}
1524+
saveGlobalState({ syncScrollingEnabled });
15111525
}
15121526

15131527
// View Mode Functions - Story 1.1 & 1.2
@@ -1730,6 +1744,7 @@ This is a fully client-side application. Your content never leaves your browser
17301744
}
17311745

17321746
initTabs();
1747+
if (loadGlobalState().syncScrollingEnabled === false) toggleSyncScrolling();
17331748
updateMobileStats();
17341749

17351750
// Initialize resizer - Story 1.3
@@ -1792,6 +1807,7 @@ This is a fully client-side application. Your content never leaves your browser
17921807
? "light"
17931808
: "dark";
17941809
document.documentElement.setAttribute("data-theme", theme);
1810+
saveGlobalState({ theme });
17951811

17961812
if (theme === "dark") {
17971813
themeToggle.innerHTML = '<i class="bi bi-sun"></i>';

0 commit comments

Comments
 (0)