Implement RectTool.ts

This commit is contained in:
2026-06-22 07:23:29 +00:00
parent d1591e3b9d
commit f3c0c65f8d
+43
View File
@@ -0,0 +1,43 @@
import { Tool } from '../Tool';
import { YjsDocument } from '../../crdt/YjsDocument';
class RectTool extends Tool {
private startPoint: { x: number; y: number } | null = null;
constructor(yjsDoc: YjsDocument) {
super(yjsDoc);
this.name = 'RectTool';
}
onMouseDown(event: MouseEvent): void {
if (!this.startPoint) {
this.startPoint = { x: event.clientX, y: event.clientY };
} else {
// Create rectangle element in Yjs document
const x = Math.min(this.startPoint.x, event.clientX);
const y = Math.min(this.startPoint.y, event.clientY);
const width = Math.abs(event.clientX - this.startPoint.x);
const height = Math.abs(event.clientY - this.startPoint.y);
this.yjsDoc.createRectangle(x, y, width, height);
this.startPoint = null;
}
}
onMouseMove(event: MouseEvent): void {
// Preview rectangle while dragging
}
onMouseUp(event: MouseEvent): void {
// Handle mouse up if needed
}
onKeyUp(event: KeyboardEvent): void {
// Handle key up events
}
reset(): void {
this.startPoint = null;
}
}
export { RectTool };