gepic
Version:
基于vue3的设计器,可视化开发页面表单
160 lines (159 loc) • 3.9 kB
JavaScript
import { ref as u, reactive as B, watchEffect as O } from "vue";
import { pluginManager as l } from "./pluginManager.js";
import "@vueuse/core";
import "../../hooks/store/index.js";
import { usePageSchema as j } from "../../hooks/plugin/usePageSchema.js";
import { findSchemas as f, getValueByPath as v, deepCompareAndModify as M } from "../common/data.js";
function L() {
const c = u({}), i = u({}), m = u(!1), p = u([]), r = B({}), { pageSchema: a, setPageSchema: b } = j();
function s(e, n = "id") {
if (n === "id")
return c.value[e] ?? null;
const t = f(
a.schemas,
(o) => v(o, n) === e,
!0
);
return t ? c.value[t.id] ?? null : null;
}
function h(e, n = "id") {
if (n === "id") {
const o = c.value[e];
return o ? [o] : [];
}
return f(
a.schemas,
(o) => v(o, n) === e
).map((o) => c.value[o.id]).filter(Boolean);
}
function C(e, n = "id") {
return console.warn("[Epic 自定义函数]: `getComponent`方法已废弃,后续版本可能移除该函数,请使用`find`方法"), s(e, n);
}
function I(e, n) {
c.value[e] = n;
}
function E(e) {
delete c.value[e];
}
function g(e, n = !1) {
const t = Object.entries(
l.publicMethods
).reduce((o, [d, N]) => (o[d] = N.handler, o), {});
try {
new Function(`const epic = this;${e}`).bind({
...t,
getComponent: C,
find: s,
findAll: h,
defineExpose: S,
publicMethods: t,
pluginManager: l
})();
} catch (o) {
n && console.error("[Epic:自定义函数]异常:", o);
}
}
function S(e) {
e != null && (i.value = e);
}
function w(e, ...n) {
if (!e || e.length === 0) {
console.warn("未提供任何动作");
return;
}
e.forEach((t) => {
const o = t.args ? JSON.parse(t.args) : n;
switch (t.type) {
case "public":
$(t, o);
break;
case "custom":
P(t, o);
break;
case "component":
k(t, o);
break;
default:
console.warn(`未知的动作类型: ${t.type}`);
break;
}
});
}
function $(e, n) {
var t;
try {
(t = l.publicMethods[e.methodName]) == null || t.handler(...n);
} catch (o) {
console.error(`[Epic:公共函数(${e.methodName})]执行异常:`, o);
}
}
function P(e, n) {
var t, o;
try {
(o = (t = i.value)[e.methodName]) == null || o.call(t, ...n);
} catch (d) {
console.error(`[Epic:自定义函数(${e.methodName})]执行异常:`, d);
}
}
function k(e, n) {
const t = e.componentId != null && s(e.componentId);
if (!t) {
console.warn(`[Epic:组件${e.componentId}]未找到`);
return;
}
try {
t[e.methodName](...n);
} catch (o) {
console.error(`[Epic:组件${e.componentId}函数(${e.methodName})]执行异常:`, o);
}
}
function x(e = !0) {
m.value = e;
}
function y(e) {
const n = f(e, () => !0);
p.value = n.map((t) => t.id);
}
function A(e, n = "default") {
if (r[n]) {
const t = r[n];
M(e, t);
}
r[n] = e;
}
function D(e, n = "default") {
if (r[n]) {
M(r[n], e, !1);
return;
}
r[n] = e;
}
return O(() => {
const e = a.script;
e && e !== "" && g(e, !m.value);
}), {
componentInstances: c,
funcs: i,
isDesignMode: m,
defaultComponentIds: p,
pageSchema: a,
setPageSchema: b,
forms: r,
addFormData: A,
setFormData: D,
// 兼容函数
getComponentInstance: s,
// 推荐使用 find 函数
find: s,
findAll: h,
addComponentInstance: I,
removeComponentInstance: E,
setMethods: g,
doActions: w,
setDesignMode: x,
setDefaultComponentIds: y
};
}
export {
L as usePageManager
};