From c5168937c22140904c9592f44c736d4ac96a62e9 Mon Sep 17 00:00:00 2001 From: udu3324 Date: Wed, 15 Jan 2025 22:25:12 -0600 Subject: [PATCH] use db.js for storage/lookup --- src/lib/db.js | 80 ++++++++++++++++++----- src/routes/+layout.svelte | 27 -------- src/routes/Footer.svelte | 16 +---- src/routes/editor/+page.svelte | 6 +- src/routes/editor/BottomBar.svelte | 47 ++++--------- src/routes/editor/TopBar.svelte | 17 +---- src/routes/editor/TrackContextMenu.svelte | 24 ++++--- src/routes/editor/Tracks.svelte | 28 ++------ 8 files changed, 104 insertions(+), 141 deletions(-) diff --git a/src/lib/db.js b/src/lib/db.js index 9c3cd67..21d2d1f 100644 --- a/src/lib/db.js +++ b/src/lib/db.js @@ -1,6 +1,11 @@ -import Dexie from 'dexie'; +import Dexie from 'dexie' +import { writable } from 'svelte/store' -export const db = new Dexie('webbase') +const db = new Dexie('webbase') + +const darkMode = writable(false) +const playSnap = writable(false) +const analysisEnterAction = writable("flag") db.version(1).stores({ load: 'projectID', @@ -16,18 +21,59 @@ db.on('populate', () => { }) }) -export async function setDarkMode(enable) { - try { - await db.preference.update(1, { darkMode: enable }) - - // Update the document's class - const html = document.documentElement - if (enable) { - html.classList.add('dark') - } else { - html.classList.remove('dark') - } - } catch (error) { - console.error('Failed to set dark mode:', error) - } -} \ No newline at end of file +db.on('ready', async () => { + //fetch preferences for svelte components + await db.preference.get(1).then(pref => { + darkMode.set(pref.darkMode) + playSnap.set(pref.playSnap) + analysisEnterAction.set(pref.analysisEnterAction) + }).catch(() => { + console.log("error loading preferences! might be missing. trying to create it...") + + //worse case scenario if db is very old + db.preference.add({ + darkMode: false, + playSnap: true, + analysisEnterAction: "flag" + }).then(() => { + console.log("sucessfully created preference db") + }).catch(() => { + console.log("could not create preference db!!!! SECOND FAIL") + }) + }) + + //subscribe to stores to save back to db + darkMode.subscribe((bool) => { + console.log("updating preference db") + + db.preference.update(1, { darkMode: bool }).then(() => { + // Update the document's class + const html = document.documentElement + if (bool) { + html.classList.add('dark') + } else { + html.classList.remove('dark') + } + }).catch((error) => { + console.error('Failed to set dark mode:', error) + }) + }) + + playSnap.subscribe((bool) => { + console.log("updating preference db") + + db.preference.update(1, { playSnap: bool }).catch((error) => { + console.error('Failed to set play snap:', error) + }) + }) + + analysisEnterAction.subscribe((str) => { + console.log("updating preference db") + + db.preference.update(1, { analysisEnterAction: str }).catch((error) => { + console.error('Failed to set analysis enter action:', error) + }) + }) +}) + +export { db, darkMode, playSnap, analysisEnterAction } \ No newline at end of file diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 6303691..f07c818 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,7 +1,4 @@ {@render children()} diff --git a/src/routes/Footer.svelte b/src/routes/Footer.svelte index 94ece36..ce1f0ea 100644 --- a/src/routes/Footer.svelte +++ b/src/routes/Footer.svelte @@ -1,27 +1,15 @@
diff --git a/src/routes/editor/+page.svelte b/src/routes/editor/+page.svelte index ddf7e85..cdcfa4b 100644 --- a/src/routes/editor/+page.svelte +++ b/src/routes/editor/+page.svelte @@ -25,8 +25,6 @@ let volume let volumeSet = false let enterAction - - let centerPlayhead //for loader let dataTable @@ -151,8 +149,8 @@
- - + + diff --git a/src/routes/editor/BottomBar.svelte b/src/routes/editor/BottomBar.svelte index ce75763..37d47aa 100644 --- a/src/routes/editor/BottomBar.svelte +++ b/src/routes/editor/BottomBar.svelte @@ -1,13 +1,10 @@ - +
- diff --git a/src/routes/editor/Tracks.svelte b/src/routes/editor/Tracks.svelte index 9eb23d2..01b1d05 100644 --- a/src/routes/editor/Tracks.svelte +++ b/src/routes/editor/Tracks.svelte @@ -4,7 +4,7 @@ import { onMount } from "svelte" import { resolution, tracksHeight } from "$lib/editor.js" - import { db } from "$lib/db.js" + import { playSnap } from "$lib/db.js" import TrackContextMenu from "./TrackContextMenu.svelte" import TrackVisualizer from "./TrackVisualizer.svelte" @@ -18,11 +18,10 @@ export let loading = true - export let centerPlayhead - // biome-ignore lint/style/useConst: it isnt actually constant let note = "" let trackContextMenu + // biome-ignore lint/style/useConst: let trackMenuHidden = "hidden" let pollingTrack @@ -58,15 +57,9 @@ } async function checkForSnap() { - const pref = await db.preference.get(1) - - if (pref.playSnap) { + if ($playSnap) { //toggle it off - db.preference.update(1, { playSnap: false }).then(() => { - console.log("unsnapped playhead sucessfully") - }) - - centerPlayhead = false + playSnap.set(false) } else { trackContextMenu.closeContext() } @@ -86,15 +79,6 @@ console.log("secondsPerBeat", secondsPerBeat, "total ticks", ticks, "tickOffset", tickOffset) } - - //track mouse position for track context menu - let mouseX - let mouseY - function handleMousemove(event) { - mouseX = event.clientX; - mouseY = event.clientY; - //console.log("m", mouseX, mouseY) - } function deleteFlag(track, flag) { console.log("removing flag", flag) @@ -303,8 +287,8 @@
-
- +
+