Skip to content

Commit bac13dc

Browse files
authored
feat(toolbar): show current heading level in editor toolbar (#696)
1 parent 1c539ee commit bac13dc

File tree

6 files changed

+19
-5
lines changed

6 files changed

+19
-5
lines changed

src/bundle/config/w-heading-config.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export const wHeading6ItemData: WToolbarListButtonItemData = {
8282
export const wHeadingListConfig: WToolbarListButtonData = {
8383
icon: icons.headline,
8484
withArrow: true,
85+
replaceActiveIcon: true,
8586
title: i18n.bind(null, 'heading'),
8687
data: [
8788
wTextItemData,

src/bundle/toolbar/utils/toolbarsConfigs.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ interface TransformedItem {
2929
icon?: ToolbarIconData;
3030
hotkey?: string;
3131
withArrow?: boolean;
32+
replaceActiveIcon?: true;
3233
doNotActivateList?: boolean;
3334
preview?: React.ReactNode;
3435
wysiwyg?: ToolbarItemWysiwyg<ToolbarDataType>;
@@ -59,7 +60,10 @@ const transformItem = (
5960
hotkey: item.view.hotkey,
6061
doNotActivateList: item.view.doNotActivateList,
6162
...(isSingleButton && {preview: (item.view as any).preview}),
62-
...(isListButton && {withArrow: (item.view as any).withArrow}),
63+
...(isListButton && {
64+
withArrow: (item.view as any).withArrow,
65+
replaceActiveIcon: (item.view as any).replaceActiveIcon,
66+
}),
6367
...(type === 'wysiwyg' && item.wysiwyg && {...item.wysiwyg}),
6468
...(type === 'markup' && item.markup && {...item.markup}),
6569
};

src/modules/toolbars/items.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ export const headingListItemView: ToolbarItemView<ToolbarDataType.ListButton> =
806806
icon: icons.headline,
807807
title: i18n.bind(null, 'heading'),
808808
withArrow: true,
809+
replaceActiveIcon: true,
809810
};
810811

811812
// ---- Lists list ----

src/modules/toolbars/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export type ToolbarItemView<T extends ToolbarDataType = ToolbarDataType.SingleBu
3434
withArrow?: boolean;
3535
icon: ToolbarIconData;
3636
title: string | (() => string);
37+
/** When state changes to active, replace default icon with icon of first active item */
38+
replaceActiveIcon?: boolean;
3739
}
3840
: {});
3941

src/toolbar/ToolbarListButton.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,13 @@ export function ToolbarListButton<E>({
3535
withArrow,
3636
data,
3737
alwaysActive,
38+
replaceActiveIcon,
3839
}: ToolbarListButtonProps<E>) {
3940
const [anchorElement, setAnchorElement] = useElementState();
4041
const [open, , hide, toggleOpen] = useBooleanState(false);
4142
const [popupItem, setPopupItem] = useState<ToolbarButtonPopupData<E>>();
4243

43-
const someActive = alwaysActive
44-
? false
45-
: data.some((item) => item.isActive(editor) && !item.doNotActivateList);
4644
const everyDisabled = alwaysActive ? false : data.every((item) => !item.isEnable(editor));
47-
4845
const popupOpen = everyDisabled ? false : open;
4946
const shouldForceHide = open && !popupOpen;
5047
useEffect(() => {
@@ -55,6 +52,13 @@ export function ToolbarListButton<E>({
5552

5653
if (data.length === 0) return null;
5754

55+
const activeItem = data.find((item) => item.isActive(editor) && !item.doNotActivateList);
56+
const someActive = alwaysActive ? false : Boolean(activeItem);
57+
58+
if (replaceActiveIcon && someActive && activeItem) {
59+
icon = activeItem.icon;
60+
}
61+
5862
const buttonContent = [<Icon key={1} data={icon.data} size={icon.size ?? 16} />];
5963
if (withArrow) {
6064
buttonContent.push(<Fragment key={2}>{''}</Fragment>);

src/toolbar/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ export type ToolbarListButtonData<E> = {
9797
data: ToolbarListButtonItemData<E>[];
9898
alwaysActive?: boolean;
9999
hideDisabled?: boolean;
100+
/** When state changes to active, replace default icon with icon of first active item */
101+
replaceActiveIcon?: boolean;
100102
};
101103

102104
/**

0 commit comments

Comments
 (0)