diff --git a/src/components/create.svelte b/src/components/create.svelte index 1d7e7ed..b42f44d 100644 --- a/src/components/create.svelte +++ b/src/components/create.svelte @@ -12,7 +12,7 @@ import { Badge } from '$lib/components/ui/badge'; import { Separator } from '$lib/components/ui/separator'; import { db, createShortform, type List, type Shortform } from '../db/main'; - import { cacheShortforms } from "../modules/shortforms"; + import { cacheShortform } from "../modules/shortforms"; import { liveQuery } from "dexie"; import { ScrollArea } from "$lib/components/ui/scroll-area/index.js"; @@ -49,13 +49,19 @@ remind: 0, updated: Date.now(), } - appState.open = "" - db.shortforms.where({listid: sf.listid, shortform: sf.shortform}).first() + //appState.open = "" + db.shortforms.where("[listid+shortform]").equals([sf.listid, sf.shortform]).first() .then(existing => { sf.id = existing.id db.shortforms.put(sf).then(result => { - shortforms.cache = cacheShortforms([sf.listid]); - cancelCreate() + cacheShortform(sf); + appState.open = "" + }) + }) + .catch(() => { + db.shortforms.put(sf).then(result => { + cacheShortform(sf); + appState.open = "" }) }) }; diff --git a/src/components/dashboard.svelte b/src/components/dashboard.svelte index 4db9256..2bc1364 100644 --- a/src/components/dashboard.svelte +++ b/src/components/dashboard.svelte @@ -12,11 +12,7 @@ import { Badge } from '$lib/components/ui/badge'; import { Separator } from '$lib/components/ui/separator'; import Import from './import/import.svelte'; - export let open = true; - - export let data = { - form: {} - }; + let {open} = $props(); import default_shortforms from '../db/shortforms.se.json'; const importDefaultList = () => { diff --git a/src/db/main.ts b/src/db/main.ts index 6dec233..1004439 100644 --- a/src/db/main.ts +++ b/src/db/main.ts @@ -35,7 +35,7 @@ export class SkrivertDB extends Dexie { super("skrivertdb"); this.version(1).stores({ syncInfo: "key, last_sync", - shortforms: "++id, listid, shortform, phrase, last_use, uses, remind, updated", + shortforms: "++id, [listid+shortform], phrase, last_use, uses, remind, updated", lists: "++id, name, type, updated", }); this.on("ready", async (db) => { diff --git a/src/lib/stores.svelte.ts b/src/lib/stores.svelte.ts index 223d1ea..2bb8af6 100644 --- a/src/lib/stores.svelte.ts +++ b/src/lib/stores.svelte.ts @@ -39,6 +39,7 @@ export const appState = $state({ text: "", menuOpen: false, open: "" }) import { toast } from "svelte-sonner"; import { db } from "../db/main" +import { cacheShortforms } from "../modules/shortforms" export const initShortformState = async () => { const lists = await db.lists.toArray() @@ -47,6 +48,7 @@ export const initShortformState = async () => { console.log("Väljer en av standardlistorna") shortforms.standardList = lists[0].id toast("Ingen standardlista vald. Väljer " + lists[0].name) + shortforms.cache = cacheShortforms([shortforms.standardList]); } } } diff --git a/src/modules/keyboard.ts b/src/modules/keyboard.ts index 75c93a9..ebb10fb 100644 --- a/src/modules/keyboard.ts +++ b/src/modules/keyboard.ts @@ -81,7 +81,8 @@ defaultExpanders.set(";", { hotkeys.set("F1", { action: (e) => { e.preventDefault() - console.log("Open help page") + console.log("Open dashboard") + appState.open = "dashboard" } }) diff --git a/src/modules/shortforms.ts b/src/modules/shortforms.ts index af46652..73838e1 100644 --- a/src/modules/shortforms.ts +++ b/src/modules/shortforms.ts @@ -1,7 +1,24 @@ import { db } from "../db/main" import { type Shortform } from "../db/main" +import { shortforms } from "../lib/stores.svelte" + +export async function cacheShortform(shortform: Shortform) { + if (shortforms.cache.has(shortform.shortform)) { + const existing = shortforms.cache.get(shortform.shortform) + const existingShortformList = await db.lists.where("id").equals(existing.listid).first() + const newShortformList = await db.lists.where("id").equals(shortform.listid).first() + + if (newShortformList >= existingShortformList) { + shortforms.cache.set(shortform.shortform, shortform) + } + + } else { + shortforms.cache.set(shortform.shortform, shortform) + } +} + export function cacheShortforms(lists: Array = []) { - let shortforms = new Map(); + let cache = new Map(); // console.log("Caching shortforms with lists:", lists); var n = 0; @@ -9,7 +26,7 @@ export function cacheShortforms(lists: Array = []) { var phrases_length = 0; if (lists.length < 1) { default_shortforms.map((sf: ShortformExportType) => { - shortforms.set(sf.sf, { phrase: sf.p, last: sf.u }); + cache.set(sf.sf, { phrase: sf.p, last: sf.u }); /* n += 1; shortforms_length += sf.sf.length; @@ -26,13 +43,13 @@ export function cacheShortforms(lists: Array = []) { console.log("looking up shortforms for this listid:", listid) db.shortforms.where("listid").equals(listid).toArray().then(sfs => { sfs.forEach(sf => { - shortforms.set(sf.shortform, sf) + cache.set(sf.shortform, sf) }) }) }) } - return shortforms; + return cache; } export function expandShortform( cache: Map, @@ -41,17 +58,18 @@ export function expandShortform( const u = new Date(); const shortform = ShortForm.toLowerCase(); if (cache.has(shortform)) { - const phrase = cache.get(shortform).phrase; - cache.set(shortform, { phrase: phrase, last: u }); + const cachedShortform = cache.get(shortform); + cachedShortform.last = u; + cache.set(shortform, cachedShortform); if (shortform.match(/(\d+)/) || ShortForm === shortform) { - return phrase; + return cachedShortform.phrase; } else if (ShortForm === shortform.toUpperCase() && ShortForm.length > 1) { - return phrase.toUpperCase(); + return cachedShortform.phrase.toUpperCase(); } else if (/^\p{Lu}(\p{L}*)/u.test(ShortForm)) { - return phrase.charAt(0).toUpperCase() + phrase.substring(1); + return cachedShortform.phrase.charAt(0).toUpperCase() + cachedShortform.phrase.substring(1); } else { - return phrase; + return cachedShortform.phrase; } } return ""; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index c18b601..609c856 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -14,12 +14,16 @@ if (appState.open == "") { textarea.focus() } + if (appState.open == "dashboard") { + console.log("should open dashboard") + showDashboard = true + } } loaded = true }) import { hotkeys } from '../modules/keyboard'; - let showDashboard: boolean = false; + let showDashboard: boolean = true; import { Toaster } from "$lib/components/ui/sonner"; const handleHotkeys = (e: KeyboardEvent) => { hotkeys.get(e.key)?.action(e); @@ -42,8 +46,8 @@