Skip to content

Commit

Permalink
feat: 为 TocHeading 组件添加 anchorProps 属性以支持自定义锚点配置
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuai2144 committed Feb 2, 2025
1 parent 7d24aed commit 879a466
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
18 changes: 12 additions & 6 deletions src/MarkdownEditor/editor/tools/Leading.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Anchor } from 'antd';
import { Anchor, AnchorProps } from 'antd';
import { nanoid } from 'nanoid';
import React, { useCallback, useEffect, useRef } from 'react';

import { Node } from 'slate';
import { useDebounce, useGetSetState } from '../../index';
import { Elements, useDebounce, useGetSetState } from '../../index';
import { useEditorStore } from '../store';
import { getOffsetTop, slugify } from '../utils/dom';

Expand Down Expand Up @@ -78,10 +78,15 @@ export const schemaToHeading = (schema: any) => {
}));
};

/**
* 配置次级标题的锚点
*/
export const TocHeading = ({ schema }: { schema: any }) => {
interface TocHeadingProps {
schema: Elements[];
anchorProps?: AnchorProps;
}

export const TocHeading: React.FC<TocHeadingProps> = ({
schema,
anchorProps,
}) => {
const { store, markdownEditorRef } = useEditorStore();
const [state, setState] = useGetSetState({
headings: [] as Leading[],
Expand Down Expand Up @@ -156,6 +161,7 @@ export const TocHeading = ({ schema }: { schema: any }) => {
minWidth: 200,
}}
offsetTop={64}
{...anchorProps}
items={buildTree(state().headings).children?.map((h: any) => ({
id: h.id,
key: h.key,
Expand Down
6 changes: 4 additions & 2 deletions src/MarkdownEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
useSystemKeyboard,
} from './editor/utils/keyboard';

import { ConfigProvider } from 'antd';
import { AnchorProps, ConfigProvider } from 'antd';
import classNames from 'classnames';
import { Subject } from 'rxjs';
import { BaseEditor, createEditor, Selection } from 'slate';
Expand Down Expand Up @@ -121,6 +121,8 @@ export type MarkdownEditorProps = {
codeProps?: {
Languages?: string[];
};

anchorProps?: AnchorProps;
/**
* 配置图片数据
*/
Expand Down Expand Up @@ -481,7 +483,7 @@ export const MarkdownEditor: React.FC<MarkdownEditorProps> = (props) => {
comment={props.comment}
/>
) : (
<TocHeading schema={schema} />
<TocHeading schema={schema} anchorProps={props.anchorProps} />
)
) : null}
</div>
Expand Down

1 comment on commit 879a466

@vercel
Copy link

@vercel vercel bot commented on 879a466 Feb 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.