Parcourir la source

fix: remove empty group

Konstantinos Kaloutas il y a 2 ans
Parent
commit
72097e6b67

+ 17 - 0
tldraw/packages/core/src/lib/shapes/TLGroupShape/TLGroupShape.tsx

@@ -3,6 +3,7 @@ import { computed, makeObservable } from 'mobx'
 import { BoundsUtils } from '../../../utils'
 import { TLBoxShape, TLBoxShapeProps } from '../TLBoxShape'
 import type { TLShape } from '../TLShape'
+import { useApp } from '@tldraw/react'
 
 export interface TLGroupShapeProps extends TLBoxShapeProps {
   children: string[] // shape ids
@@ -41,6 +42,22 @@ export class TLGroupShape<
   }
 
   getBounds = (): TLBounds => {
+    // A group without children needs to be removed
+    if (this.shapes.length === 0) {
+      const app = useApp<Shape>()
+      app.deleteShapes([this.id])
+      app.persist(true)
+
+      return {
+        minX: 0,
+        minY: 0,
+        maxX: 0,
+        maxY: 0,
+        width: 0,
+        height: 0,
+      }
+    }
+
     return BoundsUtils.getCommonBounds(this.shapes.map(s => s.getBounds()))
   }
 }