2026-01-22 15:23:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 右键菜单管理器
|
|
|
|
|
|
* 负责管理右键上下文菜单的显示和交互
|
|
|
|
|
|
*/
|
|
|
|
|
|
import { BaseManager } from '../core/base-manager';
|
2026-02-28 10:08:36 +08:00
|
|
|
|
import { ManagerRegistry } from '../core/manager-registry';
|
2025-12-09 18:34:43 +08:00
|
|
|
|
import { BimRightKey } from '../components/right-key';
|
|
|
|
|
|
import { BimMenu } from '../components/menu';
|
|
|
|
|
|
import { MenuItemConfig } from '../components/menu/item';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-22 15:23:57 +08:00
|
|
|
|
* 右键菜单管理器
|
|
|
|
|
|
* 支持注册多个上下文处理器,动态生成右键菜单
|
2025-12-09 18:34:43 +08:00
|
|
|
|
*/
|
2026-01-22 15:23:57 +08:00
|
|
|
|
export class RightKeyManager extends BaseManager {
|
|
|
|
|
|
/** 容器元素 */
|
2025-12-09 18:34:43 +08:00
|
|
|
|
private container: HTMLElement;
|
2026-01-22 15:23:57 +08:00
|
|
|
|
/** 右键面板实例 */
|
2025-12-09 18:34:43 +08:00
|
|
|
|
private rightKeyPanel: BimRightKey;
|
2026-01-22 15:23:57 +08:00
|
|
|
|
/** 上下文处理器列表 */
|
2025-12-09 18:34:43 +08:00
|
|
|
|
private contextHandlers: Array<(e: MouseEvent) => MenuItemConfig[] | null> = [];
|
|
|
|
|
|
|
2026-02-28 10:08:36 +08:00
|
|
|
|
constructor(container: HTMLElement, registry: ManagerRegistry) {
|
|
|
|
|
|
super(registry);
|
2025-12-09 18:34:43 +08:00
|
|
|
|
this.container = container;
|
|
|
|
|
|
|
2025-12-10 10:10:09 +08:00
|
|
|
|
this.rightKeyPanel = new BimRightKey({
|
|
|
|
|
|
zIndex: 9000,
|
|
|
|
|
|
container: this.container,
|
|
|
|
|
|
onContext: this.handleContextMenu
|
|
|
|
|
|
});
|
2025-12-09 18:34:43 +08:00
|
|
|
|
this.rightKeyPanel.init();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-22 15:23:57 +08:00
|
|
|
|
/** 销毁管理器 */
|
2025-12-09 18:34:43 +08:00
|
|
|
|
public destroy(): void {
|
|
|
|
|
|
this.rightKeyPanel.destroy();
|
2026-01-22 15:23:57 +08:00
|
|
|
|
super.destroy();
|
2025-12-09 18:34:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-22 15:23:57 +08:00
|
|
|
|
* 注册上下文处理器
|
|
|
|
|
|
* @param handler 处理器函数,返回菜单项配置
|
2025-12-09 18:34:43 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public registerHandler(handler: (e: MouseEvent) => MenuItemConfig[] | null): void {
|
|
|
|
|
|
this.contextHandlers.push(handler);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-22 15:23:57 +08:00
|
|
|
|
* 显示菜单
|
|
|
|
|
|
* @param x 横坐标
|
|
|
|
|
|
* @param y 纵坐标
|
|
|
|
|
|
* @param items 菜单项配置
|
|
|
|
|
|
* @param groupOrder 分组顺序
|
2025-12-09 18:34:43 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public showMenu(x: number, y: number, items: MenuItemConfig[], groupOrder?: string[]): void {
|
|
|
|
|
|
if (!items || items.length === 0) return;
|
|
|
|
|
|
|
|
|
|
|
|
const menu = new BimMenu({ items, groupOrder });
|
2026-01-22 15:23:57 +08:00
|
|
|
|
menu.init();
|
2025-12-09 18:34:43 +08:00
|
|
|
|
|
|
|
|
|
|
this.rightKeyPanel.mount(menu);
|
|
|
|
|
|
this.rightKeyPanel.show(x, y);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-22 15:23:57 +08:00
|
|
|
|
/** 隐藏菜单 */
|
2025-12-09 18:34:43 +08:00
|
|
|
|
public hide(): void {
|
|
|
|
|
|
this.rightKeyPanel.hide();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-22 15:23:57 +08:00
|
|
|
|
/** 处理右键点击事件 */
|
2025-12-09 18:34:43 +08:00
|
|
|
|
private handleContextMenu = (e: MouseEvent): void => {
|
2025-12-10 09:42:05 +08:00
|
|
|
|
let items: MenuItemConfig[] | null = null;
|
|
|
|
|
|
for (const handler of this.contextHandlers) {
|
|
|
|
|
|
const result = handler(e);
|
|
|
|
|
|
if (result && result.length > 0) {
|
|
|
|
|
|
if (!items) items = [];
|
|
|
|
|
|
items = items.concat(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-09 18:34:43 +08:00
|
|
|
|
if (items && items.length > 0) {
|
|
|
|
|
|
this.showMenu(e.clientX, e.clientY, items);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.hide();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2025-12-10 09:42:05 +08:00
|
|
|
|
}
|