2023-08-25 09:02:12 +00:00
|
|
|
import { usePluginModuleStore } from "@/stores/plugin";
|
2023-08-31 10:36:12 +00:00
|
|
|
import type { EntityFieldItem, PluginModule } from "@halo-dev/console-shared";
|
2024-06-26 10:42:50 +00:00
|
|
|
import { computed, onMounted, ref, type ComputedRef, type Ref } from "vue";
|
2023-08-25 09:02:12 +00:00
|
|
|
|
2023-08-31 03:32:22 +00:00
|
|
|
export function useEntityFieldItemExtensionPoint<T>(
|
|
|
|
extensionPointName: string,
|
|
|
|
entity: Ref<T>,
|
|
|
|
presets: ComputedRef<EntityFieldItem[]>
|
|
|
|
) {
|
|
|
|
const { pluginModules } = usePluginModuleStore();
|
|
|
|
const itemsFromPlugins = ref<EntityFieldItem[]>([]);
|
|
|
|
|
|
|
|
const allItems = computed(() => {
|
2023-08-31 10:36:12 +00:00
|
|
|
return [...presets.value, ...itemsFromPlugins.value];
|
2023-08-31 03:32:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
pluginModules.forEach((pluginModule: PluginModule) => {
|
|
|
|
const { extensionPoints } = pluginModule;
|
|
|
|
if (!extensionPoints?.[extensionPointName]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const items = extensionPoints[extensionPointName](
|
|
|
|
entity
|
|
|
|
) as EntityFieldItem[];
|
|
|
|
itemsFromPlugins.value.push(...items);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const startFields = computed(() => {
|
|
|
|
return allItems.value
|
|
|
|
.filter((item) => item.position === "start")
|
|
|
|
.sort((a, b) => {
|
|
|
|
return a.priority - b.priority;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const endFields = computed(() => {
|
|
|
|
return allItems.value
|
|
|
|
.filter((item) => item.position === "end")
|
|
|
|
.sort((a, b) => {
|
|
|
|
return a.priority - b.priority;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return { startFields, endFields };
|
|
|
|
}
|