klick utanför ruta stänger den

This commit is contained in:
botvid johansson 2025-02-14 14:33:16 +01:00
parent 9da2421b31
commit be98f2b189
7 changed files with 201 additions and 151 deletions

7
package-lock.json generated
View File

@ -14,6 +14,7 @@
"@oslojs/encoding": "^1.1.0", "@oslojs/encoding": "^1.1.0",
"@tauri-apps/api": "^2.2.0", "@tauri-apps/api": "^2.2.0",
"dexie": "^4.0.11", "dexie": "^4.0.11",
"svelte-outside": "^0.0.3",
"svelte-radix": "^2.0.1" "svelte-radix": "^2.0.1"
}, },
"devDependencies": { "devDependencies": {
@ -6845,6 +6846,12 @@
"url": "https://opencollective.com/eslint" "url": "https://opencollective.com/eslint"
} }
}, },
"node_modules/svelte-outside": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/svelte-outside/-/svelte-outside-0.0.3.tgz",
"integrity": "sha512-4mJttaDRXkBBL+8JqjfA1P5Ny64qmkZL3x5zELAW3tqehic3LLHvPNDcGAk8PVRooM2qUym9Oz3TfA9lO4OctA==",
"license": "MIT"
},
"node_modules/svelte-radix": { "node_modules/svelte-radix": {
"version": "2.0.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/svelte-radix/-/svelte-radix-2.0.1.tgz", "resolved": "https://registry.npmjs.org/svelte-radix/-/svelte-radix-2.0.1.tgz",

View File

@ -58,6 +58,7 @@
"@oslojs/encoding": "^1.1.0", "@oslojs/encoding": "^1.1.0",
"@tauri-apps/api": "^2.2.0", "@tauri-apps/api": "^2.2.0",
"dexie": "^4.0.11", "dexie": "^4.0.11",
"svelte-outside": "^0.0.3",
"svelte-radix": "^2.0.1" "svelte-radix": "^2.0.1"
} }
} }

View File

