mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-04-18 15:50:27 +08:00
feat(docs): Update blocks property getters.
This commit is contained in:
parent
dafa6b3b31
commit
e68f774a5c
@ -86,61 +86,76 @@
|
||||
// };
|
||||
// }
|
||||
|
||||
const propertyGetter = ({ property, nameSpace, propertyName, getters }) => {
|
||||
if (property.docs && property.docs.displayType === 'yaml') {
|
||||
getters.push({
|
||||
[propertyName]: {
|
||||
'_yaml.parse': {
|
||||
_if_none: [{ _state: `${nameSpace}.${propertyName}` }, ''],
|
||||
const arrayGetter = ({ data, items, path, underscores }) => {
|
||||
const getter = propertyGetter({
|
||||
data: 'args',
|
||||
path: '0',
|
||||
property: items,
|
||||
underscores: underscores + '_',
|
||||
});
|
||||
if (getter) {
|
||||
return {
|
||||
[`${underscores}array.map`]: {
|
||||
on: { [`${underscores}if_none`]: [{ [`${underscores}${data}`]: path }, []] },
|
||||
callback: {
|
||||
[`${underscores}function`]: getter,
|
||||
},
|
||||
},
|
||||
});
|
||||
} else if (
|
||||
property.docs &&
|
||||
property.docs.displayType === 'manual' &&
|
||||
property.docs.getter != null
|
||||
) {
|
||||
getters.push({ [propertyName]: property.docs.getter });
|
||||
} else if (property.type === 'object' && property.properties) {
|
||||
getters.push({
|
||||
[propertyName]: makeGetters({
|
||||
properties: property.properties,
|
||||
nameSpace: `${nameSpace}.${propertyName}`,
|
||||
}),
|
||||
});
|
||||
} else if (property.type === 'object') {
|
||||
// for display types like button, where all properties are not specified in the schema
|
||||
getters.push({
|
||||
[propertyName]: { _state: `${nameSpace}.${propertyName}` },
|
||||
});
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const makeGetters = ({ properties, nameSpace }) => {
|
||||
const assignArray = [{ _state: nameSpace }];
|
||||
const propertyGetter = ({ data, path, property, underscores }) => {
|
||||
if (property.docs && property.docs.displayType === 'yaml') {
|
||||
return {
|
||||
[`${underscores}yaml.parse`]: {
|
||||
[`${underscores}if_none`]: [{ [`${underscores}${data}`]: path }, ''],
|
||||
},
|
||||
};
|
||||
}
|
||||
if (property.docs && property.docs.displayType === 'manual' && property.docs.getter != null) {
|
||||
return property.docs.getter;
|
||||
}
|
||||
if (property.type === 'object' && property.properties) {
|
||||
return objectGetter({
|
||||
data,
|
||||
path,
|
||||
properties: property.properties,
|
||||
underscores,
|
||||
});
|
||||
}
|
||||
if (property.type === 'array' && property.items) {
|
||||
return arrayGetter({ data, items: property.items, path, underscores });
|
||||
}
|
||||
};
|
||||
|
||||
const objectGetter = ({ data, path, properties, underscores }) => {
|
||||
const getters = [];
|
||||
Object.keys(properties).forEach((key) => {
|
||||
// if (properties[key].type === 'array') {
|
||||
// propertyGetter({
|
||||
// property: properties[key].items,
|
||||
// nameSpace,
|
||||
// propertyName: `${key}.$`,
|
||||
// getters,
|
||||
// });
|
||||
// } else {
|
||||
propertyGetter({ property: properties[key], nameSpace, propertyName: key, getters });
|
||||
// }
|
||||
const getter = propertyGetter({
|
||||
data,
|
||||
path: `${path}.${key}`,
|
||||
property: properties[key],
|
||||
underscores,
|
||||
});
|
||||
if (getter) {
|
||||
getters.push({ [key]: getter });
|
||||
}
|
||||
});
|
||||
return {
|
||||
'_object.assign': assignArray.concat(getters),
|
||||
[`${underscores}object.assign`]: [{ [`${underscores}${data}`]: path }].concat(getters),
|
||||
};
|
||||
};
|
||||
|
||||
const transformer = (obj) => {
|
||||
return makeGetters({
|
||||
const x = objectGetter({
|
||||
data: 'state',
|
||||
path: 'block.properties',
|
||||
properties: obj.schema.properties.properties,
|
||||
nameSpace: 'block.properties',
|
||||
underscores: '_',
|
||||
});
|
||||
// console.log(JSON.stringify(x, null, 2));
|
||||
return x;
|
||||
};
|
||||
|
||||
module.exports = transformer;
|
||||
|
23
packages/docs/templates/test/array.test.js
vendored
23
packages/docs/templates/test/array.test.js
vendored
@ -208,6 +208,29 @@ test('array schemaArrayObject propertiesGetterTransformer', () => {
|
||||
Object {
|
||||
"_state": "block.properties",
|
||||
},
|
||||
Object {
|
||||
"options": Object {
|
||||
"_array.map": Object {
|
||||
"callback": Object {
|
||||
"_function": Object {
|
||||
"__object.assign": Array [
|
||||
Object {
|
||||
"__args": "0",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
"on": Object {
|
||||
"_if_none": Array [
|
||||
Object {
|
||||
"_state": "block.properties.options",
|
||||
},
|
||||
Array [],
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
`);
|
||||
|
5
packages/docs/templates/test/button.test.js
vendored
5
packages/docs/templates/test/button.test.js
vendored
@ -109,11 +109,6 @@ test('button propertiesGetterTransformer', () => {
|
||||
Object {
|
||||
"_state": "block.properties",
|
||||
},
|
||||
Object {
|
||||
"field": Object {
|
||||
"_state": "block.properties.field",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
`);
|
||||
|
315
packages/docs/templates/test/yaml.test.js
vendored
315
packages/docs/templates/test/yaml.test.js
vendored
@ -228,3 +228,318 @@ test('yaml schemaNested defaultValueTransformer', () => {
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
const schemaYamlInArray = {
|
||||
schema: {
|
||||
properties: {
|
||||
type: 'object',
|
||||
additionalProperties: false,
|
||||
properties: {
|
||||
arr: {
|
||||
type: 'array',
|
||||
description: 'arr description',
|
||||
items: {
|
||||
type: 'object',
|
||||
description: 'yaml description',
|
||||
docs: {
|
||||
displayType: 'yaml',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
test('yaml schemaYamlInArray propertiesFormTransformer', () => {
|
||||
expect(propertiesFormTransformer(schemaYamlInArray)).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
Object {
|
||||
"blocks": Array [
|
||||
Object {
|
||||
"id": "block.properties.arr.$",
|
||||
"layout": Object {
|
||||
"_global": "settings_input_layout",
|
||||
},
|
||||
"properties": Object {
|
||||
"label": Object {
|
||||
"disabled": true,
|
||||
},
|
||||
"size": "small",
|
||||
"title": "$",
|
||||
},
|
||||
"required": false,
|
||||
"type": "TextArea",
|
||||
},
|
||||
],
|
||||
"id": "block.properties.arr",
|
||||
"layout": Object {
|
||||
"contentGutter": 0,
|
||||
},
|
||||
"properties": Object {
|
||||
"itemStyle": Object {
|
||||
"padding": 0,
|
||||
},
|
||||
"size": "small",
|
||||
"title": "arr:",
|
||||
},
|
||||
"type": "ControlledList",
|
||||
},
|
||||
]
|
||||
`);
|
||||
});
|
||||
|
||||
test('yaml schemaYamlInArray propertiesGetterTransformer', () => {
|
||||
expect(propertiesGetterTransformer(schemaYamlInArray)).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"_object.assign": Array [
|
||||
Object {
|
||||
"_state": "block.properties",
|
||||
},
|
||||
Object {
|
||||
"arr": Object {
|
||||
"_array.map": Object {
|
||||
"callback": Object {
|
||||
"_function": Object {
|
||||
"__yaml.parse": Object {
|
||||
"__if_none": Array [
|
||||
Object {
|
||||
"__args": "0",
|
||||
},
|
||||
"",
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
"on": Object {
|
||||
"_if_none": Array [
|
||||
Object {
|
||||
"_state": "block.properties.arr",
|
||||
},
|
||||
Array [],
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
test('yaml schemaYamlInArray defaultValueTransformer', () => {
|
||||
expect(defaultValueTransformer(schemaYamlInArray)).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"arr": Array [],
|
||||
}
|
||||
`);
|
||||
const schemaYamlInArrayDV = {
|
||||
schema: {
|
||||
properties: {
|
||||
type: 'object',
|
||||
additionalProperties: false,
|
||||
properties: {
|
||||
arr: {
|
||||
type: 'array',
|
||||
default: [{ a: 1 }],
|
||||
description: 'arr description',
|
||||
items: {
|
||||
type: 'object',
|
||||
description: 'yaml description',
|
||||
docs: {
|
||||
displayType: 'yaml',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
expect(defaultValueTransformer(schemaYamlInArrayDV)).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"arr": Array [
|
||||
Object {
|
||||
"a": 1,
|
||||
},
|
||||
],
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
const schemaYamlInObjectInArray = {
|
||||
schema: {
|
||||
properties: {
|
||||
type: 'object',
|
||||
additionalProperties: false,
|
||||
properties: {
|
||||
arr: {
|
||||
type: 'array',
|
||||
description: 'arr description',
|
||||
items: {
|
||||
type: 'object',
|
||||
description: 'obj description',
|
||||
properties: {
|
||||
yaml: {
|
||||
type: 'object',
|
||||
description: 'yaml description',
|
||||
docs: {
|
||||
displayType: 'yaml',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
test('yaml schemaYamlInObjectInArray propertiesFormTransformer', () => {
|
||||
expect(propertiesFormTransformer(schemaYamlInObjectInArray)).toMatchInlineSnapshot(`
|
||||
Array [
|
||||
Object {
|
||||
"blocks": Array [
|
||||
Object {
|
||||
"blocks": Array [
|
||||
Object {
|
||||
"id": "block.properties.arr.$.yaml",
|
||||
"layout": Object {
|
||||
"_global": "settings_input_layout",
|
||||
},
|
||||
"properties": Object {
|
||||
"label": Object {
|
||||
"align": "right",
|
||||
"extra": "yaml description",
|
||||
"span": 8,
|
||||
},
|
||||
"size": "small",
|
||||
"title": "yaml",
|
||||
},
|
||||
"required": false,
|
||||
"type": "TextArea",
|
||||
},
|
||||
],
|
||||
"id": "block.properties.arr.$",
|
||||
"layout": Object {
|
||||
"contentGutter": 0,
|
||||
},
|
||||
"properties": Object {
|
||||
"bodyStyle": Object {
|
||||
"padding": 0,
|
||||
},
|
||||
"size": "small",
|
||||
"title": false,
|
||||
},
|
||||
"type": "Card",
|
||||
},
|
||||
],
|
||||
"id": "block.properties.arr",
|
||||
"layout": Object {
|
||||
"contentGutter": 0,
|
||||
},
|
||||
"properties": Object {
|
||||
"itemStyle": Object {
|
||||
"padding": 0,
|
||||
},
|
||||
"size": "small",
|
||||
"title": "arr:",
|
||||
},
|
||||
"type": "ControlledList",
|
||||
},
|
||||
]
|
||||
`);
|
||||
});
|
||||
|
||||
test('yaml schemaYamlInObjectInArray propertiesGetterTransformer', () => {
|
||||
expect(propertiesGetterTransformer(schemaYamlInObjectInArray)).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"_object.assign": Array [
|
||||
Object {
|
||||
"_state": "block.properties",
|
||||
},
|
||||
Object {
|
||||
"arr": Object {
|
||||
"_array.map": Object {
|
||||
"callback": Object {
|
||||
"_function": Object {
|
||||
"__object.assign": Array [
|
||||
Object {
|
||||
"__args": "0",
|
||||
},
|
||||
Object {
|
||||
"yaml": Object {
|
||||
"__yaml.parse": Object {
|
||||
"__if_none": Array [
|
||||
Object {
|
||||
"__args": "0.yaml",
|
||||
},
|
||||
"",
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
"on": Object {
|
||||
"_if_none": Array [
|
||||
Object {
|
||||
"_state": "block.properties.arr",
|
||||
},
|
||||
Array [],
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
test('yaml schemaYamlInObjectInArray defaultValueTransformer', () => {
|
||||
expect(defaultValueTransformer(schemaYamlInObjectInArray)).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"arr": Array [],
|
||||
}
|
||||
`);
|
||||
const schemaYamlInObjectInArrayDV = {
|
||||
schema: {
|
||||
properties: {
|
||||
type: 'object',
|
||||
additionalProperties: false,
|
||||
properties: {
|
||||
arr: {
|
||||
type: 'array',
|
||||
description: 'arr description',
|
||||
default: [{ yaml: { b: 1 } }],
|
||||
items: {
|
||||
type: 'object',
|
||||
description: 'obj description',
|
||||
properties: {
|
||||
yaml: {
|
||||
type: 'object',
|
||||
description: 'yaml description',
|
||||
docs: {
|
||||
displayType: 'yaml',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
expect(defaultValueTransformer(schemaYamlInObjectInArrayDV)).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
"arr": Array [
|
||||
Object {
|
||||
"yaml": Object {
|
||||
"b": 1,
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user