fix(store): rect
This commit is contained in:
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user