Skip to content

Commit 398f82e

Browse files
authored
chore: fix the outline content (AppFlowy-IO#76)
1 parent 4276709 commit 398f82e

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/components/editor/components/blocks/outline/Outline.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { forwardRef, memo, useCallback, useEffect, useState } from 'react';
1+
import { forwardRef, memo, useCallback, useEffect, useLayoutEffect, useState } from 'react';
22
import { useTranslation } from 'react-i18next';
33
import { Element } from 'slate';
44
import { useReadOnly, useSlate } from 'slate-react';
@@ -12,12 +12,14 @@ export const Outline = memo(
1212
const [root, setRoot] = useState<HeadingNode[]>([]);
1313
const { t } = useTranslation();
1414
const readOnly = useReadOnly() || editor.isElementReadOnly(node as unknown as Element);
15+
const [isReady, setIsReady] = useState(false);
1516

1617
useEffect(() => {
18+
if (!isReady) return;
1719
const root = nestHeadings(extractHeadings(editor, node.data.depth || 6));
1820

1921
setRoot(root);
20-
}, [editor, node.data.depth, editor.children]);
22+
}, [editor, node.data.depth, editor.children, isReady]);
2123

2224
const jumpToHeading = useCallback((heading: HeadingNode) => {
2325
const id = `heading-${heading.blockId}`;
@@ -62,6 +64,12 @@ export const Outline = memo(
6264
[jumpToHeading]
6365
);
6466

67+
useLayoutEffect(() => {
68+
setTimeout(() => {
69+
setIsReady(true);
70+
}, 1000);
71+
}, []);
72+
6573
return (
6674
<div
6775
{...attributes}
@@ -72,7 +80,7 @@ export const Outline = memo(
7280
<div className={'absolute left-0 top-0 select-none caret-transparent'}>{children}</div>
7381
<div contentEditable={false} className={`flex w-full select-none flex-col`}>
7482
<div className={'text-md my-2 font-bold'}>{t('document.outlineBlock.placeholder')}</div>
75-
{root.map(renderHeading)}
83+
{isReady && root.map(renderHeading)}
7684
</div>
7785
</div>
7886
);

src/components/editor/components/blocks/outline/utils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { BlockType } from '@/application/types';
2-
import { CustomEditor } from 'src/application/slate-yjs/command';
3-
import { HeadingNode } from '@/components/editor/editor.type';
41
import { Element, Text } from 'slate';
52
import { ReactEditor } from 'slate-react';
63

4+
import { BlockType } from '@/application/types';
5+
import { HeadingNode } from '@/components/editor/editor.type';
6+
7+
import { CustomEditor } from 'src/application/slate-yjs/command';
8+
79
export function extractHeadings (editor: ReactEditor, maxDepth: number): HeadingNode[] {
810
const headings: HeadingNode[] = [];
911
const blocks = editor.children;
@@ -20,7 +22,7 @@ export function extractHeadings (editor: ReactEditor, maxDepth: number): Heading
2022
...block,
2123
data: {
2224
level: (block as HeadingNode).data.level,
23-
text: CustomEditor.getBlockTextContent(block, 2),
25+
text: CustomEditor.getBlockTextContent(block, 2).trim(),
2426
},
2527
children: [],
2628
} as HeadingNode);

0 commit comments

Comments
 (0)