Dom widget store (#2899)

This commit is contained in:
Chenlei Hu
2025-03-06 13:23:58 -05:00
committed by GitHub
parent caaf050728
commit f7be9157e0
7 changed files with 126 additions and 23 deletions

View File

@@ -0,0 +1,36 @@
/**
* Stores all DOM widgets that are used in the canvas.
*/
import { defineStore } from 'pinia'
import { markRaw, ref } from 'vue'
import type { DOMWidget } from '@/scripts/domWidget'
export const useDomWidgetStore = defineStore('domWidget', () => {
// Map to reference actual widget instances
// Widgets are stored as raw values to avoid reactivity issues
const widgetInstances = ref(
new Map<string, DOMWidget<HTMLElement, object | string>>()
)
// Register a widget with the store
const registerWidget = <T extends HTMLElement, V extends object | string>(
widget: DOMWidget<T, V>
) => {
widgetInstances.value.set(
widget.id,
markRaw(widget as unknown as DOMWidget<HTMLElement, object | string>)
)
}
// Unregister a widget from the store
const unregisterWidget = (widgetId: string) => {
widgetInstances.value.delete(widgetId)
}
return {
widgetInstances,
registerWidget,
unregisterWidget
}
})