diff --git a/frontend/public/lsf/lsf.html b/frontend/public/lsf/lsf.html index d265b29..f0ac2f4 100644 --- a/frontend/public/lsf/lsf.html +++ b/frontend/public/lsf/lsf.html @@ -169,6 +169,33 @@ } } + function isAnnotationObject(value) { + if (!value || typeof value !== "object") return false; + return typeof value.serializeAnnotation === "function" || typeof value.serialize === "function"; + } + + function resolveSelectedAnnotation(store) { + if (!store) return null; + const annotations = Array.isArray(store.annotations) ? store.annotations : []; + if (isAnnotationObject(store.selectedAnnotation)) { + return store.selectedAnnotation; + } + if (isAnnotationObject(store.selected)) { + return store.selected; + } + const selectedId = store.selected; + if (selectedId !== undefined && selectedId !== null && annotations.length) { + const matched = annotations.find((ann) => ann && String(ann.id) === String(selectedId)); + if (isAnnotationObject(matched)) { + return matched; + } + } + if (annotations.length && isAnnotationObject(annotations[0])) { + return annotations[0]; + } + return null; + } + function exportSelectedAnnotation() { if (!lsInstance) { throw new Error("LabelStudio 未初始化"); @@ -179,10 +206,10 @@ throw new Error("无法访问 annotationStore"); } - const selected = - store.selected || - store.selectedAnnotation || - (Array.isArray(store.annotations) && store.annotations.length ? store.annotations[0] : null); + const selected = resolveSelectedAnnotation(store); + if (!selected) { + throw new Error("未找到可导出的标注对象"); + } let serialized = null; if (selected && typeof selected.serializeAnnotation === "function") { @@ -197,6 +224,10 @@ ? { id: selected?.id || serialized.id || "draft", ...serialized } : { id: selected?.id || "draft", result: (selected && selected.result) || [] }; + if (!Array.isArray(annotationPayload.result) && Array.isArray(annotationPayload.results)) { + annotationPayload.result = annotationPayload.results; + } + // 最小化对齐 Label Studio Server 的字段(DataMate 侧会原样存储) const taskId = typeof currentTask?.id === "number" ? currentTask.id : Number(currentTask?.id) || null; const fileId = currentTask?.data?.file_id || currentTask?.data?.fileId || null;