@thienvu18/designable-formily-transformer 2.0.0 → 2.0.2
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/esm/index.d.ts +14 -8
- package/esm/index.js +95 -102
- package/lib/index.d.ts +14 -8
- package/lib/index.js +105 -107
- package/package.json +18 -18
package/esm/index.d.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
import { ISchema } from '@formily/json-schema'
|
|
2
|
-
import { ITreeNode } from '@thienvu18/designable-core'
|
|
1
|
+
import { ISchema } from '@formily/json-schema'
|
|
2
|
+
import { ITreeNode } from '@thienvu18/designable-core'
|
|
3
3
|
export interface ITransformerOptions {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
designableFieldName?: string
|
|
5
|
+
designableFormName?: string
|
|
6
6
|
}
|
|
7
7
|
export interface IFormilySchema {
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
schema?: ISchema
|
|
9
|
+
form?: Record<string, any>
|
|
10
10
|
}
|
|
11
|
-
export declare const transformToSchema: (
|
|
12
|
-
|
|
11
|
+
export declare const transformToSchema: (
|
|
12
|
+
node: ITreeNode,
|
|
13
|
+
options?: ITransformerOptions
|
|
14
|
+
) => IFormilySchema
|
|
15
|
+
export declare const transformToTreeNode: (
|
|
16
|
+
formily?: IFormilySchema,
|
|
17
|
+
options?: ITransformerOptions
|
|
18
|
+
) => ITreeNode
|
package/esm/index.js
CHANGED
|
@@ -1,108 +1,101 @@
|
|
|
1
|
-
import { Schema } from '@formily/json-schema'
|
|
2
|
-
import { clone, uid } from '@thienvu18/designable-shared'
|
|
1
|
+
import { Schema } from '@formily/json-schema'
|
|
2
|
+
import { clone, uid } from '@thienvu18/designable-shared'
|
|
3
3
|
const createOptions = (options) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
4
|
+
return {
|
|
5
|
+
designableFieldName: 'Field',
|
|
6
|
+
designableFormName: 'Form',
|
|
7
|
+
...options,
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
10
|
const findNode = (node, finder) => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
if (
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return node.children[i];
|
|
20
|
-
}
|
|
21
|
-
return;
|
|
22
|
-
};
|
|
11
|
+
if (!node) return
|
|
12
|
+
if (finder(node)) return node
|
|
13
|
+
if (!node.children) return
|
|
14
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
15
|
+
if (findNode(node.children[i])) return node.children[i]
|
|
16
|
+
}
|
|
17
|
+
return
|
|
18
|
+
}
|
|
23
19
|
export const transformToSchema = (node, options) => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
});
|
|
20
|
+
const realOptions = createOptions(options)
|
|
21
|
+
const root = findNode(node, (child) => {
|
|
22
|
+
return child.componentName === realOptions.designableFormName
|
|
23
|
+
})
|
|
24
|
+
const schema = {
|
|
25
|
+
type: 'object',
|
|
26
|
+
properties: {},
|
|
27
|
+
}
|
|
28
|
+
if (!root) return { schema }
|
|
29
|
+
const createSchema = (node, schema = {}) => {
|
|
30
|
+
if (node !== root) {
|
|
31
|
+
Object.assign(schema, clone(node.props))
|
|
32
|
+
}
|
|
33
|
+
schema['x-designable-id'] = node.id
|
|
34
|
+
if (schema.type === 'array') {
|
|
35
|
+
if (node.children[0]) {
|
|
36
|
+
if (
|
|
37
|
+
node.children[0].componentName === realOptions.designableFieldName
|
|
38
|
+
) {
|
|
39
|
+
schema.items = createSchema(node.children[0])
|
|
40
|
+
schema['x-index'] = 0
|
|
64
41
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
42
|
+
}
|
|
43
|
+
node.children.slice(1).forEach((child, index) => {
|
|
44
|
+
if (child.componentName !== realOptions.designableFieldName) return
|
|
45
|
+
const key = child.props.name || child.id
|
|
46
|
+
schema.properties = schema.properties || {}
|
|
47
|
+
schema.properties[key] = createSchema(child)
|
|
48
|
+
schema.properties[key]['x-index'] = index
|
|
49
|
+
})
|
|
50
|
+
} else {
|
|
51
|
+
node.children.forEach((child, index) => {
|
|
52
|
+
if (child.componentName !== realOptions.designableFieldName) return
|
|
53
|
+
const key = child.props.name || child.id
|
|
54
|
+
schema.properties = schema.properties || {}
|
|
55
|
+
schema.properties[key] = createSchema(child)
|
|
56
|
+
schema.properties[key]['x-index'] = index
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
return schema
|
|
60
|
+
}
|
|
61
|
+
return { form: clone(root.props), schema: createSchema(root, schema) }
|
|
62
|
+
}
|
|
69
63
|
export const transformToTreeNode = (formily = {}, options) => {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
schema.mapProperties((schema) => {
|
|
99
|
-
schema['x-designable-id'] = schema['x-designable-id'] || uid();
|
|
100
|
-
appendTreeNode(current, schema);
|
|
101
|
-
});
|
|
102
|
-
};
|
|
64
|
+
const realOptions = createOptions(options)
|
|
65
|
+
const root = {
|
|
66
|
+
componentName: realOptions.designableFormName,
|
|
67
|
+
props: formily.form,
|
|
68
|
+
children: [],
|
|
69
|
+
}
|
|
70
|
+
const schema = new Schema(formily.schema)
|
|
71
|
+
const cleanProps = (props) => {
|
|
72
|
+
if (props['name'] === props['x-designable-id']) {
|
|
73
|
+
delete props.name
|
|
74
|
+
}
|
|
75
|
+
delete props['version']
|
|
76
|
+
delete props['_isJSONSchemaObject']
|
|
77
|
+
return props
|
|
78
|
+
}
|
|
79
|
+
const appendTreeNode = (parent, schema) => {
|
|
80
|
+
if (!schema) return
|
|
81
|
+
const current = {
|
|
82
|
+
id: schema['x-designable-id'] || uid(),
|
|
83
|
+
componentName: realOptions.designableFieldName,
|
|
84
|
+
props: cleanProps(schema.toJSON(false)),
|
|
85
|
+
children: [],
|
|
86
|
+
}
|
|
87
|
+
parent.children.push(current)
|
|
88
|
+
if (schema.items && !Array.isArray(schema.items)) {
|
|
89
|
+
appendTreeNode(current, schema.items)
|
|
90
|
+
}
|
|
103
91
|
schema.mapProperties((schema) => {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
|
|
92
|
+
schema['x-designable-id'] = schema['x-designable-id'] || uid()
|
|
93
|
+
appendTreeNode(current, schema)
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
schema.mapProperties((schema) => {
|
|
97
|
+
schema['x-designable-id'] = schema['x-designable-id'] || uid()
|
|
98
|
+
appendTreeNode(root, schema)
|
|
99
|
+
})
|
|
100
|
+
return root
|
|
101
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
import { ISchema } from '@formily/json-schema'
|
|
2
|
-
import { ITreeNode } from '@thienvu18/designable-core'
|
|
1
|
+
import { ISchema } from '@formily/json-schema'
|
|
2
|
+
import { ITreeNode } from '@thienvu18/designable-core'
|
|
3
3
|
export interface ITransformerOptions {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
designableFieldName?: string
|
|
5
|
+
designableFormName?: string
|
|
6
6
|
}
|
|
7
7
|
export interface IFormilySchema {
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
schema?: ISchema
|
|
9
|
+
form?: Record<string, any>
|
|
10
10
|
}
|
|
11
|
-
export declare const transformToSchema: (
|
|
12
|
-
|
|
11
|
+
export declare const transformToSchema: (
|
|
12
|
+
node: ITreeNode,
|
|
13
|
+
options?: ITransformerOptions
|
|
14
|
+
) => IFormilySchema
|
|
15
|
+
export declare const transformToTreeNode: (
|
|
16
|
+
formily?: IFormilySchema,
|
|
17
|
+
options?: ITransformerOptions
|
|
18
|
+
) => ITreeNode
|
package/lib/index.js
CHANGED
|
@@ -1,113 +1,111 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
3
|
-
exports.transformToTreeNode = exports.transformToSchema = void 0
|
|
4
|
-
const json_schema_1 = require(
|
|
5
|
-
const designable_shared_1 = require(
|
|
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
6
|
const createOptions = (options) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
7
|
+
return {
|
|
8
|
+
designableFieldName: 'Field',
|
|
9
|
+
designableFormName: 'Form',
|
|
10
|
+
...options,
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
13
|
const findNode = (node, finder) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return node.children[i];
|
|
23
|
-
}
|
|
24
|
-
return;
|
|
25
|
-
};
|
|
14
|
+
if (!node) return
|
|
15
|
+
if (finder(node)) return node
|
|
16
|
+
if (!node.children) return
|
|
17
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
18
|
+
if (findNode(node.children[i])) return node.children[i]
|
|
19
|
+
}
|
|
20
|
+
return
|
|
21
|
+
}
|
|
26
22
|
const transformToSchema = (node, options) => {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
});
|
|
23
|
+
const realOptions = createOptions(options)
|
|
24
|
+
const root = findNode(node, (child) => {
|
|
25
|
+
return child.componentName === realOptions.designableFormName
|
|
26
|
+
})
|
|
27
|
+
const schema = {
|
|
28
|
+
type: 'object',
|
|
29
|
+
properties: {},
|
|
30
|
+
}
|
|
31
|
+
if (!root) return { schema }
|
|
32
|
+
const createSchema = (node, schema = {}) => {
|
|
33
|
+
if (node !== root) {
|
|
34
|
+
Object.assign(schema, (0, designable_shared_1.clone)(node.props))
|
|
35
|
+
}
|
|
36
|
+
schema['x-designable-id'] = node.id
|
|
37
|
+
if (schema.type === 'array') {
|
|
38
|
+
if (node.children[0]) {
|
|
39
|
+
if (
|
|
40
|
+
node.children[0].componentName === realOptions.designableFieldName
|
|
41
|
+
) {
|
|
42
|
+
schema.items = createSchema(node.children[0])
|
|
43
|
+
schema['x-index'] = 0
|
|
67
44
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
45
|
+
}
|
|
46
|
+
node.children.slice(1).forEach((child, index) => {
|
|
47
|
+
if (child.componentName !== realOptions.designableFieldName) return
|
|
48
|
+
const key = child.props.name || child.id
|
|
49
|
+
schema.properties = schema.properties || {}
|
|
50
|
+
schema.properties[key] = createSchema(child)
|
|
51
|
+
schema.properties[key]['x-index'] = index
|
|
52
|
+
})
|
|
53
|
+
} else {
|
|
54
|
+
node.children.forEach((child, index) => {
|
|
55
|
+
if (child.componentName !== realOptions.designableFieldName) return
|
|
56
|
+
const key = child.props.name || child.id
|
|
57
|
+
schema.properties = schema.properties || {}
|
|
58
|
+
schema.properties[key] = createSchema(child)
|
|
59
|
+
schema.properties[key]['x-index'] = index
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
return schema
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
form: (0, designable_shared_1.clone)(root.props),
|
|
66
|
+
schema: createSchema(root, schema),
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.transformToSchema = transformToSchema
|
|
73
70
|
const transformToTreeNode = (formily = {}, options) => {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
};
|
|
71
|
+
const realOptions = createOptions(options)
|
|
72
|
+
const root = {
|
|
73
|
+
componentName: realOptions.designableFormName,
|
|
74
|
+
props: formily.form,
|
|
75
|
+
children: [],
|
|
76
|
+
}
|
|
77
|
+
const schema = new json_schema_1.Schema(formily.schema)
|
|
78
|
+
const cleanProps = (props) => {
|
|
79
|
+
if (props['name'] === props['x-designable-id']) {
|
|
80
|
+
delete props.name
|
|
81
|
+
}
|
|
82
|
+
delete props['version']
|
|
83
|
+
delete props['_isJSONSchemaObject']
|
|
84
|
+
return props
|
|
85
|
+
}
|
|
86
|
+
const appendTreeNode = (parent, schema) => {
|
|
87
|
+
if (!schema) return
|
|
88
|
+
const current = {
|
|
89
|
+
id: schema['x-designable-id'] || (0, designable_shared_1.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
|
+
}
|
|
107
98
|
schema.mapProperties((schema) => {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
|
|
99
|
+
schema['x-designable-id'] =
|
|
100
|
+
schema['x-designable-id'] || (0, designable_shared_1.uid)()
|
|
101
|
+
appendTreeNode(current, schema)
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
schema.mapProperties((schema) => {
|
|
105
|
+
schema['x-designable-id'] =
|
|
106
|
+
schema['x-designable-id'] || (0, designable_shared_1.uid)()
|
|
107
|
+
appendTreeNode(root, schema)
|
|
108
|
+
})
|
|
109
|
+
return root
|
|
110
|
+
}
|
|
111
|
+
exports.transformToTreeNode = transformToTreeNode
|
package/package.json
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thienvu18/designable-formily-transformer",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
|
+
"homepage": "https://github.com/thienvu18/designable#readme",
|
|
5
|
+
"bugs": {
|
|
6
|
+
"url": "https://github.com/thienvu18/designable/issues"
|
|
7
|
+
},
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/thienvu18/designable.git"
|
|
11
|
+
},
|
|
4
12
|
"license": "MIT",
|
|
5
13
|
"main": "lib/index.js",
|
|
6
14
|
"module": "esm/index.js",
|
|
@@ -11,29 +19,14 @@
|
|
|
11
19
|
"README.md",
|
|
12
20
|
"LICENSE.md"
|
|
13
21
|
],
|
|
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
22
|
"scripts": {
|
|
26
23
|
"build": "rimraf -rf lib esm && npm run build:cjs && npm run build:esm",
|
|
27
24
|
"build:cjs": "tsc --project tsconfig.build.json --outDir lib",
|
|
28
25
|
"build:esm": "tsc --project tsconfig.build.json --module es2020 --outDir esm"
|
|
29
26
|
},
|
|
30
|
-
"peerDependencies": {
|
|
31
|
-
"@formily/core": ">=2.3.7 <3",
|
|
32
|
-
"@formily/json-schema": ">=2.3.7 <3"
|
|
33
|
-
},
|
|
34
27
|
"dependencies": {
|
|
35
|
-
"@thienvu18/designable-core": "2.0.
|
|
36
|
-
"@thienvu18/designable-shared": "2.0.
|
|
28
|
+
"@thienvu18/designable-core": "2.0.2",
|
|
29
|
+
"@thienvu18/designable-shared": "2.0.2"
|
|
37
30
|
},
|
|
38
31
|
"devDependencies": {
|
|
39
32
|
"@formily/core": "^2.3.7",
|
|
@@ -42,6 +35,13 @@
|
|
|
42
35
|
"@formily/reactive": "^2.3.7",
|
|
43
36
|
"typescript": "5.2.2"
|
|
44
37
|
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@formily/core": ">=2.3.7 <3",
|
|
40
|
+
"@formily/json-schema": ">=2.3.7 <3"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=20 <25"
|
|
44
|
+
},
|
|
45
45
|
"publishConfig": {
|
|
46
46
|
"access": "public"
|
|
47
47
|
}
|