-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 新增 Scene 与 Layer 相关的事件属性 (#54)
* feat: 容器 LarkMap 组件新增 scene 相关的事件 props * feat: 1.新增 useLayerEvent 用于更新 L7 原子图层的回调函数 2. useSceneEvent 在组件注销时清空事件 * chore: 更新类型文件 * chore: 调整类型路径 * feat: 复合图层支持事件属性绑定 * fix: 类型引入错误 Co-authored-by: yanxiong <[email protected]> Co-authored-by: yunji <[email protected]>
- Loading branch information
1 parent
5ddfd4f
commit 1af5cec
Showing
32 changed files
with
396 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { SceneEventProps } from '../../types'; | ||
|
||
/** | ||
* 从 LarkMap 的事件名到 Scene 事件名的映射 | ||
*/ | ||
export const SceneEventMap: Record<keyof SceneEventProps, string> = { | ||
onLoaded: 'loaded', | ||
onDestroy: 'destroy', | ||
|
||
onResize: 'resize', | ||
|
||
onMapMove: 'mapmove', | ||
onMoveStart: 'movestart', | ||
onMoveEnd: 'moveend', | ||
onZoomChange: 'zoomchange', | ||
onZoomStart: 'zoomstart', | ||
onZoomEnd: 'zoomend', | ||
|
||
onClick: 'click', | ||
onDblclick: 'dblclick', | ||
onContextMenu: 'contextmenu', | ||
|
||
onMouseMove: 'mousemove', | ||
onMousewheel: 'mousewheel', | ||
onMousedown: 'mousedown', | ||
onMouseOver: 'mouseover', | ||
onMouseOut: 'mouseout', | ||
onMouseUp: 'mouseup', | ||
|
||
onDragStart: 'dragstart', | ||
onDragging: 'dragging', | ||
onDragEnd: 'dragend', | ||
}; | ||
|
||
/** | ||
* LarkMap 事件名列表 | ||
*/ | ||
export const SceneEventList = Object.keys(SceneEventMap); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { Scene } from '@antv/l7'; | ||
import { useTrackedEffect, useUnmount } from 'ahooks'; | ||
import type { SceneEventProps, SceneEventCallback } from '../../types'; | ||
import { SceneEventList, SceneEventMap } from './constant'; | ||
|
||
export const useSceneEvent = (scene: Scene, props: SceneEventProps) => { | ||
useTrackedEffect( | ||
(changeIndexList: number[], previousDeps: SceneEventCallback[] = [], currentDeps: SceneEventCallback[] = []) => { | ||
if (!scene) { | ||
return; | ||
} | ||
// 需要更新的事件对应到 deps 的数组下标,但是不包含 scene 实例的更新 | ||
let indexList = changeIndexList.filter((index) => index); | ||
|
||
// 如果本次变化为 scene 的实例化则无差别遍历所有事件类型 | ||
if (changeIndexList.includes(0)) { | ||
indexList = SceneEventList.map((_, index) => index + 1); | ||
} | ||
|
||
indexList.forEach((index) => { | ||
const eventName = SceneEventMap[SceneEventList[index]] as string; | ||
const previousCallback = previousDeps[index]; | ||
const currentCallback = currentDeps[index]; | ||
// 分别注销旧的事件回调并绑定新的事件 | ||
if (previousCallback) { | ||
scene.off(eventName, previousCallback); | ||
} | ||
if (currentCallback) { | ||
scene.on(eventName, currentCallback); | ||
} | ||
}); | ||
}, | ||
[scene, ...SceneEventList.map((eventName) => props[eventName])], | ||
); | ||
|
||
useUnmount(() => { | ||
if (!scene) { | ||
return; | ||
} | ||
SceneEventList.forEach((key) => { | ||
const eventName = SceneEventMap[key]; | ||
const callback = props[key]; | ||
if (eventName && callback) { | ||
scene.off(eventName, callback); | ||
} | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.