fix(store): rect
This commit is contained in:
@@ -6,6 +6,50 @@ import { Comment } from "./api/label/typing"
|
|||||||
import { safeClone } from "./utils/clone"
|
import { safeClone } from "./utils/clone"
|
||||||
|
|
||||||
type LabelData = Map<string, Map<string, [number, any[], any, any[]][]>>
|
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 {
|
interface LabelState {
|
||||||
// pathId pathPoints detail hollowPathPoints
|
// pathId pathPoints detail hollowPathPoints
|
||||||
label: LabelData
|
label: LabelData
|
||||||
@@ -219,7 +263,7 @@ export const useLabelStore = create(
|
|||||||
if (arr.length === 10) {
|
if (arr.length === 10) {
|
||||||
arr.shift()
|
arr.shift()
|
||||||
}
|
}
|
||||||
arr.push(newData)
|
arr.push(safeClone(newData))
|
||||||
console.log(arr)
|
console.log(arr)
|
||||||
return storeSet((state) => {
|
return storeSet((state) => {
|
||||||
return {
|
return {
|
||||||
@@ -285,12 +329,16 @@ export const useLabelStore = create(
|
|||||||
}),
|
}),
|
||||||
setOperation: (imageId, operationId, path) =>
|
setOperation: (imageId, operationId, path) =>
|
||||||
storeSet((state) => {
|
storeSet((state) => {
|
||||||
|
const normalizedPath = normalizeLabelPathTuple(path)
|
||||||
const labelData = useLabelStore.getState().label
|
const labelData = useLabelStore.getState().label
|
||||||
if (!labelData.has(imageId)) {
|
if (!labelData.has(imageId)) {
|
||||||
labelData.set(imageId, new Map())
|
labelData.set(imageId, new Map())
|
||||||
}
|
}
|
||||||
if (labelData.get(imageId)?.get(operationId)?.length) {
|
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) => {
|
.reduce((acc, curr) => {
|
||||||
const key = curr[0]
|
const key = curr[0]
|
||||||
acc.set(key, curr)
|
acc.set(key, curr)
|
||||||
@@ -299,7 +347,7 @@ export const useLabelStore = create(
|
|||||||
.values()
|
.values()
|
||||||
labelData.get(imageId)?.set(operationId, Array.from(result))
|
labelData.get(imageId)?.set(operationId, Array.from(result))
|
||||||
} else {
|
} else {
|
||||||
labelData.get(imageId)?.set(operationId, [path])
|
labelData.get(imageId)?.set(operationId, [normalizedPath])
|
||||||
}
|
}
|
||||||
console.log("绘制时", labelData)
|
console.log("绘制时", labelData)
|
||||||
useLabelStore.getState().pushStateStack(safeClone(labelData))
|
useLabelStore.getState().pushStateStack(safeClone(labelData))
|
||||||
|
|||||||
@@ -2354,15 +2354,32 @@ export const usePaperStore = create<PaperState>((set) => ({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (brushPath instanceof paper.Path) {
|
} else if (brushPath instanceof paper.Path) {
|
||||||
if (brushPath.segments && brushPath.segments.length)
|
if (brushPath.segments && brushPath.segments.length) {
|
||||||
|
const pathData = JSON.parse(
|
||||||
|
brushPath.exportJSON({ precision: 20 })
|
||||||
|
)
|
||||||
|
const brushSegments = pathData?.[1]?.segments
|
||||||
|
if (brushSegments?.length) {
|
||||||
useLabelStore
|
useLabelStore
|
||||||
.getState()
|
.getState()
|
||||||
.setOperation(
|
.setOperation(
|
||||||
brushPath.data.imageId,
|
brushPath.data.imageId,
|
||||||
brushPath.data.operationId,
|
brushPath.data.operationId,
|
||||||
[brushPath.data.id, [brushPath.segments], newDetail, []]
|
[brushPath.data.id, [brushSegments], newDetail, []]
|
||||||
)
|
)
|
||||||
else {
|
} else {
|
||||||
|
usePaperSupportStore
|
||||||
|
.getState()
|
||||||
|
.removeMaskById(brushPath.data.id)
|
||||||
|
useLabelStore
|
||||||
|
.getState()
|
||||||
|
.deleteOperation(
|
||||||
|
brushPath.data.imageId,
|
||||||
|
brushPath.data.operationId,
|
||||||
|
brushPath.data.id
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
usePaperSupportStore.getState().removeMaskById(brushPath.data.id)
|
usePaperSupportStore.getState().removeMaskById(brushPath.data.id)
|
||||||
useLabelStore
|
useLabelStore
|
||||||
.getState()
|
.getState()
|
||||||
@@ -2407,15 +2424,32 @@ export const usePaperStore = create<PaperState>((set) => ({
|
|||||||
category_id: Number(pointPath.data.operationId),
|
category_id: Number(pointPath.data.operationId),
|
||||||
}
|
}
|
||||||
if (pointPath instanceof paper.Path) {
|
if (pointPath instanceof paper.Path) {
|
||||||
if (pointPath.segments && pointPath.segments.length)
|
if (pointPath.segments && pointPath.segments.length) {
|
||||||
|
const pathData = JSON.parse(
|
||||||
|
pointPath.exportJSON({ precision: 20 })
|
||||||
|
)
|
||||||
|
const pointSegments = pathData?.[1]?.segments
|
||||||
|
if (pointSegments?.length) {
|
||||||
useLabelStore
|
useLabelStore
|
||||||
.getState()
|
.getState()
|
||||||
.setOperation(
|
.setOperation(
|
||||||
pointPath.data.imageId,
|
pointPath.data.imageId,
|
||||||
pointPath.data.operationId,
|
pointPath.data.operationId,
|
||||||
[pointPath.data.id, [pointPath.segments], newDetail, []]
|
[pointPath.data.id, [pointSegments], newDetail, []]
|
||||||
)
|
)
|
||||||
else {
|
} else {
|
||||||
|
usePaperSupportStore
|
||||||
|
.getState()
|
||||||
|
.removeMaskById(pointPath.data.id)
|
||||||
|
useLabelStore
|
||||||
|
.getState()
|
||||||
|
.deleteOperation(
|
||||||
|
pointPath.data.imageId,
|
||||||
|
pointPath.data.operationId,
|
||||||
|
pointPath.data.id
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
usePaperSupportStore.getState().removeMaskById(pointPath.data.id)
|
usePaperSupportStore.getState().removeMaskById(pointPath.data.id)
|
||||||
useLabelStore
|
useLabelStore
|
||||||
.getState()
|
.getState()
|
||||||
@@ -3329,8 +3363,7 @@ export const usePaperStore = create<PaperState>((set) => ({
|
|||||||
useTopToolsStore.getState().activeOperation,
|
useTopToolsStore.getState().activeOperation,
|
||||||
[
|
[
|
||||||
rect.data.id,
|
rect.data.id,
|
||||||
// todo check
|
[JSON.parse(rect.exportJSON({ precision: 20 }))[1]?.segments],
|
||||||
[rect?.segments.map((e) => e.point)],
|
|
||||||
{
|
{
|
||||||
...initialDetail,
|
...initialDetail,
|
||||||
uid: usePermissionStore.getState().user_id,
|
uid: usePermissionStore.getState().user_id,
|
||||||
|
|||||||
Reference in New Issue
Block a user