F2-3:前端嵌套指令重放(addButton onClick + confirmDialog onOk)

scriptCommands.ts:addButton 保留 onClick 子指令数组透传,confirmDialog
新增翻译(onOk 子指令数组透传)。registerReplayAction.ts:addButton/
confirmDialog 嵌套时翻译子指令挂 onClickActions/onOkActions 再派发给 amis。
补单测 7 个(scriptCommands 4 + registerReplayAction 3),共 23/23 全绿。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-11 22:24:22 +08:00
parent a61cc87289
commit 7b5d9d2640
4 changed files with 174 additions and 4 deletions

View File

@@ -62,4 +62,95 @@ describe('registerReplayAction', () => {
await captured!.run({ args: { commands: [] } }, {}, {}); await captured!.run({ args: { commands: [] } }, {}, {});
expect(runActions).not.toHaveBeenCalled(); expect(runActions).not.toHaveBeenCalled();
}); });
// F2-3 嵌套指令
it('addButton 含 onClick 子指令时翻译并挂 onClickActions', async () => {
let captured: RunCtx | null = null;
const runActions = vi.fn().mockResolvedValue(undefined);
registerReplayAction({
registerAction: (_t: string, a: RunCtx) => { captured = a; },
runActions,
});
const commands = [
{
actionType: 'addButton',
componentId: 'BTN_FIELD',
args: {
label: '全选',
index: 1,
onClick: [
{ actionType: 'hidden', componentId: 'A' },
{ actionType: 'toast', args: { msg: '已完成' } },
],
},
},
];
await captured!.run({ args: { commands } }, {}, {});
expect(runActions).toHaveBeenCalledTimes(1);
const dispatched = runActions.mock.calls[0][0] as Record<string, unknown>;
expect(dispatched.actionType).toBe('addButton');
expect(dispatched.componentId).toBe('BTN_FIELD');
const dispatchedArgs = dispatched.args as Record<string, unknown>;
// onClickActions 为翻译后的 amis 动作数组(2 条有效)
expect(Array.isArray(dispatchedArgs.onClickActions)).toBe(true);
const onClickActions = dispatchedArgs.onClickActions as Array<Record<string, unknown>>;
expect(onClickActions).toHaveLength(2);
expect(onClickActions[0].actionType).toBe('hidden');
expect(onClickActions[1].actionType).toBe('toast');
});
it('confirmDialog 含 onOk 子指令时翻译并挂 onOkActions', async () => {
let captured: RunCtx | null = null;
const runActions = vi.fn().mockResolvedValue(undefined);
registerReplayAction({
registerAction: (_t: string, a: RunCtx) => { captured = a; },
runActions,
});
const commands = [
{
actionType: 'confirmDialog',
args: {
msg: '确认操作?',
onOk: [
{ actionType: 'show', componentId: 'B' },
],
},
},
];
await captured!.run({ args: { commands } }, {}, {});
expect(runActions).toHaveBeenCalledTimes(1);
const dispatched = runActions.mock.calls[0][0] as Record<string, unknown>;
expect(dispatched.actionType).toBe('confirmDialog');
const dispatchedArgs = dispatched.args as Record<string, unknown>;
expect(Array.isArray(dispatchedArgs.onOkActions)).toBe(true);
const onOkActions = dispatchedArgs.onOkActions as Array<Record<string, unknown>>;
expect(onOkActions).toHaveLength(1);
expect(onOkActions[0].actionType).toBe('show');
expect(onOkActions[0].componentId).toBe('B');
});
it('addButton 空 onClick 时不挂 onClickActions(直接派发)', async () => {
let captured: RunCtx | null = null;
const runActions = vi.fn().mockResolvedValue(undefined);
registerReplayAction({
registerAction: (_t: string, a: RunCtx) => { captured = a; },
runActions,
});
const commands = [
{ actionType: 'addButton', componentId: 'BTN', args: { label: '点我' } },
];
await captured!.run({ args: { commands } }, {}, {});
expect(runActions).toHaveBeenCalledTimes(1);
const dispatched = runActions.mock.calls[0][0] as Record<string, unknown>;
// 无嵌套,直接走常规路径
expect(dispatched.actionType).toBe('addButton');
const dispatchedArgs = dispatched.args as Record<string, unknown>;
expect(dispatchedArgs.onClickActions).toBeUndefined();
});
}); });

