mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-07 14:39:25 +08:00
refactor: parse variables
This commit is contained in:
parent
a046e2a955
commit
431b16cdad
@ -100,6 +100,7 @@ export class Application {
|
||||
public dataSourceManager: DataSourceManager;
|
||||
public name: string;
|
||||
public globalVars: Record<string, any> = {};
|
||||
public globalVarsCtx: Record<string, any> = {};
|
||||
|
||||
loading = true;
|
||||
maintained = false;
|
||||
@ -484,6 +485,9 @@ export class Application {
|
||||
addGlobalVar(key: string, value: any) {
|
||||
set(this.globalVars, key, value);
|
||||
}
|
||||
setGlobalVarCtx(key: string, value: any) {
|
||||
set(this.globalVarsCtx, key, value);
|
||||
}
|
||||
|
||||
getGlobalVar(key) {
|
||||
return get(this.globalVars, key);
|
||||
@ -491,4 +495,7 @@ export class Application {
|
||||
getGlobalVars() {
|
||||
return this.globalVars;
|
||||
}
|
||||
getGlobalVarsCtx() {
|
||||
return this.globalVarsCtx;
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@
|
||||
import { isFunction } from 'lodash';
|
||||
import { useMemo } from 'react';
|
||||
import { useApp } from './';
|
||||
import { VariableOption } from '../../variables/types';
|
||||
|
||||
export const useGlobalVariable = (key: string) => {
|
||||
const app = useApp();
|
||||
@ -55,3 +56,19 @@ export const useGlobalVariables = () => {
|
||||
|
||||
return result as any;
|
||||
};
|
||||
|
||||
//获取全局变量的值
|
||||
export const useGlobalVariablesCtx = () => {
|
||||
const app = useApp();
|
||||
const variablesCtx = app.getGlobalVarsCtx();
|
||||
const uniqueValues = new Set();
|
||||
Object.entries(variablesCtx).forEach(([key, value]) => {
|
||||
if (!value) return;
|
||||
uniqueValues.add({
|
||||
name: key,
|
||||
ctx: value,
|
||||
});
|
||||
});
|
||||
|
||||
return [...uniqueValues] as VariableOption[];
|
||||
};
|
||||
|
@ -223,15 +223,18 @@ const VariablesProvider = ({ children, filterVariables }: any) => {
|
||||
[setCtx],
|
||||
);
|
||||
|
||||
const getVariable = useCallback((variableName: string): VariableOption => {
|
||||
if (!ctxRef.current[variableName]) {
|
||||
return null;
|
||||
}
|
||||
const getVariable = useCallback(
|
||||
(variableName: string): VariableOption => {
|
||||
if (!ctxRef.current[variableName] && !builtinVariables.find((v) => v.name === variableName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
...variablesStore[variableName],
|
||||
};
|
||||
}, []);
|
||||
return {
|
||||
...variablesStore[variableName],
|
||||
};
|
||||
},
|
||||
[builtinVariables],
|
||||
);
|
||||
|
||||
const removeVariable = useCallback(
|
||||
(variableName: string) => {
|
||||
@ -338,7 +341,6 @@ const VariablesProvider = ({ children, filterVariables }: any) => {
|
||||
}) as VariablesContextType,
|
||||
[getCollectionField, getVariable, parseVariable, registerVariable, removeVariable, setCtx],
|
||||
);
|
||||
|
||||
return <VariablesContext.Provider value={value}>{children}</VariablesContext.Provider>;
|
||||
};
|
||||
|
||||
|
@ -15,6 +15,7 @@ import { useAPITokenVariable } from '../../schema-settings/VariableInput/hooks/u
|
||||
import { useCurrentRoleVariable } from '../../schema-settings/VariableInput/hooks/useRoleVariable';
|
||||
import { useURLSearchParamsVariable } from '../../schema-settings/VariableInput/hooks/useURLSearchParamsVariable';
|
||||
import { VariableOption } from '../types';
|
||||
import { useGlobalVariablesCtx } from '../../application/hooks/useGlobalVariable';
|
||||
|
||||
/**
|
||||
* 相当于全局的变量
|
||||
@ -26,6 +27,7 @@ const useBuiltInVariables = () => {
|
||||
const { apiTokenCtx } = useAPITokenVariable();
|
||||
const { datetimeCtx } = useDatetimeVariable();
|
||||
const { urlSearchParamsCtx, name: urlSearchParamsName, defaultValue } = useURLSearchParamsVariable();
|
||||
const globalVariablesCtx = useGlobalVariablesCtx();
|
||||
const builtinVariables: VariableOption[] = useMemo(() => {
|
||||
return [
|
||||
{
|
||||
@ -88,9 +90,17 @@ const useBuiltInVariables = () => {
|
||||
ctx: urlSearchParamsCtx,
|
||||
defaultValue,
|
||||
},
|
||||
...globalVariablesCtx,
|
||||
];
|
||||
}, [currentRoleCtx, currentUserCtx, datetimeCtx, defaultValue, urlSearchParamsCtx, urlSearchParamsName]);
|
||||
|
||||
}, [
|
||||
currentRoleCtx,
|
||||
currentUserCtx,
|
||||
datetimeCtx,
|
||||
defaultValue,
|
||||
urlSearchParamsCtx,
|
||||
urlSearchParamsName,
|
||||
globalVariablesCtx,
|
||||
]);
|
||||
return { builtinVariables };
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user