From a019c4fe016ae56f34f1b20b61c8997e67d745f0 Mon Sep 17 00:00:00 2001 From: Paul Elliott Date: Wed, 20 Dec 2023 20:00:21 -0500 Subject: [PATCH] feat(ToolLabelEditor): warn if label name is not unique Still allows duplicate names if user presses "Done". New labels are created with unique names suffixed with a number. New labels loop through the confgi.ts:TOOL_COLORS. --- src/components/LabelControls.vue | 17 ++++++++++++++++- src/components/ToolLabelEditor.vue | 30 +++++++++++++++++++++++++----- src/store/tools/useLabels.ts | 5 +++++ 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/components/LabelControls.vue b/src/components/LabelControls.vue index 7c6561550..c5e671ee2 100644 --- a/src/components/LabelControls.vue +++ b/src/components/LabelControls.vue @@ -42,8 +42,22 @@ const editingLabel = computed(() => { return props.labelsStore.labels[editingLabelID.value]; }); +const makeUniqueName = (name: string) => { + const existingNames = new Set( + Object.values(props.labelsStore.labels).map((label) => label.labelName) + ); + let uniqueName = name; + let i = 1; + while (existingNames.has(uniqueName)) { + uniqueName = `${name} (${i})`; + i++; + } + return uniqueName; +}; + const createLabel = () => { - editingLabelID.value = props.labelsStore.addLabel(); + const labelName = makeUniqueName('New Label'); + editingLabelID.value = props.labelsStore.addLabel({ labelName }); }; function startEditing(label: LabelID) { @@ -114,6 +128,7 @@ function deleteEditingLabel() { @delete="deleteEditingLabel" @cancel="stopEditing(false)" @done="stopEditing(true)" + :labelsStore="labelsStore" /> diff --git a/src/components/ToolLabelEditor.vue b/src/components/ToolLabelEditor.vue index 9415e0d0b..c1e72c307 100644 --- a/src/components/ToolLabelEditor.vue +++ b/src/components/ToolLabelEditor.vue @@ -1,5 +1,8 @@