ze-react-component-library
Version:
ZeroETP React Component Library
666 lines (597 loc) • 17.3 kB
JavaScript
import "antd/es/message/style";
import _message from "antd/es/message";
var __assign = this && this.__assign || function () {
__assign = Object.assign || function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
function adopt(value) {
return value instanceof P ? value : new P(function (resolve) {
resolve(value);
});
}
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
}
function rejected(value) {
try {
step(generator["throw"](value));
} catch (e) {
reject(e);
}
}
function step(result) {
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
}
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = this && this.__generator || function (thisArg, body) {
var _ = {
label: 0,
sent: function sent() {
if (t[0] & 1) throw t[1];
return t[1];
},
trys: [],
ops: []
},
f,
y,
t,
g;
return g = {
next: verb(0),
"throw": verb(1),
"return": verb(2)
}, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
return this;
}), g;
function verb(n) {
return function (v) {
return step([n, v]);
};
}
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) {
try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0:
case 1:
t = op;
break;
case 4:
_.label++;
return {
value: op[1],
done: false
};
case 5:
_.label++;
y = op[1];
op = [0];
continue;
case 7:
op = _.ops.pop();
_.trys.pop();
continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
_ = 0;
continue;
}
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
_.label = op[1];
break;
}
if (op[0] === 6 && _.label < t[1]) {
_.label = t[1];
t = op;
break;
}
if (t && _.label < t[2]) {
_.label = t[2];
_.ops.push(op);
break;
}
if (t[2]) _.ops.pop();
_.trys.pop();
continue;
}
op = body.call(thisArg, _);
} catch (e) {
op = [6, e];
y = 0;
} finally {
f = t = 0;
}
}
if (op[0] & 5) throw op[1];
return {
value: op[0] ? op[1] : void 0,
done: true
};
}
};
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
import React, { useEffect, useRef, useState } from "react";
import { nanoid } from "nanoid";
import ZESchemaForm from "../ZESchemaForm";
import { typePropertyOfSchema } from "../util";
var schemaOfSchema = {
_id: "_schema",
name: "schema",
type: "entity",
properties: [{
_sid: "ID",
name: "ID",
type: "string",
primal_type: "string",
constraints: {
required: true
},
description: "数据库中表的名字"
}, {
_sid: "name",
name: "name",
type: "string",
primal_type: "string",
constraints: {
required: true
},
description: "对外显示的Schema名称"
}, {
_sid: "syno",
name: "syno",
type: "tag",
primal_type: "string",
isArray: true,
description: "同义词数组。输入一个同义词后按回车确定,接下去输入下一个同义词"
}, {
_sid: "type",
name: "type",
type: "string",
primal_type: "string",
constraints: {
required: true,
enum: ["entity", "event"]
}
}, {
_sid: "description",
name: "description",
type: "string",
primal_type: "string"
}, // {
// name: "view_sql",
// type: "sql",
// primal_type: "string",
// description:
// "高级功能。根据此sql生成一个sql视图,该视图将会代替主表。用于动态生成一些列,或者做一些leftjoin",
// },
{
_sid: "properties",
name: "properties",
type: "object",
ref: "_property",
primal_type: "object",
isArray: true,
constraints: {
required: true
},
schema: {
_id: "_property",
name: "property",
type: "entity",
properties: [{
name: "ID",
type: "string",
primal_type: "string",
constraints: {
required: true
},
description: "数据库中列的名字,建议使用英文字母和数字表示。注意⚠️:一旦设定后,修改将会删除原来的列,新增一个新列。可能会丢失数据!"
}, {
name: "name",
type: "string",
primal_type: "string",
constraints: {
required: true
}
}, {
name: "syno",
type: "tag",
primal_type: "string",
isArray: true,
description: "同义词数组。输入一个同义词后按回车确定,接下去输入下一个同义词"
}, typePropertyOfSchema, {
name: "ref",
type: "string",
description: "当类型为实体时,需要指定对应的Schema",
primal_type: "string"
}, {
name: "required",
type: "boolean",
primal_type: "boolean"
}, {
name: "isArray",
type: "boolean",
primal_type: "boolean"
}, {
name: "enum",
type: "tag",
primal_type: "string",
isArray: true,
description: "可取值的范围"
}, {
name: "unit",
type: "string",
primal_type: "string",
description: "例如: 元。此字段会显示在问答的结果旁边。"
}, {
name: "formatter",
type: "string",
primal_type: "string",
description: "例如: 0,0.0A。格式配置详情请见使用文档"
}, {
name: "description",
type: "string",
primal_type: "string",
description: "该字段的一些说明信息。如果是指标字段,会显示在问答指标卡的❓提示按钮里面。"
} // {
// name: "is_comparable",
// type: "boolean",
// primal_type: "boolean",
// description:
// "是否可以比较,仅需在类型为string的时候设置。同时必须有order属性,这个通过专家版设定吧",
// },
// UDF的配置有点难,感觉只能通过json editor配置
// {
// name: "udf",
// type: "object",
// primal_type: "object",
// },
]
}
}, {
_sid: "skip_learn",
name: "skip_learn",
type: "boolean",
primal_type: "boolean",
constraints: {
required: false
},
description: "默认开启学习。如果关闭掉的话,此表仅做参考或辅助计算使用。"
}, {
_sid: "alisa_rank",
name: "alisa_rank",
type: "int",
primal_type: "number",
constraints: {
required: false
},
description: "默认为0。越大在问答的时候优先级越高。"
}, {
_sid: "order",
name: "order",
type: "int",
primal_type: "number",
constraints: {
required: false
},
description: "可以控制在前端中的和其他Schema的排序。默认为0。越小排越前。"
}, {
_sid: "tag",
name: "tag",
type: "tag",
primal_type: "string",
isArray: true,
constraints: {
required: false
},
description: "和问答无关。便于后台管理人员查找Schema。"
}]
};
export var formValueToSchema = function formValueToSchema(value, initialSchema) {
// 转化成schema
var properties = [];
if (!value.properties) {
throw new Error("必须至少有一个属性");
}
var _loop_1 = function _loop_1(propertyDict) {
if (!propertyDict.properties) return "continue";
var property = propertyDict.properties;
property._sid = propertyDict.id;
if (!property.constraints) property.constraints = {};
property._id = property.ID;
delete property.id;
delete property.ID; // existed value
if (initialSchema === null || initialSchema === void 0 ? void 0 : initialSchema.properties) {
var existedProp = initialSchema.properties.find(function (p) {
return p._sid === property._sid;
});
if (existedProp) {
property = __assign(__assign({}, existedProp), property);
}
}
properties.push(property);
};
for (var _i = 0, _a = value.properties; _i < _a.length; _i++) {
var propertyDict = _a[_i];
_loop_1(propertyDict);
}
var schema = __assign(__assign(__assign({}, initialSchema), value), {
properties: properties,
_id: value.ID
});
delete schema.ID;
if (schema.properties.length === 0) {
throw new Error("必须至少有一个属性");
}
return schema;
};
export var schemaToFormValue = function schemaToFormValue(schema) {
if (!schema) return undefined;
var value = __assign({}, schema);
value.ID = schema._id;
delete value._id;
value.properties = (schema.properties || []).map(function (property) {
var _a, _b;
return {
id: property._sid || nanoid(),
properties: __assign(__assign({}, property), {
constraints: __assign(__assign({}, property === null || property === void 0 ? void 0 : property.constraints), {
enum: (_b = (_a = property === null || property === void 0 ? void 0 : property.constraints) === null || _a === void 0 ? void 0 : _a.enum) === null || _b === void 0 ? void 0 : _b.map(function (e) {
return String(e instanceof Array ? e.join(",") : e);
})
}),
ID: property._id,
_id: undefined
})
};
});
return value;
};
var SchemaEasyEditor = function SchemaEasyEditor(_a) {
var onSubmit = _a.onSubmit,
initialSchema = _a.initialSchema,
layoutType = _a.layoutType,
schemas = _a.schemas,
editingRecord = _a.editingRecord,
_formRef = _a.formRef,
props = __rest(_a, ["onSubmit", "initialSchema", "layoutType", "schemas", "editingRecord", "formRef"]);
var formRef = _formRef || useRef();
var _b = useState({}),
formValues = _b[0],
setFormValues = _b[1];
var resetForm = function resetForm() {
var _a, _b;
var initialValues = schemaToFormValue(initialSchema);
(_a = formRef.current) === null || _a === void 0 ? void 0 : _a.resetFields();
(_b = formRef.current) === null || _b === void 0 ? void 0 : _b.setFieldsValue(initialValues);
};
var onFinish = function onFinish(value) {
return __awaiter(void 0, void 0, void 0, function () {
var schema, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2,, 3]);
schema = formValueToSchema(value, initialSchema);
return [4
/*yield*/
, onSubmit(schema)];
case 1:
_a.sent();
return [3
/*break*/
, 3];
case 2:
error_1 = _a.sent();
_message.error(error_1.message);
return [3
/*break*/
, 3];
case 3:
return [2
/*return*/
, true];
}
});
});
};
useEffect(function () {
resetForm();
}, [JSON.stringify(initialSchema)]);
return /*#__PURE__*/React.createElement(ZESchemaForm, __assign({}, props, {
layoutType: layoutType,
schema: schemaOfSchema,
onFinish: onFinish,
formRef: formRef,
onValuesChange: function onValuesChange(v, values) {
setFormValues(values);
},
columns: [{
valueType: "group",
columns: [{
title: "ID",
dataIndex: "ID",
width: "lg",
readonly: editingRecord === null || editingRecord === void 0 ? void 0 : editingRecord._id
}, {
title: "类型",
dataIndex: "type",
width: "lg",
readonly: true,
render: function render(v) {
return v === "entity" ? "实体" : "事件";
}
}]
}, {
valueType: "group",
columns: [{
title: "名称",
dataIndex: "name",
width: "md"
}, {
title: "同义词",
dataIndex: "syno",
width: "md"
}, {
title: "标签",
dataIndex: "tag",
width: "md"
}]
}, {
title: "描述",
dataIndex: "description"
}, // {
// title: "视图sql",
// dataIndex: "view_sql",
// },
{
title: "属性列表",
dataIndex: "properties",
valueType: "table",
fieldProps: {
placeholder: "新增属性",
sortable: true,
scroll: {
y: 400
}
},
columns: [{
title: "ID",
dataIndex: "properties.ID",
width: 120,
// @ts-ignore
fixed: true
}, {
title: "名称",
dataIndex: "properties.name",
width: 120,
// @ts-ignore
fixed: true
}, {
title: "同义词",
dataIndex: "properties.syno"
}, {
title: "类型",
dataIndex: "properties.type",
valueType: "select",
fieldProps: function fieldProps(form, config) {
return {
onChange: function onChange(v) {
form.setFieldValue([config.entity.id, "properties", "ref"], undefined);
}
};
}
}, {
title: "目标实体表",
dataIndex: "properties.ref",
valueType: "select",
formItemProps: function formItemProps(form, config) {
var type = form === null || form === void 0 ? void 0 : form.getFieldValue([config.entity.id, "properties", "type"]);
if (type === "object") {
return {
rules: [{
required: true,
message: "必填项"
}]
};
}
return {
rules: []
};
},
fieldProps: function fieldProps(form, config) {
var type = form === null || form === void 0 ? void 0 : form.getFieldValue([config.entity.id, "properties", "type"]);
if (type === "object") {
var options = (schemas === null || schemas === void 0 ? void 0 : schemas.filter(function (f) {
return f.type === "entity";
}).map(function (d) {
return {
label: d.name + "\uFF08" + d._id + "\uFF09",
value: d._id
};
})) || [];
if (formValues.ID && formValues.name && !options.map(function (o) {
return o.value;
}).includes(formValues.ID)) {
options.push({
label: formValues.name + "\uFF08" + formValues.ID + "\uFF09",
value: formValues.ID
});
}
return {
dropdownMatchSelectWidth: false,
showSearch: true,
options: options
};
}
return {
disabled: true
};
}
}, {
title: "枚举",
dataIndex: "properties.constraints.enum"
}, {
title: "单位",
dataIndex: "properties.unit"
}, {
title: "数字格式",
dataIndex: "properties.ui.formatter"
}, {
title: "说明",
dataIndex: "properties.description"
}, {
title: "操作",
valueType: "option",
width: 60
}]
}, {
valueType: "group",
title: "高级配置项",
tooltip: "可选",
columns: [{
title: "是否关闭学习",
dataIndex: "skip_learn",
width: "xs"
}, {
title: "优先级",
dataIndex: "alisa_rank",
width: "xs"
}, {
title: "显示顺序",
dataIndex: "order",
width: "xs"
}]
}]
}));
};
export default SchemaEasyEditor;