@thienvu18/designable-formily-transformer 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +20 -0
- package/README.md +1 -0
- package/esm/index.d.ts +12 -0
- package/esm/index.js +108 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +113 -0
- package/package.json +48 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015-present, Alibaba Group Holding Limited. All rights reserved.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @thienvu18/designable-formily-transformer
|
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ISchema } from '@formily/json-schema';
|
|
2
|
+
import { ITreeNode } from '@thienvu18/designable-core';
|
|
3
|
+
export interface ITransformerOptions {
|
|
4
|
+
designableFieldName?: string;
|
|
5
|
+
designableFormName?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface IFormilySchema {
|
|
8
|
+
schema?: ISchema;
|
|
9
|
+
form?: Record<string, any>;
|
|
10
|
+
}
|
|
11
|
+
export declare const transformToSchema: (node: ITreeNode, options?: ITransformerOptions) => IFormilySchema;
|
|
12
|
+
export declare const transformToTreeNode: (formily?: IFormilySchema, options?: ITransformerOptions) => ITreeNode;
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Schema } from '@formily/json-schema';
|
|
2
|
+
import { clone, uid } from '@thienvu18/designable-shared';
|
|
3
|
+
const createOptions = (options) => {
|
|
4
|
+
return {
|
|
5
|
+
designableFieldName: 'Field',
|
|
6
|
+
designableFormName: 'Form',
|
|
7
|
+
...options,
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
const findNode = (node, finder) => {
|
|
11
|
+
if (!node)
|
|
12
|
+
return;
|
|
13
|
+
if (finder(node))
|
|
14
|
+
return node;
|
|
15
|
+
if (!node.children)
|
|
16
|
+
return;
|
|
17
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
18
|
+
if (findNode(node.children[i]))
|
|
19
|
+
return node.children[i];
|
|
20
|
+
}
|
|
21
|
+
return;
|
|
22
|
+
};
|
|
23
|
+
export const transformToSchema = (node, options) => {
|
|
24
|
+
const realOptions = createOptions(options);
|
|
25
|
+
const root = findNode(node, (child) => {
|
|
26
|
+
return child.componentName === realOptions.designableFormName;
|
|
27
|
+
});
|
|
28
|
+
const schema = {
|
|
29
|
+
type: 'object',
|
|
30
|
+
properties: {},
|
|
31
|
+
};
|
|
32
|
+
if (!root)
|
|
33
|
+
return { schema };
|
|
34
|
+
const createSchema = (node, schema = {}) => {
|
|
35
|
+
if (node !== root) {
|
|
36
|
+
Object.assign(schema, clone(node.props));
|
|
37
|
+
}
|
|
38
|
+
schema['x-designable-id'] = node.id;
|
|
39
|
+
if (schema.type === 'array') {
|
|
40
|
+
if (node.children[0]) {
|
|
41
|
+
if (node.children[0].componentName === realOptions.designableFieldName) {
|
|
42
|
+
schema.items = createSchema(node.children[0]);
|
|
43
|
+
schema['x-index'] = 0;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
node.children.slice(1).forEach((child, index) => {
|
|
47
|
+
if (child.componentName !== realOptions.designableFieldName)
|
|
48
|
+
return;
|
|
49
|
+
const key = child.props.name || child.id;
|
|
50
|
+
schema.properties = schema.properties || {};
|
|
51
|
+
schema.properties[key] = createSchema(child);
|
|
52
|
+
schema.properties[key]['x-index'] = index;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
node.children.forEach((child, index) => {
|
|
57
|
+
if (child.componentName !== realOptions.designableFieldName)
|
|
58
|
+
return;
|
|
59
|
+
const key = child.props.name || child.id;
|
|
60
|
+
schema.properties = schema.properties || {};
|
|
61
|
+
schema.properties[key] = createSchema(child);
|
|
62
|
+
schema.properties[key]['x-index'] = index;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return schema;
|
|
66
|
+
};
|
|
67
|
+
return { form: clone(root.props), schema: createSchema(root, schema) };
|
|
68
|
+
};
|
|
69
|
+
export const transformToTreeNode = (formily = {}, options) => {
|
|
70
|
+
const realOptions = createOptions(options);
|
|
71
|
+
const root = {
|
|
72
|
+
componentName: realOptions.designableFormName,
|
|
73
|
+
props: formily.form,
|
|
74
|
+
children: [],
|
|
75
|
+
};
|
|
76
|
+
const schema = new Schema(formily.schema);
|
|
77
|
+
const cleanProps = (props) => {
|
|
78
|
+
if (props['name'] === props['x-designable-id']) {
|
|
79
|
+
delete props.name;
|
|
80
|
+
}
|
|
81
|
+
delete props['version'];
|
|
82
|
+
delete props['_isJSONSchemaObject'];
|
|
83
|
+
return props;
|
|
84
|
+
};
|
|
85
|
+
const appendTreeNode = (parent, schema) => {
|
|
86
|
+
if (!schema)
|
|
87
|
+
return;
|
|
88
|
+
const current = {
|
|
89
|
+
id: schema['x-designable-id'] || uid(),
|
|
90
|
+
componentName: realOptions.designableFieldName,
|
|
91
|
+
props: cleanProps(schema.toJSON(false)),
|
|
92
|
+
children: [],
|
|
93
|
+
};
|
|
94
|
+
parent.children.push(current);
|
|
95
|
+
if (schema.items && !Array.isArray(schema.items)) {
|
|
96
|
+
appendTreeNode(current, schema.items);
|
|
97
|
+
}
|
|
98
|
+
schema.mapProperties((schema) => {
|
|
99
|
+
schema['x-designable-id'] = schema['x-designable-id'] || uid();
|
|
100
|
+
appendTreeNode(current, schema);
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
schema.mapProperties((schema) => {
|
|
104
|
+
schema['x-designable-id'] = schema['x-designable-id'] || uid();
|
|
105
|
+
appendTreeNode(root, schema);
|
|
106
|
+
});
|
|
107
|
+
return root;
|
|
108
|
+
};
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ISchema } from '@formily/json-schema';
|
|
2
|
+
import { ITreeNode } from '@thienvu18/designable-core';
|
|
3
|
+
export interface ITransformerOptions {
|
|
4
|
+
designableFieldName?: string;
|
|
5
|
+
designableFormName?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface IFormilySchema {
|
|
8
|
+
schema?: ISchema;
|
|
9
|
+
form?: Record<string, any>;
|
|
10
|
+
}
|
|
11
|
+
export declare const transformToSchema: (node: ITreeNode, options?: ITransformerOptions) => IFormilySchema;
|
|
12
|
+
export declare const transformToTreeNode: (formily?: IFormilySchema, options?: ITransformerOptions) => ITreeNode;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transformToTreeNode = exports.transformToSchema = void 0;
|
|
4
|
+
const json_schema_1 = require("@formily/json-schema");
|
|
5
|
+
const designable_shared_1 = require("@thienvu18/designable-shared");
|
|
6
|
+
const createOptions = (options) => {
|
|
7
|
+
return {
|
|
8
|
+
designableFieldName: 'Field',
|
|
9
|
+
designableFormName: 'Form',
|
|
10
|
+
...options,
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
const findNode = (node, finder) => {
|
|
14
|
+
if (!node)
|
|
15
|
+
return;
|
|
16
|
+
if (finder(node))
|
|
17
|
+
return node;
|
|
18
|
+
if (!node.children)
|
|
19
|
+
return;
|
|
20
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
21
|
+
if (findNode(node.children[i]))
|
|
22
|
+
return node.children[i];
|
|
23
|
+
}
|
|
24
|
+
return;
|
|
25
|
+
};
|
|
26
|
+
const transformToSchema = (node, options) => {
|
|
27
|
+
const realOptions = createOptions(options);
|
|
28
|
+
const root = findNode(node, (child) => {
|
|
29
|
+
return child.componentName === realOptions.designableFormName;
|
|
30
|
+
});
|
|
31
|
+
const schema = {
|
|
32
|
+
type: 'object',
|
|
33
|
+
properties: {},
|
|
34
|
+
};
|
|
35
|
+
if (!root)
|
|
36
|
+
return { schema };
|
|
37
|
+
const createSchema = (node, schema = {}) => {
|
|
38
|
+
if (node !== root) {
|
|
39
|
+
Object.assign(schema, (0, designable_shared_1.clone)(node.props));
|
|
40
|
+
}
|
|
41
|
+
schema['x-designable-id'] = node.id;
|
|
42
|
+
if (schema.type === 'array') {
|
|
43
|
+
if (node.children[0]) {
|
|
44
|
+
if (node.children[0].componentName === realOptions.designableFieldName) {
|
|
45
|
+
schema.items = createSchema(node.children[0]);
|
|
46
|
+
schema['x-index'] = 0;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
node.children.slice(1).forEach((child, index) => {
|
|
50
|
+
if (child.componentName !== realOptions.designableFieldName)
|
|
51
|
+
return;
|
|
52
|
+
const key = child.props.name || child.id;
|
|
53
|
+
schema.properties = schema.properties || {};
|
|
54
|
+
schema.properties[key] = createSchema(child);
|
|
55
|
+
schema.properties[key]['x-index'] = index;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
node.children.forEach((child, index) => {
|
|
60
|
+
if (child.componentName !== realOptions.designableFieldName)
|
|
61
|
+
return;
|
|
62
|
+
const key = child.props.name || child.id;
|
|
63
|
+
schema.properties = schema.properties || {};
|
|
64
|
+
schema.properties[key] = createSchema(child);
|
|
65
|
+
schema.properties[key]['x-index'] = index;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return schema;
|
|
69
|
+
};
|
|
70
|
+
return { form: (0, designable_shared_1.clone)(root.props), schema: createSchema(root, schema) };
|
|
71
|
+
};
|
|
72
|
+
exports.transformToSchema = transformToSchema;
|
|
73
|
+
const transformToTreeNode = (formily = {}, options) => {
|
|
74
|
+
const realOptions = createOptions(options);
|
|
75
|
+
const root = {
|
|
76
|
+
componentName: realOptions.designableFormName,
|
|
77
|
+
props: formily.form,
|
|
78
|
+
children: [],
|
|
79
|
+
};
|
|
80
|
+
const schema = new json_schema_1.Schema(formily.schema);
|
|
81
|
+
const cleanProps = (props) => {
|
|
82
|
+
if (props['name'] === props['x-designable-id']) {
|
|
83
|
+
delete props.name;
|
|
84
|
+
}
|
|
85
|
+
delete props['version'];
|
|
86
|
+
delete props['_isJSONSchemaObject'];
|
|
87
|
+
return props;
|
|
88
|
+
};
|
|
89
|
+
const appendTreeNode = (parent, schema) => {
|
|
90
|
+
if (!schema)
|
|
91
|
+
return;
|
|
92
|
+
const current = {
|
|
93
|
+
id: schema['x-designable-id'] || (0, designable_shared_1.uid)(),
|
|
94
|
+
componentName: realOptions.designableFieldName,
|
|
95
|
+
props: cleanProps(schema.toJSON(false)),
|
|
96
|
+
children: [],
|
|
97
|
+
};
|
|
98
|
+
parent.children.push(current);
|
|
99
|
+
if (schema.items && !Array.isArray(schema.items)) {
|
|
100
|
+
appendTreeNode(current, schema.items);
|
|
101
|
+
}
|
|
102
|
+
schema.mapProperties((schema) => {
|
|
103
|
+
schema['x-designable-id'] = schema['x-designable-id'] || (0, designable_shared_1.uid)();
|
|
104
|
+
appendTreeNode(current, schema);
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
schema.mapProperties((schema) => {
|
|
108
|
+
schema['x-designable-id'] = schema['x-designable-id'] || (0, designable_shared_1.uid)();
|
|
109
|
+
appendTreeNode(root, schema);
|
|
110
|
+
});
|
|
111
|
+
return root;
|
|
112
|
+
};
|
|
113
|
+
exports.transformToTreeNode = transformToTreeNode;
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@thienvu18/designable-formily-transformer",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"module": "esm/index.js",
|
|
7
|
+
"types": "esm/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"lib",
|
|
10
|
+
"esm",
|
|
11
|
+
"README.md",
|
|
12
|
+
"LICENSE.md"
|
|
13
|
+
],
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=20 <25"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/thienvu18/designable.git"
|
|
20
|
+
},
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/thienvu18/designable/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/thienvu18/designable#readme",
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "rimraf -rf lib esm && npm run build:cjs && npm run build:esm",
|
|
27
|
+
"build:cjs": "tsc --project tsconfig.build.json --outDir lib",
|
|
28
|
+
"build:esm": "tsc --project tsconfig.build.json --module es2020 --outDir esm"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@formily/core": ">=2.3.7 <3",
|
|
32
|
+
"@formily/json-schema": ">=2.3.7 <3"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@thienvu18/designable-core": "2.0.0",
|
|
36
|
+
"@thienvu18/designable-shared": "2.0.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@formily/core": "^2.3.7",
|
|
40
|
+
"@formily/json-schema": "^2.3.7",
|
|
41
|
+
"@formily/path": "^2.3.7",
|
|
42
|
+
"@formily/reactive": "^2.3.7",
|
|
43
|
+
"typescript": "5.2.2"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
}
|
|
48
|
+
}
|