From b451043d51358aaa8843f7ed51fe2c9547fe332e Mon Sep 17 00:00:00 2001 From: Asher Morgan <59518073+ashermorgan@users.noreply.github.com> Date: Thu, 11 Jul 2024 15:24:38 -0700 Subject: [PATCH] Save custom song lyrics in localStorage --- songs2slides/static/create.css | 4 ++ songs2slides/static/create.js | 55 +++++++++++++++++++++-- songs2slides/templates/create-step-1.html | 2 +- songs2slides/templates/create-step-2.html | 27 +++++------ tests/test_e2e.py | 29 +++++++++--- 5 files changed, 89 insertions(+), 28 deletions(-) diff --git a/songs2slides/static/create.css b/songs2slides/static/create.css index 78880e7..1d8b1e5 100644 --- a/songs2slides/static/create.css +++ b/songs2slides/static/create.css @@ -75,7 +75,11 @@ textarea { .missing summary { color: var(--error); } +summary span { + display: none; +} .missing summary span { + display: inline; float: right; font-weight: bold; } diff --git a/songs2slides/static/create.js b/songs2slides/static/create.js index 6d3a3e1..fe5b135 100644 --- a/songs2slides/static/create.js +++ b/songs2slides/static/create.js @@ -1,11 +1,16 @@ // Global Songs2Slides localStorage prefix const PREFIX = 's2s' +// HTML form +let form = null + // Page load/reload handler addEventListener('pageshow', () => { // Correct page state after returning via browser back button document.getElementById('post-submit').hidden = true + form = document.getElementById('create-form') + if (STEP === 1) { // Load songs for (let row of document.querySelectorAll('tbody tr')) { @@ -18,9 +23,10 @@ addEventListener('pageshow', () => { raw_song.children[1].children[0].value = song.title raw_song.children[2].children[0].value = song.artist } + } else if (STEP === 2) { + load_lyrics() } else if (STEP === 3) { // Load settings - const form = document.getElementById('create-form') form['title-slides'].checked = storage_get('title-slides', true) form['blank-slides'].checked = storage_get('blank-slides', true) form['output-type'].value = storage_get('output-type', 'html') @@ -32,9 +38,10 @@ addEventListener('submit', () => { // Show loading spinner document.getElementById('post-submit').hidden = false - if (STEP === 3) { + if (STEP === 2) { + save_lyrics() + } else if (STEP === 3) { // Save settings - const form = document.getElementById('create-form') storage_set('title-slides', form['title-slides'].checked) storage_set('blank-slides', form['blank-slides'].checked) storage_set('output-type', form['output-type'].value) @@ -86,7 +93,47 @@ function save_songs() { storage_set('songs', songs) } -// Step 3 helper functions +// Step 2 functions +function get_song_key(title, artist) { + return 'lyrics-' + artist.toLowerCase().replaceAll(' ', '-') + + '-' + title.toLowerCase().replaceAll(' ', '-') +} + +function save_lyrics() { + for (let i = 1; `title-${i}` in form; i++) { + const title = form[`title-${i}`].value + const artist = form[`artist-${i}`].value + const lyrics = form[`lyrics-${i}`].value + const key = get_song_key(title, artist) + storage_set(key, lyrics) + } +} + +function load_lyrics() { + songs = document.getElementsByTagName('details') + for (let i = 1; `title-${i}` in form; i++) { + const title = form[`title-${i}`].value + const artist = form[`artist-${i}`].value + const key = get_song_key(title, artist) + const saved_lyrics = storage_get(key, '') + if (saved_lyrics !== '') { + form[`lyrics-${i}`].value = saved_lyrics + songs[i - 1].classList.remove('missing') + songs[i - 1].open = false + } + } + + // Update missing label + const number = document.getElementsByClassName('missing').length + document.getElementById('missing-count').textContent = number + if (number === 0) { + document.getElementById('missing-message').hidden = true + } else { + document.getElementById('missing-message').hidden = false + } +} + +// Local storage helper functions function storage_get(key, default_value) { try { value = JSON.parse(localStorage.getItem(`${PREFIX}.${key}`)) diff --git a/songs2slides/templates/create-step-1.html b/songs2slides/templates/create-step-1.html index e529284..58922aa 100644 --- a/songs2slides/templates/create-step-1.html +++ b/songs2slides/templates/create-step-1.html @@ -8,7 +8,7 @@ {% endblock head %} {% block main %} -