Init webui

This commit is contained in:
Kai Vogelgesang 2023-06-23 15:50:17 +02:00
parent 162103e54b
commit 4cb1f6ea4d
Signed by: kai
GPG Key ID: 3FC8578CC818A9EB
16 changed files with 2655 additions and 0 deletions

24
webui/.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

3
webui/.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"recommendations": ["svelte.svelte-vscode"]
}

47
webui/README.md Normal file
View File

@ -0,0 +1,47 @@
# Svelte + TS + Vite
This template should help get you started developing with Svelte and TypeScript in Vite.
## Recommended IDE Setup
[VS Code](https://code.visualstudio.com/) + [Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode).
## Need an official Svelte framework?
Check out [SvelteKit](https://github.com/sveltejs/kit#readme), which is also powered by Vite. Deploy anywhere with its serverless-first approach and adapt to various platforms, with out of the box support for TypeScript, SCSS, and Less, and easily-added support for mdsvex, GraphQL, PostCSS, Tailwind CSS, and more.
## Technical considerations
**Why use this over SvelteKit?**
- It brings its own routing solution which might not be preferable for some users.
- It is first and foremost a framework that just happens to use Vite under the hood, not a Vite app.
This template contains as little as possible to get started with Vite + TypeScript + Svelte, while taking into account the developer experience with regards to HMR and intellisense. It demonstrates capabilities on par with the other `create-vite` templates and is a good starting point for beginners dipping their toes into a Vite + Svelte project.
Should you later need the extended capabilities and extensibility provided by SvelteKit, the template has been structured similarly to SvelteKit so that it is easy to migrate.
**Why `global.d.ts` instead of `compilerOptions.types` inside `jsconfig.json` or `tsconfig.json`?**
Setting `compilerOptions.types` shuts out all other types not explicitly listed in the configuration. Using triple-slash references keeps the default TypeScript setting of accepting type information from the entire workspace, while also adding `svelte` and `vite/client` type information.
**Why include `.vscode/extensions.json`?**
Other templates indirectly recommend extensions via the README, but this file allows VS Code to prompt the user to install the recommended extension upon opening the project.
**Why enable `allowJs` in the TS template?**
While `allowJs: false` would indeed prevent the use of `.js` files in the project, it does not prevent the use of JavaScript syntax in `.svelte` files. In addition, it would force `checkJs: false`, bringing the worst of both worlds: not being able to guarantee the entire codebase is TypeScript, and also having worse typechecking for the existing JavaScript. In addition, there are valid use cases in which a mixed codebase may be relevant.
**Why is HMR not preserving my local component state?**
HMR state preservation comes with a number of gotchas! It has been disabled by default in both `svelte-hmr` and `@sveltejs/vite-plugin-svelte` due to its often surprising behavior. You can read the details [here](https://github.com/rixo/svelte-hmr#svelte-hmr).
If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR.
```ts
// store.ts
// An extremely simple external store
import { writable } from 'svelte/store'
export default writable(0)
```

12
webui/index.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Svelte + TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>

2274
webui/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

24
webui/package.json Normal file
View File

@ -0,0 +1,24 @@
{
"name": "webui",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-check --tsconfig ./tsconfig.json"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^2.0.4",
"@tsconfig/svelte": "^4.0.1",
"svelte": "^3.58.0",
"svelte-check": "^3.3.1",
"tslib": "^2.5.0",
"typescript": "^5.1.3",
"vite": "^4.3.9"
},
"dependencies": {
"monaco-editor": "^0.39.0"
}
}

45
webui/src/App.svelte Normal file
View File

@ -0,0 +1,45 @@
<script lang="ts">
import Editor from "./Editor.svelte";
import { parseFunctionInfo } from "./analysis";
const moduleCode = "export function run(x: number): number {\n return x + 69;\n}\n";
// const blob = new Blob([moduleCode], { type: "text/javascript" });
// const url = URL.createObjectURL(blob);
// const module = import(/* @vite-ignore */ url);
let text = moduleCode;
$: info = parseFunctionInfo(text, "run");
</script>
<main>
<div class="yeet">
<Editor bind:content={text} />
</div>
{#if info !== null}
<h3>Args:</h3>
<ul>
{#each info.arguments as arg}
<li>{arg.name}: {arg.type}</li>
{/each}
</ul>
<h3>Return:</h3>
{info.returnType}
{/if}
<!--
{#await module then m}
<pre>{JSON.stringify(m)}</pre>
<pre>{JSON.stringify(m.run())}</pre>
{/await}
-->
</main>
<style>
.yeet {
width: 40vw;
height: 400px;
display: flex;
}
</style>

69
webui/src/Editor.svelte Normal file
View File

@ -0,0 +1,69 @@
<script lang="ts" context="module">
import editorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker";
import tsWorker from "monaco-editor/esm/vs/language/typescript/ts.worker?worker";
// @ts-ignore
self.MonacoEnvironment = {
getWorker: function (_moduleId: any, label: string) {
if (label === "typescript" || label === "javascript") {
return new tsWorker();
}
return new editorWorker();
},
};
</script>
<script lang="ts">
// Adapted from https://gist.github.com/KTibow/77da4597dcb22cf80be525df284e6d72
import { onMount } from "svelte";
export let content: string;
let editor;
let divEl: HTMLElement;
onMount(async () => {
const monaco = await import("monaco-editor");
editor = monaco.editor.create(divEl, {
value: content,
language: "typescript",
scrollBeyondLastLine: false,
theme: "vs-dark",
});
editor.onDidChangeModelContent(() => {
content = editor.getValue();
});
return () => {
editor.dispose();
};
});
</script>
<div class="container">
<div bind:this={divEl} class="editor" />
</div>
<svelte:window
on:resize={() => {
editor.layout({ width: 0, height: 0 });
window.requestAnimationFrame(() => {
const rect = divEl.parentElement.getBoundingClientRect();
editor.layout({ width: rect.width, height: rect.height });
});
}}
/>
<style>
.container {
flex-grow: 1;
}
.editor {
width: 100%;
height: 100%;
text-align: left;
}
</style>

24
webui/src/analysis.ts Normal file
View File

@ -0,0 +1,24 @@
import ts from 'typescript';
export function parseFunctionInfo(sourceCode: string, functionName: string): { arguments: { name: string, type: string }[], returnType: string } | null {
const sourceFile = ts.createSourceFile('temp.ts', sourceCode, ts.ScriptTarget.ES2015, true);
let functionInfo: { arguments: { name: string, type: string }[], returnType: string } | null = null;
for (const node of sourceFile.statements) {
if (ts.isFunctionDeclaration(node) && node.name && node.name.text === functionName) {
const argumentTypes: { name: string, type: string }[] = node.parameters.map(parameter => {
const name = parameter.name.getText();
const type = parameter.type ? sourceCode.substring(parameter.type.pos, parameter.type.end) : 'any';
return { name, type };
});
const returnType = node.type ? sourceCode.substring(node.type.pos, node.type.end) : 'any';
functionInfo = { arguments: argumentTypes, returnType };
break;
}
}
return functionInfo;
}

80
webui/src/app.css Normal file
View File

@ -0,0 +1,80 @@
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;
color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
}
a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}
body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}
h1 {
font-size: 3.2em;
line-height: 1.1;
}
.card {
padding: 2em;
}
#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}
button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}
@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}