@ -1,6 +1,7 @@
<script lang="ts"> <script lang="ts">
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { clickOutside } from 'svelte-outside';
import { appState, shortforms, importState } from '$lib/stores.svelte'; import { appState, shortforms, importState } from '$lib/stores.svelte';
import * as Card from '$lib/components/ui/card/index.js'; import * as Card from '$lib/components/ui/card/index.js';
import { Input } from '$lib/components/ui/input'; import { Input } from '$lib/components/ui/input';
@ -69,11 +70,14 @@
}); });
}; };
const cancelCreate = () => { const cancelCreate = () => {
if (show) {
abbForm.shortform = ''; abbForm.shortform = '';
abbForm.phrase = ''; abbForm.phrase = '';
abbForm.target = {}; abbForm.target = {};
abbForm.errors = []; abbForm.errors = [];
appState.open = ''; appState.open = '';
show = false;
}
}; };
const onSelectedChange = (v) => { const onSelectedChange = (v) => {
@ -81,7 +85,7 @@
abbForm.target = v; abbForm.target = v;
}; };
let selectedLists = []; let selectedLists = [];
let show = false;
onMount(() => { onMount(() => {
db.lists.get(shortforms.standardList).then((l) => { db.lists.get(shortforms.standardList).then((l) => {
selectedLists.push({ value: l.id, label: l.name }); selectedLists.push({ value: l.id, label: l.name });
@ -91,12 +95,14 @@
const el = document.getElementById('shortformEl'); const el = document.getElementById('shortformEl');
if (el) { if (el) {
setTimeout(() => { setTimeout(() => {
show = true;
el.focus(); el.focus();
}, 15); }, 15);
} }
}); });
</script> </script>
<div use:clickOutside={cancelCreate}>
<Card.Root class="mx-auto w-[490px]"> <Card.Root class="mx-auto w-[490px]">
<Card.Header> <Card.Header>
<Card.Title>Lägg till förkortning</Card.Title> <Card.Title>Lägg till förkortning</Card.Title>
@ -140,7 +146,10 @@
</div> </div>
{#if abbForm.errors.length > 0} {#if abbForm.errors.length > 0}
<ScrollArea class="max-h-48 rounded-md"> <ScrollArea class="max-h-48 rounded-md">
<div class="border-l-4 border-orange-500 bg-orange-100 p-4 text-orange-700" role="alert"> <div
class="border-l-4 border-orange-500 bg-orange-100 p-4 text-orange-700"
role="alert"
>
<p class="font-bold">Fel vid import</p> <p class="font-bold">Fel vid import</p>
<ul class="p-6"> <ul class="p-6">
{#each abbForms.errors as error} {#each abbForms.errors as error}
@ -160,3 +169,4 @@
</Card.Footer> </Card.Footer>
--> -->
</Card.Root> </Card.Root>
</div>

View File

@ -16,7 +16,7 @@
import { importDefaultShortforms, importShortforms } from '../../db/import'; import { importDefaultShortforms, importShortforms } from '../../db/import';
import { createShortformList, type List } from '../../db/main'; import { createShortformList, type List } from '../../db/main';
import { ScrollArea } from "$lib/components/ui/scroll-area/index.js"; import { ScrollArea } from '$lib/components/ui/scroll-area/index.js';
$: form = { $: form = {
textarea: '', textarea: '',
@ -26,35 +26,48 @@
let importing = false; let importing = false;
let progress = 0; let progress = 0;
const handleImport = async () => { const handleImport = async () => {
importState.errors = [] importState.errors = [];
if (form.textarea == "") { if (form.textarea == '') {
console.error("no name error") console.error('no name error');
importState.errors.push("Importfältet är tomt") importState.errors.push('Importfältet är tomt');
return return;
} }
if (form.name == "") { if (form.name == '') {
console.error("no name error") console.error('no name error');
importState.errors.push("Du måste döpa förkortningslistan till något") importState.errors.push('Du måste döpa förkortningslistan till något');
return return;
} }
importing = true; importing = true;
const l: List = { name: form.name, type: form.type ? 1 : 0, updated: new Date() }; const l: List = { name: form.name, type: form.type ? 1 : 0, updated: new Date() };
const listid = await createShortformList(l); const listid = await createShortformList(l);
let sfs = [] let sfs = [];
form.textarea.split('\n').forEach((line) => { const lines = form.textarea.split('\n');
const fields = line.split("=") for (let i = 0; i < lines.length; i += 1) {
const fields = lines[i].split('=');
if (fields.length == 2) { if (fields.length == 2) {
sfs.push({shortform: fields[0], phrase: fields[1], used: 0, listid: listid}) sfs.push({ shortform: fields[0], phrase: fields[1], used: 0, listid: listid });
setTimeout(() => {
progress = ((i + 1) / lines.length) * 100;
}, 1);
} }
}); }
importShortforms(sfs) importShortforms(sfs);
goto('/');
}; };
const cancelImport = () => { const cancelImport = () => {
importState.data = ''; importState.data = '';
importState.errors = []; importState.errors = [];
importState.name = ""; importState.name = '';
appState.open = "" appState.open = '';
}; };
const onsubmit = (e) => {
e.preventDefault();
console.log('yooo');
handleImport();
};
const importDefaultList = () => { const importDefaultList = () => {
importState.data = ''; importState.data = '';
importState.errors = []; importState.errors = [];
@ -62,6 +75,7 @@
importState.data += sf.sf + '=' + sf.p + '\n'; importState.data += sf.sf + '=' + sf.p + '\n';
}); });
}; };
onMount(() => { onMount(() => {
form.textarea = importState.data; form.textarea = importState.data;
}); });
@ -73,7 +87,7 @@
<Card.Description>Nån annan hjälptext.</Card.Description> <Card.Description>Nån annan hjälptext.</Card.Description>
</Card.Header> </Card.Header>
<Card.Content class="mx-auto"> <Card.Content class="mx-auto">
<form class="grid gap-4 py-4" method="post" enctype="multipart/form-data"> <form class="grid gap-4 py-4" {onsubmit}>
<div class="grid grid-cols-4 items-center gap-4"> <div class="grid grid-cols-4 items-center gap-4">
<Label>Förkortningar</Label> <Label>Förkortningar</Label>
<textarea <textarea
@ -98,7 +112,7 @@ förkn=förkortningen
{#if importState.errors.length > 0} {#if importState.errors.length > 0}
<ScrollArea class="max-h-48 rounded-md"> <ScrollArea class="max-h-48 rounded-md">
<div class="bg-orange-100 border-l-4 border-orange-500 text-orange-700 p-4" role="alert"> <div class="border-l-4 border-orange-500 bg-orange-100 p-4 text-orange-700" role="alert">
<p class="font-bold">Fel vid import</p> <p class="font-bold">Fel vid import</p>
<ul class="p-6"> <ul class="p-6">
{#each importState.errors as error} {#each importState.errors as error}
@ -111,7 +125,11 @@ förkn=förkortningen
</form> </form>
</Card.Content> </Card.Content>
<Card.Footer class="flex justify-between"> <Card.Footer class="flex justify-between">
{#if importing}
<Progress value={progress} />
{:else}
<Button on:click={cancelImport} variant="outline">Avbryt</Button> <Button on:click={cancelImport} variant="outline">Avbryt</Button>
<Button on:click={handleImport}>Importera</Button> <Button on:click={onsubmit}>Importera</Button>
{/if}
</Card.Footer> </Card.Footer>
</Card.Root> </Card.Root>

View File

@ -1,12 +1,12 @@
<script> <script>
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { clickOutside } from 'svelte-outside';
import * as Card from '$lib/components/ui/card/index'; import * as Card from '$lib/components/ui/card/index';
import * as Select from '$lib/components/ui/select'; import * as Select from '$lib/components/ui/select';
import { Label } from '$lib/components/ui/label'; import { Label } from '$lib/components/ui/label';
import { db } from '../db/main'; import { db } from '../db/main';
import { liveQuery } from 'dexie'; import { liveQuery } from 'dexie';
import { shortforms } from '$lib/stores.svelte'; import { appState, shortforms } from '$lib/stores.svelte';
import { cacheShortforms } from '../modules/shortforms'; import { cacheShortforms } from '../modules/shortforms';
const onSelectedStandardListChange = (e) => { const onSelectedStandardListChange = (e) => {
@ -43,7 +43,8 @@
let standardLists = []; let standardLists = [];
let subjectLists = []; let subjectLists = [];
onMount(() => { let show = false;
onMount(async () => {
//console.log($state.snapshot(shortforms)); //console.log($state.snapshot(shortforms));
db.lists.toArray().then((lists) => { db.lists.toArray().then((lists) => {
standardLists = lists.filter((l) => l.type == 0); standardLists = lists.filter((l) => l.type == 0);
@ -54,9 +55,20 @@
listSelectorForm.subject = { value: subject.id, label: subject.name }; listSelectorForm.subject = { value: subject.id, label: subject.name };
console.log(listSelectorForm); console.log(listSelectorForm);
}); });
setTimeout(() => {
show = true;
}, 15);
}); });
const close = () => {
if (show == true) {
appState.open = '';
}
};
</script> </script>
{#if show}
<div use:clickOutside={close}>
<Card.Root class="mx-auto w-[490px]"> <Card.Root class="mx-auto w-[490px]">
<Card.Header> <Card.Header>
<Card.Title>Välj förkortningslistor</Card.Title> <Card.Title>Välj förkortningslistor</Card.Title>
@ -116,3 +128,5 @@
</Card.Footer> </Card.Footer>
--> -->
</Card.Root> </Card.Root>
</div>
{/if}

View File

@ -114,8 +114,8 @@
" "
class="textarea h-full flex-grow" class="textarea h-full flex-grow"
id="doc" id="doc"
on:keyup={change} onkeyup={change}
on:beforeinput={input} onbeforeinput={input}
bind:value={appState.text} bind:value={appState.text}
spellcheck="false" spellcheck="false"
></textarea> ></textarea>

View File

@ -51,8 +51,8 @@
<ListSelector /> <ListSelector />
</div> </div>
{/if} {/if}
<div class="h-full">
<Menu /> <Menu />
<div class="h-full">
<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>-->
<!-- <Dashboard open={showDashboard} />--> <!-- <Dashboard open={showDashboard} />-->