fix(store): rect

This commit is contained in:
2026-02-28 17:59:19 +08:00
parent cc9bea54e5
commit 5b13594a68
2 changed files with 104 additions and 23 deletions

View File

@@ -6,6 +6,50 @@ import { Comment } from "./api/label/typing"
import { safeClone } from "./utils/clone"
type LabelData = Map<string, Map<string, [number, any[], any, any[]][]>>
type LabelPathTuple = [number, any[], any, any[]]
const isPaperPointLike = (value: any) => {
return (
value &&
typeof value === "object" &&
!Array.isArray(value) &&
typeof value.x === "number" &&
typeof value.y === "number"
)
}
const isPaperSegmentLike = (value: any) => {
return (
value &&
typeof value === "object" &&
!Array.isArray(value) &&
value.point &&
isPaperPointLike(value.point)
)
}
const normalizePointCollection = (value: any): any => {
if (Array.isArray(value)) {
return value.map((item) => normalizePointCollection(item))
}
if (isPaperSegmentLike(value)) {
return [value.point.x, value.point.y]
}
if (isPaperPointLike(value)) {
return [value.x, value.y]
}
return value
}
const normalizeLabelPathTuple = (path: LabelPathTuple): LabelPathTuple => {
return [
path[0],
normalizePointCollection(path[1]),
path[2],
normalizePointCollection(path[3]),
]
}
interface LabelState {
// pathId pathPoints detail hollowPathPoints
label: LabelData
@@ -219,7 +263,7 @@ export const useLabelStore = create(
if (arr.length === 10) {
arr.shift()
}
arr.push(newData)
arr.push(safeClone(newData))
console.log(arr)
return storeSet((state) => {
return {
@@ -285,12 +329,16 @@ export const useLabelStore = create(
}),
setOperation: (imageId, operationId, path) =>
storeSet((state) => {
const normalizedPath = normalizeLabelPathTuple(path)
const labelData = useLabelStore.getState().label
if (!labelData.has(imageId)) {
labelData.set(imageId, new Map())
}
if (labelData.get(imageId)?.get(operationId)?.length) {
let result = [...labelData.get(imageId)?.get(operationId)!, path]
let result = [
...labelData.get(imageId)?.get(operationId)!,
normalizedPath,
]
.reduce((acc, curr) => {
const key = curr[0]
acc.set(key, curr)
@@ -299,7 +347,7 @@ export const useLabelStore = create(
.values()
labelData.get(imageId)?.set(operationId, Array.from(result))
} else {
labelData.get(imageId)?.set(operationId, [path])
labelData.get(imageId)?.set(operationId, [normalizedPath])
}
console.log("绘制时", labelData)
useLabelStore.getState().pushStateStack(safeClone(labelData))