diff --git a/src/helpers/ArticleHelper.ts b/src/helpers/ArticleHelper.ts index 30264981..2b2749ed 100644 --- a/src/helpers/ArticleHelper.ts +++ b/src/helpers/ArticleHelper.ts @@ -23,14 +23,14 @@ import { } from '../constants'; import { DumpOptions } from 'js-yaml'; import { FrontMatterParser, ParsedFrontMatter } from '../parsers'; -import { Extension, Logger, Settings, SlugHelper, parseWinPath } from '.'; +import { ContentType, Extension, Logger, Settings, SlugHelper, parseWinPath } from '.'; import { format, parse } from 'date-fns'; import { Notifications } from './Notifications'; import { Article } from '../commands'; import { join } from 'path'; import { EditorHelper } from '@estruyf/vscode'; import sanitize from '../helpers/Sanitize'; -import { ContentType } from '../models'; +import { ContentType as IContentType } from '../models'; import { DateHelper } from './DateHelper'; import { DiagnosticSeverity, Position, window, Range } from 'vscode'; import { DEFAULT_FILE_TYPES } from '../constants/DefaultFileTypes'; @@ -305,14 +305,14 @@ export class ArticleHelper { * @returns */ public static getContentTypes() { - return Settings.get(SETTING_TAXONOMY_CONTENT_TYPES) || [DEFAULT_CONTENT_TYPE]; + return Settings.get(SETTING_TAXONOMY_CONTENT_TYPES) || [DEFAULT_CONTENT_TYPE]; } /** * Retrieve the content type of the current file * @param updatedMetadata */ - public static getContentType(metadata: { [field: string]: string }): ContentType { + public static getContentType(metadata: { [field: string]: string }): IContentType { const contentTypes = ArticleHelper.getContentTypes(); if (!contentTypes || !metadata) { @@ -331,6 +331,35 @@ export class ArticleHelper { contentType.fields = DEFAULT_CONTENT_TYPE.fields; } + // Check if there is a field collection + const fcFields = ContentType.findAllFieldsByType(contentType.fields || [], 'fieldCollection'); + + if (fcFields.length > 0) { + const fieldCollections = Settings.get('taxonomy.fieldCollection'); + + if (fieldCollections && fieldCollections.length > 0) { + for (const cField of fcFields) { + let fields = contentType.fields; + + for (const fieldName of cField) { + const field = fields.find((f) => f.name === fieldName); + + if (field && field.type === 'fieldCollection') { + const fieldCollection = fieldCollections.find( + (fc) => fc.id === (field as any).fieldCollectionId + ); + if (fieldCollection) { + const fieldIdx = fields.findIndex((f) => f.name === field.name); + fields.splice(fieldIdx, 1, ...fieldCollection.fields); + } + } else if (field && field.type === 'fields') { + fields = field.fields || []; + } + } + } + } + } + return contentType; } @@ -372,7 +401,7 @@ export class ArticleHelper { * @returns The new file path */ public static async createContent( - contentType: ContentType | undefined, + contentType: IContentType | undefined, folderPath: string, titleValue: string, fileExtension?: string @@ -438,7 +467,7 @@ export class ArticleHelper { public static getFilePrefix( prefix: string | null | undefined, filePath?: string, - contentType?: ContentType + contentType?: IContentType ): string | undefined { if (!prefix) { prefix = undefined; diff --git a/src/helpers/ContentType.ts b/src/helpers/ContentType.ts index 743ce51a..10c59606 100644 --- a/src/helpers/ContentType.ts +++ b/src/helpers/ContentType.ts @@ -395,6 +395,29 @@ export class ContentType { return parents; } + /** + * Find all the fields by type + * @param fields + * @param type + * @returns + */ + public static findAllFieldsByType(fields: Field[], type: FieldType): string[][] { + let parents: string[][] = []; + + for (const field of fields) { + if (field.type === type) { + parents.push([field.name]); + } else if (field.type === 'fields' && field.fields) { + const subFields = this.findAllFieldsByType(field.fields, type); + if (subFields.length > 0) { + parents = [...parents, ...subFields.map((f) => [field.name, ...f])]; + } + } + } + + return parents; + } + /** * Find the preview field in the fields * @param ctFields diff --git a/src/models/PanelSettings.ts b/src/models/PanelSettings.ts index c0a084a8..38b18b70 100644 --- a/src/models/PanelSettings.ts +++ b/src/models/PanelSettings.ts @@ -78,6 +78,7 @@ export type FieldType = | 'divider' | 'heading' | 'contentRelationship' + | 'fieldCollection' | 'customField'; export interface Field {