UNPKG

tldraw

Version:

A tiny little drawing editor.

162 lines (161 loc) 5 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var Pointing_exports = {}; __export(Pointing_exports, { Pointing: () => Pointing }); module.exports = __toCommonJS(Pointing_exports); var import_editor = require("@tldraw/editor"); class Pointing extends import_editor.StateNode { static id = "pointing"; shape; markId = ""; enterTime = 0; onEnter() { this.enterTime = Date.now(); } onExit() { this.editor.setHintingShapes([]); } onPointerMove(info) { if (Date.now() - this.enterTime < 150) return; const { editor } = this; const { isPointing } = editor.inputs; if (!isPointing) return; const { originPagePoint, currentPagePoint } = editor.inputs; const currentDragDist = Math.abs(originPagePoint.x - currentPagePoint.x); const baseMinDragDistForFixedWidth = Math.sqrt( editor.getInstanceState().isCoarsePointer ? editor.options.coarseDragDistanceSquared : editor.options.dragDistanceSquared ); const minSquaredDragDist = baseMinDragDistForFixedWidth * 6 / editor.getZoomLevel(); if (currentDragDist > minSquaredDragDist) { const id = (0, import_editor.createShapeId)(); this.markId = editor.markHistoryStoppingPoint(`creating_text:${id}`); const shape = this.createTextShape(id, originPagePoint, false, currentDragDist); if (!shape) { this.cancel(); return; } this.shape = editor.getShape(shape); editor.select(id); editor.setCurrentTool("select.resizing", { ...info, target: "selection", handle: "right", isCreating: true, creatingMarkId: this.markId, // Make sure the cursor offset takes into account how far we've already dragged creationCursorOffset: { x: currentDragDist, y: 1 }, onInteractionEnd: "text", onCreate: () => { editor.setEditingShape(shape.id); } }); } } onPointerUp() { this.complete(); } onComplete() { this.cancel(); } onCancel() { this.cancel(); } onInterrupt() { this.cancel(); } complete() { this.editor.markHistoryStoppingPoint("creating text shape"); const id = (0, import_editor.createShapeId)(); const { originPagePoint } = this.editor.inputs; const shape = this.createTextShape(id, originPagePoint, true, 20); if (!shape) return; this.editor.select(id); this.editor.setEditingShape(id); } cancel() { this.parent.transition("idle"); this.editor.bailToMark(this.markId); } createTextShape(id, point, autoSize, width) { this.editor.createShape({ id, type: "text", x: point.x, y: point.y, props: { text: "", autoSize, w: width, scale: this.editor.user.getIsDynamicResizeMode() ? 1 / this.editor.getZoomLevel() : 1 } }); const shape = this.editor.getShape(id); if (!shape) { this.cancel(); return; } const bounds = this.editor.getShapePageBounds(shape); const delta = new import_editor.Vec(); if (autoSize) { switch (shape.props.textAlign) { case "start": { delta.x = 0; break; } case "middle": { delta.x = -bounds.width / 2; break; } case "end": { delta.x = -bounds.width; break; } } } else { delta.x = 0; } delta.y = -bounds.height / 2; if ((0, import_editor.isShapeId)(shape.parentId)) { const transform = this.editor.getShapeParentTransform(shape); delta.rot(-transform.rotation()); } const shapeX = shape.x + delta.x; const shapeY = shape.y + delta.y; if (this.editor.getInstanceState().isGridMode) { const topLeft = new import_editor.Vec(shapeX, shapeY); const gridSnappedPoint = (0, import_editor.maybeSnapToGrid)(topLeft, this.editor); const gridDelta = import_editor.Vec.Sub(topLeft, gridSnappedPoint); this.editor.updateShape({ ...shape, x: shapeX - gridDelta.x, y: shapeY - gridDelta.y }); } else { this.editor.updateShape({ ...shape, x: shapeX, y: shapeY }); } return shape; } } //# sourceMappingURL=Pointing.js.map