转换器 width/colspan 精确版面:layout colspan→amis grid 按比例列宽
补 parseLayoutRows/buildTabBody/LayoutCell(并修上轮半成品致 main 不编译的断点) 真实数据只有 colspan 无像素 width→md 比例列宽,避 columnCount 等宽坑;无信号退回 group 两栏。76测 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -946,22 +946,22 @@ public class TemplateToAmisConverter {
|
||||
byName.put(String.valueOf(it.get("name")), it);
|
||||
}
|
||||
Set<String> placed = new LinkedHashSet<>();
|
||||
boolean anyGrid = false;
|
||||
List<Map<String, Object>> tabSchemas = new ArrayList<>();
|
||||
for (Element tabEl : tabEls) {
|
||||
String title = tabEl.attributeValue("name");
|
||||
if (title == null || title.isBlank()) {
|
||||
title = "页面";
|
||||
}
|
||||
List<Map<String, Object>> tabItems = new ArrayList<>();
|
||||
for (String nm : extractLocations(tabEl.getText())) {
|
||||
Map<String, Object> it = byName.get(nm);
|
||||
if (it != null && placed.add(nm)) {
|
||||
tabItems.add(it);
|
||||
}
|
||||
// 按 HTML 表格行结构解析(含 <td colspan> 跨列版面信息),字段按文档序归位
|
||||
List<List<LayoutCell>> tabRows = parseLayoutRows(tabEl.getText());
|
||||
List<Map<String, Object>> tabBody = buildTabBody(tabRows, byName, placed);
|
||||
if (tabBody.stream().anyMatch(row -> "grid".equals(row.get("type")))) {
|
||||
anyGrid = true;
|
||||
}
|
||||
Map<String, Object> tabSchema = new LinkedHashMap<>();
|
||||
tabSchema.put("title", title);
|
||||
tabSchema.put("body", buildFormRows(tabItems));
|
||||
tabSchema.put("body", tabBody);
|
||||
tabSchemas.add(tabSchema);
|
||||
}
|
||||
// 未被任何 <div location> 引用的字段(隐藏主键/未布局)收尾追加到末 tab,不丢失
|
||||
@@ -977,7 +977,8 @@ public class TemplateToAmisConverter {
|
||||
}
|
||||
result.note("form <layout> → " + tabSchemas.size() + " 个 amis tab(字段按 <div location> 归位"
|
||||
+ (leftovers.isEmpty() ? "" : ";" + leftovers.size() + " 个未布局字段收尾追加")
|
||||
+ ";width/colspan 像素版面暂按两栏近似,后续专项)");
|
||||
+ ";" + (anyGrid ? "<td colspan> 版面信息 → amis grid 按比例列宽(md/12)"
|
||||
: "无 colspan 版面信息 → 退回 group 两栏") + ")");
|
||||
if (tabSchemas.size() == 1) {
|
||||
return (List<Map<String, Object>>) tabSchemas.get(0).get("body"); // 单 tab 不套 tabs 壳
|
||||
}
|
||||
@@ -989,20 +990,150 @@ public class TemplateToAmisConverter {
|
||||
return bodyList;
|
||||
}
|
||||
|
||||
/** amis grid 总栅格数(12 列),col.md = 字段占的网格槽位按比例折算到 12。 */
|
||||
private static final int AMIS_GRID = 12;
|
||||
|
||||
/**
|
||||
* 一个版面单元格:tab 内 {@code <td class="customtablecomponent" [colspan=N]>} 包裹的
|
||||
* {@code <div location="字段名">}。{@code slots} = 该字段在老系统网格中占的列槽位
|
||||
* (1 个 caption td + colspan 个 component td;colspan 默认 1 → slots=2)。
|
||||
*/
|
||||
private record LayoutCell(String fieldName, int slots) {
|
||||
}
|
||||
|
||||
/** 匹配一个 {@code customtablecomponent} td(可带 colspan/其它属性,顺序不定)连其内 div location。 */
|
||||
private static final java.util.regex.Pattern COMPONENT_CELL = java.util.regex.Pattern.compile(
|
||||
"<td\\b([^>]*\\bcustomtablecomponent\\b[^>]*)>\\s*<div\\b[^>]*\\blocation\\s*=\\s*\"([^\"]+)\"",
|
||||
java.util.regex.Pattern.CASE_INSENSITIVE);
|
||||
|
||||
/** 兜底:无 customtablecomponent class 时退回纯 {@code <div location>}(A2 那套简表)。 */
|
||||
private static final java.util.regex.Pattern LOCATION_ATTR =
|
||||
java.util.regex.Pattern.compile("location\\s*=\\s*\"([^\"]+)\"");
|
||||
|
||||
/** 从 tab 的 HTML(CDATA)文本按文档序提取 {@code <div location="X">} 的字段名。 */
|
||||
private static List<String> extractLocations(String html) {
|
||||
List<String> names = new ArrayList<>();
|
||||
private static final java.util.regex.Pattern COLSPAN_ATTR =
|
||||
java.util.regex.Pattern.compile("\\bcolspan\\s*=\\s*\"?(\\d+)", java.util.regex.Pattern.CASE_INSENSITIVE);
|
||||
|
||||
private static final java.util.regex.Pattern ROW_SPLIT =
|
||||
java.util.regex.Pattern.compile("<tr\\b[^>]*>(.*?)</tr>",
|
||||
java.util.regex.Pattern.CASE_INSENSITIVE | java.util.regex.Pattern.DOTALL);
|
||||
|
||||
/**
|
||||
* 把 tab 的 HTML(CDATA)按 {@code <tr>} 行解析为版面行序列,每行携带其字段 +
|
||||
* 各字段占的网格槽位(含 {@code <td colspan>} 跨列信息)。
|
||||
*
|
||||
* <p>优先按 {@code customtablecomponent} td 识别(老系统标准编辑表格结构,见真实 view_page),
|
||||
* 从中读 colspan;该结构缺失(A2 简表)时退回纯 {@code <div location>}(每字段 slots=2,等宽)。
|
||||
* 正则解析规避 CDATA 内 HTML 的 {@code } 等非严格 XML 实体导致 DOM 解析失败。
|
||||
*/
|
||||
private static List<List<LayoutCell>> parseLayoutRows(String html) {
|
||||
List<List<LayoutCell>> rows = new ArrayList<>();
|
||||
if (html == null || html.isBlank()) {
|
||||
return names;
|
||||
return rows;
|
||||
}
|
||||
java.util.regex.Matcher m = LOCATION_ATTR.matcher(html);
|
||||
while (m.find()) {
|
||||
names.add(m.group(1));
|
||||
java.util.regex.Matcher rowM = ROW_SPLIT.matcher(html);
|
||||
while (rowM.find()) {
|
||||
String rowHtml = rowM.group(1);
|
||||
List<LayoutCell> cells = new ArrayList<>();
|
||||
java.util.regex.Matcher cm = COMPONENT_CELL.matcher(rowHtml);
|
||||
while (cm.find()) {
|
||||
String tdAttrs = cm.group(1);
|
||||
String field = cm.group(2);
|
||||
java.util.regex.Matcher csm = COLSPAN_ATTR.matcher(tdAttrs);
|
||||
int colspan = csm.find() ? Math.max(1, Integer.parseInt(csm.group(1))) : 1;
|
||||
cells.add(new LayoutCell(field, 1 + colspan)); // 1 caption td + colspan component td
|
||||
}
|
||||
if (!cells.isEmpty()) {
|
||||
rows.add(cells);
|
||||
}
|
||||
}
|
||||
return names;
|
||||
if (rows.isEmpty()) {
|
||||
// 无 customtablecomponent 结构(A2 简表/无版面信息)→ 退回纯 location 顺序解析,
|
||||
// 每字段单独成行(slots=2、无多列),buildTabBody 检测不到版面信号 → 走 buildFormRows 既有两栏
|
||||
java.util.regex.Matcher lm = LOCATION_ATTR.matcher(html);
|
||||
while (lm.find()) {
|
||||
List<LayoutCell> single = new ArrayList<>();
|
||||
single.add(new LayoutCell(lm.group(1), 2));
|
||||
rows.add(single);
|
||||
}
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* 由版面行序列构建 tab body。携带真实跨列版面信息(任意 colspan>1,或一行多于一个字段)
|
||||
* 时 → amis {@code grid} 行,列 {@code md} = 字段槽位/网格总槽位×12(按比例列宽,
|
||||
* <b>非</b> form.columnCount 的纯 CSS 等宽);否则退回 {@link #buildFormRows} 既有 group 两栏(向后兼容)。
|
||||
*
|
||||
* <p>宽控件(富文本/附件/多行/子表)无论 colspan 如何一律 {@code md=12} 独占整行——
|
||||
* 防止富文本被塞半栏(52c7397 TinyMCE 挤竖条根因)。{@code hidden} 字段不进 grid,平铺在行外。
|
||||
*/
|
||||
private static List<Map<String, Object>> buildTabBody(List<List<LayoutCell>> rows,
|
||||
Map<String, Map<String, Object>> byName, Set<String> placed) {
|
||||
// 先归位本 tab 的字段(按行内文档序),并判断是否有真实版面信号
|
||||
boolean hasGridSignal = false;
|
||||
int gridTotalSlots = 0;
|
||||
List<List<LayoutCell>> resolvedRows = new ArrayList<>();
|
||||
for (List<LayoutCell> row : rows) {
|
||||
List<LayoutCell> kept = new ArrayList<>();
|
||||
int rowSlots = 0;
|
||||
for (LayoutCell cell : row) {
|
||||
if (!byName.containsKey(cell.fieldName()) || !placed.add(cell.fieldName())) {
|
||||
continue; // 字段不存在或已被前面 tab 归位 → 跳过
|
||||
}
|
||||
kept.add(cell);
|
||||
rowSlots += cell.slots();
|
||||
if (cell.slots() != 2) {
|
||||
hasGridSignal = true; // colspan>1 → 真实跨列
|
||||
}
|
||||
}
|
||||
if (kept.size() > 1) {
|
||||
hasGridSignal = true; // 一行多字段 → 真实多列版面
|
||||
}
|
||||
if (!kept.isEmpty()) {
|
||||
resolvedRows.add(kept);
|
||||
gridTotalSlots = Math.max(gridTotalSlots, rowSlots);
|
||||
}
|
||||
}
|
||||
// 收集本 tab 全部归位字段(按行序),供退回路径用
|
||||
List<Map<String, Object>> items = new ArrayList<>();
|
||||
for (List<LayoutCell> row : resolvedRows) {
|
||||
for (LayoutCell cell : row) {
|
||||
items.add(byName.get(cell.fieldName()));
|
||||
}
|
||||
}
|
||||
if (!hasGridSignal || gridTotalSlots <= 0) {
|
||||
return buildFormRows(items); // 无版面信号 → 既有 group 两栏(向后兼容,零回归)
|
||||
}
|
||||
// 有版面信号 → 逐行 amis grid,列 md 按槽位比例折算到 12
|
||||
List<Map<String, Object>> body = new ArrayList<>();
|
||||
for (List<LayoutCell> row : resolvedRows) {
|
||||
List<Map<String, Object>> columns = new ArrayList<>();
|
||||
for (LayoutCell cell : row) {
|
||||
Map<String, Object> item = byName.get(cell.fieldName());
|
||||
if (Boolean.TRUE.equals(item.get("hidden"))) {
|
||||
body.add(item); // hidden 不渲染,平铺行外,不占栅格
|
||||
continue;
|
||||
}
|
||||
int md = isWideField(item)
|
||||
? AMIS_GRID // 宽控件独占整行
|
||||
: Math.max(1, Math.min(AMIS_GRID,
|
||||
Math.round((float) cell.slots() / gridTotalSlots * AMIS_GRID)));
|
||||
Map<String, Object> col = new LinkedHashMap<>();
|
||||
col.put("md", md);
|
||||
List<Map<String, Object>> colBody = new ArrayList<>();
|
||||
colBody.add(item);
|
||||
col.put("body", colBody);
|
||||
columns.add(col);
|
||||
}
|
||||
if (columns.isEmpty()) {
|
||||
continue; // 整行都是 hidden,已平铺
|
||||
}
|
||||
Map<String, Object> grid = new LinkedHashMap<>();
|
||||
grid.put("type", "grid");
|
||||
grid.put("columns", columns);
|
||||
body.add(grid);
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user