Compact children when updating parents to children. (#2072)

This PR fixes (or rather buries) a bug that could occur when the parents
to children array includes shapes that no longer exist.

### Change Type

- [x] `patch` — Bug fix
This commit is contained in:
Steve Ruiz 2023-10-13 13:12:05 +01:00 committed by GitHub
parent bfb61b12ad
commit 1f4df14d4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,7 @@
import { computed, isUninitialized, RESET_VALUE } from '@tldraw/state' import { computed, isUninitialized, RESET_VALUE } from '@tldraw/state'
import { RecordsDiff } from '@tldraw/store' import { RecordsDiff } from '@tldraw/store'
import { isShape, TLParentId, TLRecord, TLShape, TLShapeId, TLStore } from '@tldraw/tlschema' import { isShape, TLParentId, TLRecord, TLShape, TLShapeId, TLStore } from '@tldraw/tlschema'
import { compact } from '@tldraw/utils'
import { sortByIndex } from '../../utils/reordering/reordering' import { sortByIndex } from '../../utils/reordering/reordering'
type Parents2Children = Record<TLParentId, TLShapeId[]> type Parents2Children = Record<TLParentId, TLShapeId[]>
@ -102,7 +103,8 @@ export const parentsToChildren = (store: TLStore) => {
// Sort the arrays that have been marked for sorting // Sort the arrays that have been marked for sorting
for (const arr of toSort) { for (const arr of toSort) {
const shapesInArr = arr.map((id) => store.get(id)!) // It's possible that some of the shapes may be deleted. But in which case would this be so?
const shapesInArr = compact(arr.map((id) => store.get(id)))
shapesInArr.sort(sortByIndex) shapesInArr.sort(sortByIndex)
arr.splice(0, arr.length, ...shapesInArr.map((shape) => shape.id)) arr.splice(0, arr.length, ...shapesInArr.map((shape) => shape.id))
} }