Format all files

This commit is contained in:
Leonardo Murça 2022-11-11 10:57:53 -03:00
parent 5eb7ea10ad
commit a777f33fa6
5 changed files with 122 additions and 152 deletions

View file

@ -1,47 +1 @@
# Svelte + Vite # Embroidery Viewer
This template should help get you started developing with Svelte 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 + 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 `checkJs` in the JS template?**
It is likely that most cases of changing variable types in runtime are likely to be accidental, rather than deliberate. This provides advanced typechecking out of the box. Should you like to take advantage of the dynamically-typed nature of JavaScript, it is trivial to change the configuration.
**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.
```js
// store.js
// An extremely simple external store
import { writable } from 'svelte/store'
export default writable(0)
```

View file

@ -1,23 +1,24 @@
<script> <script>
import FileViewer from './lib/FileViewer.svelte' import FileViewer from "./lib/FileViewer.svelte";
import logo from './assets/logo.webp' import logo from "./assets/logo.webp";
</script> </script>
<header> <header>
<a href="/"> <a href="/">
<img <img src={logo} alt="Embroidery viewer logo." width="460" height="200" />
src={logo}
alt="Embroidery viewer logo."
width=460
height=200
/>
</a> </a>
</header> </header>
<main> <main>
<FileViewer /> <FileViewer />
</main> </main>
<footer> <footer>
<p>Copyright © 2022 <a href="https://leomurca.xyz" target="_blank" rel="noreferrer" >Leonardo Murça</a>.</p> <p>
Copyright © 2022 <a
href="https://leomurca.xyz"
target="_blank"
rel="noreferrer">Leonardo Murça</a
>.
</p>
</footer> </footer>
<style> <style>

View file

