Skip to content

Commit ba3d483

Browse files
committed
chore(engine, render): 🤖 update $CTX define
affects: engine, render
1 parent 9b30922 commit ba3d483

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

packages/engine/src/component/CustomSchemaForm/components/Setters/FunctionSetter/defaultDts.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,29 @@ type ContextType = {
4141
getGlobalState?: () => Record<any, any>;
4242
/** 更新全局状态 */
4343
updateGlobalState?: (newState: any) => void;
44-
/** 存储当前节点的数据,不具有响应性 **/
44+
/** 存储当前节点的数据,不具有响应性, 可能不是最新的值 **/
4545
staticVar?: Record<string | number, any>;
46+
/** 获取当前节点的静态变量,不具有响应性 */
4647
getStaticVar?: () => Record<string, any>;
47-
getStaticVarById?: (nodeId: string) => Record<string, any>;
48+
getStaticVarById?: (/** 节点 id */ nodeId: string) => Record<string, any>;
4849
methods?: Record<string, (...arg: any) => any>;
50+
/** 获取当前节点的方法,不具有响应性 */
4951
getMethods?: () => Record<string, (...arg: any) => any>;
50-
getMethodsById?: (nodeId: string) => Record<string, (...arg: any) => any>;
52+
getMethodsById?: (/** 节点 id */ nodeId: string) => Record<string, (...arg: any) => any>;
5153
/** 当前节点状态 **/
5254
state?: Record<any, any>;
5355
/** 更新当前节点状态 */
5456
updateState?: (newState: any) => void;
57+
/** 获取当前节点状态最新 */
5558
getState?: () => Record<any, any>;
56-
getStateById?: (nodeId: string) => Record<any, any>;
57-
updateStateById?: (nodeId: string, newState: Record<any, any>) => void;
59+
getStateById?: (/** 节点 id */ nodeId: string) => Record<any, any>;
60+
updateStateById?: (/** 节点 id */ nodeId: string, newState: Record<any, any>) => void;
61+
/** 一次性返回最新的 state 以及更新的方法 */
5862
getStateObj?: () => {
5963
state?: Record<any, any>;
6064
updateState?: (newState: any) => void;
6165
};
62-
getStateObjById?: (nodeId: string) => {
66+
getStateObjById?: (/** 节点 id */ nodeId: string) => {
6367
state?: Record<any, any>;
6468
updateState?: (newState: any) => void;
6569
};
@@ -73,14 +77,20 @@ type ContextType = {
7377
>;
7478
/** 循环数据 */
7579
loopData?: Record<any, any>;
76-
/** 组件节点的 Ref */
77-
nodeRefs?: { get: (nodeId: string) => any };
80+
/** 组件节点的 Ref, 可以通过 current 直接调用节点提供的方法,需要判断是否在存在 */
81+
nodeRefs?: { get: (/** 节点 id */ nodeId: string) => { current?: any } };
82+
/** 运行时全局的 store 管理 */
7883
storeManager?: StoreManager;
7984
/** 第三方辅助库 */
8085
thirdLibs?: Record<string, any>;
8186
};
8287

8388
/** 渲染上下文 */
8489
declare global {
90+
/** 运行时上下文
91+
* @deprecated
92+
*/
8593
const $$context: ContextType;
94+
/** 运行时上下文 */
95+
const $CTX: ContextType;
8696
}

packages/engine/src/component/CustomSchemaForm/components/Setters/FunctionSetter/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export const FunctionSetter: CSetter<any> = ({
6464
onDidMount={(editor) => {
6565
editorRef.current = editor;
6666
}}
67-
initialValue={props.value?.value ?? (initialValue || '')}
67+
initialValue={props.value?.value ?? (initialValue || 'function run() {\n}')}
6868
language={'javascript'}
6969
options={{
7070
automaticLayout: true,

packages/render/src/util/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const runExpression = (expStr: string, context: any) => {
5151
return `const ${key} = $$context['${key}'];`;
5252
});
5353
const executeCode = `
54+
$CTX = $$context;
5455
${contextVar.join('\n')}
5556
return ${expStr};
5657
`;
@@ -100,6 +101,7 @@ export const convertCodeStringToFunction = (params: {
100101
var extraParams = $$$__args__$$$.pop();
101102
var __$$storeManager__ = extraParams.storeManager;
102103
var $$context = extraParams.$$context;
104+
var $CTX = extraParams.$$context;
103105
var $$response = extraParams.$$response;
104106
var $$actionVariableSpace = extraParams.actionVariableSpace;
105107
$$context.stateManager = __$$storeManager__.getStateSnapshot();

0 commit comments

Comments
 (0)