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: option to not highlighting active list button and toParagraph action improvement #196

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading