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): close header and footer when switch doc mode (#3869)

Ran Luo888c15cf9734
diff --git a/packages/docs-ui/src/commands/commands/doc-header-footer.command.ts b/packages/docs-ui/src/commands/commands/doc-header-footer.command.ts
index 8c7b29eee7..7678538189 100644
--- a/packages/docs-ui/src/commands/commands/doc-header-footer.command.ts
+++ b/packages/docs-ui/src/commands/commands/doc-header-footer.command.ts
@@ -249,6 +249,7 @@ interface ICloseHeaderFooterParams {
export const CloseHeaderFooterCommand: ICommand<ICloseHeaderFooterParams> = {
id: 'doc.command.close-header-footer',
+
type: CommandType.COMMAND,
handler: async (accessor, params: ICloseHeaderFooterParams) => {
@@ -275,7 +276,7 @@ export const CloseHeaderFooterCommand: ICommand<ICloseHeaderFooterParams> = {
}
// TODO: @JOCS, these codes bellow should be automatically executed?
- docSelectionManagerService.replaceTextRanges([]); // Clear text selection.
+ docSelectionManagerService.replaceDocRanges([]); // Clear text selection.
transformer.clearSelectedObjects();
docSelectionRenderService.setSegment('');
docSelectionRenderService.setSegmentPage(-1);
@@ -283,6 +284,8 @@ export const CloseHeaderFooterCommand: ICommand<ICloseHeaderFooterParams> = {
skeleton.calculate();
renderObject.mainComponent?.makeDirty(true);
- return commandService.executeCommand(SidebarDocHeaderFooterPanelOperation.id, { value: 'close' });
+ commandService.executeCommand(SidebarDocHeaderFooterPanelOperation.id, { value: 'close' });
+
+ return true;
},
};
diff --git a/packages/docs-ui/src/commands/commands/switch-doc-mode.command.ts b/packages/docs-ui/src/commands/commands/switch-doc-mode.command.ts
index 57dc8ad9a9..17b86036d6 100644
--- a/packages/docs-ui/src/commands/commands/switch-doc-mode.command.ts
+++ b/packages/docs-ui/src/commands/commands/switch-doc-mode.command.ts
@@ -57,7 +57,7 @@ export const SwitchDocModeCommand: ICommand<ISwitchDocModeCommandParams> = {
const segmentPage = docSelectionRenderService?.getSegmentPage();
- const documentFlavor = docDataModel.getSnapshot().documentStyle.documentFlavor;
+ const oldDocumentFlavor = docDataModel.getSnapshot().documentStyle.documentFlavor;
const docRanges = docSelectionManagerService.getDocRanges();
@@ -66,7 +66,7 @@ export const SwitchDocModeCommand: ICommand<ISwitchDocModeCommandParams> = {
params: {
unitId,
actions: [],
- textRanges: docRanges,
+ textRanges: (oldDocumentFlavor === DocumentFlavor.TRADITIONAL && !!segmentId) ? [] : docRanges,
},
};
@@ -75,13 +75,13 @@ export const SwitchDocModeCommand: ICommand<ISwitchDocModeCommandParams> = {
const rawActions: JSONXActions = [];
let action;
- if (documentFlavor === undefined) {
+ if (oldDocumentFlavor === undefined) {
action = jsonX.insertOp(['documentStyle', 'documentFlavor'], DocumentFlavor.MODERN);
} else {
- if (documentFlavor === DocumentFlavor.MODERN) {
- action = jsonX.replaceOp(['documentStyle', 'documentFlavor'], documentFlavor, DocumentFlavor.TRADITIONAL);
+ if (oldDocumentFlavor === DocumentFlavor.MODERN) {
+ action = jsonX.replaceOp(['documentStyle', 'documentFlavor'], oldDocumentFlavor, DocumentFlavor.TRADITIONAL);
} else {
- action = jsonX.replaceOp(['documentStyle', 'documentFlavor'], documentFlavor, DocumentFlavor.MODERN);
+ action = jsonX.replaceOp(['documentStyle', 'documentFlavor'], oldDocumentFlavor, DocumentFlavor.MODERN);
}
}
@@ -93,7 +93,7 @@ export const SwitchDocModeCommand: ICommand<ISwitchDocModeCommandParams> = {
// Change all drawings' position to relative to paragraph if necessary.
// And only need to change drawing in body.
- if (documentFlavor !== DocumentFlavor.MODERN) {
+ if (oldDocumentFlavor !== DocumentFlavor.MODERN) {
const snapshot = docDataModel.getSnapshot();
const { drawings = {}, body } = snapshot;
const customBlocks = body?.customBlocks ?? [];
diff --git a/packages/docs-ui/src/controllers/doc-header-footer.controller.ts b/packages/docs-ui/src/controllers/doc-header-footer.controller.ts
index 37d562e7eb..7f30b2ab2c 100644
--- a/packages/docs-ui/src/controllers/doc-header-footer.controller.ts
+++ b/packages/docs-ui/src/controllers/doc-header-footer.controller.ts
@@ -19,12 +19,11 @@ import type { Documents, DocumentViewModel, IMouseEvent, IPageRenderConfig, IPat
import type { Nullable } from 'vitest';
import { BooleanNumber, Disposable, DocumentFlavor, ICommandService, Inject, IUniverInstanceService, LocaleService, toDisposable, Tools } from '@univerjs/core';
-import { DocSkeletonManagerService } from '@univerjs/docs';
+import { DocSkeletonManagerService, RichTextEditingMutation } from '@univerjs/docs';
import { DocumentEditArea, IRenderManagerService, PageLayoutType, Path, Rect, Vector2 } from '@univerjs/engine-render';
import { ComponentManager } from '@univerjs/ui';
import { neoGetDocObject } from '../basics/component-tools';
import { CloseHeaderFooterCommand, CoreHeaderFooterCommand } from '../commands/commands/doc-header-footer.command';
-import { SwitchDocModeCommand } from '../commands/commands/switch-doc-mode.command';
import { IEditorService } from '../services/editor/editor-manager.service';
import { DocSelectionRenderService } from '../services/selection/doc-selection-render.service';
import { COMPONENT_DOC_HEADER_FOOTER_PANEL } from '../views/header-footer/panel/component-name';
@@ -151,13 +150,32 @@ export class DocHeaderFooterController extends Disposable implements IRenderModu
// Close header footer panel when switch mode.
private _listenSwitchMode() {
+ // this.disposeWithMe(
+ // this._commandService.beforeCommandExecuted((command: ICommandInfo) => {
+ // if (SwitchDocModeCommand.id === command.id) {
+ // const viewModel = this._docSkeletonManagerService.getViewModel();
+ // const editArea = viewModel.getEditArea();
+
+ // if (editArea !== DocumentEditArea.BODY) {
+ // this._commandService.executeCommand(CloseHeaderFooterCommand.id, {
+ // unitId: this._context.unitId,
+ // });
+ // }
+ // }
+ // })
+ // );
+
this.disposeWithMe(
- this._commandService.beforeCommandExecuted((command: ICommandInfo) => {
- if (SwitchDocModeCommand.id === command.id) {
+ this._commandService.onCommandExecuted((command: ICommandInfo) => {
+ if (RichTextEditingMutation.id === command.id) {
+ const docDataModel = this._context.unit;
+
const viewModel = this._docSkeletonManagerService.getViewModel();
const editArea = viewModel.getEditArea();
- if (editArea !== DocumentEditArea.BODY) {
+ const documentFlavor = docDataModel.getSnapshot().documentStyle.documentFlavor;
+
+ if (editArea !== DocumentEditArea.BODY && documentFlavor === DocumentFlavor.MODERN) {
this._commandService.executeCommand(CloseHeaderFooterCommand.id, {
unitId: this._context.unitId,
});
diff --git a/packages/docs-ui/src/controllers/render-controllers/doc-selection-render.controller.ts b/packages/docs-ui/src/controllers/render-controllers/doc-selection-render.controller.ts
index d8810c1b4b..e8b14c2d79 100644
--- a/packages/docs-ui/src/controllers/render-controllers/doc-selection-render.controller.ts
+++ b/packages/docs-ui/src/controllers/render-controllers/doc-selection-render.controller.ts
@@ -77,9 +77,7 @@ export class DocSelectionRenderController extends Disposable implements IRenderM
return;
}
this._docSelectionRenderService.removeAllRanges();
- if (docRanges.length) {
- this._docSelectionRenderService.addDocRanges(docRanges, isEditing, options);
- }
+ this._docSelectionRenderService.addDocRanges(docRanges, isEditing, options);
})
);
}