8
webui/src/main.ts Normal file
View File

@ -0,0 +1,8 @@
import './app.css'
import App from './App.svelte'
const app = new App({
target: document.getElementById('app'),
})
export default app

2
webui/src/vite-env.d.ts vendored Normal file
View File

@ -0,0 +1,2 @@
/// <reference types="svelte" />
/// <reference types="vite/client" />

7
webui/svelte.config.js Normal file
View File

@ -0,0 +1,7 @@
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'
export default {
// Consult https://svelte.dev/docs#compile-time-svelte-preprocess
// for more information about preprocessors
preprocess: vitePreprocess(),
}

20
webui/tsconfig.json Normal file
View File

@ -0,0 +1,20 @@
{
"extends": "@tsconfig/svelte/tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"resolveJsonModule": true,
/**
* Typecheck JS in `.svelte` and `.js` files by default.
* Disable checkJs if you'd like to use dynamic types in JS.
* Note that setting allowJs false does not prevent the use
* of JS in `.svelte` files.
*/
"allowJs": true,
"checkJs": true,
"isolatedModules": true
},
"include": ["src/**/*.d.ts", "src/**/*.ts", "src/**/*.js", "src/**/*.svelte"],
"references": [{ "path": "./tsconfig.node.json" }]
}

9
webui/tsconfig.node.json Normal file
View File

@ -0,0 +1,9 @@
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler"
},
"include": ["vite.config.ts"]
}

7
webui/vite.config.ts Normal file
View File

@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [svelte()],
})