Skip to content

Commit

Permalink
fix(useLabels): keep deserialized labels with same name
Browse files Browse the repository at this point in the history
* remove clear default labels flag from mergeLabels and make explicit function.
* set max width to LabelControls label editor
  • Loading branch information
PaulHax committed Feb 22, 2024
1 parent 135cbe6 commit 40f05c0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/components/LabelControls.vue
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function deleteEditingLabel() {
</v-container>
</v-card>

<isolated-dialog v-model="editDialog">
<isolated-dialog v-model="editDialog" max-width="800px">
<ToolLabelEditor
v-if="editingLabelID"
v-model:name="editState.labelName"
Expand Down
21 changes: 15 additions & 6 deletions src/io/import/configJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { usePolygonStore } from '@/src/store/tools/polygons';
import { useViewStore } from '@/src/store/views';
import { actionToKey } from '@/src/composables/useKeyboardShortcuts';
import { useSegmentGroupStore } from '@/src/store/segmentGroups';
import { AnnotationToolStore } from '@/src/store/tools/useAnnotationTool';

const layout = z
.object({
Expand Down Expand Up @@ -82,17 +83,25 @@ const applyLabels = (manifest: Config) => {
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 = <T>(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) => {
Expand Down
7 changes: 5 additions & 2 deletions src/store/tools/useAnnotationTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '' };
Expand Down Expand Up @@ -190,9 +190,12 @@ export const useAnnotationTool = <
serialized: Maybe<Serialized>,
dataIDMap: Record<string, string>
) {
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];
})
);
Expand Down
13 changes: 4 additions & 9 deletions src/store/tools/useLabels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ export const useLabels = <Props>(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);

Expand All @@ -100,12 +98,9 @@ export const useLabels = <Props>(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<ToolLabels>,
clearDefault: boolean = true
) => {
const mergeLabels = (newLabels: Maybe<ToolLabels>) => {
Object.entries(newLabels ?? {}).forEach(([labelName, props]) =>
mergeLabel({ ...props, labelName }, clearDefault)
mergeLabel({ ...props, labelName })
);
};

Expand All @@ -116,9 +111,9 @@ export const useLabels = <Props>(newLabelDefault: Props) => {
addLabel,
deleteLabel,
updateLabel,
mergeLabel,
mergeLabels,
findLabel,
clearDefaultLabels,
};
};

Expand Down

0 comments on commit 40f05c0

Please sign in to comment.