@easyv/admin-utils
Version:
easyv/twin 公共 utils 集合
1,492 lines (1,488 loc) • 77.1 kB
JavaScript
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
var target = {};
for (var prop in source)
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
target[prop] = source[prop];
if (source != null && __getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(source)) {
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
target[prop] = source[prop];
}
return target;
};
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/gui/utils.js
function generateId() {
return Math.random().toString(36).substr(3, 20);
}
function deepFind(data = [], predicate, deepKey = "components") {
return data.reduce(
(all, item) => predicate(item) ? all.concat(item) : Array.isArray(item[deepKey]) ? all.concat(deepFind(item[deepKey], predicate, deepKey)) : all,
[]
);
}
function deepFindOne(data = [], predicate, deepKey = "components") {
for (let i = 0; i < data.length; i++) {
let item = data[i];
if (predicate(item)) {
return item;
} else {
if (Array.isArray(item[deepKey])) {
let result = deepFindOne(item[deepKey], predicate, deepKey);
if (result) {
return result;
}
}
}
}
}
function deepMap(data = [], predicate, deepKey = "components") {
return data.reduce(
(all, item) => item.hasOwnProperty(deepKey) ? all.concat(__spreadProps(__spreadValues({}, predicate(item)), { [deepKey]: deepMap(item[deepKey], predicate, deepKey) })) : all.concat(predicate(item)),
[]
);
}
function isPanel(id) {
return /^panel_/.test(id) || /^\$panel\(panel_([0-9]+)\)/.test(id);
}
function isRef(id) {
return /^ref_/.test(id) || /^\$panel\(ref_([0-9]+)\)/.test(id);
}
function isGroup(id) {
return /^group_/.test(id) || /^\$group\((group_\w+)\)/.test(id);
}
function isState(id) {
return /^state_/.test(id) || /^\$state\(([0-9]+)\)/.test(id);
}
function isScreen(id) {
return /^screen_/.test(id) || /^\$screen\(([0-9]+)\)/.test(id);
}
function isContainer(id) {
return /^container_/.test(id) || /^\$container\((container_[0-9]+)\)/.test(id);
}
function isComContainerPage(pathname = window.location.pathname) {
return /^\/create\/[0-9]+\/container_[0-9]+\/[0-9]+$/.test(pathname);
}
function getComContainerId(pathname = window.location.pathname) {
const pathnameArr = pathname.replace(/\/+$/g, "").split("/");
let containerId;
if (isComContainerPage(pathname)) {
containerId = pathnameArr[pathnameArr.length - 2];
}
return containerId;
}
function isComponent(id) {
return typeof id === "number" || /^\$component\(([0-9]+)\)/.test(id);
}
function raf(cb) {
let next = null;
let stop = true;
function run() {
requestAnimationFrame(() => {
if (!next) {
stop = true;
return;
}
stop = false;
const _next = next;
next = null;
try {
_next();
} catch (e) {
console.error(e);
} finally {
run();
}
});
}
return function(...args) {
next = () => cb.apply(null, args);
stop && run();
};
}
function safeDiv(a, b) {
return parseInt(a * 1e6) / parseInt(b * 1e6);
}
function safeModulo(a, b) {
return parseInt(a * 1e6) % parseInt(b * 1e6) / 1e6;
}
function safeAdd(a, b) {
return (parseInt(a * 1e6) + parseInt(b * 1e6)) / 1e6;
}
function safeMul(n1, n2) {
const a = n1.toString();
const al = a.length - a.indexOf(".") - 1;
const b = n2.toString();
const bl = b.length - b.indexOf(".") - 1;
return a * Math.pow(10, al) * b * Math.pow(10, bl) / Math.pow(10, al + bl);
}
function calcExpressionWithoutQuote(expression) {
if (expression.indexOf("(") > -1 || expression.indexOf(")") > -1) {
return calcExpression(expression);
}
if (!isNaN(Number(expression))) {
return Number(expression);
}
let operators = [];
let nums = [];
let lastOperatorIndex = -1;
for (let i = 0; i < expression.length; i++) {
const charAtIndex = expression.charAt(i);
if (isOperatorChar(charAtIndex)) {
operators[operators.length] = charAtIndex;
nums[nums.length] = expression.substring(lastOperatorIndex + 1, i);
lastOperatorIndex = i;
}
if (i == expression.length - 1 && lastOperatorIndex < i) {
nums[nums.length] = expression.substring(lastOperatorIndex + 1, expression.length);
}
}
if (operators.length <= 0 || nums.length <= 0) {
return Number(expression);
}
while (operators.indexOf("*") > -1 || operators.indexOf("/") > -1) {
operators.forEach(function(value, index) {
if (value == "*" || value == "/") {
var tempResult = calcExpressionWithSingleOperator(nums[index], nums[index + 1], value);
operators.splice(index, 1);
nums.splice(index, 2, [tempResult]);
}
});
}
let calcResult = nums[0] * 1;
if (operators.indexOf("+") > -1 || operators.indexOf("-") > -1) {
for (let index = 0; index < operators.length; index++) {
const value = operators[index];
if (value == "+" || value == "-") {
calcResult = calcExpressionWithSingleOperator(calcResult, nums[index + 1], value);
}
}
return calcResult;
} else {
return nums[0] * 1;
}
}
function calcExpressionWithSingleOperator(num1, num2, operator) {
if (operator == "+")
return num1 * 1 + num2 * 1;
if (operator == "-")
return num1 * 1 - num2 * 1;
if (operator == "*")
return num1 * num2;
if (operator == "/")
return num1 / num2;
return NaN;
}
function calcExpression(expression) {
if (!expression || typeof expression !== "string") {
return expression;
}
expression = expression.replace(/\s/g, "").replace(/÷/g, "/").replace(/x/g, "*").replace(/×/g, "*").replace(/X/g, "*");
if (isRepeatOperator(expression)) {
return NaN;
}
if (getCharCountInString(expression, "(") != getCharCountInString(expression, ")")) {
return NaN;
}
while (expression && expression.indexOf("(") > -1 && expression.indexOf(")") > -1) {
const firstRightQuoteIndex = expression.indexOf(")");
let leftQuoteIndex = expression.indexOf("(");
for (let i = leftQuoteIndex; i < firstRightQuoteIndex; i++) {
if (expression.charAt(i) == "(") {
leftQuoteIndex = i;
}
}
const tempExpression = expression.substring(leftQuoteIndex + 1, firstRightQuoteIndex);
const tempValue = calcExpressionWithoutQuote(tempExpression);
expression = expression.substring(0, leftQuoteIndex) + tempValue + expression.substring(firstRightQuoteIndex + 1, expression.length);
}
return calcExpressionWithoutQuote(expression);
}
function getCharCountInString(strings, chars) {
return strings.split(chars).length - 1;
}
function isOperatorChar(aimChar) {
return "+-*/".indexOf(aimChar) > -1;
}
function isRepeatOperator(expression) {
return /([\+\-\*\/]){2,}/.test(expression);
}
// src/gui/config.new.js
import { produce } from "immer";
import { isPlainObject } from "lodash-es";
function getScreenDimension(config) {
if (config && Array.isArray(config)) {
try {
const dimension = config.find((item) => item.name === "dimension").value;
const width = dimension.find((item) => item.name === "width").value;
const height = dimension.find((item) => item.name === "height").value;
/-------/;
const style = config.find((item) => item.name === "style").value;
const bgColor = style.find((item) => item.name === "backgroundColor").value;
const useBackground = style.find(
(item) => item.name === "useBackground"
).value;
const background = style.find((item) => item.name === "background").value;
return {
width,
height,
backgroundColor: bgColor,
useBackground,
background
};
} catch (e) {
console.log(e);
return {
width: 0,
height: 0,
backgroundColor: "",
useBackground: false,
background: ""
};
}
}
return {
width: 0,
height: 0,
backgroundColor: "",
useBackground: false,
background: ""
};
}
function getComponentDimension(config) {
if (config && Array.isArray(config)) {
try {
let chart = config.find((item) => item.name === "chart").value;
let dimension = chart.find((item) => item.name === "dimension").value;
let position = dimension.find(
(item) => item.name === "chartPosition"
).value;
let sizeObj = dimension.find((item) => item.name === "chartDimension");
let size = sizeObj.value;
let left = position.find((item) => item.name === "left").value;
let top = position.find((item) => item.name === "top").value;
let width = size.find((item) => item.name === "width").value;
let height = size.find((item) => item.name === "height").value;
let lock = sizeObj.config && sizeObj.config.lock;
return { left, top, width, height, lock };
} catch (error) {
console.error(error);
return { width: 0, height: 0, left: 0, top: 0 };
}
} else {
return { width: 0, height: 0, left: 0, top: 0 };
}
}
var notAtomConfigTypes = [
"array",
"object",
"group",
"colors",
"menu",
"modal",
"imgSize"
];
function reduceConfig(config) {
if (!Array.isArray(config)) {
return config;
}
return config.filter((d) => d).reduce((result, item) => {
result[item.name] = item.type && !notAtomConfigTypes.includes(item.type) ? item.value : reduceConfig(item.value);
return result;
}, {});
}
function updateArrayConfig(config, updates) {
config = Array.isArray(config) && config.length ? config : [
{
config: {
layout: "horizontal",
show: true
},
displayName: "基础配置",
name: "basicConfig",
value: []
}
];
updates = Array.isArray(updates) ? updates : [updates];
return produce(config, (newConfig) => {
updates.map((update) => {
let {
value,
path,
field = "value",
type,
activeKey,
newActiveKey,
oldIndex,
newIndex
} = update;
const item = getValueObjFromConfig(newConfig, path);
if (item) {
if (type === "add") {
if (item.config && item.config.template) {
let template = item.value.length ? item.value.find((d) => d.name === activeKey) : Array.isArray(item.config.template) ? item.config.template[0] : item.config.template;
item[field] = item.value.concat(__spreadProps(__spreadValues({}, template), {
name: newActiveKey
}));
} else {
console.error("configuration should has config.template");
}
} else if (type === "delete") {
let newValue = item.value.filter((d) => d.name !== activeKey);
item[field] = newValue;
} else if (type === "sort") {
let newValue = item.value.concat();
let target = newValue.splice(oldIndex, 1);
newValue.splice(newIndex, 0, target[0]);
item[field] = newValue;
} else {
if (field === "value") {
item.value = value;
} else {
if (item.config) {
item.config[field] = value;
} else {
item.config = {
[field]: value
};
}
}
}
}
});
return newConfig;
});
}
function getValueObjFromConfig(config, path = []) {
let nextValue = { value: config };
path.every((key) => {
if (typeof key === "number") {
return nextValue = nextValue.value[key];
}
return nextValue = nextValue.value.find((item) => item.name === key);
});
return nextValue || null;
}
function getValueFromConfig(config, path, field = "value") {
let valueObj = getValueObjFromConfig(config, path);
return valueObj && (field === "value" ? valueObj.value : valueObj.config && valueObj.config[field]);
}
function getConfig(config, type, device) {
switch (type) {
case "style":
return config.filter(
(d) => d.name !== "interaction" && d.type !== "modal" && d.name !== "children"
);
case "modal":
return config.filter((d) => d.type === "modal");
case "interaction":
let interactionConfig = config.filter((d) => d.name === "interaction");
return device === "screen" ? interactionConfig.map((d) => __spreadProps(__spreadValues({}, d), {
value: d.value.filter((o) => o.name !== "remoteControl")
})) : interactionConfig;
case "children":
let childrenConfig = getValueObjFromConfig(config, [
"children",
"component",
"list"
]);
if (childrenConfig) {
return {
default: childrenConfig.value.map((d) => ({
name: d.label,
moduleName: d.name
})),
all: childrenConfig.children.map((d) => ({
name: d.name,
moduleName: d.value
}))
};
} else {
return null;
}
default:
return config;
}
}
var mergeConfigurationByConfig = (preEqual, curEqual = {}) => {
const { config = {} } = curEqual;
if (preEqual.config && preEqual.config.show === false) {
if (curEqual.type === "select" && config && Array.isArray(config.options) && config.options.length == 0) {
return {
config: __spreadProps(__spreadValues({}, config), { options: preEqual.config.options, show: false })
};
}
return { config: __spreadProps(__spreadValues({}, config ? config : {}), { show: false }) };
} else {
if (curEqual.type === "select" && config && Array.isArray(config.options) && config.options.length == 0) {
return { config: __spreadProps(__spreadValues({}, config), { options: preEqual.config.options }) };
}
return {};
}
};
function mergeConfig(now = [], prev = []) {
if (prev.length === 0 || now.length === 0) {
return now;
}
if (!Array.isArray(prev)) {
return now;
}
return now.map((d) => {
let preEqual = prev.find((v) => v.name === d.name);
if (preEqual != null && (typeof d.value === typeof preEqual.value || !d.value && preEqual.value)) {
if (d.type === "array") {
const { config = {} } = d;
let template = Array.isArray(config.template) ? config.template[0] : config.template;
let value = preEqual.value.map((v) => {
return __spreadProps(__spreadValues({}, v), {
value: mergeConfig(template.value, v.value)
});
});
return __spreadProps(__spreadValues(__spreadValues({}, d), preEqual.config && preEqual.config.show === false ? { config: __spreadProps(__spreadValues({}, d.config ? d.config : {}), { show: false }) } : {}), {
value
});
} else if (Array.isArray(d.value) && ![
"range",
"cameras",
"checkbox",
"position",
"select",
"children",
"rangeColor"
].includes(d.type)) {
return __spreadProps(__spreadValues(__spreadValues({}, d), preEqual.config && preEqual.config.show === false ? { config: __spreadProps(__spreadValues({}, d.config ? d.config : {}), { show: false }) } : {}), {
value: mergeConfig(d.value, preEqual.value)
});
} else {
return __spreadProps(__spreadValues(__spreadValues({}, d), mergeConfigurationByConfig(preEqual, d)), {
value: preEqual.value
});
}
} else {
return d;
}
});
}
function mergeStyleConfig(current, theme, isPasteStyle = false) {
return current.map((d) => {
if (d.name === "dimension" || d.name === "interaction") {
return d;
}
let themeValue = theme.find((v) => v.name === d.name);
if (themeValue != null && (typeof d.value === typeof themeValue.value || d.type === themeValue.type)) {
if (d.type === "array") {
const { config = {} } = d;
let value = isPasteStyle ? themeValue.value.map((v, i) => {
var _a;
const { length } = d.value;
const oldV = ((_a = d.value) == null ? void 0 : _a[i]) || d.value[length - 1];
return __spreadProps(__spreadValues({}, oldV), {
name: v.name,
value: mergeStyleConfig(oldV.value, v.value)
});
}) : d.value.map((v, i) => {
return themeValue.value[i] ? __spreadProps(__spreadValues({}, v), {
value: mergeStyleConfig(
v.value,
themeValue.value[i].value,
isPasteStyle
)
}) : v;
});
return __spreadProps(__spreadValues({}, d), {
value
});
} else if (Array.isArray(d.value) && ![
"range",
"checkbox",
"position",
"select",
"children",
"rangeColor"
].includes(d.type)) {
return __spreadProps(__spreadValues({}, d), {
value: mergeStyleConfig(d.value, themeValue.value, isPasteStyle)
});
} else {
return __spreadProps(__spreadValues({}, d), {
value: themeValue.value
});
}
} else {
return d;
}
});
}
function updateArrayWithObject(arr, obj) {
if (obj == null) {
return arr;
}
return arr.reduce((all, item) => {
if (item.type === "array") {
const currentObj = obj[item.name];
const keys = isPlainObject(currentObj) ? Object.keys(currentObj) : [];
let addedKeys = keys;
let newValue = item.value.reduce((all2, current) => {
if (keys.includes(current.name)) {
addedKeys = addedKeys.filter((d) => d !== current.name);
if (currentObj[current.name] === null) {
return all2;
} else {
return all2.concat(__spreadProps(__spreadValues({}, current), {
value: updateArrayWithObject(
current.value,
currentObj[current.name]
)
}));
}
} else {
return all2.concat(current);
}
}, []);
const template = Array.isArray(item.config.template) ? item.config.template[0] : item.config.template;
newValue = newValue.concat(
addedKeys.filter((d) => currentObj[d] !== null).map((d) => __spreadProps(__spreadValues({}, template), {
name: d,
value: updateArrayWithObject(template.value, currentObj[d])
}))
);
return all.concat(__spreadProps(__spreadValues({}, item), { value: newValue }));
} else if (obj.hasOwnProperty(item.name)) {
if (Array.isArray(item.value) && item.type !== "position") {
return all.concat(__spreadProps(__spreadValues({}, item), {
value: updateArrayWithObject(item.value, obj[item.name])
}));
} else {
return all.concat(__spreadProps(__spreadValues({}, item), { value: obj[item.name] }));
}
} else {
return all.concat(item);
}
}, []);
}
// src/gui/config.old.js
import { produce as produce2 } from "immer";
import { isPlainObject as isPlainObject2 } from "lodash-es";
function getScreenDimension2(config) {
if (config && Array.isArray(config)) {
try {
const dimension = config.find((item) => item._name === "dimension")._value;
const width = dimension.find((item) => item._name === "width")._value;
const height = dimension.find((item) => item._name === "height")._value;
/-------/;
const style = config.find((item) => item._name === "style")._value;
const bgColor = style.find(
(item) => item._name === "backgroundColor"
)._value;
const useBackground = style.find(
(item) => item._name === "useBackground"
)._value;
const background = style.find((item) => item._name === "background")._value;
return {
width,
height,
backgroundColor: bgColor,
useBackground,
background
};
} catch (e) {
console.log(e);
return {
width: 0,
height: 0,
backgroundColor: "",
useBackground: false,
background: ""
};
}
}
return {
width: 0,
height: 0,
backgroundColor: "",
useBackground: false,
background: ""
};
}
function getComponentDimension2(config) {
if (config && Array.isArray(config)) {
try {
let chart = config.find((item) => item._name === "chart")._value;
let dimension = chart.find((item) => item._name === "dimension")._value;
let position = dimension.find(
(item) => item._name === "chartPosition"
)._value;
let sizeObj = dimension.find((item) => item._name === "chartDimension");
let size = sizeObj._value;
let left = position.find((item) => item._name === "left")._value;
let top = position.find((item) => item._name === "top")._value;
let width = size.find((item) => item._name === "width")._value;
let height = size.find((item) => item._name === "height")._value;
let lock = sizeObj._lock;
return { left, top, width, height, lock };
} catch (error) {
console.error(error);
return { width: 0, height: 0, left: 0, top: 0 };
}
} else {
return { width: 0, height: 0, left: 0, top: 0 };
}
}
var notAtomConfigTypes2 = [
"array",
"object",
"group",
"colors",
"menu",
"modal"
];
function reduceConfig2(config) {
if (!Array.isArray(config)) {
return config;
}
return config.filter((d) => d).reduce((result, item) => {
result[item._name] = item._type && !notAtomConfigTypes2.includes(item._type) ? item._value : reduceConfig2(item._value);
return result;
}, {});
}
function updateArrayConfig2(config, updates) {
updates = Array.isArray(updates) ? updates : [updates];
return produce2(config, (newConfig) => {
updates.map((update) => {
let {
value,
path,
field = "value",
type,
activeKey,
newActiveKey,
oldIndex,
newIndex
} = update;
const item = getValueObjFromConfig2(newConfig, path);
if (item) {
field = field.startsWith("_") ? field : `_${field}`;
if (type === "add") {
let template = item._value.length ? item._value.find((d) => d._name === activeKey) : Array.isArray(item._template) ? item._template[0] : item._template;
item[field] = item._value.concat(__spreadProps(__spreadValues({}, template), {
_name: newActiveKey
}));
} else if (type === "delete") {
let newValue = item._value.filter((d) => d._name !== activeKey);
item[field] = newValue;
} else if (type === "sort") {
let newValue = item._value.concat();
let target = newValue.splice(oldIndex, 1);
newValue.splice(newIndex, 0, target[0]);
item[field] = newValue;
} else {
item[field] = value;
}
}
});
return newConfig;
});
}
function getValueObjFromConfig2(config, path = []) {
let nextValue = { _value: config };
path.every((key) => {
if (typeof key === "number") {
return nextValue = nextValue._value[key];
}
return nextValue = nextValue._value.find((item) => item._name === key);
});
return nextValue || null;
}
function getValueFromConfig2(config, path, field = "_value") {
let valueObj = getValueObjFromConfig2(config, path);
if (!field.startsWith("_")) {
field = "_" + field;
}
return valueObj && valueObj[field];
}
function getConfig2(config, type, device) {
switch (type) {
case "style":
return config.filter(
(d) => d._name !== "interaction" && d._type !== "modal" && d._name !== "children"
);
case "modal":
return config.filter((d) => d._type === "modal");
case "interaction":
let interactionConfig = config.filter((d) => d._name === "interaction");
return device === "screen" ? interactionConfig.map((d) => __spreadProps(__spreadValues({}, d), {
_value: d._value.filter((o) => o._name !== "remoteControl")
})) : interactionConfig;
case "children":
let childrenConfig = getValueObjFromConfig2(config, [
"children",
"component",
"list"
]);
if (childrenConfig) {
return {
default: childrenConfig._value.map((d) => ({
name: d.label,
moduleName: d.name
})),
all: childrenConfig._children.map((d) => ({
name: d.name,
moduleName: d.value
}))
};
} else {
return null;
}
default:
return config;
}
}
var mergeConfigurationByConfig2 = (preEqual = {}, curEqual = {}) => {
const { _show } = preEqual;
const { _options } = curEqual;
if (_show === false) {
if (curEqual._type === "select" && Array.isArray(_options) && _options.length === 0) {
return { _show: false, _options };
}
return { _show: false };
} else {
if (curEqual._type === "select" && Array.isArray(_options) && _options.length === 0) {
return { _options };
}
return {};
}
};
function mergeConfig2(now = [], prev = []) {
if (prev.length === 0 || now.length === 0) {
return now;
}
if (!Array.isArray(prev)) {
return now;
}
return now.map((d) => {
let preEqual = prev.find((v) => v._name === d._name);
if (preEqual != null && (typeof d._value === typeof preEqual._value || !d._value && preEqual._value)) {
if (d._type === "array") {
let template = Array.isArray(d._template) ? d._template[0] : d._template;
let value = preEqual._value.map((v) => {
return __spreadProps(__spreadValues({}, v), {
_value: mergeConfig2(template._value, v._value)
});
});
return __spreadProps(__spreadValues(__spreadValues({}, d), preEqual._show === false ? { _show: false } : {}), {
_value: value
});
} else if (Array.isArray(d._value) && ![
"range",
"cameras",
"checkbox",
"position",
"select",
"children",
"rangeColor"
].includes(d._type)) {
return __spreadProps(__spreadValues(__spreadValues({}, d), preEqual._show === false ? { _show: false } : {}), {
_value: mergeConfig2(d._value, preEqual._value)
});
} else {
return __spreadProps(__spreadValues(__spreadValues({}, d), mergeConfigurationByConfig2(preEqual, d)), {
_value: preEqual._value
});
}
} else {
return d;
}
});
}
function mergeStyleConfig2(current, theme, isPasteStyle = false) {
return current.map((d) => {
if (d._name === "dimension" || d._name === "interaction") {
return d;
}
let themeValue = theme.find((v) => v._name === d._name);
if (themeValue != null && (typeof d._value === typeof themeValue._value || d._type === themeValue._type)) {
if (d._type === "array") {
let value = isPasteStyle ? themeValue._value.map((v, i) => {
var _a;
const { length } = d._value;
const oldV = ((_a = d._value) == null ? void 0 : _a[i]) || d._value[length - 1];
return __spreadProps(__spreadValues({}, oldV), {
_name: v._name,
_value: mergeStyleConfig2(oldV._value, v._value)
});
}) : d._value.map((v, i) => {
return themeValue._value[i] ? __spreadProps(__spreadValues({}, v), {
_value: mergeStyleConfig2(
v._value,
themeValue._value[i]._value,
isPasteStyle
)
}) : v;
});
return __spreadProps(__spreadValues({}, d), {
_value: value
});
} else if (Array.isArray(d._value) && ![
"range",
"checkbox",
"position",
"select",
"children",
"rangeColor"
].includes(d._type)) {
return __spreadProps(__spreadValues({}, d), {
_value: mergeStyleConfig2(d._value, themeValue._value, isPasteStyle)
});
} else {
return __spreadProps(__spreadValues({}, d), {
_value: themeValue._value
});
}
} else {
return d;
}
});
}
function updateArrayWithObject2(arr, obj) {
if (obj == null) {
return arr;
}
return arr.reduce((all, item) => {
if (item._type === "array") {
const currentObj = obj[item._name];
const keys = isPlainObject2(currentObj) ? Object.keys(currentObj) : [];
let addedKeys = keys;
let newValue = item._value.reduce((all2, current) => {
if (keys.includes(current._name)) {
addedKeys = addedKeys.filter((d) => d !== current._name);
if (currentObj[current._name] === null) {
return all2;
} else {
return all2.concat(__spreadProps(__spreadValues({}, current), {
_value: updateArrayWithObject2(
current._value,
currentObj[current._name]
)
}));
}
} else {
return all2.concat(current);
}
}, []);
const template = Array.isArray(item._template) ? item._template[0] : item._template;
newValue = newValue.concat(
addedKeys.filter((d) => currentObj[d] !== null).map((d) => __spreadProps(__spreadValues({}, template), {
_name: d,
_value: updateArrayWithObject2(template._value, currentObj[d])
}))
);
return all.concat(__spreadProps(__spreadValues({}, item), { _value: newValue }));
} else if (obj.hasOwnProperty(item._name)) {
if (Array.isArray(item._value) && item._type !== "position") {
return all.concat(__spreadProps(__spreadValues({}, item), {
_value: updateArrayWithObject2(item._value, obj[item._name])
}));
} else {
return all.concat(__spreadProps(__spreadValues({}, item), { _value: obj[item._name] }));
}
} else {
return all.concat(item);
}
}, []);
}
// src/gui/config.js
var defaultActions = [
{ name: "显示", value: "show" },
{ name: "隐藏", value: "hide" },
{ name: "显隐切换", value: "show/hide" },
{ name: "切换组件状态", value: "switchState" },
{ name: "更新组件配置", value: "updateConfig" },
{ name: "移动", value: "translate" },
{ name: "缩放", value: "scale" },
{ name: "旋转", value: "rotate" }
];
var customActions = [{ name: "切换状态", value: "setIndex" }];
function getScreenDimension3(config) {
return isOldConfig(config) ? getScreenDimension2(config) : getScreenDimension(config);
}
function reduceConfig3(config) {
return isOldConfig(config) ? reduceConfig2(config) : reduceConfig(config);
}
var _reduceConfig = (config) => {
if (!Array.isArray(config)) {
return config;
} else {
return config.reduce((result, current) => {
result[current.name] = current.type !== "componentOptions" && current.config && current.config.options && Array.isArray(current.config.options) ? _reduceConfig(
(current.config.options.find((o) => o.value === current.value) || {}).name
) : _reduceConfig(current.value);
return result;
}, {});
}
};
var _findComponentInLayers = (layers, fakeId) => {
const id = getId(fakeId);
return layers.reduce((result, current) => {
if (result)
return result;
if (current.components) {
if (!result && current.id === id) {
result = current;
} else {
result = _findComponentInLayers(current.components, fakeId);
}
} else {
if (!result && current.id === id) {
result = current;
}
}
return result;
}, null);
};
function getComponentDimension3(config) {
return isOldConfig(config) ? getComponentDimension2(config) : getComponentDimension(config);
}
function getComponentConfig(config) {
return isOldConfig(config) ? config : reduceConfig(config);
}
function updateArrayConfig3(config, updates) {
return isOldConfig(config) ? updateArrayConfig2(config, updates) : updateArrayConfig(config, updates);
}
function getConfig3(config, type, device) {
return isOldConfig(config) ? getConfig2(config, type, device) : getConfig(config, type, device);
}
function mergeConfig3(now, prev) {
let isNowOldConfig = isOldConfig(now);
let isPrevOldConfig = isOldConfig(prev);
if (isNowOldConfig && isPrevOldConfig) {
return mergeConfig2(now, prev);
} else if (!isNowOldConfig && isPrevOldConfig) {
return mergeConfig(now, transformConfig(prev));
} else {
return mergeConfig(now, prev);
}
}
function mergeStyleConfig3(current, styleConfig, isPasteStyle = false) {
let isCurrentOldConfig = isOldConfig(current);
let isStyleOldConfig = isOldConfig(styleConfig);
if (isCurrentOldConfig && isStyleOldConfig) {
return mergeStyleConfig2(current, styleConfig, isPasteStyle);
} else if (!isCurrentOldConfig && isStyleOldConfig) {
return mergeStyleConfig(current, transformConfig(styleConfig), isPasteStyle);
} else if (isCurrentOldConfig && !isStyleOldConfig) {
return mergeStyleConfig2(current, transformToOldConfig(styleConfig), isPasteStyle);
} else {
return mergeStyleConfig(current, styleConfig, isPasteStyle);
}
}
function updateArrayWithObject3(config, obj) {
return isOldConfig(config) ? updateArrayWithObject2(config, obj) : updateArrayWithObject(config, obj);
}
function transformConfig(config) {
if (!Array.isArray(config) || config.length === 0 || !isOldConfig(config)) {
return config;
}
return config.map((d) => {
if (!d._displayName && !d._value && !d._type && !d._name) {
const transformed = Object.keys(d).reduce((acc, key) => {
const newKey = key.startsWith("_") ? key.slice(1) : key;
acc[newKey] = d[key];
return acc;
}, {});
return transformed;
}
const _a = d, { _name, _displayName, _value, _type, _rule, _tip } = _a, rest = __objRest(_a, ["_name", "_displayName", "_value", "_type", "_rule", "_tip"]);
let result = {
name: _name,
displayName: _displayName,
value: transformConfig(_value),
config: {}
};
if (_type !== void 0) {
result.type = _type;
}
if (_rule !== void 0) {
result.rule = _rule;
}
if (_tip !== void 0) {
result.tip = _tip;
}
for (let k in rest) {
result.config[k.slice(1)] = rest[k];
}
return result;
});
}
function transformToOldConfig(config) {
if (!Array.isArray(config)) {
return config;
}
if (config.length === 0) {
return config;
}
if (isOldConfig(config)) {
return config;
}
return config.map((d) => {
const _a = d, { name, displayName, value, type, rule, tip, config: config2 } = _a, rest = __objRest(_a, ["name", "displayName", "value", "type", "rule", "tip", "config"]);
const result = {
_type: type,
_name: name,
_displayName: displayName,
_value: transformToOldConfig(value),
_rule: rule,
_tip: tip
};
for (let k in config2) {
result["_" + k] = config2[k];
}
for (let i in rest) {
result["_" + i] = rest[i];
}
return result;
});
}
function isOnCondition(config, rule) {
let compareConfig = config.find((d) => d.name === rule[0]);
if (!compareConfig) {
return false;
}
switch (rule[1]) {
case "$eq": {
return compareConfig.value === rule[2];
}
case "$neq": {
return compareConfig.value !== rule[2];
}
case "$gt": {
return compareConfig.value > rule[2];
}
case "$lt": {
return compareConfig.value < rule[2];
}
case "$gte": {
return compareConfig.value >= rule[2];
}
case "$lte": {
return compareConfig.value <= rule[2];
}
case "$in": {
return compareConfig.value && compareConfig.value.includes(rule[2]);
}
case "$nin": {
return compareConfig.value || !compareConfig.value.includes(rule[2]);
}
default:
return false;
}
}
function deepConfigsFind(configs, names) {
if (Array.isArray(configs) && names.length) {
const [first, ...rest] = names;
const current = configs.find((config) => config.name === first);
if (current && Array.isArray(current.value) && rest.length > 1) {
return deepConfigsFind(current.value, rest);
} else if (current && Array.isArray(current.value) && rest.length === 1) {
return current.value;
}
}
return configs;
}
function isFullConfigShow(config = [], rules = [], ruleType = "$and", fullConfig = []) {
function getConfigAndRule(rule) {
const [path, ...rest] = rule;
if (path && path.includes && path.includes("/")) {
try {
const paths = path.split("/");
const currentConfig = deepConfigsFind(fullConfig, paths);
const newPath = paths[paths.length - 1];
return {
config: currentConfig,
rule: [newPath, ...rest]
};
} catch (err) {
console.log(err);
}
}
return {
config,
rule
};
}
if (ruleType === "$or") {
return rules.some((rule) => {
const { config: newConfig, rule: newRule } = getConfigAndRule(rule);
return isOnCondition(newConfig, newRule);
});
} else {
return rules.every((rule) => {
const { config: newConfig, rule: newRule } = getConfigAndRule(rule);
return isOnCondition(newConfig, newRule);
});
}
}
function isConfigShow(config = [], rules = [], ruleType = "$and") {
if (ruleType === "$or") {
return rules.some((rule) => {
return isOnCondition(config, rule);
});
} else {
return rules.every((rule) => {
return isOnCondition(config, rule);
});
}
}
function isOldConfig(config = []) {
return config[0] && config[0].hasOwnProperty("_name");
}
function getValueObjFromConfig3(config, path) {
let isOld = isOldConfig(config);
return isOld ? getValueObjFromConfig2(config, path) : getValueObjFromConfig(config, path);
}
function getValueFromConfig3(config, path, field) {
let isOld = isOldConfig(config);
return isOld ? getValueFromConfig2(config, path, field) : getValueFromConfig(config, path, field);
}
function getLayersWithCustomId(layers = [], onlyComponent) {
return layers.reduce((result, current) => {
if (typeof current.id === "number") {
return result.concat([
{
title: current.name,
value: `$component(${current.id})`,
children: current.children && getLayersWithCustomId(current.children, onlyComponent)
}
]);
} else if (current.id.includes("group_")) {
return result.concat([
{
disabled: onlyComponent,
title: current.name,
value: `$group(${current.id})`,
children: getLayersWithCustomId(current.components, onlyComponent)
}
]);
} else if (current.id.toString().startsWith("panel_")) {
return result.concat([
{
disabled: onlyComponent,
title: current.name,
value: `$panel(${current.id})`,
children: getLayersWithCustomId(current.components, onlyComponent)
}
]);
} else if (current.id.toString().startsWith("screen_")) {
return result.concat({
disabled: true,
title: current.name,
value: `$screen(${current.id.replace("screen_", "")})`,
children: getLayersWithCustomId(current.components, onlyComponent)
});
}
}, []);
}
var getEvents = (component, layers, panels = [], components) => {
let { id, config = [], events = [] } = component;
config = transformConfig(config);
const result = [];
const originConfigs = reduceConfig3(config);
const configs = _reduceConfig(config);
const originInteraction = originConfigs.interaction;
const interaction = configs.interaction;
if (interaction) {
const events2 = interaction.events;
const originEvents = originInteraction.events;
if (events2) {
Object.keys(events2).map((eventKey) => {
const event = events2[eventKey];
const value = originEvents[eventKey];
let type = null;
let action = null;
let component2 = null;
let panel = null;
Object.keys(event).map((objKey) => {
objKey === "type" && (type = event.type);
objKey === "action" && (action = event.action);
value.action === "switchState" ? objKey === "panel" && (panel = JSON.parse(value.panel || "{}")) : objKey === "component" && (component2 = _findComponentInLayers(layers, event.component));
});
if (type && value.action !== "switchState") {
result.push({
[`${type} ${action} ${component2 && component2.name ? component2.name : "组件"}`]: value
});
}
if (type && value.action === "switchState" && panel && panel.panelId && panel.stateId) {
const panelId = (getId(panel.panelId) || "").split("_")[1];
const stateId = getId(panel.stateId);
if (panelId && stateId) {
const thePanel = panels.find((o) => +o.id === +panelId);
if (thePanel && thePanel.states) {
const theState = thePanel.states.find((o) => +o.id === +stateId);
if (theState) {
result.push({
[`${type} 切换 ${thePanel.name} ${theState.name}`]: value
});
}
}
}
}
});
}
}
let originComponent = components.find((d) => d.id == id) || {};
let { triggers = [] } = originComponent;
let allTriggers = [
{ name: "当请求完成或数据变化时", value: "dataChange" }
].concat(triggers);
function getRemoteConfigValue(trigger, config2) {
const {
component: componentIds,
action,
stateId,
translate,
rotate,
scale,
componentConfig,
actionData,
animation,
actionDataFn
} = config2;
const currentTrigger = allTriggers.find((d) => d.value === trigger) || {};
const triggerName = currentTrigger.name;
const targetComponentsInfo = componentIds.map((d) => {
if (isComponent(d)) {
return components.find((o) => o.id === getId(d));
} else if (isPanel(d)) {
return panels.find((o) => o.id == getId(d).replace("panel_", ""));
} else if (isGroup(d)) {
return _findComponentInLayers(layers, d);
}
}).filter((d) => d);
const componentInfo = targetComponentsInfo[0] || {};
const { actions = [] } = componentInfo;
const allActions = defaultActions.concat(actions).concat(customActions);
const currentAction = allActions.find((d) => d.value === action) || {};
const actionName = currentAction.name;
const targetComponentName = targetComponentsInfo.map((d) => d.name || "组件");
return {
[`${triggerName} ${action === "switchState" ? "切换" : actionName} ${targetComponentName.join("、")}`]: {
type: trigger,
action,
component: action === "switchState" ? "" : componentIds,
panel: action === "switchState" ? `{"panelId": "${componentIds[0]}","stateId": "${stateId}" }` : "{}",
componentConfig: action === "updateConfig" ? componentConfig : void 0,
actionData: defaultActions.some((d) => d.value === action) ? void 0 : actionData,
translate: action === "translate" ? translate : void 0,
scale: action === "scale" ? scale : void 0,
rotate: action === "rotate" ? rotate : void 0,
animation,
actionDataFn,
triggerComponentId: actionDataFn ? `$component(${id})` : void 0
}
};
}
const customEvents = Array.isArray(events) ? events.reduce((all, item) => {
const { trigger, actions: eventActions } = item;
if (eventActions) {
return all.concat(
eventActions.map((d) => getRemoteConfigValue(trigger, d))
);
} else {
return all.concat(getRemoteConfigValue(trigger, item));
}
}, []) : [];
return result.concat(customEvents);
};
var filterLayersWithEvents = (layers, components, componentsWithInfo, panels) => {
return layers.reduce((result, current) => {
if (isGroup(current.id)) {
const componentsResult = filterLayersWithEvents(
current.components,
components,
componentsWithInfo,
panels
);
if (componentsResult && componentsResult.length > 0) {
return result.concat([
__spreadProps(__spreadValues({}, current), {
title: current.name,
value: `$component(${current.id})`,
children: componentsResult
})
]);
} else {
return result;
}
}
if (isComponent(current.id)) {
const component = components.find((o) => +o.id === +current.id);
if (component) {
const { events: customEvents, config } = component;
const events = getEvents(
{
id: current.id,
config: JSON.parse(config),
events: JSON.parse(customEvents)
},
componentsWithInfo,
panels,
components
);
if (~components.findIndex((d) => d.parent === current.id)) {
const currentChildrens = components.filter(
(d) => d.parent === current.id
);
const componentsResult = filterLayersWithEvents(
currentChildrens,
components,
componentsWithInfo,
panels
);
if (componentsResult.length > 0) {
return result.concat([
__spreadProps(__spreadValues({}, current), {
title: current.name,
disabled: !(events.length > 0),
value: `$component(${current.id})`,
children: componentsResult
})
]);
} else if (Array.isArray(events) && events.length) {
return result.concat([
__spreadProps(__spreadValues({}, current), {
title: current.name,
value: `$component(${current.id})`
})
]);
} else {
return result;
}
} else if (events.length > 0) {
return result.concat([
__spreadProps(__spreadValues({}, current), {
title: current.name,
value: `$component(${current.id})`
})
]);
} else {
return result;
}
} else {
return result;
}
} else {
return result;
}
}, []);
};
function getStateOptions(states = []) {
return state.map((d) => ({ name: d.name, value: `$state(${d.value})` }));
}
function updateBindName(val, type) {
if (type) {
return `$${type}(${val})`;
}
return `$${val}`;
}
function getComponentOptions({
screensById,
panelsById,
componentsById,
screenId,
stateId,
mode = "current"
}) {
let screen = mode === "current" ? screensById[stateId] : screensById[screenId];
if (screen) {
const { layers = [] } = screen;
const deepMapLayers = (layers2, type = "") => {
return layers2.map((item) => {
if (isComponent(item.id)) {
let componentWithChildren = getComponentLayerWithChildren(
{ componentsById, id: item.id, name: item.name },
type
);
return componentWithChildren;
} else if (isPanel(item.id)) {
if (mode === "current") {
return {
title: item.name,
value: updateBindName(`panel(${item.id})`, type)
};
} else {
let panelWithStates = getPanelWithStates({
screensById,
panelsById,
id: item.id,
name: item.name
});
if (panelWithStates.children) {
return __spreadProps(__spreadValues({}, panelWithStates), {
children: panelWithStates.children.map((d) => {
let stateId2 = getId(d.value);
return __spreadProps(__spreadValues({}, d), {
disabled: true,
children: getComponentOptions({
screensById,
componentsById,
panelsById,
screenId: stateId2,
stateId: stateId2,
mode: "global"
})
});
})
});
} else {
return panelWithStates;
}
}
} else if (isContainer(item.id)) {
const containers = screensById[getId(item.id)];
let children = [];
if (containers) {
const screen2 = screensById[containers.subScreenId];
if (screen2) {
children = deepMapLayers(screen2.layers, item.id);
}
}
return {
title: item.name,
value: updateBindName(`container(${item.id})`,