Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(page): keep wide tables out from under the TOC rail (#21523)

Lobehub Coderc2affa24790f
diff --git a/src/features/PageEditor/PageEditor.tsx b/src/features/PageEditor/PageEditor.tsx
index fee0e024b31..a8eb635e8be 100644
--- a/src/features/PageEditor/PageEditor.tsx
+++ b/src/features/PageEditor/PageEditor.tsx
@@ -3,7 +3,7 @@
import { DEFAULT_BLOCK_ANCHOR_PADDING, EditorProvider } from '@lobehub/editor/react';
import { createStaticStyles, cssVar, Flexbox } from '@lobehub/ui';
import type { CSSProperties, FC, ReactNode, UIEvent } from 'react';
-import { memo, useCallback, useEffect, useLayoutEffect, useRef } from 'react';
+import { memo, useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
import { CONVERSATION_MIN_WIDTH } from '@/const/layoutTokens';
import type { ComposerTarget } from '@/features/Conversation/types';
@@ -33,7 +33,7 @@ import { PageAgentProvider } from './PageAgentProvider';
import { PageEditorProvider } from './PageEditorProvider';
import RightPanel from './RightPanel';
import { usePageEditorStore } from './store';
-import TableOfContents from './TableOfContents';
+import TableOfContents, { TOC_RAIL_GUTTER } from './TableOfContents';
import TitleSection from './TitleSection';
import { usePageEditable } from './usePageEditable';
@@ -102,8 +102,15 @@ const overrideStyles = createStaticStyles(({ css }) => ({
position: relative;
box-sizing: border-box;
- width: 100cqi;
+
+ /* Tables bleed to the pane edge; stop short of the TOC rail when it is shown. */
+ width: calc(100cqi - var(--lobe-pageeditor-toc-gutter, 0px));
margin-inline: calc(var(--lobe-pageeditor-table-bleed-inline) * -1);
+
+ /* The TOC rail is hidden at this width (see TableOfContents). */
+ @media (width <= 1024px) {
+ width: 100cqi;
+ }
}
.lobe-editor-table-scroll-wrapper .editor_table {
@@ -171,9 +178,11 @@ const PageEditorCanvas = memo<PageEditorCanvasProps>((props) => {
const tableBleedInline = wideScreen
? `${TABLE_BASE_BLEED}px`
: `calc(${TABLE_BASE_BLEED}px + max((100cqi - ${CONVERSATION_MIN_WIDTH}px) / 2, 0px))`;
+ const [tocVisible, setTocVisible] = useState(false);
const editorContentStyle = {
...styles.editorContent,
'--lobe-pageeditor-table-bleed-inline': tableBleedInline,
+ '--lobe-pageeditor-toc-gutter': tocVisible ? `${TOC_RAIL_GUTTER}px` : '0px',
} as CSSProperties;
const resizeFrameRef = useRef<number | undefined>(undefined);
const restoreScrollFrameRef = useRef<number | undefined>(undefined);
@@ -428,7 +437,11 @@ const PageEditorCanvas = memo<PageEditorCanvasProps>((props) => {
)}
</Flexbox>
{!isHtmlPage && (
- <TableOfContents contentRef={editorContentRef} scrollRef={contentWrapperRef} />
+ <TableOfContents
+ contentRef={editorContentRef}
+ scrollRef={contentWrapperRef}
+ onVisibleChange={setTocVisible}
+ />
)}
{documentId && <DiffAllToolbar documentId={documentId} editor={editor} />}
</Flexbox>
diff --git a/src/features/PageEditor/TableOfContents/index.test.tsx b/src/features/PageEditor/TableOfContents/index.test.tsx
index 3fbf1e0c9f0..de189559bae 100644
--- a/src/features/PageEditor/TableOfContents/index.test.tsx
+++ b/src/features/PageEditor/TableOfContents/index.test.tsx
@@ -22,7 +22,7 @@ const flush = async () => {
});
};
-const setup = () => {
+const setup = (onVisibleChange?: (visible: boolean) => void) => {
const pane = document.createElement('div');
const content = document.createElement('div');
pane.append(content);
@@ -31,8 +31,14 @@ const setup = () => {
Object.defineProperty(pane, 'scrollHeight', { configurable: true, value: 2000 });
setBody(content, '<h1>Intro</h1><h2>Usage</h2><h2>FAQ</h2>');
- render(<TableOfContents contentRef={{ current: content }} scrollRef={{ current: pane }} />);
- return { content, pane };
+ const view = render(
+ <TableOfContents
+ contentRef={{ current: content }}
+ scrollRef={{ current: pane }}
+ onVisibleChange={onVisibleChange}
+ />,
+ );
+ return { content, pane, view };
};
const activeText = () =>
@@ -149,4 +155,24 @@ describe('TableOfContents', () => {
expect(activeText()).toBe('Usage');
});
+
+ // The editor reserves room for the rail only while it is shown, so wide
+ // tables stop short of it instead of running underneath.
+ it('reports whether the rail is shown', async () => {
+ const onVisibleChange = vi.fn();
+ const { content, view } = setup(onVisibleChange);
+ await flush();
+ expect(onVisibleChange).toHaveBeenLastCalledWith(true);
+
+ setBody(content, '<h1>Only one heading</h1>');
+ await flush();
+ expect(onVisibleChange).toHaveBeenLastCalledWith(false);
+
+ setBody(content, '<h1>Intro</h1><h2>Usage</h2>');
+ await flush();
+ expect(onVisibleChange).toHaveBeenLastCalledWith(true);
+
+ view.unmount();
+ expect(onVisibleChange).toHaveBeenLastCalledWith(false);
+ });
});
diff --git a/src/features/PageEditor/TableOfContents/index.tsx b/src/features/PageEditor/TableOfContents/index.tsx
index 65acca5fc33..1709d2eca26 100644
--- a/src/features/PageEditor/TableOfContents/index.tsx
+++ b/src/features/PageEditor/TableOfContents/index.tsx
@@ -13,6 +13,9 @@ import { collectHeadings, findActiveIndex, findBodyRoot, isSameOutline } from '.
const ACTIVE_THRESHOLD = 96;
const SCROLL_MARGIN = 24;
const MIN_HEADINGS = 2;
+// Kept tight to the pane edge: the strip the rail takes is width wide tables lose.
+const RAIL_INSET = 4;
+const RAIL_PADDING_INLINE = 4;
const prefersReducedMotion = () =>
window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ?? false;
@@ -20,11 +23,12 @@ const prefersReducedMotion = () =>
const styles = stylex.create({
container: {
alignItems: 'flex-end',
+ // PageEditor drops the table gutter at the same breakpoint.
display: { 'default': 'flex', '@media (max-width: 1024px)': 'none' },
flexDirection: 'column',
// Vertically centered on the right edge, like Notion's outline rail.
insetBlockStart: '50%',
- insetInlineEnd: 12,
+ insetInlineEnd: RAIL_INSET,
position: 'absolute',
transform: 'translateY(-50%)',
zIndex: 10,
@@ -66,7 +70,7 @@ const styles = stylex.create({
flexDirection: 'column',
gap: 10,
paddingBlock: 8,
- paddingInline: 8,
+ paddingInline: RAIL_PADDING_INLINE,
},
panel: {
backgroundColor: cssVar.colorBgElevated,
@@ -96,14 +100,19 @@ const styles = stylex.create({
const LINE_WIDTH: Record<TocHeading['level'], number> = { 1: 20, 2: 14, 3: 9 };
const ITEM_INDENT: Record<TocHeading['level'], number> = { 1: 8, 2: 20, 3: 32 };
+/** Width of the strip the rail (including its hover area) takes from the pane's inline end. */
+export const TOC_RAIL_GUTTER = RAIL_INSET + RAIL_PADDING_INLINE * 2 + LINE_WIDTH[1];
+
interface TableOfContentsProps {
/** Element holding the page body; its first Lexical root is the outline source. */
contentRef: RefObject<HTMLElement | null>;
+ /** Called when the rail appears or disappears, so the pane can keep content out of its way. */
+ onVisibleChange?: (visible: boolean) => void;
/** Scrollable pane wrapping the body. */
scrollRef: RefObject<HTMLElement | null>;
}
-const TableOfContents = memo<TableOfContentsProps>(({ contentRef, scrollRef }) => {
+const TableOfContents = memo<TableOfContentsProps>(({ contentRef, onVisibleChange, scrollRef }) => {
const { t } = useTranslation('file');
const [headings, setHeadings] = useState<TocHeading[]>([]);
const [activeIndex, setActiveIndex] = useState(-1);
@@ -227,7 +236,15 @@ const TableOfContents = memo<TableOfContentsProps>(({ contentRef, scrollRef }) =
[scrollRef],
);
- if (headings.length < MIN_HEADINGS) return null;
+ const visible = headings.length >= MIN_HEADINGS;
+
+ useEffect(() => {
+ onVisibleChange?.(visible);
+ }, [onVisibleChange, visible]);
+
+ useEffect(() => () => onVisibleChange?.(false), [onVisibleChange]);
+
+ if (!visible) return null;
return (
<nav