@ -1,117 +1,133 @@
<script> <script>
import { startFileRead } from '../utils/main'; import { startFileRead } from "../utils/main";
let files; let files;
const acceptedFiles = [".pes"]; const acceptedFiles = [".pes"];
const maxFileSize = 700000; const maxFileSize = 700000;
function onSubmitHandler(evt) { function onSubmitHandler(evt) {
evt.stopPropagation(); evt.stopPropagation();
evt.preventDefault(); evt.preventDefault();
for (var i = 0, file; (file = files[i]); i++) { for (var i = 0, file; (file = files[i]); i++) {
const canvasContainer = document.getElementById("canvas-container") const canvasContainer = document.getElementById("canvas-container");
const canvasCard = document.createElement(`div`) const canvasCard = document.createElement(`div`);
const canvasEl = document.createElement(`canvas`) const canvasEl = document.createElement(`canvas`);
const fileNameEl = document.createElement(`p`) const fileNameEl = document.createElement(`p`);
canvasCard.id = `canvas-card-${i}` canvasCard.id = `canvas-card-${i}`;
canvasCard.style['display'] = "flex" canvasCard.style["display"] = "flex";
canvasCard.style['flex-direction'] = "column" canvasCard.style["flex-direction"] = "column";
canvasCard.style['justify-content'] = "center" canvasCard.style["justify-content"] = "center";
canvasCard.style['align-items'] = "center" canvasCard.style["align-items"] = "center";
canvasCard.style['width'] = "550px" canvasCard.style["width"] = "550px";
canvasCard.style['height'] = "550px" canvasCard.style["height"] = "550px";
canvasCard.style['margin-bottom'] = "15px" canvasCard.style["margin-bottom"] = "15px";
canvasCard.style['border'] = "2px solid black" canvasCard.style["border"] = "2px solid black";
canvasEl.id = `mycanvas-${i}` canvasEl.id = `mycanvas-${i}`;
canvasEl.style["height"] = "80%"; canvasEl.style["height"] = "80%";
canvasEl.style["width"] = "fit-content"; canvasEl.style["width"] = "fit-content";
fileNameEl.textContent = file.name fileNameEl.textContent = file.name;
canvasCard.appendChild(canvasEl) canvasCard.appendChild(canvasEl);
canvasCard.appendChild(fileNameEl) canvasCard.appendChild(fileNameEl);
canvasContainer.appendChild(canvasCard) canvasContainer.appendChild(canvasCard);
if (file) { if (file) {
startFileRead(file, canvasEl); startFileRead(file, canvasEl);
} }
} }
document.getElementById("selected-files-container").remove(); document.getElementById("selected-files-container").remove();
} }
function handleDragOver(evt) { function handleDragOver(evt) {
evt.stopPropagation(); evt.stopPropagation();
evt.preventDefault(); evt.preventDefault();
}
} function onDropHandler(evt) {
function onDropHandler(evt) {
evt.stopPropagation(); evt.stopPropagation();
evt.preventDefault(); evt.preventDefault();
onChangeFileHandler(evt) onChangeFileHandler(evt);
} }
function onChangeFileHandler(evt) { function onChangeFileHandler(evt) {
const changedFiles = evt.dataTransfer ? evt.dataTransfer.files : evt.target.files; const changedFiles = evt.dataTransfer
? evt.dataTransfer.files
: evt.target.files;
let filesToUpload = []; let filesToUpload = [];
for (var i = 0, file; (file = changedFiles[i]); i++) { for (var i = 0, file; (file = changedFiles[i]); i++) {
if (file) { if (file) {
if (file.size <= maxFileSize) { if (file.size <= maxFileSize) {
filesToUpload.push(file); filesToUpload.push(file);
} else { } else {
console.log("File too large!") console.log("File too large!");
console.log(file) console.log(file);
} }
} }
} }
files = filesToUpload files = filesToUpload;
} }
function handleOnClick(evt) { function handleOnClick(evt) {
document.getElementById("file-input").click(); document.getElementById("file-input").click();
} }
function handleOnKeydown(evt) { function handleOnKeydown(evt) {
if (evt.key === "Enter") { if (evt.key === "Enter") {
document.getElementById("file-input").click(); document.getElementById("file-input").click();
} }
} }
</script> </script>
<form <form on:submit={onSubmitHandler} id="form" enctype="multipart/form-data">
on:submit={onSubmitHandler}
id="form"
action="#"
enctype="multipart/form-data">
<h2>Upload files</h2> <h2>Upload files</h2>
<p>Max file size is <strong>{maxFileSize/1000}kb</strong>. Accepted formats: <strong>{acceptedFiles.join(",")}</strong>.</p> <p>
Max file size is <strong>{maxFileSize / 1000}kb</strong>. Accepted formats:
<strong>{acceptedFiles.join(",")}</strong>.
</p>
<!-- svelte-ignore a11y-no-noninteractive-tabindex --> <!-- svelte-ignore a11y-no-noninteractive-tabindex -->
<div id="dropzone" tabindex={0} on:keydown={handleOnKeydown} on:click={handleOnClick} on:dragover={handleDragOver} on:drop={onDropHandler}> <div
<label id="file-label" for="file-input">Drag and drop files here or click to upload.</label> id="dropzone"
<input id="file-input" type="file" name="files[]" accept={acceptedFiles.join(",")} multiple on:change={onChangeFileHandler} bind:files /> tabindex={0}
on:keydown={handleOnKeydown}
on:click={handleOnClick}
on:dragover={handleDragOver}
on:drop={onDropHandler}
>
<label id="file-label" for="file-input"
>Drag and drop files here or click to upload.</label
>
<input
id="file-input"
type="file"
name="files[]"
accept={acceptedFiles.join(",")}
multiple
on:change={onChangeFileHandler}
bind:files
/>
</div> </div>
<input type="submit" value="Render files"> <input type="submit" value="Render files" />
</form> </form>
<div id="selected-files-container"> <div id="selected-files-container">
{#if files} {#if files}
<h2>Selected files:</h2> <h2>Selected files:</h2>
{#each Array.from(files) as file} {#each Array.from(files) as file}
<div id="selected-file-card"> <div id="selected-file-card">
<p>{file.name} ({file.size/1000} kb) </p> <p>{file.name} ({file.size / 1000} kb)</p>
</div> </div>
{/each} {/each}
{/if} {/if}
</div> </div>
<div id="canvas-container" style="width: 100%; heigth: 100vh;"></div> <div id="canvas-container" style="width: 100%; heigth: 100vh;" />
<style> <style>
input[type="submit"] { input[type="submit"] {
width: 100%; width: 100%;
font-size: 20px; font-size: 20px;
@ -166,5 +182,4 @@ function handleOnKeydown(evt) {
border: 5px dotted #05345f; border: 5px dotted #05345f;
color: #05345f; color: #05345f;
} }
</style> </style>

View file

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

View file

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