Code a little bit more idiomatic Svelte 5 now using bind directive
This commit is contained in:
parent
9e02b6ecff
commit
7f4594e9eb
@ -20,9 +20,10 @@
|
|||||||
startX: number;
|
startX: number;
|
||||||
startY: number;
|
startY: number;
|
||||||
} | null>(null);
|
} | null>(null);
|
||||||
|
let svgElement = $state<SVGSVGElement>();
|
||||||
|
|
||||||
// Automerge document subscription
|
// Automerge document subscription
|
||||||
// This is called once
|
// This is called once to initialize the shapes
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
console.log("Subscribing to document changes"); // Debug log
|
console.log("Subscribing to document changes"); // Debug log
|
||||||
const doc = handle.docSync();
|
const doc = handle.docSync();
|
||||||
@ -34,66 +35,29 @@
|
|||||||
handle.on("change", ({ doc }) => {
|
handle.on("change", ({ doc }) => {
|
||||||
if (doc) {
|
if (doc) {
|
||||||
console.log("Document changed:", doc.shapes); // Debug log
|
console.log("Document changed:", doc.shapes); // Debug log
|
||||||
// TODO: would it make sense to use patches here to have fine grained reactivity ?
|
// it looks like Svelte is clever enough to make only the necessary DOM changes
|
||||||
// here is the original non-svelte code
|
// without having to use patches
|
||||||
// Process each patch to update, add, or remove shapes
|
|
||||||
/* patches.forEach(patch => {
|
|
||||||
console.log('Processing patch:', patch); // Debug log
|
|
||||||
if (patch.path[0] === 'shapes') {
|
|
||||||
if (patch.action === 'put') {
|
|
||||||
// Handle shape addition or update
|
|
||||||
const shapeIndex = parseInt(patch.path[1], 10);
|
|
||||||
const shapeData = shapesArray[shapeIndex];
|
|
||||||
if (!shapeData) return;
|
|
||||||
|
|
||||||
const shape = createShapeInstance(shapeData);
|
|
||||||
if (!shape) return;
|
|
||||||
|
|
||||||
// Find existing element or create new one
|
|
||||||
const elementId = String(shapeData.id);
|
|
||||||
let element = document.getElementById(elementId) as SVGCircleElement | SVGPolygonElement | null;
|
|
||||||
if (!element) {
|
|
||||||
element = shape.createSVGElement() as SVGCircleElement | SVGPolygonElement;
|
|
||||||
container.appendChild(element);
|
|
||||||
} else {
|
|
||||||
shape.updateAttributes(element);
|
|
||||||
}
|
|
||||||
} else if (patch.action === 'del') {
|
|
||||||
// Handle shape deletion
|
|
||||||
const element = document.getElementById(patch.path[1]) as SVGElement | null;
|
|
||||||
if (element) {
|
|
||||||
container.removeChild(element);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}); */
|
|
||||||
shapes = doc.shapes;
|
shapes = doc.shapes;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function handleDragStart(e: MouseEvent, shape: Shape) {
|
function handleDragStart(e: MouseEvent, shape: Shape) {
|
||||||
const svg = document.getElementById("shapesCanvas") as SVGSVGElement;
|
if (!svgElement) return;
|
||||||
if (!svg) return;
|
|
||||||
|
|
||||||
const point = svg.createSVGPoint();
|
const point = svgElement.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(svgElement.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 || !svgElement) return;
|
||||||
|
const point = svgElement.createSVGPoint();
|
||||||
const svg = document.getElementById("shapesCanvas") as SVGSVGElement;
|
|
||||||
if (!svg) return;
|
|
||||||
|
|
||||||
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(svgElement.getScreenCTM()!.inverse());
|
||||||
|
|
||||||
updateDocWithShapeMove(draggedShape.id, x, y);
|
updateDocWithShapeMove(draggedShape.id, x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,6 +72,7 @@
|
|||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<svg
|
<svg
|
||||||
|
bind:this={svgElement}
|
||||||
id="shapesCanvas"
|
id="shapesCanvas"
|
||||||
viewBox="0 0 800 600"
|
viewBox="0 0 800 600"
|
||||||
role="presentation"
|
role="presentation"
|
||||||
|
|||||||
@ -71,7 +71,6 @@ export function updateDocWithNewTriangle(x1: number, y1: number, x2: number, y2:
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const updateDocWithShapeMove = (shapeId: string, x: number, y: number) => {
|
export const updateDocWithShapeMove = (shapeId: string, x: number, y: number) => {
|
||||||
console.log('Moving shape:', { shapeId, x, y }); // Debug log
|
|
||||||
handle.change((doc: AppDocument) => {
|
handle.change((doc: AppDocument) => {
|
||||||
const shape = doc.shapes.find(s => s.id === Number(shapeId));
|
const shape = doc.shapes.find(s => s.id === Number(shapeId));
|
||||||
if (!shape) return;
|
if (!shape) return;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user