Skip to content

Commit

Permalink
#710 - Remove the fields from FM when the when clause is not met
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Nov 15, 2023
1 parent 32c6b4c commit 9ddaeda
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [#700](https://github.com/estruyf/vscode-front-matter/issues/700): Added the `{{pathToken.relPath}}` placeholder for the `previewPath` property
- [#706](https://github.com/estruyf/vscode-front-matter/issues/706): Show the error of scripts failing in the Front Matter output panel
- [#709](https://github.com/estruyf/vscode-front-matter/issues/709): Take "where clause" into account on content creation
- [#710](https://github.com/estruyf/vscode-front-matter/issues/710): Hide child field when parent field its "when clause" is not met, also remove the fields from the content

### ⚡️ Optimizations

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/ContentType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ export class ContentType {
const dateFormat = Settings.get(SETTING_DATE_FORMAT) as string;

for (const field of obj.fields) {
if (!fieldWhenClause(field, data)) {
if (!fieldWhenClause(field, data, obj.fields)) {
Logger.info(`Field ${field.name} not added because of when clause`);
continue;
}
Expand Down
25 changes: 24 additions & 1 deletion src/listeners/panel/DataListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Article, Preview } from '../../commands';
import { ParsedFrontMatter } from '../../parsers';
import { processKnownPlaceholders } from '../../helpers/PlaceholderHelper';
import { Field, PostMessageData } from '../../models';
import { encodeEmoji } from '../../utils';
import { encodeEmoji, fieldWhenClause } from '../../utils';
import { PanelProvider } from '../../panelWebView/PanelProvider';
import { MessageHandlerData } from '@estruyf/vscode';
import { SponsorAi } from '../../services/SponsorAI';
Expand Down Expand Up @@ -348,6 +348,29 @@ export class DataListener extends BaseListener {
}
}

// Verify if there are fields to be cleared due to the when clause
const allFieldNames = Object.keys(parentObj);
let ctFields = contentType.fields;
if (parents && parents.length > 0) {
for (const parent of parents) {
const crntField = ctFields.find((f) => f.name === parent);
if (crntField) {
ctFields = crntField.fields || [];
}
}
}
if (ctFields && ctFields.length > 0) {
for (const field of allFieldNames) {
const crntField = ctFields.find((f) => f.name === field);
if (crntField && crntField.when) {
const renderField = fieldWhenClause(crntField, parentObj, ctFields);
if (!renderField) {
delete parentObj[field];
}
}
}
}

// Clear the field if it is empty
if (
value === undefined ||
Expand Down
6 changes: 4 additions & 2 deletions src/panelWebView/components/Fields/WrapperField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Messenger } from '@estruyf/vscode/dist/client';
import * as React from 'react';
import { useCallback, useEffect, useState } from 'react';
import { DateHelper } from '../../../helpers/DateHelper';
import { BlockFieldData, CustomPanelViewResult, Field, PanelSettings, WhenOperator } from '../../../models';
import { BlockFieldData, CustomPanelViewResult, Field, PanelSettings } from '../../../models';
import { Command } from '../../Command';
import { CommandToCode } from '../../CommandToCode';
import { TagType } from '../../TagType';
Expand Down Expand Up @@ -36,6 +36,7 @@ import { LocalizationKey } from '../../../localization';

export interface IWrapperFieldProps {
field: Field;
allFields: Field[];
parent: IMetadata;
parentFields: string[];
metadata: IMetadata;
Expand All @@ -57,6 +58,7 @@ export interface IWrapperFieldProps {

export const WrapperField: React.FunctionComponent<IWrapperFieldProps> = ({
field,
allFields,
parent,
parentFields,
metadata,
Expand Down Expand Up @@ -158,7 +160,7 @@ export const WrapperField: React.FunctionComponent<IWrapperFieldProps> = ({

// Conditional fields
if (typeof field.when !== 'undefined') {
const shouldRender = fieldWhenClause(field, parent);
const shouldRender = fieldWhenClause(field, parent, allFields);

if (!shouldRender) {
return null;
Expand Down
1 change: 1 addition & 0 deletions src/panelWebView/components/Metadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const Metadata: React.FunctionComponent<IMetadataProps> = ({
<WrapperField
key={field.name}
field={field}
allFields={ctFields}
parent={parent}
parentFields={parentFields}
metadata={metadata}
Expand Down
10 changes: 9 additions & 1 deletion src/utils/fieldWhenClause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ import { IMetadata } from '../panelWebView/components/Metadata';
* @param parent - The parent metadata object.
* @returns A boolean indicating whether the field should be displayed.
*/
export const fieldWhenClause = (field: Field, parent: IMetadata): boolean => {
export const fieldWhenClause = (field: Field, parent: IMetadata, allFields?: Field[]): boolean => {
const when = field.when;
if (!when) {
return true;
}

let parentField = allFields?.find((f) => f.name === when.fieldRef);
if (parentField && parentField.when) {
const renderParent = fieldWhenClause(parentField, parent, allFields);
if (!renderParent) {
return false;
}
}

let whenValue = parent[when.fieldRef];
if (when.caseSensitive || typeof when.caseSensitive === 'undefined') {
return caseSensitive(when, field, whenValue);
Expand Down

0 comments on commit 9ddaeda

Please sign in to comment.