epic-designer
Version:
基于vue3的设计器,可视化开发页面表单
208 lines (207 loc) • 5.62 kB
JavaScript
import { ref as l, reactive as w, watchEffect as L } from "vue";
/* empty css */
/* empty css */
/* empty css */
import { pluginManager as u } from "./pluginManager.js";
/* empty css */
import "vue-draggable-plus";
import { findSchemas as E, getValueByPath as C } from "../../utils/src/common/data.js";
import { FormulaEngine as T } from "../../utils/src/formula/formulaEngine.js";
import "@vueuse/core";
import "../../hooks/src/store/index.js";
import { useHookManager as U } from "../../hooks/src/plugin/useHookManager.js";
import { useMountMonitor as z } from "../../hooks/src/plugin/useMountMonitor.js";
import { usePageSchema as G } from "../../hooks/src/plugin/usePageSchema.js";
const a = "default";
function ce() {
const x = new T(), i = l({}), d = l({}), h = l(!1), M = l(null), b = l([]), p = w({}), k = z(), { pageSchema: f, setPageSchema: y } = G(), P = U();
function m(e, o = "id", n = a) {
const t = g(e, o, n);
return (t == null ? void 0 : t.exposed) ?? null;
}
function I(e, o = "id", n = a) {
return v(e, o, n).map((r) => r.exposed);
}
function g(e, o = "id", n = a) {
var r, c;
if (o === "id")
return ((r = i.value[e]) == null ? void 0 : r[n]) ?? null;
const t = E(
f.schemas,
(s) => C(s, o) === e,
!0
);
return !t || !t.id ? null : ((c = i.value[t.id]) == null ? void 0 : c[n]) ?? null;
}
function v(e, o = "id", n = a) {
var r;
if (o === "id") {
const c = (r = i.value[e]) == null ? void 0 : r[n];
return c ? [c] : [];
}
return E(
f.schemas,
(c) => C(c, o) === e
).map((c) => {
var s;
return (s = i.value[c.id ?? ""]) == null ? void 0 : s[n];
}).filter(Boolean);
}
function $(e, o = "id") {
return console.warn(
"[Epic 自定义函数]: `getComponent`方法已废弃,后续版本可能移除该函数,请使用`find`方法"
), m(e, o);
}
function A(e, o, n = a) {
i.value[e] || (i.value[e] = {}), i.value[e][n] = o;
}
function D(e, o = a) {
var n;
(n = i.value[e]) == null || delete n[o];
}
function S(e, o = !1) {
const n = {};
for (const t in u.publicMethods.methodsMap)
Object.prototype.hasOwnProperty.call(
u.publicMethods.methodsMap,
t
) && (n[t] = u.publicMethods.methodsMap[t].handler);
try {
new Function(`const epic = this;${e}`).bind({
...n,
defineExpose: O,
find: m,
findAll: I,
findInstance: g,
findInstanceAll: v,
getComponent: $,
pluginManager: u,
publicMethods: n,
state: u.global
})(), M.value = null;
} catch (t) {
const r = t instanceof Error ? t : new Error(String(t));
M.value = r, o && console.error("[Epic:自定义函数]异常:", r);
}
}
function O(e) {
e && (d.value = e);
}
function _(e, o = a, ...n) {
if (!e || e.length === 0) {
console.warn("未提供任何动作");
return;
}
e.forEach((t) => {
let r = t.args ? JSON.parse(t.args) : n;
const c = {
event: n
};
switch (r = r.map((s) => s && typeof s == "object" && s.__isExpression__ ? x.calculate(s.content, c) : s), t.type) {
case "component": {
N(t, o, r);
break;
}
case "custom": {
j(t, r);
break;
}
case "public": {
F(t, r);
break;
}
default: {
console.warn(`未知的动作类型: ${t.type}`);
break;
}
}
});
}
function F(e, o) {
var n;
try {
(n = u.publicMethods.methodsMap[e.methodName]) == null || n.handler(
...o
);
} catch (t) {
console.error(`[Epic:公共函数(${e.methodName})]执行异常:`, t);
}
}
function j(e, o) {
var n, t;
try {
(t = (n = d.value)[e.methodName]) == null || t.call(n, ...o);
} catch (r) {
console.error(`[Epic:自定义函数(${e.methodName})]执行异常:`, r);
}
}
function N(e, o = a, n) {
const t = e.componentId && m(e.componentId, "id", o);
if (!t) {
console.warn(`[Epic:组件${e.componentId}]未找到`);
return;
}
try {
t[e.methodName](...n);
} catch (r) {
console.error(
`[Epic:组件${e.componentId}函数(${e.methodName})]执行异常:`,
r
);
}
}
function B(e = !0) {
h.value = e;
}
function H(e) {
const o = E(
e,
() => !0
);
b.value = o.map(
(n) => n.id
);
}
function J(e, o = "default") {
if (p[o]) {
const t = p[o];
return Object.keys(e).forEach((r) => {
t[r] = e[r];
}), t;
}
const n = w(e);
return p[o] = n, n;
}
return L(() => {
const e = f.script;
e && e !== "" && S(e, !h.value);
}), {
addComponentInstance: A,
componentInstances: i,
defaultComponentIds: b,
doActions: _,
find: m,
findAll: I,
findInstance: g,
findInstanceAll: v,
forms: p,
funcs: d,
// 兼容处理, 后续版本可能会移除
getComponentInstance: m,
hook: P,
isDesignMode: h,
mountMonitor: k,
pageSchema: f,
removeComponentInstance: D,
scriptError: M,
setDefaultComponentIds: H,
setDesignMode: B,
setFormData: J,
setMethods: S,
setPageSchema: y
};
}
export {
a as DEFAULT_SCOPE,
ce as createPageManager
};