client: fix tocs

This commit is contained in:
fantasticit 2022-05-29 23:41:29 +08:00
parent 3d5cf24ee5
commit 188cfca6d7
5 changed files with 65 additions and 16 deletions

View File

@ -0,0 +1,18 @@
export function flattenTree2Array(arr = []) {
const res = [];
const loopList = [...arr];
while (loopList.length) {
const node = loopList.pop();
res.push(node);
if (node?.children && node?.children.length) {
for (const sub of node.children) {
loopList.push(sub);
}
}
}
return res;
}

View File

@ -55,7 +55,7 @@ export const TableOfContentsWrapper = ({ editor }) => {
editor.view.dispatch(transaction); editor.view.dispatch(transaction);
setItems(headings); setItems(headings);
editor.eventEmitter.emit('TableOfContents', arrToTree(headings)); editor.eventEmitter && editor.eventEmitter.emit('TableOfContents', arrToTree(headings));
}, [editor]); }, [editor]);
useEffect(() => { useEffect(() => {

View File

@ -196,7 +196,7 @@ export const EditorInstance = forwardRef((props: IProps, ref) => {
ref={$mainContainer} ref={$mainContainer}
id={'js-tocs-container'} id={'js-tocs-container'}
style={{ style={{
padding: isMobile ? '0 24px' : '0 6rem', padding: isMobile ? `0 24px ${editable ? '42px' : 0}` : `0 6rem ${editable ? '42px' : 0}`,
}} }}
> >
<div className={cls(styles.contentWrap, editorWrapClassNames)}> <div className={cls(styles.contentWrap, editorWrapClassNames)}>

View File

@ -17,11 +17,22 @@
} }
} }
.collapsedItem { .dotWrap {
position: relative; display: flex;
width: 10px; flex-direction: column;
height: 10px; padding-left: 12px;
background-color: #d8d8d8;
border-radius: 50%; .dot {
position: relative;
width: 10px;
height: 10px;
cursor: pointer;
background-color: var(--semi-color-text-3);
border-radius: 50%;
& + .dot {
margin-top: 10px;
}
}
} }
} }

View File

@ -2,6 +2,7 @@ import { IconDoubleChevronLeft, IconDoubleChevronRight } from '@douyinfe/semi-ic
import { Anchor, Button, Tooltip } from '@douyinfe/semi-ui'; import { Anchor, Button, Tooltip } from '@douyinfe/semi-ui';
import { Editor } from '@tiptap/core'; import { Editor } from '@tiptap/core';
import { throttle } from 'helpers/throttle'; import { throttle } from 'helpers/throttle';
import { flattenTree2Array } from 'helpers/tree';
import { useDocumentStyle, Width } from 'hooks/use-document-style'; import { useDocumentStyle, Width } from 'hooks/use-document-style';
import { useToggle } from 'hooks/use-toggle'; import { useToggle } from 'hooks/use-toggle';
import React, { useCallback, useEffect } from 'react'; import React, { useCallback, useEffect } from 'react';
@ -79,6 +80,8 @@ export const Tocs: React.FC<{ tocs: Array<IToc>; editor: Editor }> = ({ tocs = [
toggleCollapsed(el.offsetWidth <= FULL_WIDTH); toggleCollapsed(el.offsetWidth <= FULL_WIDTH);
}, 200); }, 200);
handler();
window.addEventListener('resize', handler); window.addEventListener('resize', handler);
return () => { return () => {
@ -97,14 +100,31 @@ export const Tocs: React.FC<{ tocs: Array<IToc>; editor: Editor }> = ({ tocs = [
></Button> ></Button>
</header> </header>
<main> <main>
<Anchor {collapsed ? (
railTheme={collapsed ? 'muted' : 'tertiary'} <div
maxHeight={'calc(100vh - 360px)'} className={styles.dotWrap}
getContainer={getContainer} style={{
maxWidth={collapsed ? 56 : 180} maxHeight: 'calc(100vh - 360px)',
> }}
{tocs.length && tocs.map((toc) => <Toc key={toc.text} toc={toc} collapsed={collapsed} />)} >
</Anchor> {flattenTree2Array(tocs).map((toc) => {
return (
<Tooltip key={toc.text} content={toc.text} position="right">
<div className={styles.dot}></div>
</Tooltip>
);
})}
</div>
) : (
<Anchor
railTheme={collapsed ? 'muted' : 'tertiary'}
maxHeight={'calc(100vh - 360px)'}
getContainer={getContainer}
maxWidth={collapsed ? 56 : 180}
>
{tocs.length && tocs.map((toc) => <Toc key={toc.text} toc={toc} collapsed={collapsed} />)}
</Anchor>
)}
</main> </main>
</div> </div>
); );