kan lägga till förkortningar

This commit is contained in:
botvid johansson 2025-02-11 07:45:12 +01:00
parent 9a8938979c
commit 956cdaad38
7 changed files with 51 additions and 24 deletions

View File

@ -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 = ""
})
})
};

View File

@ -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 = () => {

View File

@ -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) => {

View File

@ -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]);
}
}
}

View File

@ -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"
}
})

View File

@ -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<number> = []) {
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<number> = []) {
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<number> = []) {
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<string, any>,
@ -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 "";

View File

@ -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 @@
<Menu />
<Textarea bind:ref={textarea} />
<!-- <Button variant="destructive" on:click={deleteDefaultShortforms}>Ta bort standardlista</Button>-->
<!-- <Dashboard open={showDashboard} />-->
</div>
<!--<Dashboard open={showDashboard} />-->
</div>
<Toaster />