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