UNPKG

react-json-schema-editor-antd

Version:

Json Schema Editor

899 lines (877 loc) 44.2 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } 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; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /* eslint-disable @typescript-eslint/no-unused-expressions */ import { useMemoizedFn } from 'ahooks'; import { snowflakeId } from 'apipost-tools'; import { produce } from 'immer'; import { cloneDeep, debounce, forEach, includes, isArray, isBoolean, isEmpty, isEqual, isNumber, isPlainObject, isString, isUndefined, pull } from 'lodash'; import { useEffect, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { DELETE_KEYS } from "../constants"; import { getDefaultSchema } from "../utils"; // initialSchema 为你的初始 schema export var useSchemaOperations = function useSchemaOperations(props) { var messageApi = props.messageApi, schema = props.schema, setSchema = props.setSchema; var _useTranslation = useTranslation(), t = _useTranslation.t; var schemaRef = useRef(schema); useEffect(function () { schemaRef.current = schema; }, [schema]); var updateSchema = useMemoizedFn(function (namePath, value) { var newSchema = produce(schemaRef.current, function (draft) { var _tmp_schema3, _tmp_schema4, _tmp_schema5, _tmp_schema6, _tmp_schema7; // if (namePath.length <= 0) { // return; // } var tmp_schema = draft; var lastKey = ''; namePath.reduce(function (acc, key, index) { // 自动创建中间路径 if (!acc[key] && index < namePath.length - 1) { var _tmp_schema; // 去refs内部查找 if ((_tmp_schema = tmp_schema) !== null && _tmp_schema !== void 0 && (_tmp_schema = _tmp_schema['x-schema-refs']) !== null && _tmp_schema !== void 0 && _tmp_schema[key]) { var _tmp_schema2; return (_tmp_schema2 = tmp_schema) === null || _tmp_schema2 === void 0 || (_tmp_schema2 = _tmp_schema2['x-schema-refs']) === null || _tmp_schema2 === void 0 ? void 0 : _tmp_schema2[key]; } else { acc[key] = isNumber(key) ? [] : {}; } } if (key === 'properties' || key === 'x-schema-overrides') { tmp_schema = acc; } if (index === namePath.length - 1) { // acc[key] = value; tmp_schema = acc; lastKey = key; } return acc[key]; }, draft); if (namePath.length > 0) { if (DELETE_KEYS.includes(lastKey) && (value === undefined || value === null || value === '' || isArray(value) && isEmpty(value))) { delete tmp_schema[lastKey]; } else if (!isEqual(tmp_schema[lastKey], value)) { tmp_schema[lastKey] = value; } } else { tmp_schema = value; return tmp_schema; } if (lastKey === 'examples' && (_tmp_schema3 = tmp_schema) !== null && _tmp_schema3 !== void 0 && _tmp_schema3.example) { delete tmp_schema.example; } if (lastKey === 'uniqueItems' && !value) { delete tmp_schema.uniqueItems; } if (lastKey === 'const' && (_tmp_schema4 = tmp_schema) !== null && _tmp_schema4 !== void 0 && _tmp_schema4.enum) { delete tmp_schema.enum; } if (lastKey === 'enum' && (_tmp_schema5 = tmp_schema) !== null && _tmp_schema5 !== void 0 && _tmp_schema5.const) { delete tmp_schema.const; } if (lastKey === 'exclusiveMinimum' && isBoolean(value) && !value) { delete tmp_schema.exclusiveMinimum; } if (lastKey === 'exclusiveMaximum' && isBoolean(value) && !value) { delete tmp_schema.exclusiveMaximum; } if (lastKey === 'minimum' && isNumber((_tmp_schema6 = tmp_schema) === null || _tmp_schema6 === void 0 ? void 0 : _tmp_schema6.exclusiveMinimum)) { tmp_schema.exclusiveMinimum = value; } if (lastKey === 'maximum' && isNumber((_tmp_schema7 = tmp_schema) === null || _tmp_schema7 === void 0 ? void 0 : _tmp_schema7.exclusiveMaximum)) { tmp_schema.exclusiveMaximum = value; } }); setSchema && setSchema(newSchema); }); // 修改属性名称 var renamePropertySource = useMemoizedFn(function (namePath, newKey) { var newSchema = produce(schemaRef.current, function (draft) { var tmp_schema = draft; namePath.reduce(function (acc, key, index) { // 自动创建中间路径 if (!acc[key] && index < namePath.length - 1) { var _tmp_schema8; // 去refs内部查找 if ((_tmp_schema8 = tmp_schema) !== null && _tmp_schema8 !== void 0 && (_tmp_schema8 = _tmp_schema8['x-schema-refs']) !== null && _tmp_schema8 !== void 0 && _tmp_schema8[key]) { var _tmp_schema9; return (_tmp_schema9 = tmp_schema) === null || _tmp_schema9 === void 0 || (_tmp_schema9 = _tmp_schema9['x-schema-refs']) === null || _tmp_schema9 === void 0 ? void 0 : _tmp_schema9[key]; } else { acc[key] = isNumber(key) ? [] : {}; // return ''; } } if (key === 'properties') { tmp_schema = acc; } if (key === 'x-schema-overrides') { tmp_schema = acc; } if (index === namePath.length - 1 && newKey !== key) { var _acc$key, _tmp_schema10, _tmp_schema13; if (acc[newKey]) { messageApi.warning(t('key_repeat_warning', { key: newKey })); // messageApi.warning(`Field of ${newKey} duplicate, please check`); return ''; } // 判断是否为临时属性 if ((_acc$key = acc[key]) !== null && _acc$key !== void 0 && _acc$key['x-tmp-properties']) { acc[newKey] = acc[key]; delete acc[newKey]['x-tmp-properties']; acc[newKey].type = 'string'; } else if (isUndefined(acc[key])) { return ''; } else { acc[newKey] = acc[key]; } delete acc[key]; // 替换order 和 required if (isArray((_tmp_schema10 = tmp_schema) === null || _tmp_schema10 === void 0 ? void 0 : _tmp_schema10['x-schema-orders'])) { var _tmp_schema11; var oldIndex = (_tmp_schema11 = tmp_schema) === null || _tmp_schema11 === void 0 ? void 0 : _tmp_schema11['x-schema-orders'].findIndex(function (i) { return i === key; }); tmp_schema['x-schema-orders'][oldIndex] = newKey; } else { var _tmp_schema12; if (isPlainObject((_tmp_schema12 = tmp_schema) === null || _tmp_schema12 === void 0 ? void 0 : _tmp_schema12.properties)) { tmp_schema['x-schema-orders'] = Object.keys(tmp_schema.properties); } else { tmp_schema['x-schema-orders'] = [newKey]; } } if (isArray((_tmp_schema13 = tmp_schema) === null || _tmp_schema13 === void 0 ? void 0 : _tmp_schema13.required)) { var _tmp_schema14; var _oldIndex = (_tmp_schema14 = tmp_schema) === null || _tmp_schema14 === void 0 ? void 0 : _tmp_schema14.required.findIndex(function (i) { return i === key; }); tmp_schema.required[_oldIndex] = newKey; } return ''; } return acc[key]; }, draft); }); setSchema && setSchema(newSchema); }); var renameProperty = useMemoizedFn(debounce(renamePropertySource, 100)); // 直接替换整行schema数据 var onSchemaRowUpdate = useMemoizedFn(function (namePath, newKey, newSchema) { var newSchemaDraft = produce(schemaRef.current, function (draft) { var tmp_schema = draft; namePath.reduce(function (acc, key, index) { // 查找路径 if (!acc[key] && index < namePath.length - 1) { var _tmp_schema15; // 去refs内部查找 if ((_tmp_schema15 = tmp_schema) !== null && _tmp_schema15 !== void 0 && (_tmp_schema15 = _tmp_schema15['x-schema-refs']) !== null && _tmp_schema15 !== void 0 && _tmp_schema15[key]) { var _tmp_schema16; return (_tmp_schema16 = tmp_schema) === null || _tmp_schema16 === void 0 || (_tmp_schema16 = _tmp_schema16['x-schema-refs']) === null || _tmp_schema16 === void 0 ? void 0 : _tmp_schema16[key]; } else { return ''; // acc[key] = isNumber(key) ? [] : {}; } } if (key === 'properties') { tmp_schema = acc; } if (key === 'x-schema-overrides') { tmp_schema = acc; } if (index === namePath.length - 1) { if (acc[newKey] && newKey !== key) { messageApi.warning(t('key_repeat_warning', { key: newKey })); // messageApi.warning(`Field of ${newKey} duplicate, please check`); return ''; } // 判断是否为临时属性 acc[newKey] = newSchema; if (newKey !== key) { var _tmp_schema17, _tmp_schema19; delete acc[key]; // 替换order 和 required if (isArray((_tmp_schema17 = tmp_schema) === null || _tmp_schema17 === void 0 ? void 0 : _tmp_schema17['x-schema-orders'])) { var _tmp_schema18; var oldIndex = (_tmp_schema18 = tmp_schema) === null || _tmp_schema18 === void 0 ? void 0 : _tmp_schema18['x-schema-orders'].findIndex(function (i) { return i === key; }); if (oldIndex >= 0) { tmp_schema['x-schema-orders'][oldIndex] = newKey; } else { tmp_schema['x-schema-orders'].push(newKey); } } else { tmp_schema['x-schema-orders'] = [newKey]; } if (isArray((_tmp_schema19 = tmp_schema) === null || _tmp_schema19 === void 0 ? void 0 : _tmp_schema19.required)) { var _tmp_schema20; var _oldIndex2 = (_tmp_schema20 = tmp_schema) === null || _tmp_schema20 === void 0 ? void 0 : _tmp_schema20.required.findIndex(function (i) { return i === key; }); if (_oldIndex2 > 0) { tmp_schema.required[_oldIndex2] = newKey; } } } return ''; } return acc[key]; }, draft); }); setSchema && setSchema(newSchemaDraft); }); // 更新属性是否必填 var updateRequiredProperty = useMemoizedFn(function (namePath, property, removed) { var newSchema = produce(schemaRef.current, function (draft) { var tmp_schema = draft; namePath.reduce(function (acc, key, index) { // 自动创建中间路径 if (!acc[key] && index < namePath.length - 1) { var _tmp_schema21; // 去refs内部查找 if ((_tmp_schema21 = tmp_schema) !== null && _tmp_schema21 !== void 0 && (_tmp_schema21 = _tmp_schema21['x-schema-refs']) !== null && _tmp_schema21 !== void 0 && _tmp_schema21[key]) { var _tmp_schema22; return (_tmp_schema22 = tmp_schema) === null || _tmp_schema22 === void 0 || (_tmp_schema22 = _tmp_schema22['x-schema-refs']) === null || _tmp_schema22 === void 0 ? void 0 : _tmp_schema22[key]; } else { acc[key] = isNumber(key) ? [] : {}; } } if (key === 'properties') { tmp_schema = acc; } if (key === 'x-schema-overrides') { tmp_schema = acc; } if (index === namePath.length - 1) { var _tmp_schema23; if (!isArray((_tmp_schema23 = tmp_schema) === null || _tmp_schema23 === void 0 ? void 0 : _tmp_schema23.required)) { tmp_schema.required = []; } var propertyIndex = tmp_schema.required.indexOf(property); if (!removed) { if (propertyIndex !== -1) { tmp_schema.required.splice(propertyIndex, 1); } } else { if (propertyIndex === -1) { tmp_schema.required.push(property); } } return ''; } return acc[key]; }, draft); }); setSchema && setSchema(newSchema); }); // 更新属性是否允许为空 var updateAllowNull = useMemoizedFn(function (namePath, allowNull) { var newSchema = produce(schemaRef.current, function (draft) { var tmp_schema = draft; namePath.reduce(function (acc, key, index) { // 自动创建中间路径 if (!acc[key] && index < namePath.length - 1) { var _tmp_schema24; // 去refs内部查找 if ((_tmp_schema24 = tmp_schema) !== null && _tmp_schema24 !== void 0 && (_tmp_schema24 = _tmp_schema24['x-schema-refs']) !== null && _tmp_schema24 !== void 0 && _tmp_schema24[key]) { var _tmp_schema25; return (_tmp_schema25 = tmp_schema) === null || _tmp_schema25 === void 0 || (_tmp_schema25 = _tmp_schema25['x-schema-refs']) === null || _tmp_schema25 === void 0 ? void 0 : _tmp_schema25[key]; } else { acc[key] = isNumber(key) ? [] : {}; } } if (index === namePath.length - 1) { tmp_schema = acc[key]; return ''; } return acc[key]; }, draft); if (!tmp_schema) { return; } if (allowNull) { var _tmp_schema26; if (isArray((_tmp_schema26 = tmp_schema) === null || _tmp_schema26 === void 0 ? void 0 : _tmp_schema26.type)) { tmp_schema.type.push('null'); } else { var _tmp_schema27; tmp_schema.type = [((_tmp_schema27 = tmp_schema) === null || _tmp_schema27 === void 0 ? void 0 : _tmp_schema27.type) || 'object', 'null']; } } else { var _tmp_schema28; if (isArray((_tmp_schema28 = tmp_schema) === null || _tmp_schema28 === void 0 ? void 0 : _tmp_schema28.type)) { var nullIndex = tmp_schema.type.indexOf('null'); tmp_schema.type.splice(nullIndex, 1); if (tmp_schema.type.length === 1) { tmp_schema.type = tmp_schema.type[0]; } } } }); setSchema && setSchema(newSchema); }); // 删除属性 var removeProperty = useMemoizedFn(function (namePath) { var newSchema = produce(schemaRef.current, function (draft) { var tmp_schema = draft; namePath.reduce(function (acc, key, index) { // 自动创建中间路径 if (!acc[key] && index < namePath.length - 1) { var _tmp_schema29; // 去refs内部查找 if ((_tmp_schema29 = tmp_schema) !== null && _tmp_schema29 !== void 0 && (_tmp_schema29 = _tmp_schema29['x-schema-refs']) !== null && _tmp_schema29 !== void 0 && _tmp_schema29[key]) { var _tmp_schema30; return (_tmp_schema30 = tmp_schema) === null || _tmp_schema30 === void 0 || (_tmp_schema30 = _tmp_schema30['x-schema-refs']) === null || _tmp_schema30 === void 0 ? void 0 : _tmp_schema30[key]; } else { acc[key] = isNumber(key) ? [] : {}; } } if (key === 'properties') { tmp_schema = acc; } if (key === 'x-schema-overrides') { tmp_schema = acc; } if (index === namePath.length - 1) { var _tmp_schema31, _tmp_schema32; if (isArray(acc) && isNumber(key)) { acc.splice(key, 1); } else { delete acc[key]; } if (isArray((_tmp_schema31 = tmp_schema) === null || _tmp_schema31 === void 0 ? void 0 : _tmp_schema31.required)) { for (var i = tmp_schema.required.length - 1; i >= 0; i--) { if (tmp_schema.required[i] === key) { tmp_schema.required.splice(i, 1); } } } if (isArray((_tmp_schema32 = tmp_schema) === null || _tmp_schema32 === void 0 ? void 0 : _tmp_schema32['x-schema-orders'])) { for (var _i = tmp_schema['x-schema-orders'].length - 1; _i >= 0; _i--) { if (tmp_schema['x-schema-orders'][_i] === key) { tmp_schema['x-schema-orders'].splice(_i, 1); } } } // const propertyIndex = tmp_schema.required.indexOf(property); // if(!removed){ // if (propertyIndex !== -1) { // tmp_schema.required.splice(propertyIndex, 1); // } // }else{ // if (propertyIndex === -1) { // tmp_schema.required.push(property); // } // } return ''; } return acc[key]; }, draft); }); setSchema && setSchema(newSchema); }); // 删除引用模型 var removeRef = useMemoizedFn(function (namePath) { var newSchema = produce(schemaRef.current, function (draft) { var tmp_schema = draft; namePath.reduce(function (acc, key, index) { // 自动创建中间路径 if (!acc[key] && index < namePath.length - 1) { acc[key] = isNumber(key) ? [] : {}; } if (key === 'properties' || key === 'x-schema-refs') { tmp_schema = acc; } if (index === namePath.length - 1) { var _tmp_schema33, _tmp_schema34, _tmp_schema36, _tmp_schema37; // delete acc[key]; if (isPlainObject((_tmp_schema33 = tmp_schema) === null || _tmp_schema33 === void 0 ? void 0 : _tmp_schema33['x-schema-refs']) && (_tmp_schema34 = tmp_schema) !== null && _tmp_schema34 !== void 0 && (_tmp_schema34 = _tmp_schema34['x-schema-refs']) !== null && _tmp_schema34 !== void 0 && _tmp_schema34[key]) { var _tmp_schema35; (_tmp_schema35 = tmp_schema) === null || _tmp_schema35 === void 0 || (_tmp_schema35 = _tmp_schema35['x-schema-refs']) === null || _tmp_schema35 === void 0 || delete _tmp_schema35[key]; } if (isArray((_tmp_schema36 = tmp_schema) === null || _tmp_schema36 === void 0 ? void 0 : _tmp_schema36['x-schema-orders'])) { for (var i = tmp_schema['x-schema-orders'].length - 1; i >= 0; i--) { if (tmp_schema['x-schema-orders'][i] === key) { tmp_schema['x-schema-orders'].splice(i, 1); } } } if (isArray((_tmp_schema37 = tmp_schema) === null || _tmp_schema37 === void 0 ? void 0 : _tmp_schema37.required)) { var _index = tmp_schema.required.indexOf(key); // 找到元素索引 if (_index !== -1) { tmp_schema.required.splice(_index, 1); // 删除元素 } } return ''; } return acc[key]; }, draft); }); setSchema && setSchema(newSchema); }); // 添加模型引用 var addRef = useMemoizedFn(function (namePath, refData, update) { if (!isString(refData === null || refData === void 0 ? void 0 : refData.model_id)) { return; } var newSchema = produce(schemaRef.current, function (draft) { var tmp_schema = draft; if (namePath.length > 0) { namePath.reduce(function (acc, key, index) { // 自动创建中间路径 if (!acc[key] && index < namePath.length - 1) { var _tmp_schema38; // 去refs内部查找 if ((_tmp_schema38 = tmp_schema) !== null && _tmp_schema38 !== void 0 && (_tmp_schema38 = _tmp_schema38['x-schema-refs']) !== null && _tmp_schema38 !== void 0 && _tmp_schema38[key]) { var _tmp_schema39; return (_tmp_schema39 = tmp_schema) === null || _tmp_schema39 === void 0 || (_tmp_schema39 = _tmp_schema39['x-schema-refs']) === null || _tmp_schema39 === void 0 ? void 0 : _tmp_schema39[key]; } else { acc[key] = isNumber(key) ? [] : {}; } } if (key === 'properties') { tmp_schema = acc; } if (key === 'x-schema-overrides') { tmp_schema = acc; } if (index === namePath.length - 1) { var _tmp_schema40, _tmp_schema42, _tmp_schema43; if (update) { acc[key] = { $ref: "#/components/schemas/".concat(refData === null || refData === void 0 ? void 0 : refData.model_id) }; return ''; } var tem_properties_id = snowflakeId(); if (isArray((_tmp_schema40 = tmp_schema) === null || _tmp_schema40 === void 0 ? void 0 : _tmp_schema40.required)) { var _tmp_schema41; var oldIndex = (_tmp_schema41 = tmp_schema) === null || _tmp_schema41 === void 0 ? void 0 : _tmp_schema41.required.findIndex(function (i) { return i === key; }); if (oldIndex >= 0) { tmp_schema.required[oldIndex] = tem_properties_id; } } if (isArray((_tmp_schema42 = tmp_schema) === null || _tmp_schema42 === void 0 ? void 0 : _tmp_schema42['x-schema-orders'])) { var lastKeyIndex = tmp_schema['x-schema-orders'].indexOf(key); if (lastKeyIndex !== -1) { tmp_schema['x-schema-orders'].splice(lastKeyIndex, 1, tem_properties_id); } else { tmp_schema['x-schema-orders'].push(tem_properties_id); } } else { tmp_schema['x-schema-orders'] = [tem_properties_id]; } if (!isPlainObject((_tmp_schema43 = tmp_schema) === null || _tmp_schema43 === void 0 ? void 0 : _tmp_schema43['x-schema-refs'])) { tmp_schema['x-schema-refs'] = {}; } tmp_schema['x-schema-refs'][tem_properties_id] = { $ref: "#/components/schemas/".concat(refData === null || refData === void 0 ? void 0 : refData.model_id) }; delete acc[key]; return ''; } return acc[key]; }, draft); } else { if (update) { tmp_schema = { $ref: "#/components/schemas/".concat(refData === null || refData === void 0 ? void 0 : refData.model_id) }; return tmp_schema; } } }); setSchema && setSchema(newSchema); }); // 隐藏模型内部属性 var hiddenRefProperty = useMemoizedFn(function (namePath, isHidden) { var newSchema = produce(schemaRef.current, function (draft) { var tmp_schema = draft; namePath.reduce(function (acc, key, index) { if (!acc[key] && index < namePath.length - 1) { var _tmp_schema44; // 去refs内部查找 if ((_tmp_schema44 = tmp_schema) !== null && _tmp_schema44 !== void 0 && (_tmp_schema44 = _tmp_schema44['x-schema-refs']) !== null && _tmp_schema44 !== void 0 && _tmp_schema44[key]) { var _tmp_schema45; return (_tmp_schema45 = tmp_schema) === null || _tmp_schema45 === void 0 || (_tmp_schema45 = _tmp_schema45['x-schema-refs']) === null || _tmp_schema45 === void 0 ? void 0 : _tmp_schema45[key]; } else { acc[key] = isNumber(key) ? [] : {}; } } if (key === 'properties') { tmp_schema = acc; } if (key === 'x-schema-overrides') { tmp_schema = acc[key]; } if (index === namePath.length - 1) { if (isHidden) { acc[key] = null; } else { delete tmp_schema[key]; } } return acc[key]; }, draft); }); setSchema && setSchema(newSchema); }); // 属性直接引用模型 var linkRefProperty = useMemoizedFn(function (namePath, propertySchema, link) { var newSchema = produce(schemaRef.current, function (draft) { var tmp_schema = draft; namePath.reduce(function (acc, key, index) { if (!acc[key] && index < namePath.length - 1) { var _tmp_schema46; // 去refs内部查找 if ((_tmp_schema46 = tmp_schema) !== null && _tmp_schema46 !== void 0 && (_tmp_schema46 = _tmp_schema46['x-schema-refs']) !== null && _tmp_schema46 !== void 0 && _tmp_schema46[key]) { var _tmp_schema47; return (_tmp_schema47 = tmp_schema) === null || _tmp_schema47 === void 0 || (_tmp_schema47 = _tmp_schema47['x-schema-refs']) === null || _tmp_schema47 === void 0 ? void 0 : _tmp_schema47[key]; } else { acc[key] = isNumber(key) ? [] : {}; } } if (key === 'properties') { tmp_schema = acc; } if (key === 'x-schema-overrides') { tmp_schema = acc[key]; } if (index === namePath.length - 1) { if (link) { delete acc[key]; } else { acc[key] = propertySchema; // delete tmp_schema[key] } return ''; } return acc[key]; }, draft); }); setSchema && setSchema(newSchema); }); // 解除模型引用 var unreferenceRef = useMemoizedFn(function (namePath, refSchema, schema) { if (!isPlainObject(refSchema)) { return; } var echoapiOrders = refSchema === null || refSchema === void 0 ? void 0 : refSchema['x-schema-orders']; var nodeOrders = isArray(echoapiOrders) ? echoapiOrders : Object.keys((refSchema === null || refSchema === void 0 ? void 0 : refSchema.properties) || {}); var newSchema = produce(schemaRef.current, function (draft) { var tmp_schema = draft; var previousKey = ''; if (namePath.length > 0) { namePath.reduce(function (acc, key, index) { // 自动创建中间路径 if (!acc[key] && index < namePath.length - 1) { acc[key] = isNumber(key) ? [] : {}; } if (key === 'properties' || key === 'x-schema-refs') { tmp_schema = acc; } if (index === namePath.length - 1) { var _acc$key2, _tmp_schema48, _tmp_schema50, _tmp_schema51, _tmp_schema52, _tmp_schema53, _tmp_schema55, _tmp_schema56; if (isString((_acc$key2 = acc[key]) === null || _acc$key2 === void 0 ? void 0 : _acc$key2.$ref) && acc[key].$ref.length > 0 && previousKey === 'properties') { acc[key] = cloneDeep(refSchema); return ''; } var refOrder = -1; // 查找模型所在order if (isArray((_tmp_schema48 = tmp_schema) === null || _tmp_schema48 === void 0 ? void 0 : _tmp_schema48['x-schema-orders'])) { var _tmp_schema49; refOrder = (_tmp_schema49 = tmp_schema) === null || _tmp_schema49 === void 0 ? void 0 : _tmp_schema49['x-schema-orders'].findIndex(function (i) { return i === key; }); } else { tmp_schema['x-schema-orders'] = []; } if (!isPlainObject((_tmp_schema50 = tmp_schema) === null || _tmp_schema50 === void 0 ? void 0 : _tmp_schema50['x-schema-refs'])) { tmp_schema['x-schema-refs'] = {}; } if (!isArray((_tmp_schema51 = tmp_schema) === null || _tmp_schema51 === void 0 ? void 0 : _tmp_schema51.required)) { tmp_schema.required = []; } // 解析模型数据 forEach(nodeOrders, function (nodeName) { var _schema$xSchemaOver, _refSchema$properties, _refSchema$xSchemaR; // 属性被解除关联 if (schema !== null && schema !== void 0 && (_schema$xSchemaOver = schema['x-schema-overrides']) !== null && _schema$xSchemaOver !== void 0 && _schema$xSchemaOver[nodeName]) { var _schema$xSchemaOver2; tmp_schema.properties[nodeName] = schema === null || schema === void 0 || (_schema$xSchemaOver2 = schema['x-schema-overrides']) === null || _schema$xSchemaOver2 === void 0 ? void 0 : _schema$xSchemaOver2[nodeName]; if (!tmp_schema['x-schema-orders'].includes(nodeName)) { tmp_schema['x-schema-orders'].splice(refOrder + 1, 0, nodeName); } refOrder++; } else if (refSchema !== null && refSchema !== void 0 && (_refSchema$properties = refSchema.properties) !== null && _refSchema$properties !== void 0 && _refSchema$properties[nodeName]) { var _refSchema$properties2; tmp_schema.properties[nodeName] = refSchema === null || refSchema === void 0 || (_refSchema$properties2 = refSchema.properties) === null || _refSchema$properties2 === void 0 ? void 0 : _refSchema$properties2[nodeName]; if (!tmp_schema['x-schema-orders'].includes(nodeName)) { tmp_schema['x-schema-orders'].splice(refOrder + 1, 0, nodeName); } refOrder++; // 保留是否必填 if (isArray(refSchema === null || refSchema === void 0 ? void 0 : refSchema.required) && refSchema.required.includes(nodeName) && !tmp_schema.required.includes(nodeName)) { tmp_schema.required.push(nodeName); } } if (isPlainObject(refSchema === null || refSchema === void 0 ? void 0 : refSchema['x-schema-refs']) && refSchema !== null && refSchema !== void 0 && (_refSchema$xSchemaR = refSchema['x-schema-refs']) !== null && _refSchema$xSchemaR !== void 0 && _refSchema$xSchemaR[nodeName]) { var _refSchema$xSchemaR2; if (!tmp_schema['x-schema-orders'].includes(nodeName)) { tmp_schema['x-schema-orders'].splice(refOrder + 1, 0, nodeName); } refOrder++; tmp_schema['x-schema-refs'][nodeName] = refSchema === null || refSchema === void 0 || (_refSchema$xSchemaR2 = refSchema['x-schema-refs']) === null || _refSchema$xSchemaR2 === void 0 ? void 0 : _refSchema$xSchemaR2[nodeName]; } }); // 删除模型引用 if (isPlainObject((_tmp_schema52 = tmp_schema) === null || _tmp_schema52 === void 0 ? void 0 : _tmp_schema52['x-schema-refs']) && (_tmp_schema53 = tmp_schema) !== null && _tmp_schema53 !== void 0 && (_tmp_schema53 = _tmp_schema53['x-schema-refs']) !== null && _tmp_schema53 !== void 0 && _tmp_schema53[key]) { var _tmp_schema54; (_tmp_schema54 = tmp_schema) === null || _tmp_schema54 === void 0 || (_tmp_schema54 = _tmp_schema54['x-schema-refs']) === null || _tmp_schema54 === void 0 || delete _tmp_schema54[key]; } if (isArray((_tmp_schema55 = tmp_schema) === null || _tmp_schema55 === void 0 ? void 0 : _tmp_schema55['x-schema-orders'])) { for (var i = tmp_schema['x-schema-orders'].length - 1; i >= 0; i--) { if (tmp_schema['x-schema-orders'][i] === key) { tmp_schema['x-schema-orders'].splice(i, 1); } } } // 删除required中的refid if (includes((_tmp_schema56 = tmp_schema) === null || _tmp_schema56 === void 0 ? void 0 : _tmp_schema56.required, key)) { var _tmp_schema57; pull((_tmp_schema57 = tmp_schema) === null || _tmp_schema57 === void 0 ? void 0 : _tmp_schema57.required, key); } return ''; } // 更新previousKey为当前key previousKey = key; return acc[key]; }, draft); } else { var _tmp_schema58; if (isString((_tmp_schema58 = tmp_schema) === null || _tmp_schema58 === void 0 ? void 0 : _tmp_schema58.$ref) && tmp_schema.$ref.length > 0) { tmp_schema = cloneDeep(refSchema); return tmp_schema; } } }); setSchema && setSchema(newSchema); }); // 添加临时属性 var addProperty = useMemoizedFn(function (namePath, isChild) { var newSchema = produce(schemaRef.current, function (draft) { var _pre_schema3, _pre_schema4, _pre_schema5, _pre_schema9, _pre_schema10, _pre_schema13, _pre_schema14, _pre_schema15; var pre_schema = draft; var lastKey = ''; namePath.reduce(function (acc, key, index) { // 自动创建中间路径 if (!acc[key] && index < namePath.length - 1) { var _pre_schema; // 去refs内部查找 if ((_pre_schema = pre_schema) !== null && _pre_schema !== void 0 && (_pre_schema = _pre_schema['x-schema-refs']) !== null && _pre_schema !== void 0 && _pre_schema[key]) { var _pre_schema2; return (_pre_schema2 = pre_schema) === null || _pre_schema2 === void 0 || (_pre_schema2 = _pre_schema2['x-schema-refs']) === null || _pre_schema2 === void 0 ? void 0 : _pre_schema2[key]; } else { acc[key] = isNumber(key) ? [] : {}; } } if (['properties', 'oneOf', 'anyOf', 'allOf', 'x-schema-overrides'].includes(key)) { pre_schema = acc; } if (index === namePath.length - 1) { if (isChild) { pre_schema = acc[key]; } lastKey = key; return ''; } return acc[key]; }, draft); var tem_properties_id = snowflakeId(); if (isArray((_pre_schema3 = pre_schema) === null || _pre_schema3 === void 0 ? void 0 : _pre_schema3.anyOf) || isArray((_pre_schema4 = pre_schema) === null || _pre_schema4 === void 0 ? void 0 : _pre_schema4.oneOf) || isArray((_pre_schema5 = pre_schema) === null || _pre_schema5 === void 0 ? void 0 : _pre_schema5.allOf)) { var _pre_schema6, _pre_schema7, _pre_schema8; var arr = ((_pre_schema6 = pre_schema) === null || _pre_schema6 === void 0 ? void 0 : _pre_schema6.anyOf) || ((_pre_schema7 = pre_schema) === null || _pre_schema7 === void 0 ? void 0 : _pre_schema7.oneOf) || ((_pre_schema8 = pre_schema) === null || _pre_schema8 === void 0 ? void 0 : _pre_schema8.allOf); if (isNumber(lastKey)) { arr.splice(lastKey + 1, 0, getDefaultSchema('object')); } else { arr.push(getDefaultSchema('object')); } return; } // if (isChild) { if (isString((_pre_schema9 = pre_schema) === null || _pre_schema9 === void 0 ? void 0 : _pre_schema9.$ref) && ((_pre_schema10 = pre_schema) === null || _pre_schema10 === void 0 ? void 0 : _pre_schema10.$ref.length) > 0) { var _pre_schema11, _pre_schema12; var ref_properties_id = snowflakeId(); pre_schema['x-schema-refs'] = _defineProperty({}, ref_properties_id, { $ref: (_pre_schema11 = pre_schema) === null || _pre_schema11 === void 0 ? void 0 : _pre_schema11.$ref }); (_pre_schema12 = pre_schema) === null || _pre_schema12 === void 0 || delete _pre_schema12.$ref; pre_schema.type = 'object'; pre_schema.properties = _defineProperty({}, tem_properties_id, { 'x-tmp-properties': true }); pre_schema.required = [tem_properties_id]; pre_schema['x-schema-orders'] = [ref_properties_id, tem_properties_id]; return; } // } if (!isPlainObject((_pre_schema13 = pre_schema) === null || _pre_schema13 === void 0 ? void 0 : _pre_schema13.properties) && ((_pre_schema14 = pre_schema) === null || _pre_schema14 === void 0 ? void 0 : _pre_schema14.type) === 'object') { pre_schema.properties = {}; } if (isPlainObject((_pre_schema15 = pre_schema) === null || _pre_schema15 === void 0 ? void 0 : _pre_schema15.properties)) { var _pre_schema16, _pre_schema17; pre_schema.properties[tem_properties_id] = { 'x-tmp-properties': true }; if (isArray((_pre_schema16 = pre_schema) === null || _pre_schema16 === void 0 ? void 0 : _pre_schema16.required)) { pre_schema.required.push(tem_properties_id); } if (isArray((_pre_schema17 = pre_schema) === null || _pre_schema17 === void 0 ? void 0 : _pre_schema17['x-schema-orders'])) { if (lastKey) { var lastKeyIndex = pre_schema['x-schema-orders'].indexOf(lastKey); if (lastKeyIndex !== -1) { pre_schema['x-schema-orders'].splice(lastKeyIndex + 1, 0, tem_properties_id); } else { pre_schema['x-schema-orders'].push(tem_properties_id); } } else { pre_schema['x-schema-orders'].push(tem_properties_id); } // for (let i = tmp_schema['x-schema-orders'].length - 1; i >= 0; i--) { // if (tmp_schema['x-schema-orders'][i] === key) { // tmp_schema['x-schema-orders'].splice(i, 1); // } // } } else { pre_schema['x-schema-orders'] = _toConsumableArray(Object.keys(pre_schema.properties)); } return; } }); setSchema && setSchema(newSchema); }); // 拖动处理 var handleDragEnd = useMemoizedFn(function (_ref) { var _active$data, _over$data, _active$data2, _over$data2; var active = _ref.active, over = _ref.over; var namePath = (active === null || active === void 0 || (_active$data = active.data) === null || _active$data === void 0 || (_active$data = _active$data.current) === null || _active$data === void 0 ? void 0 : _active$data.namePath) || []; var overNamePath = (over === null || over === void 0 || (_over$data = over.data) === null || _over$data === void 0 || (_over$data = _over$data.current) === null || _over$data === void 0 ? void 0 : _over$data.namePath) || []; if (!isArray(namePath) || !isArray(overNamePath) || namePath.length !== overNamePath.length) { return; } var activeId = (active === null || active === void 0 || (_active$data2 = active.data) === null || _active$data2 === void 0 || (_active$data2 = _active$data2.current) === null || _active$data2 === void 0 ? void 0 : _active$data2.orderKey) || namePath.at(-1); var overId = (over === null || over === void 0 || (_over$data2 = over.data) === null || _over$data2 === void 0 || (_over$data2 = _over$data2.current) === null || _over$data2 === void 0 ? void 0 : _over$data2.orderKey) || overNamePath.at(-1); if (activeId === overId) { return; } var newSchema = produce(schemaRef.current, function (draft) { var tmp_schema = draft; namePath.reduce(function (acc, key, index) { // 自动创建中间路径 if (!acc[key] && index < namePath.length - 1) { var _tmp_schema59; // 去refs内部查找 if ((_tmp_schema59 = tmp_schema) !== null && _tmp_schema59 !== void 0 && (_tmp_schema59 = _tmp_schema59['x-schema-refs']) !== null && _tmp_schema59 !== void 0 && _tmp_schema59[key]) { var _tmp_schema60; return (_tmp_schema60 = tmp_schema) === null || _tmp_schema60 === void 0 || (_tmp_schema60 = _tmp_schema60['x-schema-refs']) === null || _tmp_schema60 === void 0 ? void 0 : _tmp_schema60[key]; } else { acc[key] = isNumber(key) ? [] : {}; } } if (key === 'properties') { tmp_schema = acc; } if (key === 'x-schema-overrides') { tmp_schema = acc; } if (index === namePath.length - 1) { var _tmp_schema61; // 替换order if (isArray((_tmp_schema61 = tmp_schema) === null || _tmp_schema61 === void 0 ? void 0 : _tmp_schema61['x-schema-orders'])) { var _tmp_schema62, _tmp_schema63; var activeIndex = (_tmp_schema62 = tmp_schema) === null || _tmp_schema62 === void 0 ? void 0 : _tmp_schema62['x-schema-orders'].findIndex(function (i) { return i === activeId; }); var overIndex = (_tmp_schema63 = tmp_schema) === null || _tmp_schema63 === void 0 ? void 0 : _tmp_schema63['x-schema-orders'].findIndex(function (i) { return i === overId; }); var temp = tmp_schema['x-schema-orders'][activeIndex]; tmp_schema['x-schema-orders'][activeIndex] = tmp_schema['x-schema-orders'][overIndex]; tmp_schema['x-schema-orders'][overIndex] = temp; } else { var _tmp_schema64; // 根据properties 生成新的orders if (isPlainObject((_tmp_schema64 = tmp_schema) === null || _tmp_schema64 === void 0 ? void 0 : _tmp_schema64.properties)) { var _tmp_schema65; var newOrders = Object.keys((_tmp_schema65 = tmp_schema) === null || _tmp_schema65 === void 0 ? void 0 : _tmp_schema65.properties); var _activeIndex = newOrders.findIndex(function (i) { return i === activeId; }); var _overIndex = newOrders.findIndex(function (i) { return i === overId; }); var _temp = newOrders[_activeIndex]; newOrders[_activeIndex] = newOrders[_overIndex]; newOrders[_overIndex] = _temp; tmp_schema['x-schema-orders'] = newOrders; } } return ''; } return acc[key]; }, draft); }); setSchema && setSchema(newSchema); // if (!over || active.id === over.id) return; // const activeLevel = Object.keys(items).find(l => // items[l].includes(active.id) // ); // const overLevel = over.data?.accepts; // if (activeLevel === overLevel) { // setItems(prev => ({ // ...prev, // [activeLevel]: arrayMove( // prev[activeLevel], // prev[activeLevel].indexOf(active.id), // prev[activeLevel].indexOf(over.id) // ) // })); // } }); // 其他操作函数... return { updateSchema: updateSchema, renameProperty: renameProperty, renamePropertySource: renamePropertySource, onSchemaRowUpdate: onSchemaRowUpdate, updateRequiredProperty: updateRequiredProperty, updateAllowNull: updateAllowNull, removeProperty: removeProperty, removeRef: removeRef, addRef: addRef, hiddenRefProperty: hiddenRefProperty, linkRefProperty: linkRefProperty, unreferenceRef: unreferenceRef, addProperty: addProperty, handleDragEnd: handleDragEnd // 暴露其他操作函数... }; };