@sulesky/next-formily-transformer 1.0.3

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 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
+ # @designable/formily
package/esm/index.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { ISchema } from '@formily/json-schema';
2
+ import { ITreeNode } from '@sulesky/next-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,117 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ import { Schema } from '@formily/json-schema';
13
+ import { clone, uid } from '@sulesky/next-shared';
14
+ var createOptions = function (options) {
15
+ return __assign({ designableFieldName: 'Field', designableFormName: 'Form' }, options);
16
+ };
17
+ var findNode = function (node, finder) {
18
+ if (!node)
19
+ return;
20
+ if (finder(node))
21
+ return node;
22
+ if (!node.children)
23
+ return;
24
+ for (var i = 0; i < node.children.length; i++) {
25
+ if (findNode(node.children[i]))
26
+ return node.children[i];
27
+ }
28
+ return;
29
+ };
30
+ export var transformToSchema = function (node, options) {
31
+ var realOptions = createOptions(options);
32
+ var root = findNode(node, function (child) {
33
+ return child.componentName === realOptions.designableFormName;
34
+ });
35
+ var schema = {
36
+ type: 'object',
37
+ properties: {},
38
+ };
39
+ if (!root)
40
+ return { schema: schema };
41
+ var createSchema = function (node, schema) {
42
+ if (schema === void 0) { schema = {}; }
43
+ if (node !== root) {
44
+ Object.assign(schema, clone(node.props));
45
+ }
46
+ schema['x-designable-id'] = node.id;
47
+ if (schema.type === 'array') {
48
+ if (node.children[0]) {
49
+ if (node.children[0].componentName === realOptions.designableFieldName) {
50
+ schema.items = createSchema(node.children[0]);
51
+ schema['x-index'] = 0;
52
+ }
53
+ }
54
+ node.children.slice(1).forEach(function (child, index) {
55
+ if (child.componentName !== realOptions.designableFieldName)
56
+ return;
57
+ var key = child.props.name || child.id;
58
+ schema.properties = schema.properties || {};
59
+ schema.properties[key] = createSchema(child);
60
+ schema.properties[key]['x-index'] = index;
61
+ });
62
+ }
63
+ else {
64
+ node.children.forEach(function (child, index) {
65
+ if (child.componentName !== realOptions.designableFieldName)
66
+ return;
67
+ var key = child.props.name || child.id;
68
+ schema.properties = schema.properties || {};
69
+ schema.properties[key] = createSchema(child);
70
+ schema.properties[key]['x-index'] = index;
71
+ });
72
+ }
73
+ return schema;
74
+ };
75
+ return { form: clone(root.props), schema: createSchema(root, schema) };
76
+ };
77
+ export var transformToTreeNode = function (formily, options) {
78
+ if (formily === void 0) { formily = {}; }
79
+ var realOptions = createOptions(options);
80
+ var root = {
81
+ componentName: realOptions.designableFormName,
82
+ props: formily.form,
83
+ children: [],
84
+ };
85
+ var schema = new Schema(formily.schema);
86
+ var cleanProps = function (props) {
87
+ if (props['name'] === props['x-designable-id']) {
88
+ delete props.name;
89
+ }
90
+ delete props['version'];
91
+ delete props['_isJSONSchemaObject'];
92
+ return props;
93
+ };
94
+ var appendTreeNode = function (parent, schema) {
95
+ if (!schema)
96
+ return;
97
+ var current = {
98
+ id: schema['x-designable-id'] || uid(),
99
+ componentName: realOptions.designableFieldName,
100
+ props: cleanProps(schema.toJSON(false)),
101
+ children: [],
102
+ };
103
+ parent.children.push(current);
104
+ if (schema.items && !Array.isArray(schema.items)) {
105
+ appendTreeNode(current, schema.items);
106
+ }
107
+ schema.mapProperties(function (schema) {
108
+ schema['x-designable-id'] = schema['x-designable-id'] || uid();
109
+ appendTreeNode(current, schema);
110
+ });
111
+ };
112
+ schema.mapProperties(function (schema) {
113
+ schema['x-designable-id'] = schema['x-designable-id'] || uid();
114
+ appendTreeNode(root, schema);
115
+ });
116
+ return root;
117
+ };
package/lib/index.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { ISchema } from '@formily/json-schema';
2
+ import { ITreeNode } from '@sulesky/next-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,122 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.transformToTreeNode = exports.transformToSchema = void 0;
15
+ var json_schema_1 = require("@formily/json-schema");
16
+ var next_shared_1 = require("@sulesky/next-shared");
17
+ var createOptions = function (options) {
18
+ return __assign({ designableFieldName: 'Field', designableFormName: 'Form' }, options);
19
+ };
20
+ var findNode = function (node, finder) {
21
+ if (!node)
22
+ return;
23
+ if (finder(node))
24
+ return node;
25
+ if (!node.children)
26
+ return;
27
+ for (var i = 0; i < node.children.length; i++) {
28
+ if (findNode(node.children[i]))
29
+ return node.children[i];
30
+ }
31
+ return;
32
+ };
33
+ var transformToSchema = function (node, options) {
34
+ var realOptions = createOptions(options);
35
+ var root = findNode(node, function (child) {
36
+ return child.componentName === realOptions.designableFormName;
37
+ });
38
+ var schema = {
39
+ type: 'object',
40
+ properties: {},
41
+ };
42
+ if (!root)
43
+ return { schema: schema };
44
+ var createSchema = function (node, schema) {
45
+ if (schema === void 0) { schema = {}; }
46
+ if (node !== root) {
47
+ Object.assign(schema, (0, next_shared_1.clone)(node.props));
48
+ }
49
+ schema['x-designable-id'] = node.id;
50
+ if (schema.type === 'array') {
51
+ if (node.children[0]) {
52
+ if (node.children[0].componentName === realOptions.designableFieldName) {
53
+ schema.items = createSchema(node.children[0]);
54
+ schema['x-index'] = 0;
55
+ }
56
+ }
57
+ node.children.slice(1).forEach(function (child, index) {
58
+ if (child.componentName !== realOptions.designableFieldName)
59
+ return;
60
+ var key = child.props.name || child.id;
61
+ schema.properties = schema.properties || {};
62
+ schema.properties[key] = createSchema(child);
63
+ schema.properties[key]['x-index'] = index;
64
+ });
65
+ }
66
+ else {
67
+ node.children.forEach(function (child, index) {
68
+ if (child.componentName !== realOptions.designableFieldName)
69
+ return;
70
+ var key = child.props.name || child.id;
71
+ schema.properties = schema.properties || {};
72
+ schema.properties[key] = createSchema(child);
73
+ schema.properties[key]['x-index'] = index;
74
+ });
75
+ }
76
+ return schema;
77
+ };
78
+ return { form: (0, next_shared_1.clone)(root.props), schema: createSchema(root, schema) };
79
+ };
80
+ exports.transformToSchema = transformToSchema;
81
+ var transformToTreeNode = function (formily, options) {
82
+ if (formily === void 0) { formily = {}; }
83
+ var realOptions = createOptions(options);
84
+ var root = {
85
+ componentName: realOptions.designableFormName,
86
+ props: formily.form,
87
+ children: [],
88
+ };
89
+ var schema = new json_schema_1.Schema(formily.schema);
90
+ var cleanProps = function (props) {
91
+ if (props['name'] === props['x-designable-id']) {
92
+ delete props.name;
93
+ }
94
+ delete props['version'];
95
+ delete props['_isJSONSchemaObject'];
96
+ return props;
97
+ };
98
+ var appendTreeNode = function (parent, schema) {
99
+ if (!schema)
100
+ return;
101
+ var current = {
102
+ id: schema['x-designable-id'] || (0, next_shared_1.uid)(),
103
+ componentName: realOptions.designableFieldName,
104
+ props: cleanProps(schema.toJSON(false)),
105
+ children: [],
106
+ };
107
+ parent.children.push(current);
108
+ if (schema.items && !Array.isArray(schema.items)) {
109
+ appendTreeNode(current, schema.items);
110
+ }
111
+ schema.mapProperties(function (schema) {
112
+ schema['x-designable-id'] = schema['x-designable-id'] || (0, next_shared_1.uid)();
113
+ appendTreeNode(current, schema);
114
+ });
115
+ };
116
+ schema.mapProperties(function (schema) {
117
+ schema['x-designable-id'] = schema['x-designable-id'] || (0, next_shared_1.uid)();
118
+ appendTreeNode(root, schema);
119
+ });
120
+ return root;
121
+ };
122
+ exports.transformToTreeNode = transformToTreeNode;
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@sulesky/next-formily-transformer",
3
+ "version": "1.0.3",
4
+ "license": "MIT",
5
+ "main": "lib",
6
+ "engines": {
7
+ "npm": ">=3.0.0"
8
+ },
9
+ "module": "esm",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/kapelan/designable.git"
13
+ },
14
+ "types": "esm/index.d.ts",
15
+ "bugs": {
16
+ "url": "https://github.com/kapelan/designable/issues"
17
+ },
18
+ "homepage": "https://github.com/kapelan/designable#readme",
19
+ "scripts": {
20
+ "build": "rimraf -rf lib esm dist && npm run build:cjs && npm run build:esm",
21
+ "build:cjs": "tsc --project tsconfig.build.json",
22
+ "build:esm": "tsc --project tsconfig.build.json --module es2015 --outDir esm"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "devDependencies": {
28
+ "@formily/core": "^2.3.0",
29
+ "@formily/json-schema": "^2.3.0"
30
+ },
31
+ "peerDependencies": {
32
+ "@formily/core": "^2.3.0",
33
+ "@formily/json-schema": "^2.3.0"
34
+ },
35
+ "dependencies": {
36
+ "@sulesky/next-core": "1.0.3",
37
+ "@sulesky/next-shared": "1.0.3"
38
+ },
39
+ "gitHead": "c352e7550efc1420491dfc18b9418ec4fcfcc4ae"
40
+ }
@@ -0,0 +1,3 @@
1
+ import baseConfig from '../../scripts/rollup.base.mjs'
2
+
3
+ export default baseConfig('designable.formily', 'Designable.Formily')
package/src/index.ts ADDED
@@ -0,0 +1,122 @@
1
+ import { ISchema, Schema } from '@formily/json-schema'
2
+ import { ITreeNode } from '@sulesky/next-core'
3
+ import { clone, uid } from '@sulesky/next-shared'
4
+
5
+ export interface ITransformerOptions {
6
+ designableFieldName?: string
7
+ designableFormName?: string
8
+ }
9
+
10
+ export interface IFormilySchema {
11
+ schema?: ISchema
12
+ form?: Record<string, any>
13
+ }
14
+
15
+ const createOptions = (options: ITransformerOptions): ITransformerOptions => {
16
+ return {
17
+ designableFieldName: 'Field',
18
+ designableFormName: 'Form',
19
+ ...options,
20
+ }
21
+ }
22
+
23
+ const findNode = (node: ITreeNode, finder?: (node: ITreeNode) => boolean) => {
24
+ if (!node) return
25
+ if (finder(node)) return node
26
+ if (!node.children) return
27
+ for (let i = 0; i < node.children.length; i++) {
28
+ if (findNode(node.children[i])) return node.children[i]
29
+ }
30
+ return
31
+ }
32
+
33
+ export const transformToSchema = (
34
+ node: ITreeNode,
35
+ options?: ITransformerOptions
36
+ ): IFormilySchema => {
37
+ const realOptions = createOptions(options)
38
+ const root = findNode(node, (child) => {
39
+ return child.componentName === realOptions.designableFormName
40
+ })
41
+ const schema = {
42
+ type: 'object',
43
+ properties: {},
44
+ }
45
+ if (!root) return { schema }
46
+ const createSchema = (node: ITreeNode, schema: ISchema = {}) => {
47
+ if (node !== root) {
48
+ Object.assign(schema, clone(node.props))
49
+ }
50
+ schema['x-designable-id'] = node.id
51
+ if (schema.type === 'array') {
52
+ if (node.children[0]) {
53
+ if (
54
+ node.children[0].componentName === realOptions.designableFieldName
55
+ ) {
56
+ schema.items = createSchema(node.children[0])
57
+ schema['x-index'] = 0
58
+ }
59
+ }
60
+ node.children.slice(1).forEach((child, index) => {
61
+ if (child.componentName !== realOptions.designableFieldName) 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
+ } else {
68
+ node.children.forEach((child, index) => {
69
+ if (child.componentName !== realOptions.designableFieldName) return
70
+ const key = child.props.name || child.id
71
+ schema.properties = schema.properties || {}
72
+ schema.properties[key] = createSchema(child)
73
+ schema.properties[key]['x-index'] = index
74
+ })
75
+ }
76
+ return schema
77
+ }
78
+ return { form: clone(root.props), schema: createSchema(root, schema) }
79
+ }
80
+
81
+ export const transformToTreeNode = (
82
+ formily: IFormilySchema = {},
83
+ options?: ITransformerOptions
84
+ ) => {
85
+ const realOptions = createOptions(options)
86
+ const root: ITreeNode = {
87
+ componentName: realOptions.designableFormName,
88
+ props: formily.form,
89
+ children: [],
90
+ }
91
+ const schema = new Schema(formily.schema)
92
+ const cleanProps = (props: any) => {
93
+ if (props['name'] === props['x-designable-id']) {
94
+ delete props.name
95
+ }
96
+ delete props['version']
97
+ delete props['_isJSONSchemaObject']
98
+ return props
99
+ }
100
+ const appendTreeNode = (parent: ITreeNode, schema: Schema) => {
101
+ if (!schema) return
102
+ const current = {
103
+ id: schema['x-designable-id'] || uid(),
104
+ componentName: realOptions.designableFieldName,
105
+ props: cleanProps(schema.toJSON(false)),
106
+ children: [],
107
+ }
108
+ parent.children.push(current)
109
+ if (schema.items && !Array.isArray(schema.items)) {
110
+ appendTreeNode(current, schema.items)
111
+ }
112
+ schema.mapProperties((schema) => {
113
+ schema['x-designable-id'] = schema['x-designable-id'] || uid()
114
+ appendTreeNode(current, schema)
115
+ })
116
+ }
117
+ schema.mapProperties((schema) => {
118
+ schema['x-designable-id'] = schema['x-designable-id'] || uid()
119
+ appendTreeNode(root, schema)
120
+ })
121
+ return root
122
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./lib",
5
+ "paths": {
6
+ "@sulesky/next-*": ["../../packages/*", "../../formily/*"]
7
+ },
8
+ "declaration": true
9
+ }
10
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "include": ["./src/**/*.ts", "./src/**/*.tsx"],
4
+ "exclude": ["./src/__tests__/*", "./esm/*", "./lib/*"]
5
+ }