fix: 优化界面,解耦组件抽屉

This commit is contained in:
SikongJueluo 2025-07-13 13:28:45 +08:00
parent c29c3652bc
commit bad64bdfbd
No known key found for this signature in database
4 changed files with 54 additions and 49 deletions

View File

@ -1,5 +1,5 @@
<template> <template>
<div class="fixed left-1/2 top-30 z-50 -translate-x-1/2"> <div class="fixed left-1/2 top-30 z-999 -translate-x-1/2">
<transition <transition
name="alert" name="alert"
enter-active-class="alert-enter-active" enter-active-class="alert-enter-active"

View File

@ -25,13 +25,12 @@
<Plus :size="20" class="text-primary" /> <Plus :size="20" class="text-primary" />
添加元器件 添加元器件
</h3> </h3>
<label <button
for="component-drawer"
class="btn btn-ghost btn-sm btn-circle" class="btn btn-ghost btn-sm btn-circle"
@click="closeMenu" @click="closeMenu"
> >
<X :size="20" /> <X :size="20" />
</label> </button>
</div> </div>
<!-- 导航栏 --> <!-- 导航栏 -->
@ -112,8 +111,8 @@
import { ref, computed, shallowRef, onMounted } from "vue"; import { ref, computed, shallowRef, onMounted } from "vue";
import { Plus, X, Search } from "lucide-vue-next"; import { Plus, X, Search } from "lucide-vue-next";
import ItemList from "./ItemList.vue"; import ItemList from "./ItemList.vue";
import { useAlertStore } from "@/components/Alert";
import { import {
useComponentManager,
availableComponents, availableComponents,
availableVirtualDevices, availableVirtualDevices,
availableTemplates, availableTemplates,
@ -121,6 +120,7 @@ import {
type ComponentConfig, type ComponentConfig,
type VirtualDeviceConfig, type VirtualDeviceConfig,
type TemplateConfig, type TemplateConfig,
useComponentManager, // componentManager
} from "./index.ts"; } from "./index.ts";
// Props // Props
@ -130,16 +130,18 @@ interface Props {
const props = defineProps<Props>(); const props = defineProps<Props>();
const componentManager = useComponentManager(); //
//
const emit = defineEmits([ const emit = defineEmits([
"close", "close",
"add-component",
"add-template",
"update:open", "update:open",
]); ]);
// 使 componentManager
const componentManager = useComponentManager();
// 使 Alert
const alert = useAlertStore();
// //
const activeTab = ref("components"); const activeTab = ref("components");
@ -192,14 +194,19 @@ async function preloadComponentModules() {
// //
function closeMenu() { function closeMenu() {
showComponentsMenu.value = false; emit("update:open", false);
emit("close"); emit("close");
} }
// // - 使 componentManager
async function addComponent( async function addComponent(
componentTemplate: ComponentConfig | VirtualDeviceConfig, componentTemplate: ComponentConfig | VirtualDeviceConfig,
) { ) {
if (!componentManager) {
console.error("ComponentManager not available");
return;
}
// //
const moduleRef = await loadComponentModule(componentTemplate.type); const moduleRef = await loadComponentModule(componentTemplate.type);
let defaultProps: Record<string, any> = {}; let defaultProps: Record<string, any> = {};
@ -220,19 +227,32 @@ async function addComponent(
console.log(`Failed to load module for ${componentTemplate.type}`); console.log(`Failed to load module for ${componentTemplate.type}`);
} }
// try {
emit("add-component", { // 使 componentManager
await componentManager.addComponent({
type: componentTemplate.type, type: componentTemplate.type,
name: componentTemplate.name, name: componentTemplate.name,
props: defaultProps, props: defaultProps,
}); });
//
alert?.success(`成功添加元器件: ${componentTemplate.name}`);
// //
closeMenu(); closeMenu();
} catch (error) {
console.error("添加元器件失败:", error);
alert?.error("添加元器件失败,请检查控制台错误信息");
}
} }
// // - 使 componentManager
async function addTemplate(template: TemplateConfig) { async function addTemplate(template: TemplateConfig) {
if (!componentManager) {
console.error("ComponentManager not available");
return;
}
try { try {
// JSON // JSON
const response = await fetch(template.path); const response = await fetch(template.path);
@ -243,19 +263,27 @@ async function addTemplate(template: TemplateConfig) {
const templateData = await response.json(); const templateData = await response.json();
console.log("加载模板:", templateData); console.log("加载模板:", templateData);
// // 使 componentManager
emit("add-template", { const result = await componentManager.addTemplate({
id: template.id, id: template.id,
name: template.name, name: template.name,
template: templateData, template: templateData,
capsPage: template.capsPage,
}); });
if (result) {
// 使 Alert
if (result.success) {
alert?.success(result.message);
} else {
alert?.error(result.message);
}
}
// //
closeMenu(); closeMenu();
} catch (error) { } catch (error) {
console.error("加载模板出错:", error); console.error("加载模板出错:", error);
alert("无法加载模板文件,请检查控制台错误信息"); alert?.error("无法加载模板文件,请检查控制台错误信息");
} }
} }

View File

@ -92,8 +92,6 @@
<ComponentSelector <ComponentSelector
:open="showComponentsMenu" :open="showComponentsMenu"
@update:open="showComponentsMenu = $event" @update:open="showComponentsMenu = $event"
@add-component="handleAddComponent"
@add-template="handleAddTemplate"
@close="showComponentsMenu = false" @close="showComponentsMenu = false"
/> />
@ -219,27 +217,6 @@ function openComponentsMenu() {
showComponentsMenu.value = true; showComponentsMenu.value = true;
} }
// ComponentSelector
async function handleAddComponent(componentData: {
type: string;
name: string;
props: Record<string, any>;
}) {
await componentManager.addComponent(componentData);
}
//
async function handleAddTemplate(templateData: {
id: string;
name: string;
template: any;
}) {
const result = await componentManager.addTemplate(templateData);
if (result) {
alert?.show(result.message, result.success ? "success" : "error");
}
}
// //
function handleDiagramUpdated(data: DiagramData) { function handleDiagramUpdated(data: DiagramData) {
console.log("Diagram data updated:", data); console.log("Diagram data updated:", data);

View File

@ -1,7 +1,7 @@
<template> <template>
<div <div
v-if="open" v-if="open"
class="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50" class="fixed inset-0 z-50 flex items-center justify-center bg-opacity-30 backdrop-blur-sm shadow-2xl"
> >
<div class="bg-base-100 rounded-lg shadow-xl max-w-md w-full mx-4"> <div class="bg-base-100 rounded-lg shadow-xl max-w-md w-full mx-4">
<div class="p-6"> <div class="p-6">