Getting rid of all warning messages in Svelte 5 part
This commit is contained in:
parent
9400cc6a41
commit
6a9b95309c
@ -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,28 +36,28 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
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();
|
||||||
point.x = e.clientX;
|
point.x = e.clientX;
|
||||||
point.y = e.clientY;
|
point.y = e.clientY;
|
||||||
const { x, y } = point.matrixTransform(svg.getScreenCTM()!.inverse());
|
const { x, y } = point.matrixTransform(svg.getScreenCTM()!.inverse());
|
||||||
|
|
||||||
draggedShape = { id: String(shape.id), startX: x, startY: y };
|
draggedShape = { id: String(shape.id), startX: x, startY: y };
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
point.x = e.clientX;
|
point.x = e.clientX;
|
||||||
point.y = e.clientY;
|
point.y = e.clientY;
|
||||||
const { x, y } = point.matrixTransform(svg.getScreenCTM()!.inverse());
|
const { x, y } = point.matrixTransform(svg.getScreenCTM()!.inverse());
|
||||||
|
|
||||||
updateDocWithShapeMove(draggedShape.id, x, y);
|
updateDocWithShapeMove(draggedShape.id, x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,36 +71,43 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<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>
|
||||||
|
|
||||||
@ -102,7 +117,7 @@
|
|||||||
height: 100vh;
|
height: 100vh;
|
||||||
position: relative;
|
position: relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
svg {
|
svg {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
@ -113,4 +128,4 @@
|
|||||||
top: 10px;
|
top: 10px;
|
||||||
left: 10px;
|
left: 10px;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@ -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)}
|
||||||
/>
|
/>
|
||||||
@ -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)}
|
||||||
/>
|
/>
|
||||||
Loading…
Reference in New Issue
Block a user