songs2slides

A tool that automatically finds song lyrics and creates lyric slideshows
git clone https://git.ashermorgan.net/songs2slides/
Log | Files | Refs | README

commit 465fafb47183a1eb40644ae0c6a0fccb6abe0762
parent 1bba3c4a3aedd95e07a4c3f871642d8c279b16b2
Author: Asher Morgan <59518073+ashermorgan@users.noreply.github.com>
Date:   Fri,  5 Apr 2024 15:43:04 -0700

Save basic settings in localStorage

Diffstat:
Asongs2slides/static/create-step-1.js | 32++++++++++++++++++++++++++++++++
Asongs2slides/static/create-step-2.js | 41+++++++++++++++++++++++++++++++++++++++++
Dsongs2slides/static/create.js | 45---------------------------------------------
Msongs2slides/templates/create-step-1.html | 2+-
Msongs2slides/templates/create-step-2.html | 2+-
5 files changed, 75 insertions(+), 47 deletions(-)

diff --git a/songs2slides/static/create-step-1.js b/songs2slides/static/create-step-1.js @@ -0,0 +1,32 @@ +addEventListener('submit', () => { + // Show loading spinner + document.getElementById('post-submit').hidden = false +}) + +addEventListener('pageshow', () => { + // Correct page state after returning via browser back button + document.getElementById('post-submit').hidden = true + document.getElementById('step-1').hidden = false +}) + +function add_song() { + let row = document.getElementById('row-template').content.children[0].cloneNode(true) + document.getElementById('songs').appendChild(row) + renumber_songs() +} + +function remove_song(n) { + document.getElementsByTagName('tr')[n].remove() + renumber_songs() + if (document.getElementsByTagName('tr').length === 1) add_song() +} + +function renumber_songs() { + const songs = document.getElementsByTagName('tr') + for (let i = 1; i < songs.length; i++) { + songs[i].children[0].textContent = `${i}.` + songs[i].children[1].children[0].name = `title-${i}` + songs[i].children[2].children[0].name = `artist-${i}` + songs[i].children[3].children[0].onclick = () => remove_song(i) + } +} diff --git a/songs2slides/static/create-step-2.js b/songs2slides/static/create-step-2.js @@ -0,0 +1,41 @@ +addEventListener('submit', () => { + // Save settings + const form = document.getElementById('step-2') + storage_set('title-slides', form['title-slides'].checked) + storage_set('blank-slides', form['blank-slides'].checked) + storage_set('output-type', form['output-type'].value) + + // Redirect to post download message + if (form['output-type'].value === 'pptx') { + setTimeout(() => { + // On Chrome, redirecting after a form submission doesn't work + // unless setTimeout is used + // (REDIRECT_URL set in create-step-2.html template) + window.location.href = REDIRECT_URL + }, 100) + } +}) + +addEventListener('pageshow', () => { + // Load settings + const form = document.getElementById('step-2') + 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') +}) + +// Global Songs2Slides localStorage prefix +const PREFIX = 's2s' + +function storage_get(key, default_value) { + try { + value = JSON.parse(localStorage.getItem(`${PREFIX}.${key}`)) + } catch { + return clonedDefault + } + return value === null ? default_value : value +} + +function storage_set(key, value) { + localStorage.setItem(`${PREFIX}.${key}`, JSON.stringify(value)) +} diff --git a/songs2slides/static/create.js b/songs2slides/static/create.js @@ -1,45 +0,0 @@ -addEventListener('submit', () => { - if (document.getElementById('step-1')) { - // Show step 1 spinner - document.getElementById('post-submit').hidden = false - } else if (document.querySelector('input[value=pptx]').checked) { - // Redirect to post download message - // (REDIRECT_URL set in create-step-2.html template) - setTimeout(() => { - // On Chrome, redirecting after a form submission doesn't work - // unless setTimeout is used - window.location.href = REDIRECT_URL - }, 100) - } -}) - -addEventListener('pageshow', () => { - // Correct page state after returning via browser back button - if (document.getElementById('step-1')) { - document.getElementById('post-submit').hidden = true - document.getElementById('step-1').hidden = false - } -}) - -/* step 1 functions */ -function add_song() { - let row = document.getElementById('row-template').content.children[0].cloneNode(true) - document.getElementById('songs').appendChild(row) - renumber_songs() -} - -function remove_song(n) { - document.getElementsByTagName('tr')[n].remove() - renumber_songs() - if (document.getElementsByTagName('tr').length === 1) add_song() -} - -function renumber_songs() { - const songs = document.getElementsByTagName('tr') - for (let i = 1; i < songs.length; i++) { - songs[i].children[0].textContent = `${i}.` - songs[i].children[1].children[0].name = `title-${i}` - songs[i].children[2].children[0].name = `artist-${i}` - songs[i].children[3].children[0].onclick = () => remove_song(i) - } -} diff --git a/songs2slides/templates/create-step-1.html b/songs2slides/templates/create-step-1.html @@ -2,7 +2,7 @@ {% block head %} <link rel="stylesheet" href="{{ url_for('static', filename='create.css') }}"/> -<script src="{{ url_for('static', filename='create.js') }}"></script> +<script src="{{ url_for('static', filename='create-step-1.js') }}"></script> {% endblock head %} {% block main %} diff --git a/songs2slides/templates/create-step-2.html b/songs2slides/templates/create-step-2.html @@ -2,7 +2,7 @@ {% block head %} <link rel="stylesheet" href="{{ url_for('static', filename='create.css') }}"/> -<script src="{{ url_for('static', filename='create.js') }}"></script> +<script src="{{ url_for('static', filename='create-step-2.js') }}"></script> {% endblock head %} {% set format_hint =