Skip to content

refactor(maps): DRY creatMapContainer 解析逻辑,复用 super 调用 - #2880

Merged
lzxue merged 1 commit into
masterfrom
refactor/maps-creatmapcontainer-dry
Jul 21, 2026
Merged

refactor(maps): DRY creatMapContainer 解析逻辑,复用 super 调用#2880
lzxue merged 1 commit into
masterfrom
refactor/maps-creatmapcontainer-dry

Conversation

@lzxue

@lzxue lzxue commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

背景

BaseMap.creatMapContainer 已封装 id-or-element 的解析逻辑(string 走 getElementByIdHTMLDivElement 直接用)。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($amapdiv / $tdtmapdiv / container
  • 注入 cssText(position/top/height/width)
  • 设置 id 前缀(l7_mapbox_div / l7_tdt_div)与 mapdivCount++
  • 返回内部 div

amap-next/map.ts 早已通过 super.creatMapContainer(id) 调用基类,无需改动。

为何不把内部 div 创建也抽到 MapboxBaseMap

  • tdtmap 继承 BaseMap 而非 MapboxBaseMap,无法复用 MapboxBaseMap 上的方法;
  • 各适配器 id 前缀、变量命名不同,统一抽取会引入少量行为/样式调整的风险,收益有限;
  • 本次只消除最明显的重复(id/element 解析),保留各适配器现有差异点,行为完全等价。

验证

  • 本地 tsc --noEmit -p packages/maps/tsconfig.json 通过(除已知 @types 噪声外无报错)
  • prettier --check 4 个改动文件通过

改动规模

净 -12 行(+4 / -16)

## 背景
`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)
@changeset-bot

changeset-bot Bot commented Jul 19, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 90ad625

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread packages/maps/src/map/map.ts
Comment thread packages/maps/src/mapbox/map.ts
Comment thread packages/maps/src/maplibre/map.ts
Comment thread packages/maps/src/tdtmap/map.ts
@lzxue
lzxue merged commit c7c7946 into master Jul 21, 2026
8 checks passed
@lzxue
lzxue deleted the refactor/maps-creatmapcontainer-dry branch July 21, 2026 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant