refactor(right-key): move interaction logic to component and trigger on mouseup

This commit is contained in:
yuding
2025-12-10 10:10:09 +08:00
parent 1f27d02788
commit ef79b5b370
3 changed files with 62 additions and 14 deletions

View File

@@ -16,7 +16,7 @@ export class RightKeyManager extends BimComponent {
private rightKeyPanel: BimRightKey;
// 存储注册的上下文处理器
// 每个处理<EFBFBD><EFBFBD>接收鼠标事件,返回一组菜单项(如果没有对应菜单则返回 null
// 每个处理接收鼠标事件,返回一组菜单项(如果没有对应菜单则返回 null
private contextHandlers: Array<(e: MouseEvent) => MenuItemConfig[] | null> = [];
constructor(engine: BimEngine, container: HTMLElement) {
@@ -24,18 +24,16 @@ export class RightKeyManager extends BimComponent {
this.container = container;
// 初始化右键容器,设置极高的层级以覆盖所有 UI
this.rightKeyPanel = new BimRightKey({ zIndex: 9000 });
// 将事件监听和触发逻辑下放给 BimRightKey 组件
this.rightKeyPanel = new BimRightKey({
zIndex: 9000,
container: this.container,
onContext: this.handleContextMenu
});
this.rightKeyPanel.init();
this.initEventListeners();
}
private initEventListeners(): void {
this.container.addEventListener('contextmenu', this.handleContextMenu);
}
public destroy(): void {
this.container.removeEventListener('contextmenu', this.handleContextMenu);
this.rightKeyPanel.destroy();
}
@@ -78,11 +76,9 @@ export class RightKeyManager extends BimComponent {
/**
* 处理右键点击事件
* 由 BimRightKey 组件在检测到有效右键点击时调用
*/
private handleContextMenu = (e: MouseEvent): void => {
// 阻止浏览器默认的右键菜单
e.preventDefault();
// 1. 确定上下文项
// 遍历所有注册的处理器,找到第一个返回非空结果的处理器
// 这种责任链模式允许插件优先处理特定对象的右键