From fd2356a438987b5ad77b24d0de10e8642fa1d642 Mon Sep 17 00:00:00 2001 From: Tom Keefe <8655118+MisterKeefe@users.noreply.github.com> Date: Sun, 26 Nov 2023 13:24:45 +0000 Subject: [PATCH] query string parsing trick --- src/lib/frettings.ts | 5 --- src/lib/music/relativeChord.ts | 4 +++ src/routes/+page.svelte | 59 ++++++++++++++++++++++++------ src/routes/progressionStore.ts | 66 +++++++++++++++++++++++++++++----- svelte.config.js | 5 +-- 5 files changed, 114 insertions(+), 25 deletions(-) diff --git a/src/lib/frettings.ts b/src/lib/frettings.ts index 1d0948d..d13b2de 100644 --- a/src/lib/frettings.ts +++ b/src/lib/frettings.ts @@ -7,11 +7,6 @@ import { unflatten, type Pitch, type PitchWithFlats, type Quality } from './musi * pointing at arrays of frettings for that pitch and chord variant. * This is a massive data entry task but also not that bad. GLHF! */ - -export const getFrettingForChord = ({ tonic, quality }: Chord, index: number = 0): number[] => { - return frettings[unflatten(tonic)][quality][index]; -}; - export const getFrettingsForChord = ({ tonic, quality }: Chord): number[][] => { return frettings[unflatten(tonic)][quality]; }; diff --git a/src/lib/music/relativeChord.ts b/src/lib/music/relativeChord.ts index 384f478..493ce0d 100644 --- a/src/lib/music/relativeChord.ts +++ b/src/lib/music/relativeChord.ts @@ -139,3 +139,7 @@ export const parseRelativeChord = (intervalString: string): RelativeChord | null export const progression = (strings: TemplateStringsArray, ...values: any): RelativeChord[] => { return strings[0].split(/[\s-]/).map(parseRelativeChord) as RelativeChord[]; }; + +export const progressionFromString = (string: string): RelativeChord[] => { + return string.split(/[\s-]/).map(parseRelativeChord) as RelativeChord[]; +}; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index c493c8e..81f4154 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -6,6 +6,8 @@ progression, chords, randomizeApp, + randomizeTonic, + randomizeProgression, setTonic, previousProgression, nextProgression @@ -22,8 +24,21 @@ - Ukulele Chord Progression Generator - + Ukulele Chord Progressions Toy + + + + + + + + + + + + + + @@ -36,7 +51,10 @@ {/each} - Progression + + progression + randomize + - Key + + key + randomize + {#each displayPitches as pitch} ('C'); const progressionIndex = writable(0); -const progression = derived( - [progressionIndex], - ([$progressionIndex]) => progressions[$progressionIndex] -); +/** + * Update progression based on index... unless there's a location hash, + * in which case use that. + */ +const progression = derived([progressionIndex], ([$progressionIndex]) => { + return hasProgressionInUrlHash() ? progressionFromURLHash() : progressions[$progressionIndex]; +}); const chords = derived([tonic, progression], ([$tonic, $progression]) => $progression.map((interval) => intervalToChord($tonic, interval)) @@ -46,7 +59,16 @@ const setTonic = (newTonic: Pitch) => { }; const randomizeApp = () => { + randomizeTonic(); + randomizeProgression(); +}; + +const randomizeTonic = () => { tonic.update((current) => chooseWithout(pitchesWithFlats, current)); +}; + +const randomizeProgression = () => { + resetURLHashProgression(); progressionIndex.update((current) => chooseWithout( progressions.map((_, index) => index), @@ -56,13 +78,39 @@ const randomizeApp = () => { }; const previousProgression = () => { + resetURLHashProgression(); progressionIndex.update((current) => (progressions.length + current - 1) % progressions.length); }; const nextProgression = () => { + resetURLHashProgression(); progressionIndex.update((current) => (progressions.length + current + 1) % progressions.length); }; +// URL hash utilities + +const hasProgressionInUrlHash = () => { + if (!(browser && window.location.hash)) { + return false; + } + + try { + const _ = $`${window.location.hash.substring(1)}`; + } catch (err) { + return false; + } + + return true; +}; + +const progressionFromURLHash = () => { + return progressionFromString(window.location.hash.substring(1)); +}; + +const resetURLHashProgression = () => { + window.location.hash = ''; +}; + export { tonic, progression, @@ -70,6 +118,8 @@ export { frettings, setTonic, randomizeApp, + randomizeTonic, + randomizeProgression, previousProgression, nextProgression }; diff --git a/svelte.config.js b/svelte.config.js index 5ecf476..0396584 100644 --- a/svelte.config.js +++ b/svelte.config.js @@ -1,4 +1,5 @@ -import adapter from '@sveltejs/adapter-static'; +import staticAdapter from '@sveltejs/adapter-static'; +import autoAdapter from '@sveltejs/adapter-auto'; import { vitePreprocess } from '@sveltejs/kit/vite'; /** @type {import('@sveltejs/kit').Config} */ @@ -8,7 +9,7 @@ const config = { preprocess: vitePreprocess(), kit: { - adapter: adapter(), + adapter: process.env.NODE_ENV === 'production' ? staticAdapter() : autoAdapter(), paths: { base: process.env.NODE_ENV === 'production' ? '/ukulele-progressions' : '' }