Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(docs-ui): docs heading-menu auto scroll (#4906)

ZhangWei2990f2a0efd3
diff --git a/packages/design/src/components/side-menu/index.tsx b/packages/design/src/components/side-menu/index.tsx
index d2bb02c05e..ff1d349934 100644
--- a/packages/design/src/components/side-menu/index.tsx
+++ b/packages/design/src/components/side-menu/index.tsx
@@ -56,9 +56,17 @@ export const SideMenu = forwardRef<ISideMenuInstance, ISideMenuProps>((props, re
const { menus, onClick, className, style, mode, maxHeight, activeId, open, onOpenChange, maxWidth, wrapperClass, wrapperStyle, iconClass, iconStyle } = props;
const isSideBar = mode === 'side-bar';
const containerRef = useRef<HTMLDivElement>(null);
+ const menusRef = useRef(menus);
+ menusRef.current = menus;
const instance: ISideMenuInstance = useMemo(() => ({
scrollTo: (id: string) => {
- document.getElementById(`univer-side-menu-${id}`)?.scrollIntoView({ behavior: 'smooth' });
+ if (!menusRef.current) return;
+ const index = menusRef.current.findIndex((menu) => menu.id === id);
+ if (index === -1) return;
+ containerRef.current?.scrollTo({
+ behavior: 'smooth',
+ top: 32 * index,
+ });
},
}), []);
diff --git a/packages/docs-ui/src/commands/commands/doc-delete.command.ts b/packages/docs-ui/src/commands/commands/doc-delete.command.ts
index 20a64ce38d..12fda5d0ec 100644
--- a/packages/docs-ui/src/commands/commands/doc-delete.command.ts
+++ b/packages/docs-ui/src/commands/commands/doc-delete.command.ts
@@ -157,6 +157,7 @@ export const MergeTwoParagraphCommand: ICommand<IMergeTwoParagraphParams> = {
return false;
}
+ const dataStream = originBody.dataStream;
const actualRange = activeRange;
const unitId = docDataModel.getUnitId();
@@ -223,21 +224,24 @@ export const MergeTwoParagraphCommand: ICommand<IMergeTwoParagraphParams> = {
len: nextParagraph.startIndex - curParagraph.startIndex - 1,
});
}
-
- textX.push({
- t: TextXActionType.RETAIN,
- len: 1,
- coverType: UpdateDocsAttributeType.REPLACE,
- body: {
- dataStream: '',
- paragraphs: [
- {
- ...Tools.deepClone(curParagraph),
- startIndex: 0,
- },
- ],
- },
- });
+ const tokens = Object.values(DataStreamTreeTokenType) as string[];
+ const lastToken = dataStream[curParagraph.startIndex - 1];
+ if ((lastToken && !tokens.includes(lastToken)) || lastToken === ' ') {
+ textX.push({
+ t: TextXActionType.RETAIN,
+ len: 1,
+ coverType: UpdateDocsAttributeType.REPLACE,
+ body: {
+ dataStream: '',
+ paragraphs: [
+ {
+ ...Tools.deepClone(curParagraph),
+ startIndex: 0,
+ },
+ ],
+ },
+ });
+ }
const path = getRichTextEditPath(docDataModel, segmentId);
doMutation.params.actions = jsonX.editOp(textX.serialize(), path);
diff --git a/packages/docs-ui/src/views/side-menu/index.tsx b/packages/docs-ui/src/views/side-menu/index.tsx
index ef89661ace..f048c263b0 100644
--- a/packages/docs-ui/src/views/side-menu/index.tsx
+++ b/packages/docs-ui/src/views/side-menu/index.tsx
@@ -104,17 +104,19 @@ export function DocSideMenu() {
};
});
- const menus = [
- ...(title
- ? [{
- id: TITLE_ID,
- text: title,
- level: NamedStyleType.TITLE,
- isTitle: true,
- }]
- : []),
- ...(paragraphMenus ?? []),
- ].filter(Boolean);
+ const menus = paragraphMenus?.find((p) => p.isTitle)
+ ? paragraphMenus
+ : [
+ ...(title
+ ? [{
+ id: TITLE_ID,
+ text: title,
+ level: NamedStyleType.TITLE,
+ isTitle: true,
+ }]
+ : []),
+ ...(paragraphMenus ?? []),
+ ].filter(Boolean);
const [open, setOpen] = useState(true);
const activeId = findActiveHeading(paragraphBounds ?? [], scrollTop, bottom);