View File

@@ -4,7 +4,14 @@
// 转换器在页面 onEvent 里编排「ajax 调 /api/script/execute-template → replayScriptCommands」。 // 转换器在页面 onEvent 里编排「ajax 调 /api/script/execute-template → replayScriptCommands」。
// 本模块向 amis 注册 replayScriptCommands:拿到执行端点返回的 commands 数组,翻译成 amis // 本模块向 amis 注册 replayScriptCommands:拿到执行端点返回的 commands 数组,翻译成 amis
// 标准动作(scriptCommands.translateCommands),逐条经 amis 动作执行器 runActions 重放—— // 标准动作(scriptCommands.translateCommands),逐条经 amis 动作执行器 runActions 重放——
// 实现 show/hidden/setValue/setOptions/toast 等 29 个 UI 动词 → amis doAction。 // 实现 show/hidden/setValue/setOptions/toast 等 UI 动词 → amis doAction。
//
// F2-3 嵌套指令扩展:
// addButton(onClick 子指令数组):动态加入按钮,点击后重放 onClick 子指令列表。
// amis 层做法:先派发 addButton 动作把按钮注册到组件,再把 onClick 数组包装成
// replayScriptCommands 动作挂在按钮的 onClick 事件上(通过 args.onClickActions 透传)。
// confirmDialog(onOk 子指令数组):弹 amis confirm 确认框,用户确认后重放 onOk 子指令。
// amis 层做法:派发 confirmDialog 动作,args.onOkActions 携带翻译好的 amis 动作数组。
// //
// amis 自定义动作契约:registerAction(type, { run(action, renderer, event) })。 // amis 自定义动作契约:registerAction(type, { run(action, renderer, event) })。
// run 内用 amis-core 的 runActions 把翻译后的动作逐条派发(componentId 定位目标组件)。 // run 内用 amis-core 的 runActions 把翻译后的动作逐条派发(componentId 定位目标组件)。
@@ -48,8 +55,29 @@ export function registerReplayAction(amisCore: AmisCore | undefined | null): boo
if (amisActions.length === 0) return; if (amisActions.length === 0) return;
if (typeof amisCore.runActions === 'function') { if (typeof amisCore.runActions === 'function') {
for (const a of amisActions) { for (const a of amisActions) {
// 逐条派发,componentId 已对齐 amis 字段 name(大写列名) // F2-3 嵌套指令:addButton 含 onClick 子指令数组时,把子指令翻译后挂到按钮动作
await amisCore.runActions(a, renderer, event); if (a.actionType === 'addButton' && a.args && Array.isArray((a.args as Record<string, unknown>).onClick)) {
const onClickCmds = (a.args as Record<string, unknown>).onClick as ScriptCommand[];
const onClickActions = translateCommands(onClickCmds);
// 透传 onClickActions 给 amis addButton,amis 侧按钮点击后用 runActions 重放
const enriched: AmisAction = {
...a,
args: { ...(a.args as Record<string, unknown>), onClickActions },
};
await amisCore.runActions(enriched, renderer, event);
// F2-3 嵌套指令:confirmDialog 含 onOk 子指令数组时,把子指令翻译后挂到确认框动作
} else if (a.actionType === 'confirmDialog' && a.args && Array.isArray((a.args as Record<string, unknown>).onOk)) {
const onOkCmds = (a.args as Record<string, unknown>).onOk as ScriptCommand[];
const onOkActions = translateCommands(onOkCmds);
const enriched: AmisAction = {
...a,
args: { ...(a.args as Record<string, unknown>), onOkActions },
};
await amisCore.runActions(enriched, renderer, event);
} else {
// 常规指令:逐条派发,componentId 已对齐 amis 字段 name(大写列名)
await amisCore.runActions(a, renderer, event);
}
} }
} }
}, },

