diff --git a/src/toolbars/HookCenter.js b/src/toolbars/HookCenter.js index d504f953..0cb845d1 100644 --- a/src/toolbars/HookCenter.js +++ b/src/toolbars/HookCenter.js @@ -117,13 +117,21 @@ const HookList = { export default class HookCenter { constructor(toolbar) { this.toolbar = toolbar; - // 保存所有菜单实例 + /** + * @type {{[key: string]: import('@/toolbars/MenuBase').default}} 保存所有菜单实例 + */ this.hooks = {}; - // 所有注册的菜单名称 + /** + * @type {string[]} 所有注册的菜单名称 + */ this.allMenusName = []; - // 一级菜单的名称 + /** + * @type {string[]} 一级菜单的名称 + */ this.level1MenusName = []; - // 二级菜单的名称 {一级菜单名称: [二级菜单名称1, 二级菜单名称2]} + /** + * @type {{ [parentName: string]: string[]}} 二级菜单的名称, e.g. {一级菜单名称: [二级菜单名称1, 二级菜单名称2]} + */ this.level2MenusName = {}; this.init(); } diff --git a/src/toolbars/MenuBase.js b/src/toolbars/MenuBase.js index 13b8bfe4..1c57f0b6 100644 --- a/src/toolbars/MenuBase.js +++ b/src/toolbars/MenuBase.js @@ -65,7 +65,6 @@ export default class MenuBase { /** * 子菜单的定位方式 * @property - * @private * @type {'absolute' | 'fixed'} */ this.positionModel = 'absolute'; @@ -163,7 +162,7 @@ export default class MenuBase { /** * 处理菜单项点击事件 - * @param {MouseEvent} event 点击事件 + * @param {MouseEvent | KeyboardEvent | undefined} [event] 点击事件 * @returns {void} */ fire(event, shortKey = '') { diff --git a/src/toolbars/Toolbar.js b/src/toolbars/Toolbar.js index 874ed276..200f0149 100644 --- a/src/toolbars/Toolbar.js +++ b/src/toolbars/Toolbar.js @@ -17,8 +17,14 @@ import { mac } from 'codemirror/src/util/browser'; import HookCenter from './HookCenter'; import Event from '@/Event'; import { createElement } from '@/utils/dom'; +import Logger from '@/Logger'; export default class Toolbar { + /** + * @type {Record} 外部获取 toolbarHandler + */ + toolbarHandlers = {}; + constructor(options) { // 存储所有菜单的实例 this.menus = {}; @@ -43,6 +49,7 @@ export default class Toolbar { init() { this.collectShortcutKey(); + this.collectToolbarHandler(); Event.on(this.instanceId, Event.Events.cleanAllSubMenus, () => this.hidAlleSubMenu()); } @@ -57,20 +64,22 @@ export default class Toolbar { } isHasLevel2Menu(name) { + // FIXME: return boolean return this.menus.level2MenusName[name]; } isHasConfigMenu(name) { + // FIXME: return boolean return this.menus.hooks[name].subMenuConfig || []; } /** * 判断是否有子菜单,目前有两种子菜单配置方式:1、通过`subMenuConfig`属性 2、通过`buttonConfig`配置属性 - * @param {String} name + * @param {string} name * @returns {boolean} 是否有子菜单 */ isHasSubMenu(name) { - return this.isHasLevel2Menu(name) || this.isHasConfigMenu(name).length > 0; + return Boolean(this.isHasLevel2Menu(name) || this.isHasConfigMenu(name).length > 0); } /** @@ -171,6 +180,24 @@ export default class Toolbar { }); } + collectToolbarHandler() { + this.toolbarHandlers = this.menus.allMenusName.reduce((handlerMap, name) => { + const menuHook = this.menus.hooks[name]; + if (!menuHook) { + return handlerMap; + } + handlerMap[name] = (shortcut, _callback) => { + if (typeof _callback === 'function') { + Logger.warn( + 'MenuBase#onClick param callback is no longer supported. Please register the callback via MenuBase#registerAfterClickCb instead.', + ); + } + menuHook.fire.call(menuHook, undefined, shortcut); + }; + return handlerMap; + }, {}); + } + /** * 监测是否有对应的快捷键 * @param {KeyboardEvent} evt keydown 事件