Skip to content

Commit

Permalink
Widgets: make widget names unique
Browse files Browse the repository at this point in the history
  • Loading branch information
Williangalvani authored and rafaellehmkuhl committed Dec 18, 2024
1 parent ea67831 commit 5d8a195
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/components/EditMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@
<v-btn
type="flat"
class="bg-[#FFFFFF33] text-white w-[95%]"
@click="store.addWidget(WidgetType.CustomWidgetBase, store.currentView)"
@click="store.addWidget(makeNewWidget(WidgetType.CustomWidgetBase), store.currentView)"
>Add widget base
</v-btn>
</div>
Expand Down Expand Up @@ -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<string, any>): 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,
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 5d8a195

Please sign in to comment.