tldraw
Version:
A tiny little drawing editor.
8 lines (7 loc) • 5.93 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../../../src/lib/shapes/note/toolStates/Pointing.ts"],
"sourcesContent": ["import {\n\tEditor,\n\tStateNode,\n\tTLNoteShape,\n\tTLPointerEventInfo,\n\tTLShapeId,\n\tVec,\n\tcreateShapeId,\n\tmaybeSnapToGrid,\n} from '@tldraw/editor'\n\nimport {\n\tNOTE_ADJACENT_POSITION_SNAP_RADIUS,\n\tgetAvailableNoteAdjacentPositions,\n} from '../noteHelpers'\n\nexport class Pointing extends StateNode {\n\tstatic override id = 'pointing'\n\n\tdragged = false\n\n\tinfo = {} as TLPointerEventInfo\n\n\tmarkId = ''\n\n\tshape = {} as TLNoteShape\n\n\toverride onEnter() {\n\t\tconst { editor } = this\n\n\t\tconst id = createShapeId()\n\t\tthis.markId = editor.markHistoryStoppingPoint(`creating_note:${id}`)\n\n\t\t// Check for note pits; if the pointer is close to one, place the note centered on the pit\n\t\tconst center = this.editor.inputs.originPagePoint.clone()\n\t\tconst offset = getNoteShapeAdjacentPositionOffset(\n\t\t\tthis.editor,\n\t\t\tcenter,\n\t\t\tthis.editor.user.getIsDynamicResizeMode() ? 1 / this.editor.getZoomLevel() : 1\n\t\t)\n\t\tif (offset) {\n\t\t\tcenter.sub(offset)\n\t\t}\n\t\tthis.shape = createNoteShape(this.editor, id, center)\n\t}\n\n\toverride onPointerMove(info: TLPointerEventInfo) {\n\t\tif (this.editor.inputs.isDragging) {\n\t\t\tthis.editor.setCurrentTool('select.translating', {\n\t\t\t\t...info,\n\t\t\t\ttarget: 'shape',\n\t\t\t\tshape: this.shape,\n\t\t\t\tonInteractionEnd: 'note',\n\t\t\t\tisCreating: true,\n\t\t\t\tcreatingMarkId: this.markId,\n\t\t\t\tonCreate: () => {\n\t\t\t\t\tthis.editor.setEditingShape(this.shape.id)\n\t\t\t\t\tthis.editor.setCurrentTool('select.editing_shape')\n\t\t\t\t},\n\t\t\t})\n\t\t}\n\t}\n\n\toverride onPointerUp() {\n\t\tthis.complete()\n\t}\n\n\toverride onInterrupt() {\n\t\tthis.cancel()\n\t}\n\n\toverride onComplete() {\n\t\tthis.complete()\n\t}\n\n\toverride onCancel() {\n\t\tthis.cancel()\n\t}\n\n\tprivate complete() {\n\t\tif (this.editor.getInstanceState().isToolLocked) {\n\t\t\tthis.parent.transition('idle')\n\t\t} else {\n\t\t\tthis.editor.setEditingShape(this.shape.id)\n\t\t\tthis.editor.setCurrentTool('select.editing_shape', {\n\t\t\t\t...this.info,\n\t\t\t\ttarget: 'shape',\n\t\t\t\tshape: this.shape,\n\t\t\t})\n\t\t}\n\t}\n\n\tprivate cancel() {\n\t\tthis.editor.bailToMark(this.markId)\n\t\tthis.parent.transition('idle', this.info)\n\t}\n}\n\nexport function getNoteShapeAdjacentPositionOffset(editor: Editor, center: Vec, scale: number) {\n\tlet min = NOTE_ADJACENT_POSITION_SNAP_RADIUS / editor.getZoomLevel() // in screen space\n\tlet offset: Vec | undefined\n\tfor (const pit of getAvailableNoteAdjacentPositions(editor, 0, scale, 0)) {\n\t\t// only check page rotations of zero\n\t\tconst deltaToPit = Vec.Sub(center, pit)\n\t\tconst dist = deltaToPit.len()\n\t\tif (dist < min) {\n\t\t\tmin = dist\n\t\t\toffset = deltaToPit\n\t\t}\n\t}\n\treturn offset\n}\n\nexport function createNoteShape(editor: Editor, id: TLShapeId, center: Vec) {\n\teditor\n\t\t.createShape({\n\t\t\tid,\n\t\t\ttype: 'note',\n\t\t\tx: center.x,\n\t\t\ty: center.y,\n\t\t\tprops: {\n\t\t\t\tscale: editor.user.getIsDynamicResizeMode() ? 1 / editor.getZoomLevel() : 1,\n\t\t\t},\n\t\t})\n\t\t.select(id)\n\n\tconst shape = editor.getShape<TLNoteShape>(id)!\n\tconst bounds = editor.getShapeGeometry(shape).bounds\n\tconst newPoint = maybeSnapToGrid(\n\t\tnew Vec(shape.x - bounds.width / 2, shape.y - bounds.height / 2),\n\t\teditor\n\t)\n\n\t// Center the text around the created point\n\teditor.updateShapes([\n\t\t{\n\t\t\tid,\n\t\t\ttype: 'note',\n\t\t\tx: newPoint.x,\n\t\t\ty: newPoint.y,\n\t\t},\n\t])\n\n\treturn editor.getShape<TLNoteShape>(id)!\n}\n"],
"mappings": "AAAA;AAAA,EAEC;AAAA,EAIA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAEP;AAAA,EACC;AAAA,EACA;AAAA,OACM;AAEA,MAAM,iBAAiB,UAAU;AAAA,EACvC,OAAgB,KAAK;AAAA,EAErB,UAAU;AAAA,EAEV,OAAO,CAAC;AAAA,EAER,SAAS;AAAA,EAET,QAAQ,CAAC;AAAA,EAEA,UAAU;AAClB,UAAM,EAAE,OAAO,IAAI;AAEnB,UAAM,KAAK,cAAc;AACzB,SAAK,SAAS,OAAO,yBAAyB,iBAAiB,EAAE,EAAE;AAGnE,UAAM,SAAS,KAAK,OAAO,OAAO,gBAAgB,MAAM;AACxD,UAAM,SAAS;AAAA,MACd,KAAK;AAAA,MACL;AAAA,MACA,KAAK,OAAO,KAAK,uBAAuB,IAAI,IAAI,KAAK,OAAO,aAAa,IAAI;AAAA,IAC9E;AACA,QAAI,QAAQ;AACX,aAAO,IAAI,MAAM;AAAA,IAClB;AACA,SAAK,QAAQ,gBAAgB,KAAK,QAAQ,IAAI,MAAM;AAAA,EACrD;AAAA,EAES,cAAc,MAA0B;AAChD,QAAI,KAAK,OAAO,OAAO,YAAY;AAClC,WAAK,OAAO,eAAe,sBAAsB;AAAA,QAChD,GAAG;AAAA,QACH,QAAQ;AAAA,QACR,OAAO,KAAK;AAAA,QACZ,kBAAkB;AAAA,QAClB,YAAY;AAAA,QACZ,gBAAgB,KAAK;AAAA,QACrB,UAAU,MAAM;AACf,eAAK,OAAO,gBAAgB,KAAK,MAAM,EAAE;AACzC,eAAK,OAAO,eAAe,sBAAsB;AAAA,QAClD;AAAA,MACD,CAAC;AAAA,IACF;AAAA,EACD;AAAA,EAES,cAAc;AACtB,SAAK,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