diff options
| author | elioat <{ID}+{username}@users.noreply.github.com> | 2025-09-30 09:34:59 -0400 |
|---|---|---|
| committer | elioat <{ID}+{username}@users.noreply.github.com> | 2025-09-30 09:34:59 -0400 |
| commit | dd79baacf8dcaaea21d3b11c104677e344f49c4d (patch) | |
| tree | 6ded52dd1811ffd9841b3155c8f9dbc301c1f5ac /html/rtb/public/app.js | |
| parent | f1c601a66df41cb4f31f8a521df0f0b2ac88169b (diff) | |
| download | tour-dd79baacf8dcaaea21d3b11c104677e344f49c4d.tar.gz | |
*
Diffstat (limited to 'html/rtb/public/app.js')
| -rw-r--r-- | html/rtb/public/app.js | 90 |
1 files changed, 81 insertions, 9 deletions
diff --git a/html/rtb/public/app.js b/html/rtb/public/app.js index 4d682b4..db91289 100644 --- a/html/rtb/public/app.js +++ b/html/rtb/public/app.js @@ -12,7 +12,8 @@ function createInitialModel() { currentSentence: 0, error: null, chunkCache: {}, - cacheAccessOrder: [] + cacheAccessOrder: [], + darkMode: null // null = auto, true = dark, false = light }; } @@ -25,7 +26,8 @@ function loadState() { ...createInitialModel(), readingProgress: state.readingProgress || {}, currentBookId: state.currentBookId || null, - currentSentence: state.currentSentence || 0 + currentSentence: state.currentSentence || 0, + darkMode: state.darkMode !== undefined ? state.darkMode : null }; } } catch (e) { @@ -39,7 +41,8 @@ function saveState(model) { const toSave = { readingProgress: model.readingProgress, currentBookId: model.currentBookId, - currentSentence: model.currentSentence + currentSentence: model.currentSentence, + darkMode: model.darkMode }; localStorage.setItem(STORAGE_KEY, JSON.stringify(toSave)); } catch (e) { @@ -63,6 +66,33 @@ function getBook(model, bookId) { return model.books.find(b => b.id === bookId); } +function getSortableTitle(title) { + // Remove leading articles for sorting + const articles = ['the ', 'a ', 'an ']; + let sortableTitle = title.toLowerCase(); + + for (const article of articles) { + if (sortableTitle.startsWith(article)) { + sortableTitle = sortableTitle.substring(article.length); + break; + } + } + + return sortableTitle; +} + +function getEffectiveDarkMode(model) { + if (model.darkMode !== null) { + return model.darkMode; + } + return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches; +} + +function applyDarkMode(model) { + const isDark = getEffectiveDarkMode(model); + document.documentElement.classList.toggle('dark-mode', isDark); +} + function updateCache(model, chunkIndex, sentences) { const newCache = { ...model.chunkCache, [chunkIndex]: sentences }; const newOrder = model.cacheAccessOrder.filter(i => i !== chunkIndex); @@ -255,6 +285,15 @@ async function update(msg, model) { }; } + case 'SetDarkMode': { + const newModel = { + ...model, + darkMode: msg.darkMode + }; + saveState(newModel); + return newModel; + } + default: return model; } @@ -262,6 +301,7 @@ async function update(msg, model) { function render(model) { const app = document.getElementById('app'); + applyDarkMode(model); if (model.view === 'loading') { app.innerHTML = '<div class="loading">Loading...</div>'; @@ -269,7 +309,13 @@ function render(model) { } if (model.view === 'selecting') { - const bookItems = model.books.map(book => { + const sortedBooks = [...model.books].sort((a, b) => { + const titleA = getSortableTitle(a.title); + const titleB = getSortableTitle(b.title); + return titleA.localeCompare(titleB); + }); + + const bookItems = sortedBooks.map(book => { const progress = model.readingProgress[book.id]; const percentage = progress ? Math.round((progress.currentSentence / book.totalSentences) * 100) @@ -285,9 +331,17 @@ function render(model) { }).join(''); app.innerHTML = ` - <h1>Read the Book</h1> + <div class="header"> + <h1>Read the Book</h1> + <select id="dark-mode-select" class="dark-mode-select" title="Choose dark mode preference"> + <option value="auto" ${model.darkMode === null ? 'selected' : ''}>🌓 Auto</option> + <option value="light" ${model.darkMode === false ? 'selected' : ''}>☀️ Light</option> + <option value="dark" ${model.darkMode === true ? 'selected' : ''}>🌙 Dark</option> + </select> + </div> ${model.error ? `<div class="error">${model.error}</div>` : ''} <ul class="book-list">${bookItems}</ul> + <footer><p><a href="about.html">About</a></p></footer> `; document.querySelectorAll('.book-item').forEach(item => { @@ -296,6 +350,15 @@ function render(model) { dispatch({ type: 'SelectBook', bookId }); }); }); + + const darkModeSelect = document.getElementById('dark-mode-select'); + if (darkModeSelect) { + darkModeSelect.addEventListener('change', (e) => { + const value = e.target.value; + const newDarkMode = value === 'auto' ? null : value === 'dark'; + dispatch({ type: 'SetDarkMode', darkMode: newDarkMode }); + }); + } return; } @@ -321,7 +384,7 @@ function render(model) { ${book.title} — ${progress}% complete </div> <button class="secondary" id="select-another" style="margin-top: 2rem;"> - Choose Another Book + Choose a Different Book </button> </div> `; @@ -350,10 +413,10 @@ function render(model) { app.innerHTML = ` <div class="completed-view"> - <h2>Congratulations!</h2> - <p>You've completed <em>${book ? book.title : 'this book'}</em></p> + <h2>The End!</h2> + <p>You've finished <em>${book ? book.title : 'this book'}</em></p> <button id="select-another" style="margin-top: 2rem;"> - Choose Another Book + Choose A New Book </button> </div> `; @@ -371,4 +434,13 @@ async function dispatch(msg) { render(currentModel); } +// Listen for system preference changes +if (window.matchMedia) { + window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => { + if (currentModel.darkMode === null) { + applyDarkMode(currentModel); + } + }); +} + dispatch({ type: 'Initialize' });
\ No newline at end of file |