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 { Triangle } from './shapes/Triangle';
import { Circle } from './shapes/Circle.ts-obsolete';
import { Triangle } from './shapes/Triangle.ts-obsolete';
import { Shape, Patch, CircleShape, TriangleShape } from './types';
// 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
patches.forEach(patch => {
console.log('Processing patch:', patch); // Debug log
if (patch.path[0] === 'shapes') {
if (patch.action === 'put') {
// Handle shape addition or update

View File

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

View File

@ -1,12 +1,5 @@
<script lang="ts">
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 { id, x, y, radius, color, handleDragStart } = $props();
const shape = {
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) => {
console.log('Moving shape:', { shapeId, x, y }); // Debug log
handle.change((doc: AppDocument) => {
const shape = doc.shapes.find(s => s.id === Number(shapeId));
if (!shape) return;

View File

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

View File

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