Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: theme item highlight #959

Merged
merged 4 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Cherry.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ const defaultConfig = {
*/
// theme: 'dark', // light or dark
showToolbar: true, // false:不展示顶部工具栏; true:展示工具栏; toolbars.showToolbar=false 与 toolbars.toolbar=false 等效
subMenuHighlight: true, // 是否高亮子菜单
RSS1102 marked this conversation as resolved.
Show resolved Hide resolved
toolbar: [
'bold',
'italic',
Expand Down
8 changes: 8 additions & 0 deletions src/sass/cherry.scss
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@
background: $toolbarBtnBgHoverLight;
color: $toolbarBtnHoverColorLight;
}
&__selected {
background: $toolbarBtnBgHoverLight;
color: $toolbarBtnHoverColorLight;
}

.ch-icon {
margin-right: 10px;
Expand All @@ -169,6 +173,10 @@
background: $toolbarBtnBgHoverDark;
color: $toolbarBtnHoverColorDark;
}
&__selected {
background: $toolbarBtnBgHoverDark;
color: $toolbarBtnHoverColorLight;
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/sass/themes/blue.scss
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ $mdBlockquoteBg: $VIOLET1;
background: $toolbarBg;
.cherry-dropdown-item {
color: $toolbarBtnColor;
// 选中子菜单高亮
&__selected {
background-color: $toolbarBtnBgHover;
color: $toolbarBtnHoverColor;
}
&:hover {
background-color: $toolbarBtnBgHover;
color: $toolbarBtnHoverColor;
Expand Down
5 changes: 5 additions & 0 deletions src/sass/themes/dark.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ $mdSvgTextColor: rgb(250, 160, 0);
background: $toolbarBg;
.cherry-dropdown-item {
color: $toolbarBtnColor;
// 选中子菜单高亮
&__selected {
background-color: $toolbarBtnBgHover;
color: $toolbarBtnHoverColor;
}
&:hover {
background-color: $toolbarBtnBgHover;
color: $toolbarBtnHoverColor;
Expand Down
5 changes: 5 additions & 0 deletions src/sass/themes/green.scss
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ $mdBlockquoteBg: $GREEN1;
background: $toolbarBg;
.cherry-dropdown-item {
color: $toolbarBtnColor;
// 选中子菜单高亮
&__selected {
background-color: $toolbarBtnBgHover;
color: $toolbarBtnHoverColor;
}
&:hover {
background-color: $toolbarBtnBgHover;
color: $toolbarBtnHoverColor;
Expand Down
5 changes: 5 additions & 0 deletions src/sass/themes/light.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ $mdBlockquoteBg: rgb(231, 245, 255);
background: $toolbarBg;
.cherry-dropdown-item {
color: $toolbarBtnColor;
// 选中子菜单高亮
&__selected {
background-color: $toolbarBtnBgHover;
color: $toolbarBtnHoverColor;
}
&:hover {
background-color: $toolbarBtnBgHover;
color: $toolbarBtnHoverColor;
Expand Down
5 changes: 5 additions & 0 deletions src/sass/themes/red.scss
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ $mdBlockquoteBg: $PINK1;
background: $toolbarBg;
.cherry-dropdown-item {
color: $toolbarBtnColor;
// 选中子菜单高亮
&__selected {
background-color: $toolbarBtnBgHover;
color: $toolbarBtnHoverColor;
}
&:hover {
background-color: $toolbarBtnBgHover;
color: $toolbarBtnHoverColor;
Expand Down
5 changes: 5 additions & 0 deletions src/sass/themes/violet.scss
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ $mdBlockquoteBg: $VIOLET1;
background: $toolbarBg;
.cherry-dropdown-item {
color: $toolbarBtnColor;
// 选中子菜单高亮
&__selected {
background-color: $toolbarBtnBgHover;
color: $toolbarBtnHoverColor;
}
&:hover {
background-color: $toolbarBtnBgHover;
color: $toolbarBtnHoverColor;
Expand Down
9 changes: 9 additions & 0 deletions src/toolbars/MenuBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,4 +544,13 @@ export default class MenuBase {
}
return parent;
}

/**
* 绑定子菜单点击事件
* @param {HTMLDivElement} subMenuDomPanel
* @returns {number} 当前激活的子菜单索引
*/
getActiveSubMenuIndex(subMenuDomPanel) {
return -1;
}
}
14 changes: 14 additions & 0 deletions src/toolbars/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@ export default class Toolbar {
}
}

/**
* 激活二级菜单添加选中颜色
* @param {string} name
*/
activeSubMenuItem(name) {
const subMenu = this.subMenus[name];
const index = this.menus.hooks?.[name]?.getActiveSubMenuIndex(subMenu);
subMenu?.querySelectorAll('.cherry-dropdown-item').forEach((item, i) => {
item.classList.toggle('cherry-dropdown-item__selected', i === index);
});
}

/**
* 展开/收起二级菜单
*/
Expand All @@ -244,13 +256,15 @@ export default class Toolbar {
this.hideAllSubMenu();
this.drawSubMenus(name);
this.subMenus[name].style.display = 'block';
this.activeSubMenuItem(name);
return;
}
if (this.subMenus[name].style.display === 'none') {
// 如果是隐藏的,则先隐藏所有二级菜单,再显示当前二级菜单
this.hideAllSubMenu();
this.subMenus[name].style.display = 'block';
this.setSubMenuPosition(this.menus.hooks[name], this.subMenus[name]);
this.activeSubMenuItem(name);
} else {
// 如果是显示的,则隐藏当前二级菜单
this.subMenus[name].style.display = 'none';
Expand Down
36 changes: 20 additions & 16 deletions src/toolbars/hooks/Theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,27 @@ export default class Theme extends MenuBase {
this.setName('theme', 'insertChart');
this.subMenuConfig = [];
const self = this;
if (typeof $cherry.options.theme !== 'undefined') {
$cherry.options.theme.forEach((one) => {
self.subMenuConfig.push({
iconName: one.className,
name: one.label,
onclick: self.bindSubClick.bind(self, one.className),
});
});
} else {
$cherry.options.themeSettings.themeList.forEach((one) => {
self.subMenuConfig.push({
iconName: one.className,
name: one.label,
onclick: self.bindSubClick.bind(self, one.className),
});

const themes = $cherry.options.theme || $cherry.options.themeSettings.themeList;
themes.forEach((one) => {
self.subMenuConfig.push({
iconName: one.className,
name: one.label,
onclick: self.bindSubClick.bind(self, one.className),
});
}
});
}

/**
* 绑定子菜单点击事件
* @param {HTMLDivElement} subMenuDomPanel
* @returns {number} 当前激活的子菜单索引
*/
getActiveSubMenuIndex(subMenuDomPanel) {
const theme = this.$cherry.wrapperDom.className.match(/theme__([^\s]+)/)?.[1] || '';
return Array.from(subMenuDomPanel.querySelectorAll('.cherry-dropdown-item')).findIndex((item) =>
item.querySelector(`.ch-icon-${theme}`),
);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions types/cherry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,8 @@ export interface CherryToolbarsOptions<F extends CherryToolbarsCustomType = Cher
hiddenToolbar?: any[];
/** 是否展示顶部工具栏 */
showToolbar?: boolean;
/** 是否高亮子菜单 */
subMenuHighlight: boolean;
RSS1102 marked this conversation as resolved.
Show resolved Hide resolved
/** 侧边栏配置 */
sidebar?: any[] | false;
/** 选中悬停菜单配置 */
Expand Down
Loading