diff --git a/components/label/components/PaperContainer.tsx b/components/label/components/PaperContainer.tsx index 43af6c9..5042816 100644 --- a/components/label/components/PaperContainer.tsx +++ b/components/label/components/PaperContainer.tsx @@ -52,7 +52,7 @@ import { renderGroupPath } from "../useRenderGroupPath" import useRenderTag from "../useRenderTag" import { useRightToolsStore } from "../useRightToolsStore" import { useTopToolsStore } from "../useTopToolsStore" -import { checkCommentsIsSame } from "../util" +import { buildSubAttributeFormValues, checkCommentsIsSame } from "../util" import { safeClone } from "../utils/clone" import { labelTypeMap } from "../utils/constants" import { adjustPoints } from "../utils/paperjs" @@ -3315,50 +3315,26 @@ const PaperContainer = ( .get(activeImage) ?.get(operationId) ?.find((item) => item[0] === id) + const operationSchema = getOperationSchema(operationId) + const nowComment = + currentPath?.[2].comment?.[currentPath?.[2].comment.length - 1] - form.setFieldValue("id", id.toString()) - form.setFieldValue("operation", operationId) - if (currentPath?.[2].sub_attributes) { - let operationSchema = getOperationSchema(operationId) - let obj: { - [key: string]: string | string[] - } = {} - Object.entries(currentPath?.[2].sub_attributes).map(([key, value]) => { - let select_mode = operationSchema?.sub_attributes_describe?.find( - (item) => item.chinese_name === key - )?.select_mode - switch (select_mode) { - case 2: - obj[key] = Array.isArray(value) - ? value - : (value as string) - .split(",") - .map((item) => item.trim()) - .filter(Boolean) - return - default: - obj[key] = value as string - return - } - }) - form.setFieldValue("sub_attributes", obj) - } - if (currentPath?.[2].comment) { - const nowComment = - currentPath?.[2].comment[currentPath?.[2].comment.length - 1] - form.setFieldValue( - "check_box_comments", + form.setValues({ + id: id.toString(), + operation: operationId, + sub_attributes: buildSubAttributeFormValues( + operationSchema, + currentPath?.[2].sub_attributes || {} + ), + check_box_comments: nowComment && nowComment.check_box_comments.length ? nowComment.check_box_comments - : [] - ) - form.setFieldValue( - "text_comments", + : [], + text_comments: nowComment && nowComment.text_comments.length ? nowComment.text_comments[0] - : "" - ) - } + : "", + }) // 可选的ids let images = Array.from(labelState.keys()).filter( (key) => key !== activeImage @@ -3580,11 +3556,10 @@ const PaperContainer = ( }) } // newOperation的subAttributes - let subAttr = Object.assign( - {}, + let subAttr = safeClone( useTopToolsStore.getState().subAttributes[ operationSchema?.label_class ?? "" - ] + ] || {} ) // 修改labeldata setOperation(activeImage, newOperation, [ @@ -3612,16 +3587,21 @@ const PaperContainer = ( ) const handleChangeContextMenuSubAttr = useCallback( - (key: string, value: string) => { + (key: string, value: string | string[]) => { + const currentSubAttributes = safeClone( + (pathItem?.[2]?.sub_attributes || + newDetail.sub_attributes || + {}) as Record + ) + currentSubAttributes[key] = Array.isArray(value) ? value.join(",") : value + setOperation(activeImage, operationId, [ id, pathItem?.[1] || [], { ...newDetail, ...(pathItem?.[2] || {}), - sub_attributes: Object.assign(newDetail.sub_attributes || {}, { - [key]: value, - }), + sub_attributes: currentSubAttributes, }, pathItem?.[3] || [], ]) @@ -3892,7 +3872,7 @@ const PaperContainer = ( ) handleChangeContextMenuSubAttr( item.chinese_name, - value.toString() + value ) }}> diff --git a/components/label/components/TopTools.tsx b/components/label/components/TopTools.tsx index e818b8e..a3ef089 100644 --- a/components/label/components/TopTools.tsx +++ b/components/label/components/TopTools.tsx @@ -98,7 +98,7 @@ import { NODE_SIZE_MIN, useTopToolsStore, } from "../useTopToolsStore" -import { getSubAttrStatus } from "../util" +import { buildSubAttributeFormValues, getSubAttrStatus } from "../util" import { safeClone } from "../utils/clone" import { labelTypeMap, TaskStatusEnum } from "../utils/constants" import { adjustAllPoints } from "../utils/paperjs" @@ -1488,36 +1488,10 @@ const TopTools = ( operationId: string, values: Record = {} ) => { - const schema = getOperationSchema(operationId) - if (!schema?.sub_attributes_describe?.length) return {} - - const nextValues: Record = {} - - schema.sub_attributes_describe.forEach((item) => { - const currentValue = values[item.chinese_name] - if ( - currentValue === undefined || - currentValue === null || - currentValue === "" - ) - return - - if (item.select_mode === 2) { - nextValues[item.chinese_name] = Array.isArray(currentValue) - ? currentValue - : currentValue - .split(",") - .map((value) => value.trim()) - .filter(Boolean) - return - } - - nextValues[item.chinese_name] = Array.isArray(currentValue) - ? currentValue[0] || "" - : currentValue - }) - - return nextValues + return buildSubAttributeFormValues( + getOperationSchema(operationId), + values + ) }, [getOperationSchema] ) @@ -1627,6 +1601,10 @@ const TopTools = ( size="xs" onClick={() => { form.reset() + form.setFieldValue( + "sub_attributes", + getPresetFormValues(activeOperation, {}) + ) if (operationSchema?.label_class) setsubAttributes(operationSchema.label_class, {}) }}> diff --git a/components/label/util.ts b/components/label/util.ts index 3e0bce3..9849c77 100644 --- a/components/label/util.ts +++ b/components/label/util.ts @@ -86,6 +86,45 @@ export const getSubAttrStatus = ( return bool } +export const buildSubAttributeFormValues = ( + operationSchema: Project.LabelSchemaList | null, + detail: Record = {} +) => { + const nextValues: Record = {} + if (!operationSchema?.sub_attributes_describe?.length) return nextValues + + operationSchema.sub_attributes_describe.forEach((item) => { + nextValues[item.chinese_name] = item.select_mode === 2 ? [] : "" + + const currentValue = detail?.[item.chinese_name] + if ( + currentValue === undefined || + currentValue === null || + currentValue === "" || + (Array.isArray(currentValue) && currentValue.length === 0) + ) { + return + } + + if (item.select_mode === 2) { + nextValues[item.chinese_name] = Array.isArray(currentValue) + ? currentValue + : currentValue + .toString() + .split(",") + .map((value: string) => value.trim()) + .filter(Boolean) + return + } + + nextValues[item.chinese_name] = Array.isArray(currentValue) + ? currentValue[0] || "" + : currentValue.toString() + }) + + return nextValues +} + // export const checkCommentsIsSame: ( a: any[] | null,