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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(docs-ui): paragraph menu issues (#4951)
ZhangWeiec94108fe918
diff --git a/packages/docs-ui/src/commands/commands/break-line.command.ts b/packages/docs-ui/src/commands/commands/break-line.command.tsindex ddcc196fb5..606d91e5a7 100644--- a/packages/docs-ui/src/commands/commands/break-line.command.ts+++ b/packages/docs-ui/src/commands/commands/break-line.command.ts@@ -16,7 +16,7 @@import type { DocumentDataModel, ICommand, IDocumentBody, IMutationInfo, IParagraph, IParagraphBorder, ITextRangeParam } from '@univerjs/core';import type { IRichTextEditingMutationParams } from '@univerjs/docs';-import { BuildTextUtils, CommandType, DataStreamTreeTokenType, ICommandService, IUniverInstanceService, JSONX, PresetListType, TextX, TextXActionType, Tools, UniverInstanceType, UpdateDocsAttributeType } from '@univerjs/core';+import { BuildTextUtils, CommandType, DataStreamTreeTokenType, generateRandomId, ICommandService, IUniverInstanceService, JSONX, PresetListType, TextX, TextXActionType, Tools, UniverInstanceType, UpdateDocsAttributeType } from '@univerjs/core';import { DocSelectionManagerService, RichTextEditingMutation } from '@univerjs/docs';import { getTextRunAtPosition } from '../../basics/paragraph';import { DocMenuStyleService } from '../../services/doc-menu-style.service';@@ -50,6 +50,9 @@ export function generateParagraphs(if (prevParagraph.paragraphStyle) {paragraph.paragraphStyle = Tools.deepClone(prevParagraph.paragraphStyle);delete paragraph.paragraphStyle.borderBottom;+ if (prevParagraph.paragraphStyle.headingId) {+ paragraph.paragraphStyle.headingId = generateRandomId(6);+ }}}}@@ -119,12 +122,11 @@ export const BreakLineCommand: ICommand<IBreakLineCommandParams> = {if (!prevParagraph) {return false;}-+ const isAtParagraphEnd = startOffset === prevParagraph.startIndex;const prevParagraphIndex = prevParagraph.startIndex;const defaultTextStyle = docMenuStyleService.getDefaultStyle();const styleCache = docMenuStyleService.getStyleCache();const curTextRun = getTextRunAtPosition(originBody, endOffset, defaultTextStyle, styleCache);-const insertBody: IDocumentBody = {dataStream: DataStreamTreeTokenType.PARAGRAPH,paragraphs: generateParagraphs(@@ -210,8 +212,12 @@ export const BreakLineCommand: ICommand<IBreakLineCommandParams> = {...prevParagraph,paragraphStyle: {...prevParagraph.paragraphStyle,- headingId: undefined,- namedStyleType: undefined,+ ...isAtParagraphEnd+ ? {+ headingId: undefined,+ namedStyleType: undefined,+ }+ : null,},startIndex: 0,bullet: prevParagraph.paragraphStyle?.headingId@@ -235,7 +241,6 @@ export const BreakLineCommand: ICommand<IBreakLineCommandParams> = {const path = getRichTextEditPath(docDataModel, segmentId);doMutation.params.actions = jsonX.editOp(textX.serialize(), path);-const result = commandService.syncExecuteCommand<IRichTextEditingMutationParams,IRichTextEditingMutationParamsdiff --git a/packages/docs-ui/src/commands/commands/set-heading.command.ts b/packages/docs-ui/src/commands/commands/set-heading.command.tsindex d609e23fa7..084939d76b 100644--- a/packages/docs-ui/src/commands/commands/set-heading.command.ts+++ b/packages/docs-ui/src/commands/commands/set-heading.command.ts@@ -16,6 +16,7 @@import type { DocumentDataModel, ICommand, IMutationInfo, ITextRangeParam } from '@univerjs/core';import type { IRichTextEditingMutationParams } from '@univerjs/docs';+import type { ITextRangeWithStyle } from '@univerjs/engine-render';import { BuildTextUtils, CommandType, generateRandomId, ICommandService, IUniverInstanceService, JSONX, NamedStyleType, TextX, TextXActionType, UniverInstanceType } from '@univerjs/core';import { DocSelectionManagerService, RichTextEditingMutation } from '@univerjs/docs';import { getRichTextEditPath } from '../util';@@ -34,19 +35,19 @@ export const SetParagraphNamedStyleCommand: ICommand<ISetParagraphNamedStyleComm}const univerInstanceService = accessor.get(IUniverInstanceService);- const documentDataModel = univerInstanceService.getCurrentUnitOfType<DocumentDataModel>(UniverInstanceType.UNIVER_DOC);- if (!documentDataModel) {+ const doc = univerInstanceService.getCurrentUnitOfType<DocumentDataModel>(UniverInstanceType.UNIVER_DOC);+ if (!doc) {return false;}- const unitId = documentDataModel.getUnitId();+ const unitId = doc.getUnitId();const selectionService = accessor.get(DocSelectionManagerService);- const selections = params.textRanges ?? selectionService.getTextRanges({ unitId, subUnitId: unitId })?.filter((i) => !i.segmentId);+ const selections = params.textRanges ?? selectionService.getTextRanges({ unitId, subUnitId: unitId });if (!selections?.length) {return false;}-+ const segmentId = selections[0].segmentId;const textX = BuildTextUtils.paragraph.style.set({- document: documentDataModel,+ document: doc.getSelfOrHeaderFooterModel(segmentId),textRanges: selections,style: {namedStyleType: params.value,@@ -62,13 +63,14 @@ export const SetParagraphNamedStyleCommand: ICommand<ISetParagraphNamedStyleCommid: RichTextEditingMutation.id,params: {actions: [],- textRanges: selections,+ textRanges: selections as ITextRangeWithStyle[],unitId,+ segmentId,},};const jsonX = JSONX.getInstance();- const path = getRichTextEditPath(documentDataModel);+ const path = getRichTextEditPath(doc, segmentId);doMutation.params.actions = jsonX.editOp(textX.serialize(), path);const commandService = accessor.get(ICommandService);const result = commandService.syncExecuteCommand(doMutation.id, doMutation.params);diff --git a/packages/docs-ui/src/components/paragraph-menu/index.tsx b/packages/docs-ui/src/components/paragraph-menu/index.tsxindex 08c6b3f223..b6c1c0d09e 100644--- a/packages/docs-ui/src/components/paragraph-menu/index.tsx+++ b/packages/docs-ui/src/components/paragraph-menu/index.tsx@@ -14,17 +14,16 @@* limitations under the License.*/+import type { DocumentDataModel } from '@univerjs/core';import type { IPopup } from '@univerjs/ui';-import { ICommandService, Injector, NamedStyleType } from '@univerjs/core';-import { SetTextSelectionsOperation } from '@univerjs/docs';+import { ICommandService, IUniverInstanceService, NamedStyleType, UniverInstanceType } from '@univerjs/core';import { IRenderManagerService } from '@univerjs/engine-render';import { DownSingle } from '@univerjs/icons';import { ContextMenuPosition, DesktopMenu, ILayoutService, RectPopup, useDependency, useObservable } from '@univerjs/ui';import { useMemo, useRef, useState } from 'react';-import { BehaviorSubject, Observable } from 'rxjs';-import { SetInlineFormatFontSizeCommand } from '../../commands/commands/inline-format.command';-import { getParagraphStyleAtCursor } from '../../controllers/menu/menu';+import { BehaviorSubject } from 'rxjs';import { HEADING_ICON_MAP } from '../../controllers/menu/paragraph-menu';+import { DocEventManagerService } from '../../services/doc-event-manager.service';import { DocParagraphMenuService } from '../../services/doc-paragraph-menu.service';export const ParagraphMenu = ({ popup }: { popup: IPopup }) => {@@ -35,37 +34,17 @@ export const ParagraphMenu = ({ popup }: { popup: IPopup }) => {const anchorRef = useRef<HTMLDivElement>(null);const isMouseOver = useRef(false);const renderManagerService = useDependency(IRenderManagerService);+ const univerInstanceService = useDependency(IUniverInstanceService);const renderUnit = renderManagerService.getRenderById(popup.unitId);+ const doc = univerInstanceService.getUnit<DocumentDataModel>(popup.unitId, UniverInstanceType.UNIVER_DOC);const docParagraphMenuService = renderUnit?.with(DocParagraphMenuService);-- const accessor = useDependency(Injector);- const textType = useObservable(useMemo(() => new Observable<NamedStyleType>((subscriber) => {- const DEFAULT_TYPE = NamedStyleType.NORMAL_TEXT;- const calc = () => {- const paragraph = getParagraphStyleAtCursor(accessor);- if (paragraph == null) {- subscriber.next(DEFAULT_TYPE);- return;- }-- const namedStyleType = paragraph.paragraphStyle?.namedStyleType ?? DEFAULT_TYPE;- subscriber.next(namedStyleType);- };-- const disposable = commandService.onCommandExecuted((c) => {- const id = c.id;-- if (id === SetTextSelectionsOperation.id || id === SetInlineFormatFontSizeCommand.id) {- calc();- }- });-- calc();-- return () => disposable.dispose();- }), [commandService]));-- const icon = HEADING_ICON_MAP[textType ?? NamedStyleType.NORMAL_TEXT];+ const docEventManagerService = renderUnit?.with(DocEventManagerService);+ const paragraph = useObservable(docEventManagerService?.hoverParagraph$);+ const paragraphLeft = useObservable(docEventManagerService?.hoverParagraphLeft$);+ const startIndex = (paragraph ?? paragraphLeft)?.startIndex;+ const paragraphObj = useMemo(() => doc?.getBody()?.paragraphs?.find((p) => p.startIndex === startIndex), [doc, startIndex]);+ const namedStyleType = paragraphObj?.paragraphStyle?.namedStyleType;+ const icon = HEADING_ICON_MAP[namedStyleType ?? NamedStyleType.NORMAL_TEXT];const anchorRect$ = useMemo(() => new BehaviorSubject({left: 0,right: 0,diff --git a/packages/docs-ui/src/controllers/render-controllers/doc-checklist.render-controller.ts b/packages/docs-ui/src/controllers/render-controllers/doc-checklist.render-controller.tsindex 6b9e90df9f..323473ac66 100644--- a/packages/docs-ui/src/controllers/render-controllers/doc-checklist.render-controller.ts+++ b/packages/docs-ui/src/controllers/render-controllers/doc-checklist.render-controller.ts@@ -14,11 +14,11 @@* limitations under the License.*/+import type { DocumentDataModel } from '@univerjs/core';+import type { Documents, IRenderContext, IRenderModule, Viewport } from '@univerjs/engine-render';import { Disposable, ICommandService, Inject } from '@univerjs/core';import { DocSelectionManagerService, DocSkeletonManagerService } from '@univerjs/docs';import { CURSOR_TYPE, Vector2 } from '@univerjs/engine-render';-import type { DocumentDataModel } from '@univerjs/core';-import type { Documents, IRenderContext, IRenderModule, Viewport } from '@univerjs/engine-render';import { ToggleCheckListCommand } from '../../commands/commands/list.command';import { DocEventManagerService } from '../../services/doc-event-manager.service';diff --git a/packages/docs-ui/src/services/doc-event-manager.service.ts b/packages/docs-ui/src/services/doc-event-manager.service.tsindex 8143fd7ae2..b6ef935724 100644--- a/packages/docs-ui/src/services/doc-event-manager.service.ts+++ b/packages/docs-ui/src/services/doc-event-manager.service.ts@@ -345,12 +345,13 @@ export class DocEventManagerService extends Disposable implements IRenderModuleif (down.button === 2) {return;}- const ranges = this._calcActiveRanges(down);+ const point = transformOffset2Bound(down.offsetX, down.offsetY, this._context.scene);+ const ranges = this._calcActiveRanges(point);if (ranges.length) {this._clickCustomRanges$.next(ranges.pop()!);}- const bullet = this._calcActiveBullet(down);+ const bullet = this._calcActiveBullet(point);if (bullet) {this._clickBullet$.next(bullet);}diff --git a/packages/docs-ui/src/services/doc-paragraph-menu.service.ts b/packages/docs-ui/src/services/doc-paragraph-menu.service.tsindex cbdf38c012..539544e1e3 100644--- a/packages/docs-ui/src/services/doc-paragraph-menu.service.ts+++ b/packages/docs-ui/src/services/doc-paragraph-menu.service.ts@@ -18,8 +18,8 @@ import type { DocumentDataModel, INeedCheckDisposable, IParagraphRange, Nullableimport type { IRenderContext, IRenderModule } from '@univerjs/engine-render';import type { IMutiPageParagraphBound } from './doc-event-manager.service';import { Disposable, Inject, isInternalEditorID } from '@univerjs/core';-import { DocSelectionManagerService } from '@univerjs/docs';-import { ComponentManager } from '@univerjs/ui';+import { DocSelectionManagerService, DocSkeletonManagerService } from '@univerjs/docs';+import { DocumentEditArea } from '@univerjs/engine-render';import { combineLatest, first, throttleTime } from 'rxjs';import { VIEWPORT_KEY } from '../basics/docs-view-key';import { DocEventManagerService } from './doc-event-manager.service';@@ -41,7 +41,7 @@ export class DocParagraphMenuService extends Disposable implements IRenderModule@Inject(DocSelectionManagerService) private _docSelectionManagerService: DocSelectionManagerService,@Inject(DocEventManagerService) private _docEventManagerService: DocEventManagerService,@Inject(DocCanvasPopManagerService) private _docPopupManagerService: DocCanvasPopManagerService,- @Inject(ComponentManager) private _componentManager: ComponentManager+ @Inject(DocSkeletonManagerService) private _docSkeletonManagerService: DocSkeletonManagerService) {super();@@ -87,13 +87,16 @@ export class DocParagraphMenuService extends Disposable implements IRenderModuleprivate _init() {const handleHoverParagraph = (paragraph: Nullable<IMutiPageParagraphBound>) => {- if (this._paragrahMenu?.active) {- return;- }-- if (paragraph) {- this.showParagraphMenu(paragraph);- return;+ const viewModel = this._docSkeletonManagerService.getViewModel();+ if (viewModel.getEditArea() === DocumentEditArea.BODY) {+ if (this._paragrahMenu?.active) {+ return;+ }++ if (paragraph) {+ this.showParagraphMenu(paragraph);+ return;+ }}this.hideParagraphMenu(true);