51 lines
1.2 KiB
TypeScript
51 lines
1.2 KiB
TypeScript
|
|
import { Tool } from '../Tool';
|
||
|
|
import { YjsDocument } from '../../crdt/YjsDocument';
|
||
|
|
|
||
|
|
class PolylineTool extends Tool {
|
||
|
|
private points: Array<{ x: number; y: number }> = [];
|
||
|
|
private isDrawing: boolean = false;
|
||
|
|
|
||
|
|
constructor(yjsDoc: YjsDocument) {
|
||
|
|
super(yjsDoc);
|
||
|
|
this.name = 'PolylineTool';
|
||
|
|
}
|
||
|
|
|
||
|
|
onMouseDown(event: MouseEvent): void {
|
||
|
|
if (!this.isDrawing) {
|
||
|
|
// Start drawing
|
||
|
|
this.isDrawing = true;
|
||
|
|
this.points = [{ x: event.clientX, y: event.clientY }];
|
||
|
|
} else {
|
||
|
|
// Add point to polyline
|
||
|
|
this.points.push({ x: event.clientX, y: event.clientY });
|
||
|
|
|
||
|
|
// If shift key is not pressed, finish the polyline
|
||
|
|
if (!event.shiftKey) {
|
||
|
|
this.yjsDoc.createPolyline([...this.points]);
|
||
|
|
this.reset();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
onMouseMove(event: MouseEvent): void {
|
||
|
|
// Preview polyline while drawing
|
||
|
|
}
|
||
|
|
|
||
|
|
onMouseUp(event: MouseEvent): void {
|
||
|
|
// Handle mouse up if needed
|
||
|
|
}
|
||
|
|
|
||
|
|
onKeyUp(event: KeyboardEvent): void {
|
||
|
|
// Handle key up events
|
||
|
|
if (event.key === 'Escape') {
|
||
|
|
this.reset();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
reset(): void {
|
||
|
|
this.points = [];
|
||
|
|
this.isDrawing = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export { PolylineTool };
|