Tagged obsolete code, removed some code duplication, working version

This commit is contained in:
Stephan Egli 2025-01-20 10:51:56 +01:00
parent 6a9b95309c
commit 4a6b461045
8 changed files with 14 additions and 27 deletions

View File

@ -1,5 +1,5 @@
import { Circle } from './shapes/Circle'; import { Circle } from './shapes/Circle.ts-obsolete';
import { Triangle } from './shapes/Triangle'; import { Triangle } from './shapes/Triangle.ts-obsolete';
import { Shape, Patch, CircleShape, TriangleShape } from './types'; import { Shape, Patch, CircleShape, TriangleShape } from './types';
// Main function to handle both initial render and updates of shapes // Main function to handle both initial render and updates of shapes
@ -28,6 +28,7 @@ export function createOrUpdateShapes(
// Process each patch to update, add, or remove shapes // Process each patch to update, add, or remove shapes
patches.forEach(patch => { patches.forEach(patch => {
console.log('Processing patch:', patch); // Debug log
if (patch.path[0] === 'shapes') { if (patch.path[0] === 'shapes') {
if (patch.action === 'put') { if (patch.action === 'put') {
// Handle shape addition or update // Handle shape addition or update

View File

@ -22,7 +22,9 @@
} | null>(null); } | null>(null);
// Automerge document subscription // Automerge document subscription
// This is called once
$effect(() => { $effect(() => {
console.log('Subscribing to document changes'); // Debug log
const doc = handle.docSync(); const doc = handle.docSync();
if (doc) { if (doc) {
shapes = doc.shapes; shapes = doc.shapes;
@ -31,6 +33,8 @@
handle.on("change", ({ doc }) => { handle.on("change", ({ doc }) => {
if (doc) { if (doc) {
console.log('Document changed:', doc.shapes); // Debug log
// TODO: would it make sense to use patches here to have fine grained reactivity ?
shapes = doc.shapes; shapes = doc.shapes;
} }
}); });
@ -75,19 +79,7 @@
id="shapesCanvas" id="shapesCanvas"
viewBox="0 0 800 600" viewBox="0 0 800 600"
role="presentation" role="presentation"
onmousemove={(e) => { onmousemove={(e) => {handleDragMove(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} onmouseup={() => draggedShape = null}
> >
{#each shapes as shape (shape.id)} {#each shapes as shape (shape.id)}

View File

@ -1,12 +1,5 @@
<script lang="ts"> <script lang="ts">
const { id, x, y, radius, color, handleDragStart } = $props<{ const { id, x, y, radius, color, handleDragStart } = $props();
id: string;
x: number;
y: number;
radius: number;
color: string;
handleDragStart: (e: MouseEvent, shape: any) => void;
}>();
const shape = { const shape = {
id, id,

View File

@ -71,6 +71,7 @@ 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;

View File

@ -1,5 +1,5 @@
import { Shape } from './Shape'; import { Shape } from './Shape.ts-obsolete';
import { CircleProps } from './types'; import { CircleProps } from './types.ts-obsolete';
export class Circle extends Shape { export class Circle extends Shape {
private x: number; private x: number;

View File

@ -1,5 +1,5 @@
import { Shape } from './Shape'; import { Shape } from './Shape.ts-obsolete';
import { Coordinate, TriangleProps } from './types'; import { Coordinate, TriangleProps } from './types.ts-obsolete';
export class Triangle extends Shape { export class Triangle extends Shape {
private coordinates: [Coordinate, Coordinate, Coordinate]; private coordinates: [Coordinate, Coordinate, Coordinate];