Skip to content

Commit

Permalink
fix(toolbar): fix missing Toolbar#toolbarHandlers (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
lyngai committed Nov 8, 2022
1 parent a37c0ac commit e55673f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
16 changes: 12 additions & 4 deletions src/toolbars/HookCenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
3 changes: 1 addition & 2 deletions src/toolbars/MenuBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export default class MenuBase {
/**
* 子菜单的定位方式
* @property
* @private
* @type {'absolute' | 'fixed'}
*/
this.positionModel = 'absolute';
Expand Down Expand Up @@ -163,7 +162,7 @@ export default class MenuBase {

/**
* 处理菜单项点击事件
* @param {MouseEvent} event 点击事件
* @param {MouseEvent | KeyboardEvent | undefined} [event] 点击事件
* @returns {void}
*/
fire(event, shortKey = '') {
Expand Down
31 changes: 29 additions & 2 deletions src/toolbars/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>} 外部获取 toolbarHandler
*/
toolbarHandlers = {};

constructor(options) {
// 存储所有菜单的实例
this.menus = {};
Expand All @@ -43,6 +49,7 @@ export default class Toolbar {

init() {
this.collectShortcutKey();
this.collectToolbarHandler();
Event.on(this.instanceId, Event.Events.cleanAllSubMenus, () => this.hidAlleSubMenu());
}

Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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 事件
Expand Down

0 comments on commit e55673f

Please sign in to comment.