From 79eb8e208755d9cf4baeefb36745ee64cb05edeb Mon Sep 17 00:00:00 2001 From: zhengwr Date: Sun, 14 Jun 2026 14:58:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(converter):=20onEvent=20=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E6=8C=82=E5=9B=9E=E6=94=B9=E7=94=B1=20XML=20=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E9=A9=B1=E5=8A=A8=EF=BC=8C=E8=A6=86=E7=9B=96?= =?UTF-8?q?=20NO=5FS3+=E6=8C=89=E9=92=AE=E8=81=94=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WS-B Phase1-3:把老模板事件脚本按事件类型正确挂回 amis onEvent。 Phase1 解析: - scriptKeyToSeq(Element):" + + "" + + ""; + org.dom4j.Element root = org.dom4j.DocumentHelper.parseText(xml).getRootElement(); + Map m = converter.scriptKeyToSeq(root); + assertEquals(1, m.get("K1")); + assertEquals(2, m.get("K2")); } + /** Phase 1.1:无 段返回空 map(不抛异常)。 */ + @Test + void scriptKeyToSeq_无scripts段返回空() throws Exception { + org.dom4j.Element root = org.dom4j.DocumentHelper.parseText("").getRootElement(); + assertTrue(converter.scriptKeyToSeq(root).isEmpty()); + } + + /** Phase 1.2:递归收集 onselectedscript / onloadscript 事件绑定。 */ + @Test + void extractEventBindings_收集onselected与onload() throws Exception { + String xml = "" + + "" + + "
"; + org.dom4j.Element root = org.dom4j.DocumentHelper.parseText(xml).getRootElement(); + List bs = converter.extractEventBindings(root); + List sig = new ArrayList<>(); + for (TemplateToAmisConverter.EventBinding b : bs) { + sig.add(b.eventType + ":" + b.scriptKey); + } + assertEquals(2, sig.size()); + assertTrue(sig.contains("onselectedscript:K1")); + assertTrue(sig.contains("onloadscript:K2")); + } + + /** Phase 1.2:无事件属性返回空列表。 */ + @Test + void extractEventBindings_无事件属性返回空() throws Exception { + org.dom4j.Element root = org.dom4j.DocumentHelper.parseText( + "").getRootElement(); + assertTrue(converter.extractEventBindings(root).isEmpty()); + } + + /** Phase 2.1:scriptActionChain 支持自定义 item 表达式。 */ + @Test + void scriptActionChain_自定义item表达式() throws Exception { + // 经 attachScriptOnEvent 间接验证选中链的 item 表达式 + String xml = ""; + Map schema = converter.convertGraded(xml, new ArrayList<>(), noPage).getSchema(); + var bindings = List.of(new TemplateToAmisConverter.EventBinding("onselectedscript", "K1")); + converter.attachScriptOnEvent(schema, "tid", bindings, Map.of("K1", 1)); + Map onEvent = (Map) body(schema).get("onEvent"); + Map selected = (Map) onEvent.get("selectedChange"); + List actions = (List) selected.get("actions"); + Map ajax = (Map) actions.get(0); + Map data = (Map) ((Map) ajax.get("api")).get("data"); + assertEquals("${event.data.selectedItems[0]}", data.get("item")); + } + + /** Phase 2.2:onselected→selectedChange(item=选中行) / onload→inited(item=${&})。 */ + @Test + void attachScriptOnEvent_onselected挂selectedChange_onload挂inited() { + String xml = ""; + Map schema = converter.convertGraded(xml, new ArrayList<>(), noPage).getSchema(); + var bindings = List.of( + new TemplateToAmisConverter.EventBinding("onselectedscript", "K1"), + new TemplateToAmisConverter.EventBinding("onloadscript", "K2")); + converter.attachScriptOnEvent(schema, "tid", bindings, Map.of("K1", 1, "K2", 2)); + + Map onEvent = (Map) body(schema).get("onEvent"); + assertNotNull(onEvent); + assertTrue(onEvent.containsKey("selectedChange")); + assertTrue(onEvent.containsKey("inited")); + // 旧语义(change/把每段同挂 inited+change)已作废 + assertFalse(onEvent.containsKey("change")); + + // selectedChange 链:seq=1 + item=选中行 + Map selected = (Map) onEvent.get("selectedChange"); + List selActions = (List) selected.get("actions"); + assertEquals(2, selActions.size()); // ajax + 重放 + Map selData = (Map) + ((Map) ((Map) selActions.get(0)).get("api")).get("data"); + assertEquals(1, selData.get("seq")); + assertEquals("${event.data.selectedItems[0]}", selData.get("item")); + + // inited 链:seq=2 + item=${&} + Map inited = (Map) onEvent.get("inited"); + List initActions = (List) inited.get("actions"); + Map initData = (Map) + ((Map) ((Map) initActions.get(0)).get("api")).get("data"); + assertEquals(2, initData.get("seq")); + assertEquals("${&}", initData.get("item")); + } + + /** Phase 2.2:动作链调执行端点并重放指令(含 templateId)。 */ @Test void attachScriptOnEvent_动作链调执行端点并重放指令() { String xml = ""; Map schema = converter.convertGraded(xml, new ArrayList<>(), noPage).getSchema(); - converter.attachScriptOnEvent(schema, "tid-x", List.of(1)); + var bindings = List.of(new TemplateToAmisConverter.EventBinding("onloadscript", "K1")); + converter.attachScriptOnEvent(schema, "tid-x", bindings, Map.of("K1", 1)); Map onEvent = (Map) body(schema).get("onEvent"); Map inited = (Map) onEvent.get("inited"); @@ -1002,7 +1086,6 @@ class TemplateToAmisConverterTest { Map data = (Map) api.get("data"); assertEquals("tid-x", data.get("templateId")); assertEquals(1, data.get("seq")); - assertEquals("${&}", data.get("item")); // 全量表单数据作脚本 item Map replay = (Map) actions.get(1); assertEquals("replayScriptCommands", replay.get("actionType")); @@ -1010,28 +1093,73 @@ class TemplateToAmisConverterTest { assertTrue(((String) args.get("commands")).contains("commands")); } + /** Phase 2.2:NO_S3 脚本(不在任何 full_auto_index)也挂——只要事件绑定引用了存在的 key。 */ @Test - void attachScriptOnEvent_多段脚本动作链累加() { + void attachScriptOnEvent_NO_S3脚本也挂() { + // 模拟 ddb75614:seq=1 NO_S3 按钮联动(onselected)、seq=2 FULL_AUTO 只读(onload) String xml = ""; Map schema = converter.convertGraded(xml, new ArrayList<>(), noPage).getSchema(); - converter.attachScriptOnEvent(schema, "tid-x", List.of(1, 2, 3)); + var bindings = List.of( + new TemplateToAmisConverter.EventBinding("onselectedscript", "K_NO_S3"), + new TemplateToAmisConverter.EventBinding("onloadscript", "K_FULL")); + converter.attachScriptOnEvent(schema, "ddb75614", bindings, Map.of("K_NO_S3", 1, "K_FULL", 2)); Map onEvent = (Map) body(schema).get("onEvent"); - Map change = (Map) onEvent.get("change"); - List actions = (List) change.get("actions"); - assertEquals(6, actions.size()); // 3 段 × (ajax + 重放) + // NO_S3 的 onselected 必须挂到 selectedChange(旧逻辑会丢) + assertTrue(onEvent.containsKey("selectedChange")); + Map selData = (Map) + ((Map) ((Map) + ((List) ((Map) onEvent.get("selectedChange")).get("actions")).get(0)) + .get("api")).get("data"); + assertEquals(1, selData.get("seq")); } + /** Phase 2.2:事件绑定引用了不存在的 key → 跳过(fail-closed,不挂错脚本)。 */ + @Test + void attachScriptOnEvent_引用不存在key跳过() { + String xml = ""; + Map schema = converter.convertGraded(xml, new ArrayList<>(), noPage).getSchema(); + var bindings = List.of( + new TemplateToAmisConverter.EventBinding("onselectedscript", "K_MISSING"), + new TemplateToAmisConverter.EventBinding("onloadscript", "K_OK")); + converter.attachScriptOnEvent(schema, "tid", bindings, Map.of("K_OK", 5)); + + Map onEvent = (Map) body(schema).get("onEvent"); + // 不存在的 K_MISSING → 无 selectedChange;K_OK 存在 → 有 inited + assertFalse(onEvent.containsKey("selectedChange")); + assertTrue(onEvent.containsKey("inited")); + } + + /** Phase 2.2:空入参/空绑定/空 templateId 不挂 onEvent。 */ @Test void attachScriptOnEvent_无脚本或空入参不挂onEvent() { String xml = ""; Map schema = converter.convertGraded(xml, new ArrayList<>(), noPage).getSchema(); - converter.attachScriptOnEvent(schema, "tid-x", List.of()); // 空序号列表 + converter.attachScriptOnEvent(schema, "tid-x", List.of(), Map.of("K1", 1)); // 空绑定 assertNull(body(schema).get("onEvent")); - converter.attachScriptOnEvent(schema, "", List.of(1)); // 空 templateId + var b = List.of(new TemplateToAmisConverter.EventBinding("onloadscript", "K1")); + converter.attachScriptOnEvent(schema, "", b, Map.of("K1", 1)); // 空 templateId assertNull(body(schema).get("onEvent")); } + /** Phase 3:工具栏按钮补 id(= 中文名,与 enableButton 实参同源),供脚本启停定位。 */ + @Test + void buildToolbar_按钮含id等于中文名() { + // insert 按钮 name=「添加」→ headerToolbar 中应有 id=「添加」的按钮 + String xml = "" + + "