mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-08-10 02:42:55 +02:00
#291 - Hierarchy field support
This commit is contained in:
@@ -30,6 +30,13 @@ export const Item: React.FunctionComponent<IItemProps> = ({ fmFilePath, date, ti
|
||||
}
|
||||
|
||||
const tagField = settings.dashboardState.contents.tags;
|
||||
|
||||
if (tagField === "tags") {
|
||||
return pageData.fmTags;
|
||||
} else if (tagField === "categories") {
|
||||
return pageData.fmCategories;
|
||||
}
|
||||
|
||||
const tagsValue = pageData[tagField] || [];
|
||||
|
||||
if (Array.isArray(tagsValue)) {
|
||||
|
||||
@@ -51,7 +51,7 @@ export const TaxonomyLookup: React.FunctionComponent<ITaxonomyLookupProps> = ({
|
||||
if (taxonomy === "tags" || taxonomy === "categories") {
|
||||
return (
|
||||
<button
|
||||
className={total ? `text-teal-900 hover:text-teal-600` : ``}
|
||||
className={total ? `text-teal-900 hover:text-teal-600 font-bold` : ``}
|
||||
title={total ? `Show contents with ${value} in ${taxonomy}` : ``}
|
||||
onClick={onNavigate}>
|
||||
{total || `-`}
|
||||
|
||||
@@ -298,6 +298,32 @@ export class ContentType {
|
||||
|
||||
return fieldValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the field value
|
||||
* @param data
|
||||
* @param parents
|
||||
* @returns
|
||||
*/
|
||||
public static setFieldValue(data: any, parents: string[], value: any) {
|
||||
let crntPageData = data;
|
||||
|
||||
for (let i = 0; i < parents.length; i++) {
|
||||
const crntField = parents[i];
|
||||
|
||||
if (i === parents.length - 1) {
|
||||
crntPageData[crntField] = value;
|
||||
} else {
|
||||
if (!crntPageData[crntField]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
crntPageData = crntPageData[crntField];
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the field by its type
|
||||
@@ -306,13 +332,13 @@ export class ContentType {
|
||||
* @param parents
|
||||
* @returns
|
||||
*/
|
||||
public static findFieldByType(fields: Field[], type: FieldType, parents: string[] = []) {
|
||||
public static findFieldByType(fields: Field[], type: FieldType, parents: string[] = []): 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);
|
||||
const subFields = this.findFieldByType(field.fields, type, parents);
|
||||
if (subFields.length > 0) {
|
||||
return [...parents, field.name, ...subFields];
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { getTaxonomyField } from './getTaxonomyField';
|
||||
import { EXTENSION_NAME, SETTING_TAXONOMY_CUSTOM } from "../constants";
|
||||
import { CustomTaxonomy, TaxonomyType } from "../models";
|
||||
import { CustomTaxonomy, TaxonomyType, ContentType as IContentType } from "../models";
|
||||
import { FilesHelper } from "./FilesHelper";
|
||||
import { ProgressLocation, window } from "vscode";
|
||||
import { parseWinPath } from "./parseWinPath";
|
||||
@@ -10,6 +10,7 @@ import { DumpOptions } from "js-yaml";
|
||||
import { Settings } from "./SettingsHelper";
|
||||
import { Notifications } from "./Notifications";
|
||||
import { ArticleHelper } from './ArticleHelper';
|
||||
import { ContentType } from './ContentType';
|
||||
|
||||
|
||||
export class TaxonomyHelper {
|
||||
@@ -191,19 +192,12 @@ export class TaxonomyHelper {
|
||||
try {
|
||||
const article = FrontMatterParser.fromFile(mdFile);
|
||||
const contentType = ArticleHelper.getContentType(article.data);
|
||||
let fieldName: string | undefined;
|
||||
|
||||
if (taxonomyName === "tags") {
|
||||
fieldName = contentType.fields.find(f => f.type === "tags")?.name || "tags";
|
||||
} else if (taxonomyName === "categories") {
|
||||
fieldName = contentType.fields.find(f => f.type === "categories")?.name || "categories";
|
||||
} else {
|
||||
fieldName = contentType.fields.find(f => f.type === "taxonomy" && f.taxonomyId === taxonomyName)?.name;
|
||||
}
|
||||
let fieldNames: string[] = this.getFieldsHierarchy(taxonomyType, contentType);
|
||||
|
||||
if (fieldName && article && article.data) {
|
||||
if (fieldNames.length > 0 && article && article.data) {
|
||||
const { data } = article;
|
||||
let taxonomies: string[] = data[fieldName];
|
||||
let taxonomies: string[] = ContentType.getFieldValue(data, fieldNames);
|
||||
|
||||
if (taxonomies && taxonomies.length > 0) {
|
||||
const idx = taxonomies.findIndex(o => o === oldValue);
|
||||
@@ -215,7 +209,8 @@ export class TaxonomyHelper {
|
||||
taxonomies = taxonomies.filter(o => o !== oldValue);
|
||||
}
|
||||
|
||||
data[fieldName] = [...new Set(taxonomies)].sort();
|
||||
const newTaxValue = [...new Set(taxonomies)].sort();
|
||||
ContentType.setFieldValue(data, fieldNames, newTaxValue);
|
||||
|
||||
const spaces = window.activeTextEditor?.options?.tabSize;
|
||||
// Update the file
|
||||
@@ -300,13 +295,13 @@ export class TaxonomyHelper {
|
||||
const article = FrontMatterParser.fromFile(mdFile);
|
||||
const contentType = ArticleHelper.getContentType(article.data);
|
||||
|
||||
let oldFieldName: string | undefined = getTaxonomyField(type, contentType);
|
||||
let newFieldName: string | undefined = getTaxonomyField(answer, contentType);
|
||||
let oldFieldNames: string[] = this.getFieldsHierarchy(oldType, contentType);
|
||||
let newFieldNames: string[] = this.getFieldsHierarchy(newType, contentType, true);
|
||||
|
||||
if (oldFieldName && newFieldName && article && article.data) {
|
||||
if (oldFieldNames.length > 0 && newFieldNames.length > 0 && article && article.data) {
|
||||
const { data } = article;
|
||||
let oldTaxonomies: string[] = data[oldFieldName];
|
||||
let newTaxonomies: string[] = data[newFieldName] || [];
|
||||
let oldTaxonomies: string[] = ContentType.getFieldValue(data, oldFieldNames) || [];
|
||||
let newTaxonomies: string[] = ContentType.getFieldValue(data, newFieldNames) || [];
|
||||
|
||||
if (oldTaxonomies && oldTaxonomies.length > 0) {
|
||||
const idx = oldTaxonomies.findIndex(o => o === value);
|
||||
@@ -314,7 +309,8 @@ export class TaxonomyHelper {
|
||||
if (idx !== -1) {
|
||||
newTaxonomies.push(value);
|
||||
|
||||
data[newFieldName] = [...new Set(newTaxonomies)].sort();
|
||||
const newTaxonomiesValues = [...new Set(newTaxonomies)].sort();
|
||||
ContentType.setFieldValue(data, newFieldNames, newTaxonomiesValues);
|
||||
|
||||
const spaces = window.activeTextEditor?.options?.tabSize;
|
||||
// Update the file
|
||||
@@ -338,6 +334,38 @@ export class TaxonomyHelper {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve the fields for the taxonomy field
|
||||
* @returns
|
||||
*/
|
||||
private static getFieldsHierarchy(taxonomyType: TaxonomyType | string, contentType: IContentType, fallback: boolean = false): string[] {
|
||||
let fieldNames: string[] = [];
|
||||
if (taxonomyType === TaxonomyType.Tag) {
|
||||
fieldNames = ContentType.findFieldByType(contentType.fields, "tags");
|
||||
} else if (taxonomyType === TaxonomyType.Category) {
|
||||
fieldNames = ContentType.findFieldByType(contentType.fields, "categories");
|
||||
} else {
|
||||
const taxFieldName = getTaxonomyField(taxonomyType, contentType);
|
||||
fieldNames = taxFieldName ? [taxFieldName] : [];
|
||||
}
|
||||
|
||||
if (fallback && fieldNames.length === 0) {
|
||||
let taxFieldName;
|
||||
if (taxonomyType === TaxonomyType.Tag) {
|
||||
taxFieldName = getTaxonomyField("tags", contentType);
|
||||
} else if (taxonomyType === TaxonomyType.Category) {
|
||||
taxFieldName = getTaxonomyField("categories", contentType);
|
||||
}
|
||||
|
||||
if (taxFieldName) {
|
||||
fieldNames = [taxFieldName];
|
||||
}
|
||||
}
|
||||
|
||||
return fieldNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the taxonomy value to the settings
|
||||
* @param taxonomyType
|
||||
|
||||
Reference in New Issue
Block a user