Fix binding bugs

This commit is contained in:
Steve Ruiz 2021-09-04 22:12:07 +01:00
parent 010a09ff19
commit 13a40d1b9c
6 changed files with 103 additions and 86 deletions

View file

@ -112,4 +112,6 @@ describe('Delete command', () => {
expect(tlstate.getShape('newGroup')).toBeUndefined()
})
})
it.todo('Does not delete uneffected bindings.')
})

View file

@ -158,4 +158,6 @@ describe('Duplicate command', () => {
expect(Object.keys(tlstate.page.shapes).length).toBe(beforeShapeIds.length + 1)
})
})
it.todo('Does not delete uneffected bindings.')
})

View file

@ -83,12 +83,14 @@ export function duplicate(data: Data, ids: string[]): TLDrawCommand {
})
// Which ids did we end up duplicating?
const duplicatedShapeIds = Object.keys(duplicateMap)
const dupedShapeIds = new Set(Object.keys(duplicateMap))
// Handle bindings that effect duplicated shapes
Object.values(page.bindings).forEach((binding) => {
if (duplicatedShapeIds.includes(binding.fromId)) {
if (duplicatedShapeIds.includes(binding.toId)) {
Object.values(page.bindings)
.filter((binding) => dupedShapeIds.has(binding.fromId) || dupedShapeIds.has(binding.toId))
.forEach((binding) => {
if (dupedShapeIds.has(binding.fromId)) {
if (dupedShapeIds.has(binding.toId)) {
// If the binding is between two duplicating shapes then
// duplicate the binding, too
const duplicatedBindingId = Utils.uniqueId()
@ -142,7 +144,9 @@ export function duplicate(data: Data, ids: string[]): TLDrawCommand {
[currentPageId]: after,
},
pageStates: {
[currentPageId]: { selectedIds: duplicatedShapeIds.map((id) => duplicateMap[id]) },
[currentPageId]: {
selectedIds: Array.from(dupedShapeIds.values()).map((id) => duplicateMap[id]),
},
},
},
},

View file

@ -247,4 +247,6 @@ describe('Move to page command', () => {
])
})
})
it.todo('Does not delete uneffected bindings.')
})

View file

@ -52,7 +52,9 @@ export function removeShapesFromPage(data: Data, ids: string[], pageId: string)
const page = TLDR.getPage(data, pageId)
// We also need to delete bindings that reference the deleted shapes
Object.values(page.bindings).forEach((binding) => {
Object.values(page.bindings)
.filter((binding) => deletedIds.has(binding.fromId) || deletedIds.has(binding.toId))
.forEach((binding) => {
for (const id of [binding.toId, binding.fromId]) {
// If the binding references a deleted shape...
if (after.shapes[id] === undefined) {
@ -82,7 +84,10 @@ export function removeShapesFromPage(data: Data, ids: string[], pageId: string)
if (!deletedIds.has(id)) {
after.shapes[id] = {
...after.shapes[id],
handles: { ...after.shapes[id]?.handles, [handle.id]: { bindingId: undefined } },
handles: {
...after.shapes[id]?.handles,
[handle.id]: { bindingId: undefined },
},
}
}
})

View file

@ -382,15 +382,17 @@ export function getTranslateSnapshot(data: Data) {
// Potentially confusing name here: these are the ids of the
// original shapes that were cloned, not their clones' ids.
const clonedShapeIds = Object.keys(cloneMap)
const clonedShapeIds = new Set(Object.keys(cloneMap))
const bindingsToDelete: TLDrawBinding[] = []
// Create cloned bindings for shapes where both to and from shapes are selected
// (if the user clones, then we will create a new binding for the clones)
Object.values(page.bindings).forEach((binding) => {
if (clonedShapeIds.includes(binding.fromId)) {
if (clonedShapeIds.includes(binding.toId)) {
Object.values(page.bindings)
.filter((binding) => clonedShapeIds.has(binding.fromId) || clonedShapeIds.has(binding.toId))
.forEach((binding) => {
if (clonedShapeIds.has(binding.fromId)) {
if (clonedShapeIds.has(binding.toId)) {
const cloneId = Utils.uniqueId()
const cloneBinding = {
...Utils.deepClone(binding),