From 3b89c6bf91ce29e8796d563b125d490c774bfb95 Mon Sep 17 00:00:00 2001 From: Mads Hansen Lund Date: Tue, 20 Feb 2024 17:39:05 +0100 Subject: [PATCH] Add missing TypeScript types and general cleanup --- package-lock.json | 12 ++-- package.json | 4 +- src/lib/DasmaAutocomplete.svelte | 20 ++++++ src/lib/DasmaQl.svelte | 25 +++++++- src/routes/+page.svelte | 101 ++++++++++++++++++++++++++++++- tsconfig.json | 1 + 6 files changed, 151 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index ae47f48..ebdcffc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "dasmaql-svelte", - "version": "0.1.0", + "version": "0.2.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "dasmaql-svelte", - "version": "0.1.0", + "version": "0.2.1", "license": "GPL-3.0-or-later", "dependencies": { - "dasmaql": "^0.2.1" + "dasmaql": "^0.2.17" }, "devDependencies": { "@sveltejs/adapter-auto": "^3.0.0", @@ -1616,9 +1616,9 @@ } }, "node_modules/dasmaql": { - "version": "0.2.1", - "resolved": "https://registry.npmjs.org/dasmaql/-/dasmaql-0.2.1.tgz", - "integrity": "sha512-jyHmu2WdEwv3Uz1RuJrnJmBp+eyannfcs9OzGM1ssH+uvfuAhJETDiYEr5Ja3yawVsKEUX0EUUhjidrsggXLhA==" + "version": "0.2.17", + "resolved": "https://registry.npmjs.org/dasmaql/-/dasmaql-0.2.17.tgz", + "integrity": "sha512-g/vHKHvyhlYh6WHJuz4gB+Bv0xlMUUg6hPNMh8YXy4xgpzcJaoRZ93srJKTm+ZHh2UpNo6n8uydhOyiBv+PyAA==" }, "node_modules/debug": { "version": "4.3.4", diff --git a/package.json b/package.json index aabd09c..ff193fe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dasmaql-svelte", - "version": "0.2.1", + "version": "0.2.2", "description": "A Svelte component providing an example of how DasmaQL can be utilized. DasmaQL is a lightweight query language used for data retrieval.", "scripts": { "dev": "vite dev", @@ -29,7 +29,7 @@ "svelte": "^4.0.0" }, "dependencies": { - "dasmaql": "^0.2.1" + "dasmaql": "^0.2.17" }, "devDependencies": { "@sveltejs/adapter-auto": "^3.0.0", diff --git a/src/lib/DasmaAutocomplete.svelte b/src/lib/DasmaAutocomplete.svelte index 6fa551b..64f4329 100644 --- a/src/lib/DasmaAutocomplete.svelte +++ b/src/lib/DasmaAutocomplete.svelte @@ -1,5 +1,6 @@ {#if visible} @@ -65,6 +84,7 @@ tabindex={index} class:selected={index === selectedIndex} on:click={handleMouseClick} + class="dasma-suggestion" > {getLabel(suggestion)} diff --git a/src/lib/DasmaQl.svelte b/src/lib/DasmaQl.svelte index 485d09b..4525e90 100644 --- a/src/lib/DasmaQl.svelte +++ b/src/lib/DasmaQl.svelte @@ -4,7 +4,12 @@ import { onMount } from 'svelte'; import DasmaAutocomplete from '$lib/DasmaAutocomplete.svelte'; import type { FormEventHandler, KeyboardEventHandler, MouseEventHandler } from 'svelte/elements'; - import { dasmaQlAutocomplete, dasmaQlHighlighter } from 'dasmaql'; + import { + dasmaQlAutocomplete, + type QlAutocomplete + } from 'dasmaql/src/modules/dasmaQlAutocomplete'; + import { dasmaQlHighlighter, type QlHighlighter } from 'dasmaql/src/modules/dasmaQlHighlighter'; + import { dasmaQlSyntax, type QlSyntax, type QlModel } from 'dasmaql/src/modules/dasmaQlSyntax'; export let fields: string[] = []; export let callbackSearch: ( @@ -14,8 +19,9 @@ return []; }; - let autocomplete: dasmaQlAutocomplete; - let highlighter: dasmaQlHighlighter; + let autocomplete: QlAutocomplete; + let highlighter: QlHighlighter; + let syntaxModel: QlSyntax; const debounceHighlighter = debouncer(200); const debounceAutocomplete = debouncer(400); @@ -33,6 +39,16 @@ let currentPosition = 0; let selectedIndex = -1; + export const generateModel: () => QlModel = () => { + const input = outputContainer.innerText.trim(); + const model: QlModel = syntaxModel.parse(input); + + if (model.validation?.error?.location.start) { + CaretHandler.setCaretPosition(outputContainer, model.validation.error.location.start.offset); + } + return model; + }; + const handleInputChange: FormEventHandler = (event) => { const input = getInputTextContent(event); if (input !== lastQl) { @@ -149,6 +165,9 @@ fields: fields, callbackSearchParameter: callbackSearch }); + syntaxModel = dasmaQlSyntax({ + fields: fields + }); }); diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 9762d04..3571894 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,6 +1,7 @@ - + + +
+
+
+        {@html qlModel ? syntaxHighlight(qlModel) : ''}
+    
+
+ + diff --git a/tsconfig.json b/tsconfig.json index 0f47472..1d343c4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,6 +3,7 @@ "compilerOptions": { "allowJs": true, "checkJs": true, + "allowSyntheticDefaultImports": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "resolveJsonModule": true,