Skip to content

Commit 90ad625

Browse files
committed
refactor(maps): DRY creatMapContainer getElementById 解析,复用 super 调用
## 背景 `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)
1 parent 3f81df9 commit 90ad625

4 files changed

Lines changed: 4 additions & 16 deletions

File tree

packages/maps/src/map/map.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,7 @@ export default class DefaultMapService extends MapboxBaseMap<Map> {
113113
}
114114

115115
protected creatMapContainer(id: string | HTMLDivElement) {
116-
let wrapper = id as HTMLDivElement;
117-
if (typeof id === 'string') {
118-
wrapper = document.getElementById(id) as HTMLDivElement;
119-
}
116+
const wrapper = super.creatMapContainer(id);
120117
const container = document.createElement('div');
121118
container.style.cssText += `
122119
position: absolute;

packages/maps/src/mapbox/map.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,7 @@ export default class MapboxService extends MapboxBaseMap<Map & IMapboxInstance>
191191
}
192192

193193
protected creatMapContainer(id: string | HTMLDivElement) {
194-
let $wrapper = id as HTMLDivElement;
195-
if (typeof id === 'string') {
196-
$wrapper = document.getElementById(id) as HTMLDivElement;
197-
}
194+
const $wrapper = super.creatMapContainer(id);
198195
const $amapdiv = document.createElement('div');
199196
$amapdiv.style.cssText += `
200197
position: absolute;

packages/maps/src/maplibre/map.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,7 @@ export default class Service extends MapboxBaseMap<Map & IMapboxInstance> {
189189
}
190190

191191
protected creatMapContainer(id: string | HTMLDivElement) {
192-
let $wrapper = id as HTMLDivElement;
193-
if (typeof id === 'string') {
194-
$wrapper = document.getElementById(id) as HTMLDivElement;
195-
}
192+
const $wrapper = super.creatMapContainer(id);
196193
const $amapdiv = document.createElement('div');
197194
$amapdiv.style.cssText += `
198195
position: absolute;

packages/maps/src/tdtmap/map.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,7 @@ export default class TdtMapService extends BaseMap<any> {
527527
}
528528

529529
protected creatMapContainer(id: string | HTMLDivElement) {
530-
let $wrapper = id as HTMLDivElement;
531-
if (typeof id === 'string') {
532-
$wrapper = document.getElementById(id) as HTMLDivElement;
533-
}
530+
const $wrapper = super.creatMapContainer(id);
534531
const $tdtmapdiv = document.createElement('div');
535532
$tdtmapdiv.style.cssText += `
536533
position: absolute;

0 commit comments

Comments
 (0)