From beee186d72d26243d4c3cecd906450a402961b03 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Thu, 23 May 2024 11:00:00 +0200 Subject: [PATCH] Add support for field groups + block fields #808 --- CHANGELOG.md | 1 + src/helpers/ContentType.ts | 35 +++++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31df5ffe..52a8f079 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - [#798](https://github.com/estruyf/vscode-front-matter/issues/798): Changed dialog to slide-over for the snippet forms - [#799](https://github.com/estruyf/vscode-front-matter/issues/799): Added `frontMatter.logging` setting to define the logging output. Options are `info`, `warn`, `error`, and `verbose`. Default is `info`. - [#800](https://github.com/estruyf/vscode-front-matter/issues/800): Add colors for the Front Matter CMS output +- [#808](https://github.com/estruyf/vscode-front-matter/issues/808): Add support to generate field groups and `block` fields in content type generation - [#810](https://github.com/estruyf/vscode-front-matter/issues/810): Update the tab title based on the view ### ⚡️ Optimizations diff --git a/src/helpers/ContentType.ts b/src/helpers/ContentType.ts index ad9cca86..ec49b945 100644 --- a/src/helpers/ContentType.ts +++ b/src/helpers/ContentType.ts @@ -280,7 +280,7 @@ export class ContentType { pageBundle = pageBundleAnswer === l10n.t(LocalizationKey.commonYes); } - const fields = ContentType.generateFields(content.data); + const fields = await ContentType.generateFields(content.data); // Update the type field in the page if (!overrideBool && editor) { @@ -344,7 +344,7 @@ export class ContentType { } const contentType = await ArticleHelper.getContentType(article); - const updatedFields = ContentType.generateFields(article.data, contentType.fields); + const updatedFields = await ContentType.generateFields(article.data, contentType.fields); const contentTypes = ContentType.getAll() || []; const index = contentTypes.findIndex((ct) => ct.name === contentType.name); @@ -744,7 +744,7 @@ export class ContentType { * @param fields * @returns */ - private static generateFields(data: any, fields: any[] = []) { + private static async generateFields(data: any, fields: any[] = []) { for (const field in data) { const fieldData = data[field]; @@ -782,15 +782,38 @@ export class ContentType { fieldData.length > 0 && typeof fieldData[0] === 'object' ) { - const newFields = ContentType.generateFields(fieldData); + const newFields = await ContentType.generateFields(fieldData); + + // Combine all the fields for the field group + const blockFields: Field[] = []; + for (const newField of newFields) { + for (const field of newField.fields) { + if (blockFields.some((f) => f.name === field.name)) { + continue; + } + + blockFields.push(field); + } + } + + // Generate a new field group + const fieldGroups = Settings.get(SETTING_TAXONOMY_FIELD_GROUPS) || []; + const fieldGroupName = `${field}_group`; + const newFieldGroup: FieldGroup = { + id: fieldGroupName, + fields: blockFields + }; + fieldGroups.push(newFieldGroup); + await Settings.update(SETTING_TAXONOMY_FIELD_GROUPS, fieldGroups, true); + fields.push({ title: field, name: field, type: 'block', - fields: newFields + fieldGroup: [fieldGroupName] } as Field); } else if (fieldData && fieldData instanceof Object) { - const newFields = ContentType.generateFields(fieldData); + const newFields = await ContentType.generateFields(fieldData); fields.push({ title: field, name: field,