diff --git a/docs/plans/2026-06-14-onEvent脚本挂回修复-plan.md b/docs/plans/2026-06-14-onEvent脚本挂回修复-plan.md new file mode 100644 index 0000000..a97b584 --- /dev/null +++ b/docs/plans/2026-06-14-onEvent脚本挂回修复-plan.md @@ -0,0 +1,326 @@ +# onEvent 脚本挂回修复(按钮联动 / 表单只读联动)Implementation Plan + +> **For Claude:** REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task. + +**Goal:** 让转换器把老模板的事件脚本(onselectedscript 行选中按钮联动、onloadscript 加载联动)正确挂回 amis onEvent,使「列表选中行→按 STATUS 点亮/置灰按钮」「表单加载→字段只读/显隐」这类联动在新系统真实生效。 + +**Architecture:** 当前 `attachScriptOnEvent` 只挂 `full_auto_index.json` 里的 FULL_AUTO 脚本,且把每段同时挂 `inited`+`change` 不区分事件类型/组件 → onselectedscript(多为 NO_S3 标签)被系统性丢弃。改法:在转换期从 view_template.xml 解析事件绑定属性(`onselectedscript`/`onloadscript` = 脚本 key)→ 由 `` 文档序解析 key→seq → 按事件类型挂到正确的 amis 事件(onselected→crud `selectedChange`、onload→`inited`)与正确组件,并给工具栏按钮补 `id` 以便 `enableButton` 指令能定位。覆盖 NO_S3 与 FULL_AUTO 两类脚本。运行时 `RewrittenScriptStore` 已能按 (templateId,seq) 加载任意脚本,无需改。 + +**Tech Stack:** Java 21(JAVA_HOME=/opt/homebrew/opt/openjdk@21,mvn 默认 Java26 会崩 Groovy)、dom4j(XML 解析)、JUnit5、amis 6.13(前端 TS/Vitest)、PG15(localhost:5433/eip4dev,schema eip3)。 + +--- + +## 背景与根因(已证据级核实,2026-06-14) + +- 用户实测:新闻管理列表选中行按钮不按老逻辑点亮 + 查看页弹 2 个「ajax 动作执行失败,原因:未知」。 +- **P0(已修,前置)**:`application-dev.yml` rewritten-root 写成相对 `../tools/...`,标准 jar 从 cwd=eip4-platform 启动 → 解析到不存在的 `eip3/tools/...` → 改写脚本全找不到。已改为 `tools/s3_rewriter/output/rewritten` 并验证(execute-template passed:true、浏览器 0 errors)。**本 plan 不含 P0,但实施需在 P0 已生效的环境上做。** +- **P0+(本 plan 修)**:即使路径修好,挂的也是错脚本。新闻管理 ddb75614 原始 XML 两段: + - `" + + "" + + ""; + Element root = DocumentHelper.parseText(xml).getRootElement(); + Map m = converter.scriptKeyToSeq(root); + assertThat(m).containsEntry("K1", 1).containsEntry("K2", 2); +} +``` +**Step 2:** 运行确认失败(方法不存在)。`mvn -o -pl platform-converter test -Dtest=TemplateToAmisConverterTest#scriptKeyToSeq_按scripts文档序1based` +**Step 3:最小实现** +```java +/** 解析 " + + "
"; + Element root = DocumentHelper.parseText(xml).getRootElement(); + List bs = converter.extractEventBindings(root); + assertThat(bs).extracting(b -> b.eventType + ":" + b.scriptKey) + .containsExactlyInAnyOrder("onselectedscript:K1", "onloadscript:K2"); +} +``` +**Step 2:** 运行确认失败。 +**Step 3:最小实现**(新增内部记录类 + 递归扫描) +```java +/** 一条事件绑定:原始事件属性名 + 引用的脚本 key(后续解析 seq)。 */ +public static final class EventBinding { + public final String eventType; // onselectedscript | onloadscript + public final String scriptKey; + public EventBinding(String eventType, String scriptKey) { this.eventType = eventType; this.scriptKey = scriptKey; } +} +private static final List EVENT_ATTRS = List.of("onselectedscript", "onloadscript"); +List extractEventBindings(Element root) { + List out = new ArrayList<>(); + collectBindings(root, out); + return out; +} +private void collectBindings(Element el, List out) { + for (String attr : EVENT_ATTRS) { + String v = el.attributeValue(attr); + if (v != null && !v.isBlank()) out.add(new EventBinding(attr, v)); + } + for (Element c : el.elements()) collectBindings(c, out); +} +``` +**Step 4:** 运行通过。 +**Step 5:** commit `feat(converter): 提取 onselected/onload 事件绑定` + +> 注:Spike 若发现属性还挂在 `//