From 40f05c0e4f3687838e728f9ef66410591738e15c Mon Sep 17 00:00:00 2001 From: Paul Elliott Date: Wed, 20 Dec 2023 17:50:33 -0500 Subject: [PATCH] fix(useLabels): keep deserialized labels with same name * remove clear default labels flag from mergeLabels and make explicit function. * set max width to LabelControls label editor --- src/components/LabelControls.vue | 2 +- src/io/import/configJson.ts | 21 +++++++++++++++------ src/store/tools/useAnnotationTool.ts | 7 +++++-- src/store/tools/useLabels.ts | 13 ++++--------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/components/LabelControls.vue b/src/components/LabelControls.vue index 5ef17e9c4..7c6561550 100644 --- a/src/components/LabelControls.vue +++ b/src/components/LabelControls.vue @@ -105,7 +105,7 @@ function deleteEditingLabel() { - + { if (!manifest.labels) return; // pass through null labels, use fallback labels if undefined - const labelsIfUndefined = ( - toolLabels: (typeof manifest.labels)[keyof typeof manifest.labels] - ) => { + const defaultLabelsIfUndefined = (toolLabels: T) => { if (toolLabels === undefined) return manifest.labels?.defaultLabels; return toolLabels; }; + const applyLabelsToStore = ( + maybeLabels: (typeof manifest.labels)[keyof typeof manifest.labels], + store: AnnotationToolStore + ) => { + const labelsOrFallback = defaultLabelsIfUndefined(maybeLabels); + if (!labelsOrFallback) return; + store.clearDefaultLabels(); + store.mergeLabels(labelsOrFallback); + }; + const { rulerLabels, rectangleLabels, polygonLabels } = manifest.labels; - useRulerStore().mergeLabels(labelsIfUndefined(rulerLabels)); - useRectangleStore().mergeLabels(labelsIfUndefined(rectangleLabels)); - usePolygonStore().mergeLabels(labelsIfUndefined(polygonLabels)); + applyLabelsToStore(rulerLabels, useRulerStore()); + applyLabelsToStore(rectangleLabels, useRectangleStore()); + applyLabelsToStore(polygonLabels, usePolygonStore()); }; const applySampleData = (manifest: Config) => { diff --git a/src/store/tools/useAnnotationTool.ts b/src/store/tools/useAnnotationTool.ts index 1be76c6d1..94a584219 100644 --- a/src/store/tools/useAnnotationTool.ts +++ b/src/store/tools/useAnnotationTool.ts @@ -73,7 +73,7 @@ export const useAnnotationTool = < ...annotationToolLabelDefault, ...newLabelDefault, }); - labels.mergeLabels(initialLabels, false); + labels.mergeLabels(initialLabels); function makePropsFromLabel(label: string | undefined) { if (!label) return { labelName: '' }; @@ -190,9 +190,12 @@ export const useAnnotationTool = < serialized: Maybe, dataIDMap: Record ) { + if (serialized?.labels) { + labels.clearDefaultLabels(); + } const labelIDMap = Object.fromEntries( Object.entries(serialized?.labels ?? {}).map(([id, label]) => { - const newID = labels.mergeLabel(label); // side effect + const newID = labels.addLabel(label); // side effect in Array.map return [id, newID]; }) ); diff --git a/src/store/tools/useLabels.ts b/src/store/tools/useLabels.ts index 955aa6aec..148fb285b 100644 --- a/src/store/tools/useLabels.ts +++ b/src/store/tools/useLabels.ts @@ -79,9 +79,7 @@ export const useLabels = (newLabelDefault: Props) => { * param label: label to merge * param clearDefault: if true, clear initial labels, do nothing if initial labels already cleared */ - const mergeLabel = (label: ToolLabel, clearDefault: boolean = true) => { - if (clearDefault) clearDefaultLabels(); - + const mergeLabel = (label: ToolLabel) => { const { labelName } = label; const matchingName = findLabel(labelName); @@ -100,12 +98,9 @@ export const useLabels = (newLabelDefault: Props) => { * param newLabels: each key is the label name * param clearDefault: if true, clear initial labels, do nothing if initial labels already cleared */ - const mergeLabels = ( - newLabels: Maybe, - clearDefault: boolean = true - ) => { + const mergeLabels = (newLabels: Maybe) => { Object.entries(newLabels ?? {}).forEach(([labelName, props]) => - mergeLabel({ ...props, labelName }, clearDefault) + mergeLabel({ ...props, labelName }) ); }; @@ -116,9 +111,9 @@ export const useLabels = (newLabelDefault: Props) => { addLabel, deleteLabel, updateLabel, - mergeLabel, mergeLabels, findLabel, + clearDefaultLabels, }; };