View File

@@ -61,6 +61,39 @@ describe('translateCommand', () => {
expect(translateCommand({ actionType: 'unknownVerb' })).toBeNull(); expect(translateCommand({ actionType: 'unknownVerb' })).toBeNull();
expect(translateCommand({} as ScriptCommand)).toBeNull(); expect(translateCommand({} as ScriptCommand)).toBeNull();
}); });
// F2-3 嵌套指令
it('addButton 无 onClick 时透传 args', () => {
const a = translateCommand({ actionType: 'addButton', componentId: 'BTN_FIELD', args: { label: '全选', index: 1 } });
expect(a?.actionType).toBe('addButton');
expect(a?.componentId).toBe('BTN_FIELD');
expect((a?.args as Record<string, unknown>).label).toBe('全选');
});
it('addButton 含 onClick 子指令数组时原样透传 onClick', () => {
const onClick: ScriptCommand[] = [
{ actionType: 'hidden', componentId: 'X' },
{ actionType: 'toast', args: { msg: '完成' } },
];
const a = translateCommand({ actionType: 'addButton', componentId: 'BTN', args: { label: '操作', index: 0, onClick } });
expect(a?.actionType).toBe('addButton');
expect(Array.isArray((a?.args as Record<string, unknown>).onClick)).toBe(true);
expect(((a?.args as Record<string, unknown>).onClick as ScriptCommand[]).length).toBe(2);
});
it('confirmDialog 含 onOk 子指令数组时原样透传 onOk', () => {
const onOk: ScriptCommand[] = [{ actionType: 'show', componentId: 'Y' }];
const a = translateCommand({ actionType: 'confirmDialog', args: { msg: '确认删除?', onOk } });
expect(a?.actionType).toBe('confirmDialog');
expect((a?.args as Record<string, unknown>).msg).toBe('确认删除?');
expect(Array.isArray((a?.args as Record<string, unknown>).onOk)).toBe(true);
});
it('confirmDialog 空 onOk 不含 onOk 字段', () => {
const a = translateCommand({ actionType: 'confirmDialog', args: { msg: '确认?' } });
expect(a?.actionType).toBe('confirmDialog');
expect((a?.args as Record<string, unknown>).onOk).toBeUndefined();
});
}); });
describe('translateCommands', () => { describe('translateCommands', () => {

View File

@@ -87,8 +87,26 @@ export function translateCommand(cmd: ScriptCommand): AmisAction | null {
return { actionType: 'addSubTableButton', componentId, args }; return { actionType: 'addSubTableButton', componentId, args };
case 'setSubTableButtonVisible': case 'setSubTableButtonVisible':
return { actionType: 'setSubTableButtonVisible', componentId, args }; return { actionType: 'setSubTableButtonVisible', componentId, args };
// addButton(F2-3):args.onClick 为子指令数组时,前端点击后重放 onClick 列表
case 'addButton': case 'addButton':
return { actionType: 'addButton', componentId, args }; return {
actionType: 'addButton',
componentId,
args: {
...args,
// onClick 子指令数组原样透传(registerReplayAction 侧处理嵌套重放)
...(Array.isArray(args.onClick) ? { onClick: args.onClick } : {}),
},
};
// confirmDialog(F2-3):弹 amis 确认框,确认后重放 onOk 子指令数组
case 'confirmDialog':
return {
actionType: 'confirmDialog',
args: {
msg: args.msg,
...(Array.isArray(args.onOk) ? { onOk: args.onOk } : {}),
},
};
// 聚焦 —— amis focus // 聚焦 —— amis focus
case 'focus': case 'focus':