UNPKG

tldraw

Version:

A tiny little drawing editor.

79 lines (78 loc) 2.37 kB
import { StateNode } from "@tldraw/editor"; import { clearArrowTargetState, updateArrowTargetState } from "../arrowTargetState.mjs"; class Idle extends StateNode { static id = "idle"; options = this.editor.getShapeUtil("arrow").options; isPrecise = false; isPreciseTimerId = null; preciseTargetId = null; onPointerMove() { this.update(); } onPointerDown(info) { this.parent.transition("pointing", { ...info, isPrecise: this.isPrecise }); } onEnter() { this.editor.setCursor({ type: "cross", rotation: 0 }); this.update(); } onCancel() { this.editor.setCurrentTool("select"); } onExit() { clearArrowTargetState(this.editor); if (this.isPreciseTimerId !== null) { clearTimeout(this.isPreciseTimerId); } } onKeyDown() { this.update(); } onKeyUp(info) { this.update(); if (info.key === "Enter") { if (this.editor.getIsReadonly()) return null; const onlySelectedShape = this.editor.getOnlySelectedShape(); if (onlySelectedShape && this.editor.getShapeUtil(onlySelectedShape).canEdit(onlySelectedShape)) { this.editor.setCurrentTool("select"); this.editor.setEditingShape(onlySelectedShape.id); this.editor.root.getCurrent()?.transition("editing_shape", { ...info, target: "shape", shape: onlySelectedShape }); } } } update() { const targetState = updateArrowTargetState({ editor: this.editor, pointInPageSpace: this.editor.inputs.currentPagePoint, arrow: void 0, isPrecise: this.isPrecise, isExact: this.editor.inputs.altKey, currentBinding: void 0, oppositeBinding: void 0 }); if (targetState && targetState.target.id !== this.preciseTargetId) { if (this.isPreciseTimerId !== null) { clearTimeout(this.isPreciseTimerId); } this.preciseTargetId = targetState.target.id; this.isPreciseTimerId = this.editor.timers.setTimeout(() => { this.isPrecise = true; this.update(); }, this.options.hoverPreciseTimeout); } else if (!targetState && this.preciseTargetId) { this.isPrecise = false; this.preciseTargetId = null; if (this.isPreciseTimerId !== null) { clearTimeout(this.isPreciseTimerId); } } } } export { Idle }; //# sourceMappingURL=Idle.mjs.map