Skip to content

Commit

Permalink
feat: option to not highlighting active list button and toParagraph a…
Browse files Browse the repository at this point in the history
…ction improvement
  • Loading branch information
smsochneg committed Feb 20, 2024
1 parent c64dd68 commit a505cc2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/extensions/base/BaseSchema/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {setBlockType} from 'prosemirror-commands';
import {chainCommands, setBlockType} from 'prosemirror-commands';
import type {Command} from 'prosemirror-state';
import {hasParentNodeOfType} from 'prosemirror-utils';

Expand Down Expand Up @@ -31,9 +31,11 @@ export const BaseSchema: ExtensionAuto<BaseSchemaOptions> = (builder, opts) => {
builder.addAction(pAction, ({schema}) => {
const p = pType(schema);
const cmd = setBlockType(p);
const isParagraph: Command = (state) => hasParentNodeOfType(p)(state.selection);

return {
isActive: (state) => hasParentNodeOfType(p)(state.selection),
isEnable: cmd,
isActive: isParagraph,
isEnable: chainCommands(isParagraph, cmd),
run: cmd,
};
});
Expand Down
8 changes: 5 additions & 3 deletions src/toolbar/ToolbarListButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {i18n} from '../i18n/common';
import {useBooleanState} from '../react-utils/hooks';

import {ToolbarTooltipDelay} from './const';
import {ToolbarBaseProps, ToolbarIconData, ToolbarItemData} from './types';
import {ToolbarBaseProps, ToolbarIconData, ToolbarListButtonItemData} from './types';

import './ToolbarListButton.scss';

Expand All @@ -20,7 +20,7 @@ export type ToolbarListButtonData<E> = {
icon: ToolbarIconData;
title: string | (() => string);
withArrow?: boolean;
data: ToolbarItemData<E>[];
data: ToolbarListButtonItemData<E>[];
alwaysActive?: boolean;
hideDisabled?: boolean;
};
Expand All @@ -41,7 +41,9 @@ export function ToolbarListButton<E>({
const buttonRef = React.useRef<HTMLButtonElement>(null);
const [open, , hide, toggleOpen] = useBooleanState(false);

const someActive = alwaysActive ? false : data.some((item) => item.isActive(editor));
const someActive = alwaysActive
? false
: data.some((item) => item.isActive(editor) && !item.doNotActivateList);
const everyDisabled = alwaysActive ? false : data.every((item) => !item.isEnable(editor));

const popupOpen = everyDisabled ? false : open;
Expand Down
4 changes: 4 additions & 0 deletions src/toolbar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export type ToolbarReactComponentData<E> = {
component: React.ComponentType<ToolbarBaseProps<E>>;
};

export type ToolbarListButtonItemData<E> = ToolbarItemData<E> & {
doNotActivateList?: boolean;
};

export type ToolbarReactNodeData = {
id: string;
type: ToolbarDataType.ReactNode;
Expand Down

0 comments on commit a505cc2

Please sign in to comment.