{
  "version": 3,
  "sources": ["../../../../src/lib/ui/hooks/useFlatten.ts"],
  "sourcesContent": ["import {\n\tBox,\n\tEditor,\n\tIndexKey,\n\tTLImageAsset,\n\tTLImageShape,\n\tTLShape,\n\tTLShapeId,\n\tVec,\n\tcompact,\n\tcreateShapeId,\n\tisShapeId,\n\ttransact,\n\tuseEditor,\n} from '@tldraw/editor'\nimport { useCallback } from 'react'\n\nexport async function flattenShapesToImages(\n\teditor: Editor,\n\tshapeIds: TLShapeId[],\n\tflattenImageBoundsExpand?: number\n) {\n\tconst shapes = compact(\n\t\tshapeIds.map((id) => {\n\t\t\tconst shape = editor.getShape(id)\n\t\t\tif (!shape) return\n\t\t\tconst util = editor.getShapeUtil(shape.type)\n\t\t\t// skip shapes that don't have a toSvg method\n\t\t\tif (util.toSvg === undefined) return\n\t\t\treturn shape\n\t\t})\n\t)\n\n\tif (shapes.length === 0) return\n\n\t// Don't flatten if it's just one image\n\tif (shapes.length === 1) {\n\t\tconst shape = shapes[0]\n\t\tif (!shape) return\n\t\tif (editor.isShapeOfType(shape, 'image')) return\n\t}\n\n\tconst groups: { shapes: TLShape[]; bounds: Box; asset?: TLImageAsset }[] = []\n\n\tif (flattenImageBoundsExpand !== undefined) {\n\t\tconst expandedBounds = shapes.map((shape) => {\n\t\t\treturn {\n\t\t\t\tshape,\n\t\t\t\tbounds: editor.getShapeMaskedPageBounds(shape)!.clone().expandBy(flattenImageBoundsExpand),\n\t\t\t}\n\t\t})\n\n\t\tfor (let i = 0; i < expandedBounds.length; i++) {\n\t\t\tconst item = expandedBounds[i]\n\t\t\tif (i === 0) {\n\t\t\t\tgroups[0] = {\n\t\t\t\t\tshapes: [item.shape],\n\t\t\t\t\tbounds: item.bounds,\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\tlet didLand = false\n\n\t\t\tfor (const group of groups) {\n\t\t\t\tif (group.bounds.includes(item.bounds)) {\n\t\t\t\t\tgroup.shapes.push(item.shape)\n\t\t\t\t\tgroup.bounds.expand(item.bounds)\n\t\t\t\t\tdidLand = true\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (!didLand) {\n\t\t\t\tgroups.push({\n\t\t\t\t\tshapes: [item.shape],\n\t\t\t\t\tbounds: item.bounds,\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\t} else {\n\t\tconst bounds = Box.Common(shapes.map((shape) => editor.getShapeMaskedPageBounds(shape)!))\n\t\tgroups.push({\n\t\t\tshapes,\n\t\t\tbounds,\n\t\t})\n\t}\n\n\tconst padding = editor.options.flattenImageBoundsPadding\n\n\tfor (const group of groups) {\n\t\tif (flattenImageBoundsExpand !== undefined) {\n\t\t\t// shrink the bounds again, removing the expanded area\n\t\t\tgroup.bounds.expandBy(-flattenImageBoundsExpand)\n\t\t}\n\n\t\t// get an image for the shapes\n\t\tconst svgResult = await editor.getSvgString(group.shapes, {\n\t\t\tpadding,\n\t\t})\n\t\tif (!svgResult?.svg) continue\n\n\t\t// get an image asset for the image\n\t\tconst blob = new Blob([svgResult.svg], { type: 'image/svg+xml' })\n\t\tconst asset = (await editor.getAssetForExternalContent({\n\t\t\ttype: 'file',\n\t\t\tfile: new File([blob], 'asset.svg', { type: 'image/svg+xml' }),\n\t\t})) as TLImageAsset\n\t\tif (!asset) continue\n\n\t\t// add it to the group\n\t\tgroup.asset = asset\n\t}\n\n\tconst createdShapeIds: TLShapeId[] = []\n\n\ttransact(() => {\n\t\tfor (const group of groups) {\n\t\t\tconst { asset, bounds, shapes } = group\n\t\t\tif (!asset) continue\n\n\t\t\tconst commonAncestorId = editor.findCommonAncestor(shapes) ?? editor.getCurrentPageId()\n\t\t\tif (!commonAncestorId) continue\n\n\t\t\tlet index: IndexKey = 'a1' as IndexKey\n\t\t\tfor (const shape of shapes) {\n\t\t\t\tif (shape.parentId === commonAncestorId) {\n\t\t\t\t\tif (shape.index > index) {\n\t\t\t\t\t\tindex = shape.index\n\t\t\t\t\t}\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlet x: number\n\t\t\tlet y: number\n\t\t\tlet rotation: number\n\n\t\t\tif (isShapeId(commonAncestorId)) {\n\t\t\t\tconst commonAncestor = editor.getShape(commonAncestorId)\n\t\t\t\tif (!commonAncestor) continue\n\t\t\t\t// put the point in the parent's space\n\t\t\t\tconst point = editor.getPointInShapeSpace(commonAncestor, {\n\t\t\t\t\tx: bounds.x,\n\t\t\t\t\ty: bounds.y,\n\t\t\t\t})\n\t\t\t\t// get the parent's rotation\n\t\t\t\trotation = editor.getShapePageTransform(commonAncestorId).rotation()\n\t\t\t\t// rotate the point against the parent's rotation\n\t\t\t\tpoint.sub(new Vec(padding, padding).rot(-rotation))\n\t\t\t\tx = point.x\n\t\t\t\ty = point.y\n\t\t\t} else {\n\t\t\t\t// if the common ancestor is the page, then just adjust for the padding\n\t\t\t\tx = bounds.x - padding\n\t\t\t\ty = bounds.y - padding\n\t\t\t\trotation = 0\n\t\t\t}\n\n\t\t\t// delete the shapes\n\t\t\teditor.deleteShapes(shapes)\n\n\t\t\t// create the asset\n\t\t\teditor.createAssets([{ ...asset, id: asset.id }])\n\n\t\t\tconst shapeId = createShapeId()\n\n\t\t\t// create an image shape in the same place as the shapes\n\t\t\teditor.createShape<TLImageShape>({\n\t\t\t\tid: shapeId,\n\t\t\t\ttype: 'image',\n\t\t\t\tindex,\n\t\t\t\tparentId: commonAncestorId,\n\t\t\t\tx,\n\t\t\t\ty,\n\t\t\t\trotation: -rotation,\n\t\t\t\tprops: {\n\t\t\t\t\tassetId: asset.id,\n\t\t\t\t\tw: bounds.w + padding * 2,\n\t\t\t\t\th: bounds.h + padding * 2,\n\t\t\t\t},\n\t\t\t})\n\n\t\t\tcreatedShapeIds.push(shapeId)\n\t\t}\n\t})\n\n\treturn createdShapeIds\n}\n\nexport function useFlatten() {\n\tconst editor = useEditor()\n\treturn useCallback(\n\t\t(ids: TLShapeId[]) => {\n\t\t\treturn flattenShapesToImages(editor, ids)\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