diff --git a/src/helpers/ContentType.ts b/src/helpers/ContentType.ts index 22c3be94..2bf1ef59 100644 --- a/src/helpers/ContentType.ts +++ b/src/helpers/ContentType.ts @@ -1,8 +1,8 @@ import { ModeListener } from './../listeners/general/ModeListener'; import { PagesListener } from './../listeners/dashboard'; import { ArticleHelper, Settings } from "."; -import { FEATURE_FLAG, SETTING_CONTENT_DRAFT_FIELD, SETTING_DATE_FORMAT, SETTING_FRAMEWORK_ID, SETTING_TAXONOMY_CONTENT_TYPES, TelemetryEvent } from "../constants"; -import { ContentType as IContentType, DraftField, Field } from '../models'; +import { FEATURE_FLAG, SETTING_CONTENT_DRAFT_FIELD, SETTING_DATE_FORMAT, SETTING_FRAMEWORK_ID, SETTING_TAXONOMY_CONTENT_TYPES, SETTING_TAXONOMY_FIELD_GROUPS, TelemetryEvent } from "../constants"; +import { ContentType as IContentType, DraftField, Field, FieldGroup, FieldType } from '../models'; import { Uri, commands, window } from 'vscode'; import { Folders } from "../commands/Folders"; import { Questions } from "./Questions"; @@ -272,6 +272,121 @@ export class ContentType { ArticleHelper.update(editor!, content); } + /** + * Retrieve the field value + * @param data + * @param parents + * @returns + */ + public static getFieldValue(data: any, parents: string[]): string[] { + let fieldValue = []; + let crntPageData = data; + + for (let i = 0; i < parents.length; i++) { + const crntField = parents[i]; + + if (i === parents.length - 1) { + fieldValue = crntPageData[crntField]; + } else { + if (!crntPageData[crntField]) { + continue; + } + + crntPageData = crntPageData[crntField]; + } + } + + return fieldValue; + } + + /** + * Find the field by its type + * @param fields + * @param type + * @param parents + * @returns + */ + public static findFieldByType(fields: Field[], type: FieldType, parents: string[] = []) { + for (const field of fields) { + if (field.type === type) { + parents = [...parents, field.name]; + return parents; + } else if (field.type === "fields" && field.fields) { + const subFields = this.findPreviewField(field.fields); + if (subFields.length > 0) { + return [...parents, field.name, ...subFields]; + } + } + } + + return parents; + } + + /** + * Find the preview field in the fields + * @param ctFields + * @param parents + * @returns + */ + public static findPreviewField(ctFields: Field[], parents: string[] = []): string[] { + for (const field of ctFields) { + if (field.isPreviewImage && field.type === "image") { + parents = [...parents, field.name]; + return parents; + } else if (field.type === "fields" && field.fields) { + const subFields = this.findPreviewField(field.fields); + if (subFields.length > 0) { + return [...parents, field.name, ...subFields]; + } + } else if (field.type === "block") { + const subFields = this.findPreviewInBlockField(field); + if (subFields.length > 0) { + return [...parents, field.name, ...subFields]; + } + } + } + + return parents; + } + + /** + * Look for the preview image in the block field + * @param field + * @param parents + * @returns + */ + private static findPreviewInBlockField(field: Field) { + const groups = field.fieldGroup && Array.isArray(field.fieldGroup) ? field.fieldGroup : [field.fieldGroup]; + if (!groups) { + return []; + } + + const blocks = Settings.get(SETTING_TAXONOMY_FIELD_GROUPS); + if (!blocks) { + return []; + } + + let found = false; + for (const group of groups) { + const block = blocks.find(block => block.id === group); + if (!block) { + continue; + } + + let newParents: string[] = []; + if (!found) { + newParents = this.findPreviewField(block?.fields, []); + } + + if (newParents.length > 0) { + found = true; + return newParents; + } + } + + return []; + } + /** * Generate the fields from the data * @param data diff --git a/src/listeners/dashboard/PagesListener.ts b/src/listeners/dashboard/PagesListener.ts index 7e642746..f61fb634 100644 --- a/src/listeners/dashboard/PagesListener.ts +++ b/src/listeners/dashboard/PagesListener.ts @@ -5,7 +5,7 @@ import { basename, dirname, join } from "path"; import { commands, FileSystemWatcher, RelativePattern, TextDocument, Uri, workspace } from "vscode"; import { Dashboard } from "../../commands/Dashboard"; import { Folders } from "../../commands/Folders"; -import { COMMAND_NAME, DefaultFields, ExtensionState, SETTING_CONTENT_STATIC_FOLDER, SETTING_SEO_DESCRIPTION_FIELD, SETTING_TAXONOMY_FIELD_GROUPS } from "../../constants"; +import { COMMAND_NAME, DefaultFields, ExtensionState, SETTING_CONTENT_STATIC_FOLDER, SETTING_SEO_DESCRIPTION_FIELD } from "../../constants"; import { DashboardCommand } from "../../dashboardWebView/DashboardCommand"; import { DashboardMessage } from "../../dashboardWebView/DashboardMessage"; import { Page } from "../../dashboardWebView/models"; @@ -14,7 +14,6 @@ import { ContentType } from "../../helpers/ContentType"; import { DateHelper } from "../../helpers/DateHelper"; import { Notifications } from "../../helpers/Notifications"; import { BaseListener } from "./BaseListener"; -import { Field, FieldGroup, FieldType } from '../../models'; import { DataListener } from '../panel'; import Fuse from 'fuse.js'; @@ -298,7 +297,7 @@ export class PagesListener extends BaseListener { page.fmContentType = contentType.name; } - let previewFieldParents = this.findPreviewField(contentType.fields); + let previewFieldParents = ContentType.findPreviewField(contentType.fields); if (previewFieldParents.length === 0) { const previewField = contentType.fields.find(field => field.type === "image" && field.name === "preview"); if (previewField) { @@ -306,11 +305,11 @@ export class PagesListener extends BaseListener { } } - let tagParents = this.findFieldByType(contentType.fields, "tags"); - page.fmTags = this.getFieldValue(article.data, tagParents.length !== 0 ? tagParents : ["tags"]); + let tagParents = ContentType.findFieldByType(contentType.fields, "tags"); + page.fmTags = ContentType.getFieldValue(article.data, tagParents.length !== 0 ? tagParents : ["tags"]); - let categoryParents = this.findFieldByType(contentType.fields, "categories"); - page.fmCategories = this.getFieldValue(article.data, categoryParents.length !== 0 ? categoryParents : ["categories"]); + let categoryParents = ContentType.findFieldByType(contentType.fields, "categories"); + page.fmCategories = ContentType.getFieldValue(article.data, categoryParents.length !== 0 ? categoryParents : ["categories"]); // Check if parent fields were retrieved, if not there was no image present if (previewFieldParents.length > 0) { @@ -376,119 +375,4 @@ export class PagesListener extends BaseListener { return; } - - /** - * Retrieve the field value - * @param data - * @param parents - * @returns - */ - private static getFieldValue(data: any, parents: string[]): string[] { - let fieldValue = []; - let crntPageData = data; - - for (let i = 0; i < parents.length; i++) { - const crntField = parents[i]; - - if (i === parents.length - 1) { - fieldValue = crntPageData[crntField]; - } else { - if (!crntPageData[crntField]) { - continue; - } - - crntPageData = crntPageData[crntField]; - } - } - - return fieldValue; - } - - /** - * Find the field by its type - * @param fields - * @param type - * @param parents - * @returns - */ - private static findFieldByType(fields: Field[], type: FieldType, parents: string[] = []) { - for (const field of fields) { - if (field.type === type) { - parents = [...parents, field.name]; - return parents; - } else if (field.type === "fields" && field.fields) { - const subFields = this.findPreviewField(field.fields); - if (subFields.length > 0) { - return [...parents, field.name, ...subFields]; - } - } - } - - return parents; - } - - /** - * Find the preview field in the fields - * @param ctFields - * @param parents - * @returns - */ - private static findPreviewField(ctFields: Field[], parents: string[] = []): string[] { - for (const field of ctFields) { - if (field.isPreviewImage && field.type === "image") { - parents = [...parents, field.name]; - return parents; - } else if (field.type === "fields" && field.fields) { - const subFields = this.findPreviewField(field.fields); - if (subFields.length > 0) { - return [...parents, field.name, ...subFields]; - } - } else if (field.type === "block") { - const subFields = this.findPreviewInBlockField(field); - if (subFields.length > 0) { - return [...parents, field.name, ...subFields]; - } - } - } - - return parents; - } - - /** - * Look for the preview image in the block field - * @param field - * @param parents - * @returns - */ - private static findPreviewInBlockField(field: Field) { - const groups = field.fieldGroup && Array.isArray(field.fieldGroup) ? field.fieldGroup : [field.fieldGroup]; - if (!groups) { - return []; - } - - const blocks = Settings.get(SETTING_TAXONOMY_FIELD_GROUPS); - if (!blocks) { - return []; - } - - let found = false; - for (const group of groups) { - const block = blocks.find(block => block.id === group); - if (!block) { - continue; - } - - let newParents: string[] = []; - if (!found) { - newParents = this.findPreviewField(block?.fields, []); - } - - if (newParents.length > 0) { - found = true; - return newParents; - } - } - - return []; - } } \ No newline at end of file