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