From 4533efdbcfbf808717d5e18f0e2bf10d637fb195 Mon Sep 17 00:00:00 2001 From: Willian Galvani Date: Tue, 10 Dec 2024 15:53:17 -0300 Subject: [PATCH] Widgets: make widget names unique --- src/components/EditMenu.vue | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/components/EditMenu.vue b/src/components/EditMenu.vue index 13227a4d3..c88bb7e5e 100644 --- a/src/components/EditMenu.vue +++ b/src/components/EditMenu.vue @@ -472,7 +472,7 @@ Add widget base @@ -714,6 +714,35 @@ const emit = defineEmits<{ (e: 'update:editMode', editMode: boolean): void }>() +const findUniqueName = (name: string): string => { + let newName = name + let i = 1 + const existingNames = store.currentView.widgets.map((widget) => widget.name) + while (existingNames.includes(newName)) { + newName = `${name} ${i}` + i++ + } + return newName +} +/* + * Makes a new widget with an unique name + */ +const makeNewWidget = (widget: WidgetType, name?: string, options?: Record): ExtendedWidget => { + const newName = name || widget + return { + name: findUniqueName(newName), + component: widget, + options: options || {}, + } +} + +const makeWidgetUnique = (widget: ExtendedWidget): ExtendedWidget => { + return { + ...widget, + name: findUniqueName(widget.name), + } +} + const availableWidgetTypes = computed(() => Object.values(WidgetType).map((widgetType) => { return { @@ -1020,8 +1049,8 @@ const onRegularWidgetDragStart = (event: DragEvent): void => { } } -const onRegularWidgetDragEnd = (widgetType: ExtendedWidget): void => { - store.addWidget(widgetType, store.currentView) +const onRegularWidgetDragEnd = (widget: ExtendedWidget): void => { + store.addWidget(makeWidgetUnique(widget), store.currentView) const widgetCards = document.querySelectorAll('[draggable="true"]') widgetCards.forEach((card) => {