diff --git a/src/components/EditMenu.vue b/src/components/EditMenu.vue index 6771b4907..6544da511 100644 --- a/src/components/EditMenu.vue +++ b/src/components/EditMenu.vue @@ -472,7 +472,7 @@ Add widget base @@ -712,7 +712,36 @@ watch( } ) -const availableWidgetTypes = computed(() => +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): InternalWidgetSetupInfo => { + const newName = name || widget + return { + name: findUniqueName(newName), + component: widget, + options: options || {}, + } +} + +const makeWidgetUnique = (widget: InternalWidgetSetupInfo): InternalWidgetSetupInfo => { + return { + ...widget, + name: findUniqueName(widget.name), + } +} + +const availableInternalWidgets = computed(() => Object.values(WidgetType).map((widgetType) => { return { component: widgetType, @@ -1018,8 +1047,8 @@ const onRegularWidgetDragStart = (event: DragEvent): void => { } } -const onRegularWidgetDragEnd = (widgetType: ExtendedWidget): void => { - store.addWidget(widgetType, store.currentView) +const onRegularWidgetDragEnd = (widget: InternalWidgetSetupInfo): void => { + store.addWidget(makeWidgetUnique(widget), store.currentView) const widgetCards = document.querySelectorAll('[draggable="true"]') widgetCards.forEach((card) => {