@wufengteam/inputs
Version:
平台提供的右侧属性编辑器,需要在主工程中注册
76 lines • 3.6 kB
JavaScript
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
/**
* 获取表单字段
* @param components
* @returns
*/
function getFields(components) {
var fields = [];
components.forEach(function (component) {
var _a, _b;
if (((_a = component === null || component === void 0 ? void 0 : component.props) === null || _a === void 0 ? void 0 : _a.name) && ((_b = component === null || component === void 0 ? void 0 : component.props) === null || _b === void 0 ? void 0 : _b.label)) {
fields.push({
label: component.props.label,
name: component.props.name,
component: component
});
}
if (Array.isArray(component.components)) {
fields.push.apply(fields, _toConsumableArray(getFields(component.components)));
}
});
return fields;
}
function processComponent(component) {
// 低代码组件标识是用 compname 字段
if (!component.type && component.compName) {
// eslint-disable-next-line no-param-reassign
component.type = component.compName;
}
}
/**
* 处理组件,兼容低代码
* @param components
*/
function processComponents(components) {
components.forEach(function (component) {
processComponent(component);
if (Array.isArray(component.components)) {
processComponents(component.components);
}
});
}
var useDSLCore = function useDSLCore(_ref) {
var DSLCore = _ref.DSLCore;
var _a, _b, _c, _d, _e;
if (!DSLCore) {
// 如果 DSLCore 不存在,说明不在低代码环境
return {};
}
var dsl = DSLCore.toJSON();
var pageData = dsl.components;
processComponents(pageData);
var currentNode = (_a = DSLCore.current) === null || _a === void 0 ? void 0 : _a.toJSON();
processComponent(currentNode);
var currentField = {
label: (_b = currentNode === null || currentNode === void 0 ? void 0 : currentNode.props) === null || _b === void 0 ? void 0 : _b.label,
name: (_c = currentNode === null || currentNode === void 0 ? void 0 : currentNode.props) === null || _c === void 0 ? void 0 : _c.name
};
var fields = getFields(pageData);
var pageId = (_e = (_d = DSLCore === null || DSLCore === void 0 ? void 0 : DSLCore.document) === null || _d === void 0 ? void 0 : _d.rootNode) === null || _e === void 0 ? void 0 : _e.id;
return {
pageData: pageData,
currentField: currentField,
fields: fields,
component: currentNode,
pageInfo: {
pageId: pageId
}
};
};
export default useDSLCore;