From cdba86459877987f52def4bcfeba5c7bef7de407 Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Fri, 15 Mar 2024 07:47:42 +0800 Subject: [PATCH] DEV: Support description for properties in objects schema (#26172) Why this change? When editing a objects typed theme setting, the input fields which are rendered should include a description so that the user knows the purpose of the field which they are changing. What does this change do? This change adds support for adding description to each property in the schema for an object by following a given convention in the locale file. For a schema like this: ``` objects_setting: type: objects schema: name: section properties: name: type: string required: true links: type: objects schema: name: link properties: name: type: string required: true validations: max_length: 20 url: type: string ``` Description for each property in the object can be added like so: ``` en: theme_metadata: settings: objects_setting: description: for the setting schema: properties: name: links: name: url: ``` If the a description is not present, the input field will simply not have an description. Also note that a description for a theme setting can now be added like so: ``` en: theme_metadata: settings: some_other_setting: objects_setting: description: ``` --- .../schema-theme-setting/editor.gjs | 36 ++++++++-- .../components/schema-theme-setting/field.gjs | 22 +++++- .../fixtures/theme-setting-schema-data.js | 1 + .../editor-test.gjs | 67 ++++++++++++++++++- .../stylesheets/common/admin/admin_base.scss | 1 + .../common/admin/schema_field.scss | 16 +++++ app/serializers/theme_settings_serializer.rb | 27 ++++++-- .../theme_locales/objects_settings/en.yaml | 11 +++ .../theme_settings/objects_settings.yaml | 2 +- .../theme_settings_serializer_spec.rb | 55 ++++++++++++--- ...diting_objects_typed_theme_setting_spec.rb | 29 ++++++++ .../admin_objects_theme_setting_editor.rb | 20 +++++- 12 files changed, 263 insertions(+), 24 deletions(-) create mode 100644 app/assets/stylesheets/common/admin/schema_field.scss create mode 100644 spec/fixtures/theme_locales/objects_settings/en.yaml diff --git a/app/assets/javascripts/admin/addon/components/schema-theme-setting/editor.gjs b/app/assets/javascripts/admin/addon/components/schema-theme-setting/editor.gjs index f3cf1a3ace2..c7660b23d90 100644 --- a/app/assets/javascripts/admin/addon/components/schema-theme-setting/editor.gjs +++ b/app/assets/javascripts/admin/addon/components/schema-theme-setting/editor.gjs @@ -18,13 +18,15 @@ class Node { schema; index; active = false; + parentTree; trees = []; - constructor({ text, index, object, schema }) { + constructor({ text, index, object, schema, parentTree }) { this.text = text; this.index = index; this.object = object; this.schema = schema; + this.parentTree = parentTree; } } @@ -47,20 +49,21 @@ export default class SchemaThemeSettingEditor extends Component { get tree() { let schema = this.schema; let data = this.data; + let tree = new Tree(); for (const point of this.history) { + tree.propertyName = point.propertyName; data = data[point.node.index][point.propertyName]; schema = schema.properties[point.propertyName].schema; } - const tree = new Tree(); - data.forEach((object, index) => { const node = new Node({ index, schema, object, - text: object[schema.identifier], + text: object[schema.identifier] || `${schema.name} ${index + 1}`, + parentTree: tree, }); if (index === this.activeIndex) { @@ -78,10 +81,13 @@ export default class SchemaThemeSettingEditor extends Component { (childObj, childIndex) => { subtree.nodes.push( new Node({ - text: childObj[childObjectsProperty.schema.identifier], + text: + childObj[childObjectsProperty.schema.identifier] || + `${childObjectsProperty.schema.name} ${childIndex + 1}`, index: childIndex, object: childObj, schema: childObjectsProperty.schema, + parentTree: subtree, }) ); } @@ -117,6 +123,7 @@ export default class SchemaThemeSettingEditor extends Component { name, spec, value: node.object[name], + description: this.fieldDescription(name), }); } @@ -198,6 +205,24 @@ export default class SchemaThemeSettingEditor extends Component { this.activeNode.object[field.name] = newVal; } + fieldDescription(fieldName) { + const descriptions = this.args.setting.objects_schema_property_descriptions; + + if (!descriptions) { + return; + } + + let key; + + if (this.activeNode.parentTree.propertyName) { + key = `${this.activeNode.parentTree.propertyName}.${fieldName}`; + } else { + key = `${fieldName}`; + } + + return descriptions[key]; + } +