Refactored main.js for better code organization
This commit is contained in:
parent
39a6efc076
commit
bfe44bec1e
115
src/main.js
115
src/main.js
@ -2,12 +2,51 @@
|
|||||||
import { initRepo, addCircle, addTriangle, moveShape } from "./docManager.js"
|
import { initRepo, addCircle, addTriangle, moveShape } from "./docManager.js"
|
||||||
import { createOrUpdateShapes } from "./canvasManager.js"
|
import { createOrUpdateShapes } from "./canvasManager.js"
|
||||||
|
|
||||||
|
const CONFIG = {
|
||||||
|
THROTTLE_MS: 16, // 60fps
|
||||||
|
DEFAULT_SHAPES: {
|
||||||
|
CIRCLE: { x: 100, y: 100, radius: 30 },
|
||||||
|
TRIANGLE: { x1: 200, y1: 200, x2: 220, y2: 220, x3: 180, y3: 220 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Group related event handlers together
|
||||||
|
function initDragAndDrop(container) {
|
||||||
let isDragging = false;
|
let isDragging = false;
|
||||||
let selectedShapeId = null;
|
let selectedShapeId = null;
|
||||||
let offsetX = 0;
|
|
||||||
let offsetY = 0;
|
|
||||||
|
|
||||||
const container = document.getElementById("shapesCanvas");
|
const throttledMoveShape = throttle((shapeId, x, y) => {
|
||||||
|
moveShape(shapeId, x, y);
|
||||||
|
}, CONFIG.THROTTLE_MS);
|
||||||
|
|
||||||
|
container.addEventListener("mousedown", (e) => {
|
||||||
|
const clickedElement = e.target;
|
||||||
|
if (clickedElement.matches('circle, polygon')) {
|
||||||
|
isDragging = true;
|
||||||
|
selectedShapeId = clickedElement.id;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
container.addEventListener("mousemove", (e) => {
|
||||||
|
if (!isDragging || !selectedShapeId) return;
|
||||||
|
|
||||||
|
const svgPoint = getSVGPoint(container, e.clientX, e.clientY);
|
||||||
|
throttledMoveShape(selectedShapeId, svgPoint.x, svgPoint.y);
|
||||||
|
});
|
||||||
|
|
||||||
|
container.addEventListener("mouseup", () => {
|
||||||
|
isDragging = false;
|
||||||
|
selectedShapeId = null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function for SVG coordinate conversion
|
||||||
|
function getSVGPoint(container, x, y) {
|
||||||
|
const pt = container.createSVGPoint();
|
||||||
|
pt.x = x;
|
||||||
|
pt.y = y;
|
||||||
|
return pt.matrixTransform(container.getScreenCTM().inverse());
|
||||||
|
}
|
||||||
|
|
||||||
// Add throttle function at the top
|
// Add throttle function at the top
|
||||||
function throttle(func, limit) {
|
function throttle(func, limit) {
|
||||||
@ -21,65 +60,35 @@ function throttle(func, limit) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize the document
|
async function initApp() {
|
||||||
const handle = await initRepo()
|
const container = document.getElementById("shapesCanvas");
|
||||||
|
const handle = await initRepo();
|
||||||
|
|
||||||
|
// Initial render
|
||||||
|
createOrUpdateShapes(container, handle.docSync().shapes);
|
||||||
|
|
||||||
// draw current state
|
// Subscribe to changes
|
||||||
createOrUpdateShapes(container, handle.docSync().shapes)
|
|
||||||
|
|
||||||
// 2. Subscribe so we can re-render whenever doc changes
|
|
||||||
handle.on("change", (change) => {
|
handle.on("change", (change) => {
|
||||||
createOrUpdateShapes(container, change.doc.shapes, change.patches);
|
createOrUpdateShapes(container, change.doc.shapes, change.patches);
|
||||||
});
|
});
|
||||||
|
|
||||||
// 3. Buttons holen
|
// Initialize shape buttons
|
||||||
const addCircleButton = document.getElementById("addCircleButton");
|
initShapeButtons();
|
||||||
const addTriangleButton = document.getElementById("addTriangleButton");
|
initDragAndDrop(container);
|
||||||
|
|
||||||
// 4. Event Listener für Buttons
|
|
||||||
addCircleButton.addEventListener("click", () => {
|
|
||||||
// z. B. zufällige Position/Radius
|
|
||||||
addCircle(100, 100, 30);
|
|
||||||
});
|
|
||||||
|
|
||||||
addTriangleButton.addEventListener("click", () => {
|
|
||||||
// z. B. zufällige Koordinaten
|
|
||||||
addTriangle( 200, 200, 220, 220, 180, 220);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Mousedown: Prüfen, ob wir auf ein Shape klicken
|
|
||||||
container.addEventListener("mousedown", (e) => {
|
|
||||||
const clickedElement = e.target;
|
|
||||||
if (clickedElement.tagName === 'circle' || clickedElement.tagName === 'polygon') {
|
|
||||||
isDragging = true;
|
|
||||||
selectedShapeId = clickedElement.id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function initShapeButtons() {
|
||||||
|
document.getElementById("addCircleButton").addEventListener("click", () => {
|
||||||
|
const { x, y, radius } = CONFIG.DEFAULT_SHAPES.CIRCLE;
|
||||||
|
addCircle(x, y, radius);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Throttled version of moveShape
|
document.getElementById("addTriangleButton").addEventListener("click", () => {
|
||||||
const throttledMoveShape = throttle((shapeId, x, y) => {
|
const { x1, y1, x2, y2, x3, y3 } = CONFIG.DEFAULT_SHAPES.TRIANGLE;
|
||||||
moveShape(shapeId, x, y);
|
addTriangle(x1, y1, x2, y2, x3, y3);
|
||||||
}, 16); // 50ms throttle time (20 updates per second)
|
|
||||||
|
|
||||||
// Mousemove: Shape verschieben, sofern dragging aktiv ist
|
|
||||||
container.addEventListener("mousemove", (e) => {
|
|
||||||
if (!isDragging || selectedShapeId === null) return;
|
|
||||||
|
|
||||||
const pt = container.createSVGPoint();
|
|
||||||
pt.x = e.clientX;
|
|
||||||
pt.y = e.clientY;
|
|
||||||
const svgP = pt.matrixTransform(container.getScreenCTM().inverse());
|
|
||||||
|
|
||||||
const newX = svgP.x - offsetX;
|
|
||||||
const newY = svgP.y - offsetY;
|
|
||||||
|
|
||||||
throttledMoveShape(selectedShapeId, newX, newY);
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Mouseup: Dragging beenden
|
// Start the app
|
||||||
container.addEventListener("mouseup", () => {
|
initApp().catch(console.error);
|
||||||
isDragging = false;
|
|
||||||
selectedShapeId = null;
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user