|
472 | 472 | <v-btn |
473 | 473 | type="flat" |
474 | 474 | class="bg-[#FFFFFF33] text-white w-[95%]" |
475 | | - @click="store.addWidget(WidgetType.CustomWidgetBase, store.currentView)" |
| 475 | + @click="store.addWidget(makeNewWidget(WidgetType.CustomWidgetBase), store.currentView)" |
476 | 476 | >Add widget base |
477 | 477 | </v-btn> |
478 | 478 | </div> |
@@ -714,7 +714,36 @@ const emit = defineEmits<{ |
714 | 714 | (e: 'update:editMode', editMode: boolean): void |
715 | 715 | }>() |
716 | 716 |
|
717 | | -const availableWidgetTypes = computed(() => |
| 717 | +const findUniqueName = (name: string): string => { |
| 718 | + let newName = name |
| 719 | + let i = 1 |
| 720 | + const existingNames = store.currentView.widgets.map((widget) => widget.name) |
| 721 | + while (existingNames.includes(newName)) { |
| 722 | + newName = `${name} ${i}` |
| 723 | + i++ |
| 724 | + } |
| 725 | + return newName |
| 726 | +} |
| 727 | +/* |
| 728 | + * Makes a new widget with an unique name |
| 729 | + */ |
| 730 | +const makeNewWidget = (widget: WidgetType, name?: string, options?: Record<string, any>): InternalWidgetSetupInfo => { |
| 731 | + const newName = name || widget |
| 732 | + return { |
| 733 | + name: findUniqueName(newName), |
| 734 | + component: widget, |
| 735 | + options: options || {}, |
| 736 | + } |
| 737 | +} |
| 738 | +
|
| 739 | +const makeWidgetUnique = (widget: InternalWidgetSetupInfo): InternalWidgetSetupInfo => { |
| 740 | + return { |
| 741 | + ...widget, |
| 742 | + name: findUniqueName(widget.name), |
| 743 | + } |
| 744 | +} |
| 745 | +
|
| 746 | +const availableInternalWidgets = computed(() => |
718 | 747 | Object.values(WidgetType).map((widgetType) => { |
719 | 748 | return { |
720 | 749 | component: widgetType, |
@@ -1020,8 +1049,8 @@ const onRegularWidgetDragStart = (event: DragEvent): void => { |
1020 | 1049 | } |
1021 | 1050 | } |
1022 | 1051 |
|
1023 | | -const onRegularWidgetDragEnd = (widgetType: ExtendedWidget): void => { |
1024 | | - store.addWidget(widgetType, store.currentView) |
| 1052 | +const onRegularWidgetDragEnd = (widget: InternalWidgetSetupInfo): void => { |
| 1053 | + store.addWidget(makeWidgetUnique(widget), store.currentView) |
1025 | 1054 |
|
1026 | 1055 | const widgetCards = document.querySelectorAll('[draggable="true"]') |
1027 | 1056 | widgetCards.forEach((card) => { |
|
0 commit comments