diff --git a/components/label/utils/objectVisibility.ts b/components/label/utils/objectVisibility.ts index 2ae76ca..20312f9 100644 --- a/components/label/utils/objectVisibility.ts +++ b/components/label/utils/objectVisibility.ts @@ -13,6 +13,13 @@ const getTagVisibilityByConfig = (tagCategory?: string) => { return true } +const resetSelectedItemVisual = (item: paper.Item) => { + if (!(item instanceof paper.Path || item instanceof paper.CompoundPath)) + return + if (!["rectangle", "polygon"].includes(item.data?.type || "")) return + item.fillColor = item.data?.blankColor || item.fillColor || null +} + export const getObjectTagVisibility = ( imageId: string, objectId: number, @@ -30,17 +37,12 @@ const setSelectedItemFalse = (id: number) => { if (item.data.type === "mask" || item.data.type === "text") { item.remove() } + resetSelectedItemVisual(item) if (item.data.selected) item.data.selected = false }) } const clearHiddenObjectSelectionState = (imageId: string, objectId: number) => { - const { selectedPath, updateSelectedPath } = useObjectStore.getState() - if (selectedPath[imageId]?.includes(objectId)) { - updateSelectedPath(imageId, "DELETE", objectId) - usePaperSupportStore.getState().clearAll() - } - const otherToolsState = useOtherToolsStore.getState() if ( otherToolsState.showContextMenu && @@ -84,6 +86,69 @@ const syncObjectItemsVisibility = ( }) } +const applySelectedItemVisual = (item: paper.Item) => { + if (!(item instanceof paper.Path || item instanceof paper.CompoundPath)) + return + const itemType = item.data?.type + if (!["rectangle", "polygon", "brush", "point"].includes(itemType || "")) + return + + if (itemType === "rectangle" && item instanceof paper.Path) { + item.data.selected = true + usePaperSupportStore.getState().handleBufferPaths(item) + usePaperSupportStore.getState().handleCircles(item) + item.fillColor = item.data.fillColor || item.fillColor || null + } else if (itemType === "brush") { + item.data.selected = true + if (item instanceof paper.Path) { + usePaperSupportStore.getState().handleMask(item) + usePaperSupportStore.getState().handleBufferPaths(item) + usePaperSupportStore.getState().handleCircles(item) + } + } else if (itemType === "point" && item instanceof paper.Path) { + item.data.selected = true + usePaperSupportStore.getState().handleMask(item) + usePaperSupportStore.getState().handleText(item) + usePaperSupportStore.getState().handleBufferPaths(item) + usePaperSupportStore.getState().handleCircles(item) + } else if (itemType === "polygon") { + item.data.selected = true + if (!(item instanceof paper.Path) || !item.data?.isHollowPolygon) { + item.fillColor = item.data.fillColor || item.fillColor || null + } + } + + item.bringToFront() +} + +const syncSelectedObjectsVisualState = (imageId: string) => { + const group = usePaperStore.getState().group + if (!group) return + + group + .getItems({ + match: (item: paper.Item) => !!item.data?.selected, + }) + .forEach((item) => { + resetSelectedItemVisual(item) + item.data.selected = false + }) + + usePaperSupportStore.getState().clearAll() + + const { selectedPath, pathStatus } = useObjectStore.getState() + ;(selectedPath[imageId] || []).forEach((selectedId) => { + if (pathStatus[imageId + selectedId]?.HIDE) return + usePaperStore + .getState() + .getItemsById(selectedId) + .forEach((item) => { + if (item.data?.type === "tag") return + applySelectedItemVisual(item) + }) + }) +} + export const toggleObjectHideByShortcut = ( imageId: string, operationId: string, @@ -98,6 +163,7 @@ export const toggleObjectHideByShortcut = ( syncObjectItemsVisibility(imageId, objectId, hide) setSelectedItemFalse(objectId) clearHiddenObjectSelectionState(imageId, objectId) + syncSelectedObjectsVisualState(imageId) const operationChildren = storeLabel.get(imageId)?.get(operationId) || [] const hasVisibleChild = operationChildren.some((child) => { @@ -115,4 +181,5 @@ export const toggleObjectHideByShortcut = ( updateOperationStatus(imageId + operationId, "HIDE", hide) updatePathStatus(imageId + objectId, "HIDE", hide) syncObjectItemsVisibility(imageId, objectId, hide) + syncSelectedObjectsVisualState(imageId) }