diff --git a/components/label/usePaperStore.ts b/components/label/usePaperStore.ts index 41e618e..c9c4ce4 100644 --- a/components/label/usePaperStore.ts +++ b/components/label/usePaperStore.ts @@ -299,6 +299,39 @@ const handleDelete = () => { // }); } +const cleanupDeletedObjectItems = ( + imageId: string, + pathId?: number, + parentGroupId?: number +) => { + if (!pathId) return + + usePaperStore + .getState() + .group?.getItems({ + data: { id: pathId }, + }) + .forEach((item) => { + item.remove() + }) + + useObjectStore.getState().updateSelectedPath(imageId, "DELETE", pathId) + + if (!parentGroupId) return + + const groupMap = useRightToolsStore.getState().pathGroupMap.get(imageId) + const currentGroupIds = groupMap + ?.get(parentGroupId) + ?.filter((id) => id !== pathId) + + if (!currentGroupIds || currentGroupIds.length <= 1) { + groupMap?.delete(parentGroupId) + return + } + + groupMap?.set(parentGroupId, currentGroupIds) +} + const handleShift = (flag: boolean) => { useKeyEventStore.getState().setShift(flag) } @@ -2556,6 +2589,11 @@ export const usePaperStore = create((set) => ({ rectPath.data.operationId, rectPath.data.id ) + cleanupDeletedObjectItems( + rectPath.data.imageId, + rectPath.data.id, + rectPath.data.parentGroupId + ) } forceUpdate() @@ -2807,6 +2845,11 @@ export const usePaperStore = create((set) => ({ polygonPath.data.operationId, polygonPath.data.id ) + cleanupDeletedObjectItems( + polygonPath.data.imageId, + polygonPath.data.id, + polygonPath.data.parentGroupId + ) } } @@ -2886,6 +2929,11 @@ export const usePaperStore = create((set) => ({ brushPath.data.operationId, brushPath.data.id ) + cleanupDeletedObjectItems( + brushPath.data.imageId, + brushPath.data.id, + brushPath.data.parentGroupId + ) } } } else if (brushPath instanceof paper.Path) { @@ -2913,6 +2961,11 @@ export const usePaperStore = create((set) => ({ brushPath.data.operationId, brushPath.data.id ) + cleanupDeletedObjectItems( + brushPath.data.imageId, + brushPath.data.id, + brushPath.data.parentGroupId + ) } } else { usePaperSupportStore.getState().removeMaskById(brushPath.data.id) @@ -2923,6 +2976,11 @@ export const usePaperStore = create((set) => ({ brushPath.data.operationId, brushPath.data.id ) + cleanupDeletedObjectItems( + brushPath.data.imageId, + brushPath.data.id, + brushPath.data.parentGroupId + ) } } @@ -2983,6 +3041,11 @@ export const usePaperStore = create((set) => ({ pointPath.data.operationId, pointPath.data.id ) + cleanupDeletedObjectItems( + pointPath.data.imageId, + pointPath.data.id, + pointPath.data.parentGroupId + ) } } else { usePaperSupportStore.getState().removeMaskById(pointPath.data.id) @@ -2993,6 +3056,11 @@ export const usePaperStore = create((set) => ({ pointPath.data.operationId, pointPath.data.id ) + cleanupDeletedObjectItems( + pointPath.data.imageId, + pointPath.data.id, + pointPath.data.parentGroupId + ) } }