Skip to content

Commit

Permalink
feat: 增加配置快捷键的功能 close #406
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsonliu committed Aug 17, 2023
1 parent a1df190 commit 55e582f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/Cherry.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ const defaultConfig = {
sidebar: [],
bubble: ['bold', 'italic', 'underline', 'strikethrough', 'sub', 'sup', 'quote', '|', 'size', 'color'], // array or false
float: ['h1', 'h2', 'h3', '|', 'checklist', 'quote', 'table', 'code'], // array or false
// 快捷键配置,如果配置为空,则使用toolbar的配置
shortcutKey: {
// 'Alt-1': 'header',
// 'Alt-2': 'header',
// 'Ctrl-b': 'bold',
// 'Ctrl-Alt-m': 'formula',
},
},
// 打开draw.io编辑页的url,如果为空则drawio按钮失效
drawioIframeUrl: '',
Expand Down
9 changes: 5 additions & 4 deletions src/Cherry.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ export default class Cherry extends CherryStatic {
$cherry: this,
buttonConfig: this.options.toolbars.toolbar,
customMenu: this.options.toolbars.customMenu,
shortcutKey: this.options.toolbars.shortcutKey,
});
return this.toolbar;
}
Expand All @@ -411,7 +412,7 @@ export default class Cherry extends CherryStatic {
buttonConfig: this.options.toolbars.toolbarRight,
customMenu: this.options.toolbars.customMenu,
});
this.toolbar.toolbarHandlers = Object.assign({}, this.toolbar.toolbarHandlers, this.toolbarRight.toolbarHandlers);
this.toolbar.collectMenuInfo(this.toolbarRight);
return this.toolbarRight;
}

Expand All @@ -430,7 +431,7 @@ export default class Cherry extends CherryStatic {
buttonConfig: this.options.toolbars.sidebar,
customMenu: this.options.toolbars.customMenu,
});
this.toolbar.toolbarHandlers = Object.assign({}, this.toolbar.toolbarHandlers, this.sidebar.toolbarHandlers);
this.toolbar.collectMenuInfo(this.sidebar);
wrapperFragment.appendChild(this.sidebar.options.dom);
}
}
Expand All @@ -449,7 +450,7 @@ export default class Cherry extends CherryStatic {
buttonConfig: this.options.toolbars.float,
customMenu: this.options.toolbars.customMenu,
});
this.toolbar.toolbarHandlers = Object.assign({}, this.toolbar.toolbarHandlers, this.floatMenu.toolbarHandlers);
this.toolbar.collectMenuInfo(this.floatMenu);
}
}

Expand All @@ -468,7 +469,7 @@ export default class Cherry extends CherryStatic {
customMenu: this.options.toolbars.customMenu,
engine: this.engine,
});
this.toolbar.toolbarHandlers = Object.assign({}, this.toolbar.toolbarHandlers, this.bubble.toolbarHandlers);
this.toolbar.collectMenuInfo(this.bubble);
}
}

Expand Down
28 changes: 24 additions & 4 deletions src/toolbars/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,35 @@ export default class Toolbar {
});
}

/**
* 收集工具栏的各项信息,主要有:
* this.toolbarHandlers
* this.menus.hooks
* this.shortcutKeyMap
* @param {Toolbar} toolbarObj 工具栏对象
*/
collectMenuInfo(toolbarObj) {
this.toolbarHandlers = Object.assign({}, this.toolbarHandlers, toolbarObj.toolbarHandlers);
this.menus.hooks = Object.assign({}, this.menus.hooks, toolbarObj.menus.hooks);
// 只有没设置自定义快捷键的时候才需要收集其他toolbar对象的快捷键配置
if (!this.options.shortcutKey || Object.keys(this.options.shortcutKey).length <= 0) {
this.shortcutKeyMap = Object.assign({}, this.shortcutKeyMap, toolbarObj.shortcutKeyMap);
}
}

/**
* 收集快捷键
*/
collectShortcutKey() {
this.menus.allMenusName.forEach((name) => {
this.menus.hooks[name].shortcutKeys?.forEach((key) => {
this.shortcutKeyMap[key] = name;
if (this.options.shortcutKey && Object.keys(this.options.shortcutKey).length > 0) {
this.shortcutKeyMap = this.options.shortcutKey;
} else {
this.menus.allMenusName.forEach((name) => {
this.menus.hooks[name].shortcutKeys?.forEach((key) => {
this.shortcutKeyMap[key] = name;
});
});
});
}
}

collectToolbarHandler() {
Expand Down
2 changes: 2 additions & 0 deletions types/cherry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ export interface CherryToolbarOptions {
/** 新行悬停菜单配置 */
float?: any[] | false;
customMenu?: Record<string, any>;
/** 自定义快捷键 */
shortcutKey?: Object | false;
}

export interface CherryFileUploadHandler {
Expand Down

0 comments on commit 55e582f

Please sign in to comment.