UNPKG

tldraw

Version:

A tiny little drawing editor.

160 lines (159 loc) 4.58 kB
import { StateNode, createShapeId, maybeSnapToGrid } from "@tldraw/editor"; import { clearArrowTargetState, updateArrowTargetState } from "../arrowTargetState.mjs"; class Pointing extends StateNode { static id = "pointing"; shape; isPrecise = false; isPreciseTimerId = null; markId = ""; onEnter(info) { this.markId = ""; this.isPrecise = !!info.isPrecise; const targetState = updateArrowTargetState({ editor: this.editor, pointInPageSpace: this.editor.inputs.getCurrentPagePoint(), arrow: void 0, isPrecise: this.isPrecise, currentBinding: void 0, oppositeBinding: void 0 }); if (!targetState) { this.createArrowShape(); if (!this.shape) { this.cancel(); return; } } this.startPreciseTimeout(); } onExit() { this.shape = void 0; clearArrowTargetState(this.editor); this.clearPreciseTimeout(); } onPointerMove() { if (this.editor.inputs.getIsDragging()) { if (!this.shape) { this.createArrowShape(); } if (!this.shape) { this.cancel(); return; } this.updateArrowShapeEndHandle(); this.editor.setCurrentTool("select.dragging_handle", { shape: this.shape, handle: { id: "end", type: "vertex", index: "a3", x: 0, y: 0 }, isCreating: true, creatingMarkId: this.markId || void 0, onInteractionEnd: "arrow" }); } } onPointerUp() { this.cancel(); } onCancel() { this.cancel(); } onComplete() { this.cancel(); } onInterrupt() { this.cancel(); } cancel() { if (this.shape) { this.editor.bailToMark(this.markId); } this.parent.transition("idle"); } createArrowShape() { const originPagePoint = this.editor.inputs.getOriginPagePoint(); const id = createShapeId(); this.markId = this.editor.markHistoryStoppingPoint(`creating_arrow:${id}`); const newPoint = maybeSnapToGrid(originPagePoint, this.editor); this.editor.createShape({ id, type: "arrow", x: newPoint.x, y: newPoint.y, props: { scale: this.editor.getResizeScaleFactor() } }); const shape = this.editor.getShape(id); if (!shape) return; const handles = this.editor.getShapeHandles(shape); if (!handles) throw Error(`expected handles for arrow`); const util = this.editor.getShapeUtil("arrow"); const initial = this.shape; const startHandle = handles.find((h) => h.id === "start"); const change = util.onHandleDrag?.(shape, { handle: { ...startHandle, x: 0, y: 0 }, isPrecise: true, isCreatingShape: true, initial }); if (change) { this.editor.updateShapes([change]); } this.shape = this.editor.getShape(id); this.editor.select(id); } updateArrowShapeEndHandle() { const shape = this.shape; if (!shape) throw Error(`expected shape`); const handles = this.editor.getShapeHandles(shape); if (!handles) throw Error(`expected handles for arrow`); { const util = this.editor.getShapeUtil("arrow"); const initial = this.shape; const startHandle = handles.find((h) => h.id === "start"); const change = util.onHandleDrag?.(shape, { handle: { ...startHandle, x: 0, y: 0 }, isPrecise: this.isPrecise, isCreatingShape: true, initial }); if (change) { this.editor.updateShapes([change]); } } { const util = this.editor.getShapeUtil("arrow"); const initial = this.shape; const point = this.editor.getPointInShapeSpace( shape, this.editor.inputs.getCurrentPagePoint() ); const endHandle = handles.find((h) => h.id === "end"); const change = util.onHandleDrag?.(this.editor.getShape(shape), { handle: { ...endHandle, x: point.x, y: point.y }, isPrecise: this.isPrecise, isCreatingShape: true, initial }); if (change) { this.editor.updateShapes([change]); } } this.shape = this.editor.getShape(shape.id); } startPreciseTimeout() { const arrowUtil = this.editor.getShapeUtil("arrow"); this.isPreciseTimerId = this.editor.timers.setTimeout(() => { if (!this.getIsActive()) return; this.isPrecise = true; }, arrowUtil.options.pointingPreciseTimeout); } clearPreciseTimeout() { if (this.isPreciseTimerId !== null) { clearTimeout(this.isPreciseTimerId); } } } export { Pointing }; //# sourceMappingURL=Pointing.mjs.map