mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-07-03 00:11:36 +02:00
#362 - additional operators
This commit is contained in:
@@ -92,18 +92,19 @@ export enum WhenOperator {
|
||||
notEquals = "neq",
|
||||
contains = "contains",
|
||||
notContains = "notContains",
|
||||
startsWith = "startsWith",
|
||||
endsWith = "endsWith",
|
||||
greaterThan = "gt",
|
||||
greaterThanOrEqual = "gte",
|
||||
lessThan = "lt",
|
||||
lessThanOrEqual = "lte",
|
||||
startsWith = "startsWith",
|
||||
endsWith = "endsWith",
|
||||
}
|
||||
|
||||
export interface WhenClause {
|
||||
fieldRef: string;
|
||||
operator: WhenOperator;
|
||||
value: any;
|
||||
caseSensitive?: boolean;
|
||||
}
|
||||
|
||||
export interface DateInfo {
|
||||
|
||||
@@ -14,6 +14,7 @@ import { JsonField } from '../JsonField';
|
||||
import { IMetadata } from '../Metadata';
|
||||
import { TagPicker } from '../TagPicker';
|
||||
import { ChoiceField, DataFileField, DateTimeField, DraftField, FieldTitle, FileField, ListField, Toggle, TextField, SlugField, PreviewImageField, PreviewImageValue, NumberField } from '.';
|
||||
import { fieldWhenClause } from '../../../utils/fieldWhenClause';
|
||||
|
||||
export interface IWrapperFieldProps {
|
||||
field: Field;
|
||||
@@ -111,22 +112,10 @@ export const WrapperField: React.FunctionComponent<IWrapperFieldProps> = ({
|
||||
|
||||
// Conditional fields
|
||||
if (typeof field.when !== "undefined") {
|
||||
debugger
|
||||
const when = field.when;
|
||||
const whenValue = parent[when.fieldRef];
|
||||
const shouldRender = fieldWhenClause(field, parent);
|
||||
|
||||
if (field.when.operator === WhenOperator.equals) {
|
||||
if (whenValue !== when.value) {
|
||||
return null;
|
||||
}
|
||||
} else if (field.when.operator === WhenOperator.notEquals) {
|
||||
if (whenValue === when.value) {
|
||||
return null;
|
||||
}
|
||||
} else if (field.when.operator === WhenOperator.contains) {
|
||||
if ((typeof whenValue === "string" || whenValue instanceof Array) && !whenValue.includes(when.value)) {
|
||||
return null;
|
||||
}
|
||||
if (!shouldRender) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import { Field, WhenOperator } from "../models";
|
||||
import { IMetadata } from "../panelWebView/components/Metadata";
|
||||
|
||||
|
||||
export const fieldWhenClause = (field: Field, parent: IMetadata): boolean => {
|
||||
const when = field.when;
|
||||
if (!when) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const whenValue = parent[when.fieldRef];
|
||||
|
||||
switch (when.operator) {
|
||||
case WhenOperator.equals:
|
||||
if (whenValue !== when.value) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case WhenOperator.notEquals:
|
||||
if (whenValue === when.value) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case WhenOperator.contains:
|
||||
if ((typeof whenValue === "string" || whenValue instanceof Array) && !whenValue.includes(when.value)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case WhenOperator.notContains:
|
||||
if ((typeof whenValue === "string" || whenValue instanceof Array) && whenValue.includes(when.value)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case WhenOperator.startsWith:
|
||||
if (typeof whenValue === "string" && !whenValue.startsWith(when.value)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case WhenOperator.endsWith:
|
||||
if (typeof whenValue === "string" && !whenValue.endsWith(when.value)) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case WhenOperator.greaterThan:
|
||||
if (typeof whenValue === "number" && whenValue <= when.value) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case WhenOperator.greaterThanOrEqual:
|
||||
if (typeof whenValue === "number" && whenValue < when.value) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case WhenOperator.lessThan:
|
||||
if (typeof whenValue === "number" && whenValue >= when.value) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case WhenOperator.lessThanOrEqual:
|
||||
if (typeof whenValue === "number" && whenValue > when.value) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
export * from './copyFileAsync';
|
||||
export * from './existsAsync';
|
||||
export * from './fieldWhenClause';
|
||||
export * from './mkdirAsync';
|
||||
export * from './readFileAsync';
|
||||
export * from './readdirAsync';
|
||||
|
||||
Reference in New Issue
Block a user