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 { Badge } from '$lib/components/ui/badge';
import { Separator } from '$lib/components/ui/separator'; import { Separator } from '$lib/components/ui/separator';
import { db, createShortform, type List, type Shortform } from '../db/main'; 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 { liveQuery } from "dexie";
import { ScrollArea } from "$lib/components/ui/scroll-area/index.js"; import { ScrollArea } from "$lib/components/ui/scroll-area/index.js";
@ -49,13 +49,19 @@
remind: 0, remind: 0,
updated: Date.now(), updated: Date.now(),
} }
appState.open = "" //appState.open = ""
db.shortforms.where({listid: sf.listid, shortform: sf.shortform}).first() db.shortforms.where("[listid+shortform]").equals([sf.listid, sf.shortform]).first()
.then(existing => { .then(existing => {
sf.id = existing.id sf.id = existing.id
db.shortforms.put(sf).then(result => { db.shortforms.put(sf).then(result => {
shortforms.cache = cacheShortforms([sf.listid]); cacheShortform(sf);
cancelCreate() 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 { Badge } from '$lib/components/ui/badge';
import { Separator } from '$lib/components/ui/separator'; import { Separator } from '$lib/components/ui/separator';
import Import from './import/import.svelte'; import Import from './import/import.svelte';
export let open = true; let {open} = $props();
export let data = {
form: {}
};
import default_shortforms from '../db/shortforms.se.json'; import default_shortforms from '../db/shortforms.se.json';
const importDefaultList = () => { const importDefaultList = () => {

View File

@ -35,7 +35,7 @@ export class SkrivertDB extends Dexie {
super("skrivertdb"); super("skrivertdb");
this.version(1).stores({ this.version(1).stores({
syncInfo: "key, last_sync", 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", lists: "++id, name, type, updated",
}); });
this.on("ready", async (db) => { 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 { toast } from "svelte-sonner";
import { db } from "../db/main" import { db } from "../db/main"
import { cacheShortforms } from "../modules/shortforms"
export const initShortformState = async () => { export const initShortformState = async () => {
const lists = await db.lists.toArray() const lists = await db.lists.toArray()
@ -47,6 +48,7 @@ export const initShortformState = async () => {
console.log("Väljer en av standardlistorna") console.log("Väljer en av standardlistorna")
shortforms.standardList = lists[0].id shortforms.standardList = lists[0].id
toast("Ingen standardlista vald. Väljer " + lists[0].name) 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", { hotkeys.set("F1", {
action: (e) => { action: (e) => {
e.preventDefault() 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 { db } from "../db/main"
import { type Shortform } 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> = []) { export function cacheShortforms(lists: Array<number> = []) {
let shortforms = new Map(); let cache = new Map();
// console.log("Caching shortforms with lists:", lists); // console.log("Caching shortforms with lists:", lists);
var n = 0; var n = 0;
@ -9,7 +26,7 @@ export function cacheShortforms(lists: Array<number> = []) {
var phrases_length = 0; var phrases_length = 0;
if (lists.length < 1) { if (lists.length < 1) {
default_shortforms.map((sf: ShortformExportType) => { 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; n += 1;
shortforms_length += sf.sf.length; shortforms_length += sf.sf.length;
@ -26,13 +43,13 @@ export function cacheShortforms(lists: Array<number> = []) {
console.log("looking up shortforms for this listid:", listid) console.log("looking up shortforms for this listid:", listid)
db.shortforms.where("listid").equals(listid).toArray().then(sfs => { db.shortforms.where("listid").equals(listid).toArray().then(sfs => {
sfs.forEach(sf => { sfs.forEach(sf => {
shortforms.set(sf.shortform, sf) cache.set(sf.shortform, sf)
}) })
}) })
}) })
} }
return shortforms; return cache;
} }
export function expandShortform( export function expandShortform(
cache: Map<string, any>, cache: Map<string, any>,
@ -41,17 +58,18 @@ export function expandShortform(
const u = new Date(); const u = new Date();
const shortform = ShortForm.toLowerCase(); const shortform = ShortForm.toLowerCase();
if (cache.has(shortform)) { if (cache.has(shortform)) {
const phrase = cache.get(shortform).phrase; const cachedShortform = cache.get(shortform);
cache.set(shortform, { phrase: phrase, last: u }); cachedShortform.last = u;
cache.set(shortform, cachedShortform);
if (shortform.match(/(\d+)/) || ShortForm === shortform) { if (shortform.match(/(\d+)/) || ShortForm === shortform) {
return phrase; return cachedShortform.phrase;
} else if (ShortForm === shortform.toUpperCase() && ShortForm.length > 1) { } else if (ShortForm === shortform.toUpperCase() && ShortForm.length > 1) {
return phrase.toUpperCase(); return cachedShortform.phrase.toUpperCase();
} else if (/^\p{Lu}(\p{L}*)/u.test(ShortForm)) { } 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 { } else {
return phrase; return cachedShortform.phrase;
} }
} }
return ""; return "";

View File

@ -14,12 +14,16 @@
if (appState.open == "") { if (appState.open == "") {
textarea.focus() textarea.focus()
} }
if (appState.open == "dashboard") {
console.log("should open dashboard")
showDashboard = true
}
} }
loaded = true loaded = true
}) })
import { hotkeys } from '../modules/keyboard'; import { hotkeys } from '../modules/keyboard';
let showDashboard: boolean = false; let showDashboard: boolean = true;
import { Toaster } from "$lib/components/ui/sonner"; import { Toaster } from "$lib/components/ui/sonner";
const handleHotkeys = (e: KeyboardEvent) => { const handleHotkeys = (e: KeyboardEvent) => {
hotkeys.get(e.key)?.action(e); hotkeys.get(e.key)?.action(e);
@ -42,8 +46,8 @@
<Menu /> <Menu />
<Textarea bind:ref={textarea} /> <Textarea bind:ref={textarea} />
<!-- <Button variant="destructive" on:click={deleteDefaultShortforms}>Ta bort standardlista</Button>--> <!-- <Button variant="destructive" on:click={deleteDefaultShortforms}>Ta bort standardlista</Button>-->
</div>
<!-- <Dashboard open={showDashboard} />--> <!-- <Dashboard open={showDashboard} />-->
</div> </div>
</div>
<Toaster /> <Toaster />