refactor(maps): DRY creatMapContainer 解析逻辑,复用 super 调用 - #2880
Conversation
## 背景 `BaseMap.creatMapContainer` 已封装 id-or-element 的解析逻辑(string 走 getElementById,HTMLDivElement 直接用)。map/mapbox/maplibre/tdtmap 四个适配器 的 `creatMapContainer` 重写各自重复实现了一遍同样的解析,与基类逻辑冗余。 ## 改动 将 4 个文件中重复的 id/element 解析分支替换为 `super.creatMapContainer(id)` 调用: - packages/maps/src/map/map.ts - packages/maps/src/mapbox/map.ts - packages/maps/src/maplibre/map.ts - packages/maps/src/tdtmap/map.ts 每个文件其余逻辑(创建内部 div、注入 cssText、设置 id 前缀如 l7_mapbox_div / l7_tdt_div、mapdivCount 自增)保持不变,行为与改动前完全一致。 amap-next/map.ts 早已正确通过 super.creatMapContainer(id) 调用基类,无需改动。 ## 为何不把内部 div 创建也抽到 MapboxBaseMap - tdtmap 继承 BaseMap 而非 MapboxBaseMap,无法复用 MapboxBaseMap 上的方法; - 各适配器 id 前缀、变量命名不同($amapdiv / $tdtmapdiv / container),统一 抽取会引入少量行为或样式调整的风险,收益有限; - 本次只消除最明显的重复(id/element 解析),保留各适配器现有的差异点, 行为完全等价。 ## 验证 - 本地 tsc --noEmit -p packages/maps/tsconfig.json 通过(除已知 @types 噪声外) - prettier --check 4 个改动文件通过 净 -12 行(+4 / -16)
|
There was a problem hiding this comment.
Code Review
This pull request refactors the creatMapContainer method across multiple map service implementations (DefaultMapService, MapboxService, Service, and TdtMapService) to delegate the retrieval of the map container wrapper to the parent class using super.creatMapContainer(id). The review feedback highlights a potential runtime error: if the container element is not found in the DOM, super.creatMapContainer(id) may return null, leading to a TypeError when appending child elements. It is recommended to add defensive null checks in all modified files to handle this scenario gracefully and throw a clear error message.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
背景
BaseMap.creatMapContainer已封装 id-or-element 的解析逻辑(string 走getElementById,HTMLDivElement直接用)。map/mapbox/maplibre/tdtmap四个适配器的creatMapContainer重写各自重复实现了一遍同样的解析,与基类逻辑冗余。改动
将 4 个文件中重复的 id/element 解析分支替换为
super.creatMapContainer(id)调用:packages/maps/src/map/map.tspackages/maps/src/mapbox/map.tspackages/maps/src/maplibre/map.tspackages/maps/src/tdtmap/map.ts每个文件其余逻辑保持不变,行为与改动前完全一致:
$amapdiv/$tdtmapdiv/container)cssText(position/top/height/width)l7_mapbox_div/l7_tdt_div)与mapdivCount++amap-next/map.ts早已通过super.creatMapContainer(id)调用基类,无需改动。为何不把内部 div 创建也抽到
MapboxBaseMaptdtmap继承BaseMap而非MapboxBaseMap,无法复用MapboxBaseMap上的方法;验证
tsc --noEmit -p packages/maps/tsconfig.json通过(除已知@types噪声外无报错)prettier --check4 个改动文件通过改动规模
净 -12 行(+4 / -16)