Skip to content

Commit

Permalink
Merge pull request #3350 from udecode/feat/heading
Browse files Browse the repository at this point in the history
Expose toc plugin queryHeading fuc
  • Loading branch information
zbeyens authored Jul 11, 2024
2 parents 0493014 + 6fc347c commit 89ea61f
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-moose-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-heading": patch
---

Expose toc plugin queryHeading fuction, this allows to customize the heading I need
14 changes: 12 additions & 2 deletions packages/heading/src/toc/createTocPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { createPluginFactory } from '@udecode/plate-common';
import {
type PlateEditor,
type Value,
createPluginFactory,
} from '@udecode/plate-common';

import type { Heading } from './types';

export const ELEMENT_TOC = 'toc';

export const createTocPlugin = createPluginFactory({
export interface TocPlugin {
queryHeading?: <V extends Value = Value>(editor: PlateEditor<V>) => Heading[];
}

export const createTocPlugin = createPluginFactory<TocPlugin>({
isElement: true,
isVoid: true,
key: ELEMENT_TOC,
Expand Down
6 changes: 4 additions & 2 deletions packages/heading/src/toc/hooks/useTocElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {

import type { Heading } from '../types';

import { getHeadingList, heightToTop } from '../../utils';
import { heightToTop } from '../../utils';
import { getHeadingList } from '../../utils/getHeadingList';

export type useTocElementStateProps = {
isScroll: boolean;
Expand All @@ -21,9 +22,10 @@ export const useTocElementState = ({
isScroll,
topOffset,
}: useTocElementStateProps) => {
const headingList = useEditorSelector(getHeadingList, []);
const editor = useEditorRef();

const headingList = useEditorSelector(getHeadingList, []);

const containerRef = React.useRef<HTMLElement | null>(null);

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/heading/src/toc/tocSideBar/useContentObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
useEditorSelector,
} from '@udecode/plate-common';

import { getHeadingList } from '../../utils';
import { getHeadingList } from '../../utils/getHeadingList';

interface UseContentObserver {
editorContentRef: React.RefObject<HTMLElement>;
Expand Down
3 changes: 2 additions & 1 deletion packages/heading/src/toc/tocSideBar/useTocSideBarState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
import type { Heading, TocSideBarProps } from '../types';

import { useContentController, useTocController } from '.';
import { checkIn, getHeadingList } from '../../utils';
import { checkIn } from '../../utils';
import { getHeadingList } from '../../utils/getHeadingList';

export const useTocSideBarState = ({
containerRef,
Expand Down
17 changes: 13 additions & 4 deletions packages/heading/src/utils/getHeadingList.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
type PlateEditor,
type TElement,
type Value,
getNodeEntries,
getNodeString,
getPluginOptions,
} from '@udecode/plate-common';

import type { Heading } from '../toc';

import {
ELEMENT_H1,
ELEMENT_H2,
Expand All @@ -15,9 +15,10 @@ import {
ELEMENT_H5,
ELEMENT_H6,
} from '../heading';
import { ELEMENT_TOC, type Heading, type TocPlugin } from '../toc';
import { isHeading } from './isHeading';

const headingDepth: Record<string, number> = {
export const headingDepth: Record<string, number> = {
[ELEMENT_H1]: 1,
[ELEMENT_H2]: 2,
[ELEMENT_H3]: 3,
Expand All @@ -26,7 +27,15 @@ const headingDepth: Record<string, number> = {
[ELEMENT_H6]: 6,
};

export const getHeadingList = (editor: PlateEditor) => {
export const getHeadingList = <V extends Value = Value>(
editor: PlateEditor<V>
) => {
const options = getPluginOptions<TocPlugin, V>(editor, ELEMENT_TOC);

if (options.queryHeading) {
return options.queryHeading<V>(editor);
}

const headingList: Heading[] = [];

const values = getNodeEntries(editor, {
Expand Down
1 change: 0 additions & 1 deletion packages/heading/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
*/

export * from './checkIn';
export * from './getHeadingList';
export * from './heightToTop';
export * from './isHeading';

0 comments on commit 89ea61f

Please sign in to comment.