UNPKG

tldraw

Version:

A tiny little drawing editor.

8 lines (7 loc) 5.16 kB
{ "version": 3, "sources": ["../../../../src/lib/utils/frames/frames.ts"], "sourcesContent": ["import {\n\tBox,\n\tEditor,\n\tTLFrameShape,\n\tTLShape,\n\tTLShapeId,\n\tTLShapePartial,\n\tVec,\n\tcompact,\n\tkickoutOccludedShapes,\n} from '@tldraw/editor'\n\n/**\n * Remove a frame.\n *\n * @param editor - tldraw editor instance.\n * @param ids - Ids of the frames you wish to remove.\n *\n * @public\n */\nexport function removeFrame(editor: Editor, ids: TLShapeId[]) {\n\tconst frames = compact(\n\t\tids\n\t\t\t.map((id) => editor.getShape<TLFrameShape>(id))\n\t\t\t.filter((f) => f && editor.isShapeOfType(f, 'frame'))\n\t)\n\tif (!frames.length) return\n\n\tconst allChildren: TLShapeId[] = []\n\teditor.run(() => {\n\t\tframes.map((frame) => {\n\t\t\tconst children = editor.getSortedChildIdsForParent(frame.id)\n\t\t\tif (children.length) {\n\t\t\t\tkickoutOccludedShapes(editor, children, {\n\t\t\t\t\tfilter: (s) => !frames.find((f) => f.id === s.id),\n\t\t\t\t})\n\t\t\t\tallChildren.push(...children)\n\t\t\t}\n\t\t})\n\t\teditor.setSelectedShapes(allChildren)\n\t\teditor.deleteShapes(ids)\n\t})\n}\n\n/** @internal */\nexport const DEFAULT_FRAME_PADDING = 50\n\nexport function getFrameChildrenBounds(\n\tchildren: (TLShape | undefined)[],\n\teditor: Editor,\n\topts: { padding: number } = { padding: DEFAULT_FRAME_PADDING }\n) {\n\tconst bounds = Box.FromPoints(\n\t\tchildren.flatMap((shape) => {\n\t\t\tif (!shape) return []\n\t\t\tconst geometry = editor.getShapeGeometry(shape.id)\n\t\t\tconst transform = editor.getShapeLocalTransform(shape)\n\t\t\treturn transform?.applyToPoints(geometry.vertices) ?? []\n\t\t})\n\t)\n\n\tconst padding = opts.padding ?? DEFAULT_FRAME_PADDING\n\tconst w = bounds.w + 2 * padding\n\tconst h = bounds.h + 2 * padding\n\tconst dx = padding - bounds.minX\n\tconst dy = padding - bounds.minY\n\n\treturn { w, h, dx, dy }\n}\n\n/**\n * Fit a frame to its content.\n *\n * @param id - Id of the frame you wish to fit to content.\n * @param editor - tlraw editor instance.\n * @param opts - Options for fitting the frame.\n *\n * @public\n */\nexport function fitFrameToContent(editor: Editor, id: TLShapeId, opts = {} as { padding: number }) {\n\tconst frame = editor.getShape<TLFrameShape>(id)\n\tif (!frame) return\n\n\tconst childIds = editor.getSortedChildIdsForParent(frame.id)\n\tconst children = compact(childIds.map((id) => editor.getShape(id)))\n\tif (!children.length) return\n\n\tconst { w, h, dx, dy } = getFrameChildrenBounds(children, editor, opts)\n\n\t// The shapes already perfectly fit the frame.\n\tif (dx === 0 && dy === 0 && frame.props.w === w && frame.props.h === h) return\n\n\tconst diff = new Vec(dx, dy).rot(frame.rotation)\n\teditor.run(() => {\n\t\tconst changes: TLShapePartial[] = childIds.map((child) => {\n\t\t\tconst shape = editor.getShape(child)!\n\t\t\treturn {\n\t\t\t\tid: shape.id,\n\t\t\t\ttype: shape.type,\n\t\t\t\tx: shape.x + dx,\n\t\t\t\ty: shape.y + dy,\n\t\t\t}\n\t\t})\n\n\t\tchanges.push({\n\t\t\tid: frame.id,\n\t\t\ttype: frame.type,\n\t\t\tx: frame.x - diff.x,\n\t\t\ty: frame.y - diff.y,\n\t\t\tprops: {\n\t\t\t\tw,\n\t\t\t\th,\n\t\t\t},\n\t\t})\n\n\t\teditor.updateShapes(changes)\n\t})\n}\n"], "mappings": "AAAA;AAAA,EACC;AAAA,EAMA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAUA,SAAS,YAAY,QAAgB,KAAkB;AAC7D,QAAM,SAAS;AAAA,IACd,IACE,IAAI,CAAC,OAAO,OAAO,SAAuB,EAAE,CAAC,EAC7C,OAAO,CAAC,MAAM,KAAK,OAAO,cAAc,GAAG,OAAO,CAAC;AAAA,EACtD;AACA,MAAI,CAAC,OAAO,OAAQ;AAEpB,QAAM,cAA2B,C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