119 lines
3.4 KiB
TypeScript
119 lines
3.4 KiB
TypeScript
import paper from "paper"
|
|
import { useLabelStore, useObjectStore } from "../store"
|
|
import { useOtherToolsStore } from "../useOtherToolsStore"
|
|
import { usePaperStore } from "../usePaperStore"
|
|
import { usePaperSupportStore } from "../usePaperSupportStore"
|
|
import { useTopToolsStore } from "../useTopToolsStore"
|
|
|
|
const getTagVisibilityByConfig = (tagCategory?: string) => {
|
|
const { showTags, showTagsConfigs } = useTopToolsStore.getState()
|
|
if (!showTags) return false
|
|
if (tagCategory === "mask") return showTagsConfigs.includes("外框")
|
|
if (tagCategory === "point_text") return showTagsConfigs.includes("点ID")
|
|
return true
|
|
}
|
|
|
|
export const getObjectTagVisibility = (
|
|
imageId: string,
|
|
objectId: number,
|
|
tagCategory?: string
|
|
) => {
|
|
const { pathStatus } = useObjectStore.getState()
|
|
if (pathStatus[imageId + objectId]?.HIDE) return false
|
|
return getTagVisibilityByConfig(tagCategory)
|
|
}
|
|
|
|
const setSelectedItemFalse = (id: number) => {
|
|
if (!id) return
|
|
const sameIdItems = usePaperStore.getState().getItemsById(id)
|
|
sameIdItems.forEach((item) => {
|
|
if (item.data.type === "mask" || item.data.type === "text") {
|
|
item.remove()
|
|
}
|
|
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 &&
|
|
otherToolsState.imageId === imageId &&
|
|
otherToolsState.id === objectId
|
|
) {
|
|
otherToolsState.setShowContextMenu({
|
|
showContextMenu: false,
|
|
position: {
|
|
top: 0,
|
|
left: 0,
|
|
},
|
|
imageId: "",
|
|
operationId: "",
|
|
id: 0,
|
|
})
|
|
}
|
|
}
|
|
|
|
const syncObjectItemsVisibility = (
|
|
imageId: string,
|
|
objectId: number,
|
|
hide: boolean
|
|
) => {
|
|
const sameIdItems = usePaperStore.getState().getItemsById(objectId)
|
|
if (!sameIdItems?.length) return
|
|
sameIdItems.forEach((item) => {
|
|
if (hide) {
|
|
item.visible = false
|
|
return
|
|
}
|
|
if (item.data?.type === "tag") {
|
|
item.visible = getObjectTagVisibility(
|
|
imageId,
|
|
objectId,
|
|
item.data?.tag_category
|
|
)
|
|
return
|
|
}
|
|
item.visible = true
|
|
})
|
|
}
|
|
|
|
export const toggleObjectHideByShortcut = (
|
|
imageId: string,
|
|
operationId: string,
|
|
objectId: number,
|
|
hide: boolean
|
|
) => {
|
|
const { updatePathStatus, updateOperationStatus } = useObjectStore.getState()
|
|
const storeLabel = useLabelStore.getState().label
|
|
|
|
if (hide) {
|
|
updatePathStatus(imageId + objectId, "HIDE", hide)
|
|
syncObjectItemsVisibility(imageId, objectId, hide)
|
|
setSelectedItemFalse(objectId)
|
|
clearHiddenObjectSelectionState(imageId, objectId)
|
|
|
|
const operationChildren = storeLabel.get(imageId)?.get(operationId) || []
|
|
const hasVisibleChild = operationChildren.some((child) => {
|
|
const item = usePaperStore
|
|
.getState()
|
|
.getItemById(child[0]) as paper.Item | null
|
|
return !!item?.visible
|
|
})
|
|
if (!hasVisibleChild) {
|
|
updateOperationStatus(imageId + operationId, "HIDE", hide)
|
|
}
|
|
return
|
|
}
|
|
|
|
updateOperationStatus(imageId + operationId, "HIDE", hide)
|
|
updatePathStatus(imageId + objectId, "HIDE", hide)
|
|
syncObjectItemsVisibility(imageId, objectId, hide)
|
|
}
|