feat: update extractTemplateElements to return helpers and refactor usage in VariablesProvider

This commit is contained in:
Sheldon Guo 2025-03-13 07:16:31 +08:00
parent 21d45bf59a
commit 967724929c
2 changed files with 8 additions and 4 deletions

View File

@ -277,7 +277,7 @@ const VariablesProvider = ({ children, filterVariables }: any) => {
fieldOperator?: string | void; fieldOperator?: string | void;
}, },
) => { ) => {
const { fullVariable, filters } = extractTemplateElements(str); const { fullVariable, helpers } = extractTemplateElements(str);
if (!fullVariable) { if (!fullVariable) {
return str; return str;
} }
@ -288,8 +288,8 @@ const VariablesProvider = ({ children, filterVariables }: any) => {
const path = getPath(str); const path = getPath(str);
const result = await getResult(path, localVariables as VariableOption[], options); const result = await getResult(path, localVariables as VariableOption[], options);
if (Array.isArray(filters) && filters.length > 0) { if (Array.isArray(helpers) && helpers.length > 0) {
result.value = filters.reduce((acc, filter) => filter.handler(...[acc, ...filter.args]), result.value); result.value = helpers.reduce((acc, helper) => helper.handler(...[acc, ...helper.args]), result.value);
} }
return { return {

View File

@ -28,7 +28,11 @@ export function extractTemplateVariable(template: string): string | null {
} }
} }
export function extractTemplateElements(template: string) { export function extractTemplateElements(template: string): {
fullVariable: string | null;
variableSegments: string[];
helpers: Filter[];
} {
const escapedTemplate = escape(template ?? ''); const escapedTemplate = escape(template ?? '');
try { try {
const fullVariable = engine.fullVariablesSync(escapedTemplate)[0] ?? ''; const fullVariable = engine.fullVariablesSync(escapedTemplate)[0] ?? '';