Skip to content

Commit

Permalink
Fix bug not being able to disable H1 (#247)
Browse files Browse the repository at this point in the history
* Fixes bug not being able to omit heading1

---------

Co-authored-by: Steven DeMartini <[email protected]>
  • Loading branch information
mortmoe and sjdemartini authored Jul 22, 2024
1 parent 7a5a7f2 commit d312748
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/controls/MenuSelectHeading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,6 @@ export default function MenuSelectHeading({

const isCurrentlyParagraphOrHeading = selectedValue !== "";
const canSetParagraph = !!editor?.can().setParagraph();
// We have to pass a level when running `can`, so this is just an arbitrary
// one. And we have to check `currentLevel` to prevent all other heading
// levels from being disabled when Heading 1 is selected (see
// https://github.com/sjdemartini/mui-tiptap/issues/197).
const canSetHeading =
currentLevel === 1 || !!editor?.can().setHeading({ level: 1 });

// Figure out which settings the user has enabled with the heading extension
const enabledHeadingLevels: Set<Level> = useMemo(() => {
Expand All @@ -200,6 +194,22 @@ export default function MenuSelectHeading({
return new Set(headingExtension?.options.levels ?? []);
}, [editor]);

// In determining whether we can set a heading, at least one heading level
// must be enabled in the extension configuration. We have to pass a level
// when running `can().setHeading()`, so we just use the first one that is
// enabled. And since some Tiptap versions return `false` for
// `can().setHeading()` when passing the current level, we also have to check
// whether that arbitrary first level is the `currentLevel` (see
// https://github.com/sjdemartini/mui-tiptap/issues/197).
const firstEnabledHeadingResult = enabledHeadingLevels.values().next();
const firstEnabledHeading = firstEnabledHeadingResult.done
? undefined
: firstEnabledHeadingResult.value;
const canSetHeading =
firstEnabledHeading !== undefined &&
(currentLevel === firstEnabledHeading ||
!!editor?.can().setHeading({ level: firstEnabledHeading }));

return (
// We currently have to specify that the value is of type
// `HeadingOptionValue | ""` rather than just `HeadingOptionValue` due to
Expand Down

0 comments on commit d312748

Please sign in to comment.