Files
AFFiNE-Mirror/libs/components/board-tools/src/triangle-tool/triangle-tool.ts
T
2022-07-22 15:49:21 +08:00

50 lines
1.5 KiB
TypeScript

import { Utils, TLPointerEventHandler, TLBoundsCorner } from '@tldraw/core';
import Vec from '@tldraw/vec';
import { Triangle } from '@toeverything/components/board-shapes';
import { SessionType, TDShapeType } from '@toeverything/components/board-types';
import { BaseTool, BaseToolStatus } from '@toeverything/components/board-state';
export class TriangleTool extends BaseTool {
override type = TDShapeType.Triangle as const;
/* ----------------- Event Handlers ----------------- */
override onPointerDown: TLPointerEventHandler = () => {
if (this.status !== BaseToolStatus.Idle) return;
const {
currentPoint,
currentGrid,
settings: { showGrid },
appState: { currentPageId, currentStyle },
document: { id: workspace },
} = this.app;
const childIndex = this.getNextChildIndex();
const id = Utils.uniqueId();
const newShape = Triangle.create({
id,
parentId: currentPageId,
childIndex,
point: showGrid
? Vec.snap(currentPoint, currentGrid)
: currentPoint,
style: { ...currentStyle },
workspace,
});
this.app.patchCreate([newShape]);
this.app.startSession(
SessionType.TransformSingle,
newShape.id,
TLBoundsCorner.BottomRight,
true
);
this.set_status(BaseToolStatus.Creating);
};
}