Getting rid of all warning messages in Svelte 5 part

This commit is contained in:
Stephan Egli 2025-01-17 17:43:39 +01:00
parent 9400cc6a41
commit 6a9b95309c
3 changed files with 83 additions and 42 deletions

View File

@ -1,17 +1,25 @@
<script lang="ts"> <script lang="ts">
import Circle from './shapes/Circle.svelte'; import Circle from "./shapes/Circle.svelte";
import Triangle from './shapes/Triangle.svelte'; import Triangle from "./shapes/Triangle.svelte";
import type { Shape } from '../types'; import type { Shape } from "../types";
import type { DocHandle } from "@automerge/automerge-repo"; import type { DocHandle } from "@automerge/automerge-repo";
import type { AppDocument } from '../types'; import type { AppDocument } from "../types";
import { updateDocWithNewCircle, updateDocWithNewTriangle, updateDocWithShapeMove } from '../docManager'; import {
updateDocWithNewCircle,
updateDocWithNewTriangle,
updateDocWithShapeMove,
} from "../docManager";
const { handle } = $props<{ const { handle } = $props<{
handle: DocHandle<AppDocument>; handle: DocHandle<AppDocument>;
}>(); }>();
let shapes = $state<Shape[]>([]); let shapes = $state<Shape[]>([]);
let draggedShape = $state<{ id: string, startX: number, startY: number } | null>(null); let draggedShape = $state<{
id: string;
startX: number;
startY: number;
} | null>(null);
// Automerge document subscription // Automerge document subscription
$effect(() => { $effect(() => {
@ -28,7 +36,7 @@
}); });
function handleDragStart(e: MouseEvent, shape: Shape) { function handleDragStart(e: MouseEvent, shape: Shape) {
const svg = document.getElementById('shapesCanvas') as SVGSVGElement; const svg = document.getElementById("shapesCanvas") as SVGSVGElement;
if (!svg) return; if (!svg) return;
const point = svg.createSVGPoint(); const point = svg.createSVGPoint();
@ -42,7 +50,7 @@
function handleDragMove(e: MouseEvent) { function handleDragMove(e: MouseEvent) {
if (!draggedShape) return; if (!draggedShape) return;
const svg = document.getElementById('shapesCanvas') as SVGSVGElement; const svg = document.getElementById("shapesCanvas") as SVGSVGElement;
if (!svg) return; if (!svg) return;
const point = svg.createSVGPoint(); const point = svg.createSVGPoint();
@ -66,33 +74,40 @@
<svg <svg
id="shapesCanvas" id="shapesCanvas"
viewBox="0 0 800 600" viewBox="0 0 800 600"
on:mouseup={() => draggedShape = null} role="presentation"
on:mousemove={handleDragMove} onmousemove={(e) => {
if (!draggedShape) return;
const svg = document.getElementById("shapesCanvas") as SVGSVGElement;
if (!svg) return;
const point = svg.createSVGPoint();
point.x = e.clientX;
point.y = e.clientY;
const { x, y } = point.matrixTransform(svg.getScreenCTM()!.inverse());
updateDocWithShapeMove(draggedShape.id, x, y);
}}
onmouseup={() => draggedShape = null}
> >
{#each shapes as shape (shape.id)} {#each shapes as shape (shape.id)}
{#if shape.type === 'circle'} {#if shape.type === "circle"}
<Circle <Circle
id={String(shape.id)} {...shape}
x={shape.x} handleDragStart={(e, shape) => handleDragStart(e, shape)}
y={shape.y}
radius={shape.radius}
color={shape.color}
on:mousedown={(e: MouseEvent) => handleDragStart(e, shape)}
/> />
{:else if shape.type === 'triangle'} {:else if shape.type === "triangle"}
<Triangle <Triangle
id={String(shape.id)} {...shape}
coordinates={shape.coordinates.map(c => [c.x, c.y]).flat() as [number, number, number, number, number, number]} handleDragStart={(e, shape) => handleDragStart(e, shape)}
color={shape.color}
on:mousedown={(e: MouseEvent) => handleDragStart(e, shape)}
/> />
{/if} {/if}
{/each} {/each}
</svg> </svg>
<div class="controls"> <div class="controls">
<button on:click={addTestCircle}>Add Circle</button> <button onclick={addTestCircle}>Add Circle</button>
<button on:click={addTestTriangle}>Add Triangle</button> <button onclick={addTestTriangle}>Add Triangle</button>
</div> </div>
</div> </div>

View File

@ -1,11 +1,21 @@
<script lang="ts"> <script lang="ts">
const { id, x, y, radius, color } = $props<{ const { id, x, y, radius, color, handleDragStart } = $props<{
id: string; id: string;
x: number; x: number;
y: number; y: number;
radius: number; radius: number;
color: string; color: string;
handleDragStart: (e: MouseEvent, shape: any) => void;
}>(); }>();
const shape = {
id,
type: 'circle',
x,
y,
radius,
color
};
</script> </script>
<circle <circle
@ -14,5 +24,8 @@
cy={y} cy={y}
r={radius} r={radius}
fill={color} fill={color}
on:mousedown role="button"
tabindex="0"
aria-label="Draggable circle"
onmousedown={(e) => handleDragStart(e, shape)}
/> />

View File

@ -1,19 +1,32 @@
<script lang="ts"> <script lang="ts">
const { id, coordinates, color } = $props<{ const { id, coordinates, color, handleDragStart } = $props<{
id: string; id: string;
coordinates: [number, number, number, number, number, number]; coordinates: [number, number, number, number, number, number];
color: string; color: string;
handleDragStart: (e: MouseEvent, shape: any) => void;
}>(); }>();
let points = $state(coordinates.join(' ')); let points = $state('');
$effect(() => { $effect(() => {
points = coordinates.join(' '); points = coordinates
.map(p => `${p.x},${p.y}`)
.join(" ");
}); });
const shape = {
id,
type: 'triangle',
coordinates,
color
};
</script> </script>
<polygon <polygon
{id} {id}
{points} {points}
fill={color} fill={color}
on:mousedown role="button"
tabindex="0"
aria-label="Draggable triangle"
onmousedown={(e) => handleDragStart(e, shape)}
/> />