react-json-schema-editor-antd
Version:
Json Schema Editor
126 lines (123 loc) • 7.15 kB
JavaScript
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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 _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { DndContext, pointerWithin } from '@dnd-kit/core';
import { useMemoizedFn } from 'ahooks';
import { ConfigProvider, message } from 'antd';
import { isNumber } from 'lodash';
import React, { forwardRef, memo, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
import ThemeContext from "../context";
import "../locales/i18n";
import { useSchemaInit } from "./hooks/useSchemaInit";
import { useSchemaOperations } from "./hooks/useSchemaOperations";
import SchemaItem from "./SchemaItem";
import { getInitDefaultSchema } from "./utils";
var JsonSchemaEditor = /*#__PURE__*/forwardRef(function (props, ref) {
var _initSchema$type;
// const { t } = useTranslation();
var containerRef = useRef(null);
var _message$useMessage = message.useMessage(),
_message$useMessage2 = _slicedToArray(_message$useMessage, 2),
messageApi = _message$useMessage2[0],
contextHolder = _message$useMessage2[1];
var _useState = useState((props === null || props === void 0 ? void 0 : props.keyWidth) || 260),
_useState2 = _slicedToArray(_useState, 2),
keyWidth = _useState2[0],
setKeyWidth = _useState2[1];
var keyWidthChange = useMemoizedFn(function (val) {
setKeyWidth(val);
if (props !== null && props !== void 0 && props.onKeyWidthChange) {
props.onKeyWidthChange(val);
}
});
useEffect(function () {
if (isNumber(props === null || props === void 0 ? void 0 : props.keyWidth)) {
setKeyWidth(props.keyWidth);
}
}, [props === null || props === void 0 ? void 0 : props.keyWidth]);
var initSchema = useMemo(function () {
return getInitDefaultSchema(props === null || props === void 0 ? void 0 : props.schema);
}, [props === null || props === void 0 ? void 0 : props.schema]);
var _widthClass = {
widthClass: 0
},
widthClass = _widthClass.widthClass;
// const [schema, setSchema] = useImmer<JSONSchema7>(initSchema);
var _useSchemaOperations = useSchemaOperations({
messageApi: messageApi,
schema: initSchema,
setSchema: props === null || props === void 0 ? void 0 : props.onSchemaChange
}),
updateSchema = _useSchemaOperations.updateSchema,
renameProperty = _useSchemaOperations.renameProperty,
onSchemaRowUpdate = _useSchemaOperations.onSchemaRowUpdate,
updateRequiredProperty = _useSchemaOperations.updateRequiredProperty,
updateAllowNull = _useSchemaOperations.updateAllowNull,
removeProperty = _useSchemaOperations.removeProperty,
removeRef = _useSchemaOperations.removeRef,
addRef = _useSchemaOperations.addRef,
hiddenRefProperty = _useSchemaOperations.hiddenRefProperty,
linkRefProperty = _useSchemaOperations.linkRefProperty,
unreferenceRef = _useSchemaOperations.unreferenceRef,
addProperty = _useSchemaOperations.addProperty,
handleDragEnd = _useSchemaOperations.handleDragEnd;
useSchemaInit({
language: props === null || props === void 0 ? void 0 : props.language
});
useImperativeHandle(ref, function () {
return {
updateSchema: updateSchema
};
});
var components = useMemo(function () {
return (props === null || props === void 0 ? void 0 : props.components) || {};
}, [props === null || props === void 0 ? void 0 : props.components]);
// 新增:入口 namePath 稳定引用
var rootNamePath = useMemo(function () {
return [];
}, []);
return /*#__PURE__*/React.createElement(ConfigProvider, _extends({}, (props === null || props === void 0 ? void 0 : props.parentAntdConfig) || {}, {
prefixCls: "ant"
}), /*#__PURE__*/React.createElement(ThemeContext.Provider, {
value: (props === null || props === void 0 ? void 0 : props.theme) || 'white'
}, /*#__PURE__*/React.createElement(DndContext, {
collisionDetection: pointerWithin,
onDragEnd: handleDragEnd
}, /*#__PURE__*/React.createElement("div", {
ref: containerRef,
className: "json-shcema-editor",
style: {
paddingTop: '10px 10px 0 10px'
}
}, contextHolder, /*#__PURE__*/React.createElement(SchemaItem, {
schema: initSchema,
components: components,
updateSchema: updateSchema,
removeRef: removeRef,
addRef: addRef,
current_ref_id: props === null || props === void 0 ? void 0 : props.refId
// refIds={isString(props?.refId) && !isEmpty(props.refId) ? [props.refId] : []}
,
unreferenceRef: unreferenceRef,
linkRefProperty: linkRefProperty,
hiddenRefProperty: hiddenRefProperty,
renameProperty: renameProperty,
onSchemaRowUpdate: onSchemaRowUpdate,
allowNull: initSchema === null || initSchema === void 0 || (_initSchema$type = initSchema.type) === null || _initSchema$type === void 0 ? void 0 : _initSchema$type.includes('null'),
removeProperty: removeProperty,
addProperty: addProperty,
updateRequiredProperty: updateRequiredProperty,
updateAllowNull: updateAllowNull,
widthClass: widthClass,
renderValueComponents: props === null || props === void 0 ? void 0 : props.renderValueComponents,
renderKeyComponents: props === null || props === void 0 ? void 0 : props.renderKeyComponents,
keyWidth: keyWidth,
setKeyWidth: keyWidthChange,
namePath: rootNamePath
})))));
});
export default /*#__PURE__*/memo(JsonSchemaEditor);