UNPKG

tldraw

Version:

A tiny little drawing editor.

136 lines (135 loc) 4.04 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, createNoteShape: () => createNoteShape, getNoteShapeAdjacentPositionOffset: () => getNoteShapeAdjacentPositionOffset }); module.exports = __toCommonJS(Pointing_exports); var import_editor = require("@tldraw/editor"); var import_selectHelpers = require("../../../tools/SelectTool/selectHelpers"); var import_noteHelpers = require("../noteHelpers"); class Pointing extends import_editor.StateNode { static id = "pointing"; dragged = false; info = {}; markId = ""; shape = {}; onEnter() { const { editor } = this; const id = (0, import_editor.createShapeId)(); this.markId = editor.markHistoryStoppingPoint(`creating_note:${id}`); const center = this.editor.inputs.getOriginPagePoint().clone(); const offset = getNoteShapeAdjacentPositionOffset( this.editor, center, this.editor.getResizeScaleFactor() ); if (offset) { center.sub(offset); } const shape = createNoteShape(this.editor, id, center); if (shape) { this.shape = shape; } else { this.cancel(); } } onPointerMove(info) { if (this.editor.inputs.getIsDragging()) { this.editor.setCurrentTool("select.translating", { ...info, target: "shape", shape: this.shape, onInteractionEnd: "note", isCreating: true, creatingMarkId: this.markId, onCreate: () => { (0, import_selectHelpers.startEditingShapeWithRichText)(this.editor, this.shape.id); } }); } } onPointerUp() { this.complete(); } onInterrupt() { this.cancel(); } onComplete() { this.complete(); } onCancel() { this.cancel(); } complete() { if (this.editor.getInstanceState().isToolLocked) { this.parent.transition("idle"); } else { (0, import_selectHelpers.startEditingShapeWithRichText)(this.editor, this.shape.id, { info: this.info }); } } cancel() { this.editor.bailToMark(this.markId); this.parent.transition("idle", this.info); } } function getNoteShapeAdjacentPositionOffset(editor, center, scale) { let min = import_noteHelpers.NOTE_ADJACENT_POSITION_SNAP_RADIUS / editor.getZoomLevel(); let offset; for (const pit of (0, import_noteHelpers.getAvailableNoteAdjacentPositions)(editor, 0, scale, 0)) { const deltaToPit = import_editor.Vec.Sub(center, pit); const dist = deltaToPit.len(); if (dist < min) { min = dist; offset = deltaToPit; } } return offset; } function createNoteShape(editor, id, center) { editor.createShape({ id, type: "note", x: center.x, y: center.y, props: { scale: editor.getResizeScaleFactor() } }); const shape = editor.getShape(id); if (!shape) return; editor.select(id); const bounds = editor.getShapeGeometry(shape).bounds; const newPoint = (0, import_editor.maybeSnapToGrid)( new import_editor.Vec(shape.x - bounds.width / 2, shape.y - bounds.height / 2), editor ); editor.updateShapes([ { id, type: "note", x: newPoint.x, y: newPoint.y } ]); return editor.getShape(id); } //# sourceMappingURL=Pointing.js.map