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.

feat(docs-ui): support docs zoom bar & context menu (#2830)

zhangwd840cb5db042
diff --git a/examples/src/docs/main.ts b/examples/src/docs/main.ts
index 59cfe79d28..6b04182e36 100644
--- a/examples/src/docs/main.ts
+++ b/examples/src/docs/main.ts
@@ -57,7 +57,7 @@ univer.registerPlugin(UniverFormulaEnginePlugin);
univer.registerPlugin(UniverDebuggerPlugin);
univer.registerPlugin(UniverUIPlugin, {
container: 'app',
- footer: false,
+ // footer: false,
});
univer.registerPlugin(UniverDocsPlugin);
diff --git a/packages/core/src/common/const.ts b/packages/core/src/common/const.ts
index 4e51e17460..d433a076ee 100644
--- a/packages/core/src/common/const.ts
+++ b/packages/core/src/common/const.ts
@@ -20,6 +20,8 @@ export const DOCS_NORMAL_EDITOR_UNIT_ID_KEY = `${PREFIX}DOCS_NORMAL`;
export const DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY = `${PREFIX}DOCS_FORMULA_BAR`;
+export const DOCS_ZEN_EDITOR_UNIT_ID_KEY = '__defaultDocumentZenEditorSpecialUnitId_20231218__';
+
export const DEFAULT_EMPTY_DOCUMENT_VALUE = '\r\n';
export function createInternalEditorID(id: string) {
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 439e76dcdd..a0a579b4a3 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -31,6 +31,7 @@ export {
DEFAULT_EMPTY_DOCUMENT_VALUE,
DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
+ DOCS_ZEN_EDITOR_UNIT_ID_KEY,
createInternalEditorID,
isInternalEditorID,
} from './common/const';
diff --git a/packages/debugger/src/controllers/debugger.controller.ts b/packages/debugger/src/controllers/debugger.controller.ts
index c79d1a5605..1d30f520d1 100644
--- a/packages/debugger/src/controllers/debugger.controller.ts
+++ b/packages/debugger/src/controllers/debugger.controller.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import { Disposable, ICommandService, Inject, Injector } from '@univerjs/core';
+import { Disposable, ICommandService, Inject, Injector, LifecycleStages, OnLifecycle } from '@univerjs/core';
import type { IMenuItemFactory, MenuConfig } from '@univerjs/ui';
import { ComponentManager, IMenuService } from '@univerjs/ui';
@@ -66,6 +66,7 @@ export interface IUniverDebuggerConfig {
export const DefaultDebuggerConfig = {};
+@OnLifecycle(LifecycleStages.Ready, DebuggerController)
export class DebuggerController extends Disposable {
constructor(
private readonly _config: Partial<IUniverDebuggerConfig>,
diff --git a/packages/debugger/src/debugger-plugin.ts b/packages/debugger/src/debugger-plugin.ts
index bba977f827..320a92d430 100644
--- a/packages/debugger/src/debugger-plugin.ts
+++ b/packages/debugger/src/debugger-plugin.ts
@@ -48,11 +48,10 @@ export class UniverDebuggerPlugin extends Plugin {
useFactory: () => this._injector.createInstance(DebuggerController, this._config),
},
]);
-
- this._debuggerController = this._injector.get(DebuggerController);
}
getDebuggerController() {
+ this._debuggerController = this._injector.get(DebuggerController);
return this._debuggerController;
}
}
diff --git a/packages/docs-hyper-link-ui/src/controllers/menu.ts b/packages/docs-hyper-link-ui/src/controllers/menu.ts
index 3991455e5b..aa4cc31551 100644
--- a/packages/docs-hyper-link-ui/src/controllers/menu.ts
+++ b/packages/docs-hyper-link-ui/src/controllers/menu.ts
@@ -28,7 +28,7 @@ export const DOC_LINK_ICON = 'doc-hyper-link-icon';
export function AddHyperLinkMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
return {
id: ShowDocHyperLinkEditPopupOperation.id,
- group: MenuGroup.TOOLBAR_LAYOUT,
+ group: MenuGroup.CONTEXT_MENU_DATA,
type: MenuItemType.BUTTON,
icon: DOC_LINK_ICON,
title: 'docLink.menu.tooltip',
diff --git a/packages/docs-hyper-link-ui/src/views/hyper-link-edit/index.tsx b/packages/docs-hyper-link-ui/src/views/hyper-link-edit/index.tsx
index 1885189220..40070df3da 100644
--- a/packages/docs-hyper-link-ui/src/views/hyper-link-edit/index.tsx
+++ b/packages/docs-hyper-link-ui/src/views/hyper-link-edit/index.tsx
@@ -76,9 +76,9 @@ export const DocHyperLinkEdit = () => {
}, [editingId, hyperLinkModel, textSelectionManagerService, univerInstanceService]);
useEffect(() => {
- textSelectionRenderManager.blur();
+ textSelectionRenderManager.blurEditor();
return () => {
- textSelectionRenderManager.focus();
+ textSelectionRenderManager.focusEditor();
};
}, [textSelectionRenderManager]);
diff --git a/packages/docs-thread-comment-ui/src/commands/operations/show-comment-panel.operation.ts b/packages/docs-thread-comment-ui/src/commands/operations/show-comment-panel.operation.ts
index a2b9346ad6..42efeb849a 100644
--- a/packages/docs-thread-comment-ui/src/commands/operations/show-comment-panel.operation.ts
+++ b/packages/docs-thread-comment-ui/src/commands/operations/show-comment-panel.operation.ts
@@ -20,6 +20,7 @@ import type { ActiveCommentInfo } from '@univerjs/thread-comment-ui';
import { getDT, ThreadCommentPanelService } from '@univerjs/thread-comment-ui';
import { ISidebarService } from '@univerjs/ui';
import { getSelectionText, TextSelectionManagerService } from '@univerjs/docs';
+import { ITextSelectionRenderManager } from '@univerjs/engine-render';
import { DocThreadCommentPanel } from '../../views/doc-thread-comment-panel';
import { DEFAULT_DOC_SUBUNIT_ID } from '../../common/const';
import { DocThreadCommentService } from '../../services/doc-thread-comment.service';
@@ -52,6 +53,30 @@ export const ShowCommentPanelOperation: ICommand<IShowCommentPanelOperationParam
},
};
+export const ToggleCommentPanelOperation: ICommand = {
+ id: 'docs.operation.toggle-comment-panel',
+ type: CommandType.OPERATION,
+ handler(accessor) {
+ const panelService = accessor.get(ThreadCommentPanelService);
+ const sidebarService = accessor.get(ISidebarService);
+
+ if (!panelService.panelVisible) {
+ sidebarService.open({
+ header: { title: 'threadCommentUI.panel.title' },
+ children: { label: DocThreadCommentPanel.componentKey },
+ width: 320,
+ onClose: () => panelService.setPanelVisible(false),
+ });
+ panelService.setPanelVisible(true);
+ } else {
+ sidebarService.close();
+ panelService.setPanelVisible(false);
+ panelService.setActiveComment(null);
+ }
+ return true;
+ },
+};
+
export const StartAddCommentOperation: ICommand = {
id: 'docs.operation.start-add-comment',
type: CommandType.OPERATION,
@@ -60,6 +85,7 @@ export const StartAddCommentOperation: ICommand = {
const univerInstanceService = accessor.get(IUniverInstanceService);
const doc = univerInstanceService.getCurrentUnitForType<DocumentDataModel>(UniverInstanceType.UNIVER_DOC);
const textSelectionManagerService = accessor.get(TextSelectionManagerService);
+ const textSelectionRenderService = accessor.get(ITextSelectionRenderManager);
const userManagerService = accessor.get(UserManagerService);
const docCommentService = accessor.get(DocThreadCommentService);
const commandService = accessor.get(ICommandService);
@@ -104,6 +130,7 @@ export const StartAddCommentOperation: ICommand = {
threadId: commentId,
};
+ textSelectionRenderService.blurEditor();
docCommentService.startAdd(comment);
panelService.setActiveComment({
unitId,
diff --git a/packages/docs-thread-comment-ui/src/controllers/doc-thread-comment-selection.controller.ts b/packages/docs-thread-comment-ui/src/controllers/doc-thread-comment-selection.controller.ts
index ccb87f4001..8a32665b01 100644
--- a/packages/docs-thread-comment-ui/src/controllers/doc-thread-comment-selection.controller.ts
+++ b/packages/docs-thread-comment-ui/src/controllers/doc-thread-comment-selection.controller.ts
@@ -14,12 +14,13 @@
* limitations under the License.
*/
-import type { DocumentDataModel } from '@univerjs/core';
+import type { DocumentDataModel, ITextRange } from '@univerjs/core';
import { Disposable, ICommandService, Inject, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';
import type { ISetTextSelectionsOperationParams } from '@univerjs/docs';
import { SetTextSelectionsOperation } from '@univerjs/docs';
import { SetActiveCommentOperation, ThreadCommentPanelService } from '@univerjs/thread-comment-ui';
import { DocBackScrollRenderController } from '@univerjs/docs-ui';
+import type { ITextRangeWithStyle } from '@univerjs/engine-render';
import { IRenderManagerService } from '@univerjs/engine-render';
import { ThreadCommentModel } from '@univerjs/thread-comment';
import { DEFAULT_DOC_SUBUNIT_ID } from '../common/const';
@@ -43,13 +44,18 @@ export class DocThreadCommentSelectionController extends Disposable {
}
private _initSelectionChange() {
+ let lastSelection: ITextRange | undefined;
this.disposeWithMe(
this._commandService.onCommandExecuted((commandInfo) => {
if (commandInfo.id === SetTextSelectionsOperation.id) {
const params = commandInfo.params as ISetTextSelectionsOperationParams;
const { unitId, ranges } = params;
const doc = this._univerInstanceService.getUnit<DocumentDataModel>(unitId, UniverInstanceType.UNIVER_DOC);
- const primary = ranges[0];
+ const primary = ranges[0] as ITextRangeWithStyle | undefined;
+ if (lastSelection?.startOffset === primary?.startOffset && lastSelection?.endOffset === primary?.endOffset) {
+ return;
+ }
+ lastSelection = primary;
if (primary && doc) {
const { startOffset, endOffset, collapsed } = primary;
let customRange;
diff --git a/packages/docs-thread-comment-ui/src/controllers/doc-thread-comment-ui.controller.ts b/packages/docs-thread-comment-ui/src/controllers/doc-thread-comment-ui.controller.ts
index 884594e5b7..a812e7c58d 100644
--- a/packages/docs-thread-comment-ui/src/controllers/doc-thread-comment-ui.controller.ts
+++ b/packages/docs-thread-comment-ui/src/controllers/doc-thread-comment-ui.controller.ts
@@ -20,9 +20,9 @@ import { ComponentManager, IMenuService } from '@univerjs/ui';
import { CommentSingle } from '@univerjs/icons';
import { AddDocCommentComment } from '../commands/commands/add-doc-comment.command';
import { DocThreadCommentPanel } from '../views/doc-thread-comment-panel';
-import { ShowCommentPanelOperation, StartAddCommentOperation } from '../commands/operations/show-comment-panel.operation';
+import { ShowCommentPanelOperation, StartAddCommentOperation, ToggleCommentPanelOperation } from '../commands/operations/show-comment-panel.operation';
import { DeleteDocCommentComment } from '../commands/commands/delete-doc-comment.command';
-import { AddDocCommentMenuItemFactory } from './menu';
+import { AddDocCommentMenuItemFactory, ToolbarDocCommentMenuItemFactory } from './menu';
export interface IDocThreadCommentUIConfig {
menu: MenuConfig;
@@ -49,13 +49,17 @@ export class DocThreadCommentUIController extends Disposable {
DeleteDocCommentComment,
ShowCommentPanelOperation,
StartAddCommentOperation,
+ ToggleCommentPanelOperation,
].forEach((command) => {
this.disposeWithMe(this._commandService.registerCommand(command));
});
}
private _initMenus() {
- [AddDocCommentMenuItemFactory].forEach((menuFactory) => {
+ [
+ AddDocCommentMenuItemFactory,
+ ToolbarDocCommentMenuItemFactory,
+ ].forEach((menuFactory) => {
this.disposeWithMe(this._menuService.addMenuItem(menuFactory(this._injector), this._config.menu));
});
}
diff --git a/packages/docs-thread-comment-ui/src/controllers/menu.ts b/packages/docs-thread-comment-ui/src/controllers/menu.ts
index 1a3d3897b3..be6ad121ae 100644
--- a/packages/docs-thread-comment-ui/src/controllers/menu.ts
+++ b/packages/docs-thread-comment-ui/src/controllers/menu.ts
@@ -21,10 +21,11 @@ import { getMenuHiddenObservable, MenuGroup, MenuItemType, MenuPosition } from '
import { DocSkeletonManagerService, TextSelectionManagerService } from '@univerjs/docs';
import { DocumentEditArea, IRenderManagerService } from '@univerjs/engine-render';
import { debounceTime, Observable } from 'rxjs';
-import { StartAddCommentOperation } from '../commands/operations/show-comment-panel.operation';
+import { StartAddCommentOperation, ToggleCommentPanelOperation } from '../commands/operations/show-comment-panel.operation';
export const shouldDisableAddComment = (accessor: IAccessor) => {
const renderManagerService = accessor.get(IRenderManagerService);
+ const textSelectionManagerService = accessor.get(TextSelectionManagerService);
const render = renderManagerService.getCurrent();
const skeleton = render?.with(DocSkeletonManagerService).getSkeleton();
const editArea = skeleton?.getViewModel().getEditArea();
@@ -32,18 +33,23 @@ export const shouldDisableAddComment = (accessor: IAccessor) => {
return true;
}
+ const range = textSelectionManagerService.getActiveRange();
+ if (!range || range.collapsed) {
+ return true;
+ }
+
return false;
};
export function AddDocCommentMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
return {
id: StartAddCommentOperation.id,
- group: MenuGroup.TOOLBAR_LAYOUT,
+ group: MenuGroup.CONTEXT_MENU_DATA,
type: MenuItemType.BUTTON,
icon: 'CommentSingle',
title: 'threadCommentUI.panel.addComment',
tooltip: 'threadCommentUI.panel.addComment',
- positions: [MenuPosition.TOOLBAR_START, MenuPosition.CONTEXT_MENU],
+ positions: [MenuPosition.CONTEXT_MENU],
hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_DOC),
disabled$: new Observable(function (subscribe) {
const textSelectionService = accessor.get(TextSelectionManagerService);
@@ -57,3 +63,16 @@ export function AddDocCommentMenuItemFactory(accessor: IAccessor): IMenuButtonIt
}),
};
}
+
+export function ToolbarDocCommentMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
+ return {
+ id: ToggleCommentPanelOperation.id,
+ group: MenuGroup.CONTEXT_MENU_DATA,
+ type: MenuItemType.BUTTON,
+ icon: 'CommentSingle',
+ title: 'threadCommentUI.panel.addComment',
+ tooltip: 'threadCommentUI.panel.addComment',
+ positions: [MenuPosition.TOOLBAR_START],
+ hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_DOC),
+ };
+}
diff --git a/packages/docs-thread-comment-ui/src/views/doc-thread-comment-panel/index.tsx b/packages/docs-thread-comment-ui/src/views/doc-thread-comment-panel/index.tsx
index 683fef637e..0cfa499abd 100644
--- a/packages/docs-thread-comment-ui/src/views/doc-thread-comment-panel/index.tsx
+++ b/packages/docs-thread-comment-ui/src/views/doc-thread-comment-panel/index.tsx
@@ -18,7 +18,7 @@ import type { DocumentDataModel } from '@univerjs/core';
import { ICommandService, IUniverInstanceService, UniverInstanceType, useDependency, useObservable } from '@univerjs/core';
import { ThreadCommentPanel } from '@univerjs/thread-comment-ui';
import React, { useEffect, useMemo, useState } from 'react';
-import { Observable } from 'rxjs';
+import { debounceTime, Observable } from 'rxjs';
import { RichTextEditingMutation, TextSelectionManagerService } from '@univerjs/docs';
import { DEFAULT_DOC_SUBUNIT_ID } from '../../common/const';
import { StartAddCommentOperation } from '../../commands/operations/show-comment-panel.operation';
@@ -33,7 +33,11 @@ export const DocThreadCommentPanel = () => {
const doc = useObservable(doc$);
const subUnitId$ = useMemo(() => new Observable<string>((sub) => sub.next(DEFAULT_DOC_SUBUNIT_ID)), []);
const textSelectionManagerService = useDependency(TextSelectionManagerService);
- const textRange = useObservable(textSelectionManagerService.textSelection$)?.textRanges[0];
+ const selectionChange$ = useMemo(
+ () => textSelectionManagerService.textSelection$.pipe(debounceTime(16)),
+ [textSelectionManagerService.textSelection$]
+ );
+ useObservable(selectionChange$);
const commandService = useDependency(ICommandService);
const docCommentService = useDependency(DocThreadCommentService);
const tempComment = useObservable(docCommentService.addingComment$);
@@ -67,7 +71,13 @@ export const DocThreadCommentPanel = () => {
if (!doc) {
return null;
}
- const isInValidSelection = textRange && textRange.endOffset === textRange.startOffset;
+
+ const activeRange = textSelectionManagerService.getActiveRange();
+ const isInValidSelection = Boolean(
+ activeRange &&
+ (activeRange.endOffset === activeRange.startOffset || activeRange.segmentId)
+ );
+
const unitId = doc.getUnitId();
return (
diff --git a/packages/docs-ui/src/controllers/doc-ui.controller.ts b/packages/docs-ui/src/controllers/doc-ui.controller.ts
index ff7b53e5b0..ba3ba0a990 100644
--- a/packages/docs-ui/src/controllers/doc-ui.controller.ts
+++ b/packages/docs-ui/src/controllers/doc-ui.controller.ts
@@ -14,9 +14,9 @@
* limitations under the License.
*/
-import { Disposable, Inject, Injector, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';
+import { connectInjector, Disposable, Inject, Injector, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';
import type { IMenuItemFactory } from '@univerjs/ui';
-import { ComponentManager, ILayoutService, IMenuService } from '@univerjs/ui';
+import { BuiltInUIPart, ComponentManager, ILayoutService, IMenuService, IShortcutService, IUIPartsService } from '@univerjs/ui';
import { ITextSelectionRenderManager } from '@univerjs/engine-render';
import { COLOR_PICKER_COMPONENT, ColorPicker } from '../components/color-picker';
@@ -28,6 +28,7 @@ import {
} from '../components/font-family';
import { FONT_SIZE_COMPONENT, FontSize } from '../components/font-size';
import type { IUniverDocsUIConfig } from '../basics';
+import { DocFooter } from '../views/doc-footer';
import {
AlignCenterMenuItemFactory,
AlignJustifyMenuItemFactory,
@@ -48,6 +49,8 @@ import {
TextColorSelectorMenuItemFactory,
UnderlineMenuItemFactory,
} from './menu/menu';
+import { CopyMenuFactory, CutMenuFactory, DeleteMenuFactory, PasteMenuFactory } from './menu/context-menu';
+import { AlignCenterShortCut, AlignJustifyShortCut, AlignLeftShortCut, AlignRightShortCut, BoldShortCut, BulletListShortCut, ItalicShortCut, OrderListShortCut, StrikeThroughShortCut, SubscriptShortCut, SuperscriptShortCut, UnderlineShortCut } from './shortcut/toolbar.shortcut';
// FIXME: LifecycleStages.Rendered must be used, otherwise the menu cannot be added to the DOM, but the sheet ui plug-in can be added in LifecycleStages.Ready
@OnLifecycle(LifecycleStages.Rendered, DocUIController)
@@ -57,7 +60,10 @@ export class DocUIController extends Disposable {
@Inject(Injector) private readonly _injector: Injector,
@Inject(ComponentManager) private readonly _componentManager: ComponentManager,
@ILayoutService private readonly _layoutService: ILayoutService,
- @IMenuService private readonly _menuService: IMenuService
+ @IMenuService private readonly _menuService: IMenuService,
+ @IUIPartsService private readonly _uiPartsService: IUIPartsService,
+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
+ @IShortcutService private readonly _shortcutService: IShortcutService
) {
super();
@@ -72,6 +78,13 @@ export class DocUIController extends Disposable {
this.disposeWithMe(componentManager.register(FONT_SIZE_COMPONENT, FontSize));
}
+ private _initUiParts() {
+ const workbook = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SHEET);
+ if (this._config.layout?.docContainerConfig?.footer && !workbook) {
+ this.disposeWithMe(this._uiPartsService.registerComponent(BuiltInUIPart.FOOTER, () => connectInjector(DocFooter, this._injector)));
+ }
+ }
+
private _initMenus(): void {
const { menu = {} } = this._config;
@@ -100,12 +113,46 @@ export class DocUIController extends Disposable {
).forEach((factory) => {
this.disposeWithMe(this._menuService.addMenuItem(this._injector.invoke(factory), menu));
});
+
+ [
+ CopyMenuFactory,
+ CutMenuFactory,
+ PasteMenuFactory,
+ DeleteMenuFactory,
+ ].forEach((factory) => {
+ try {
+ this.disposeWithMe(this._menuService.addMenuItem(this._injector.invoke(factory), menu));
+ } catch (error) {
+
+ }
+ });
+ }
+
+ private _initShortCut() {
+ [
+ BoldShortCut,
+ ItalicShortCut,
+ UnderlineShortCut,
+ StrikeThroughShortCut,
+ SubscriptShortCut,
+ SuperscriptShortCut,
+ AlignCenterShortCut,
+ AlignJustifyShortCut,
+ AlignRightShortCut,
+ AlignLeftShortCut,
+ OrderListShortCut,
+ BulletListShortCut,
+ ].forEach((shortcut) => {
+ this.disposeWithMe(this._shortcutService.registerShortcut(shortcut));
+ });
}
private _init(): void {
this._initCustomComponents();
this._initMenus();
this._initFocusHandler();
+ this._initUiParts();
+ this._initShortCut();
}
private _initFocusHandler(): void {
diff --git a/packages/docs-ui/src/controllers/menu/context-menu.ts b/packages/docs-ui/src/controllers/menu/context-menu.ts
new file mode 100644
index 0000000000..fe8a16be22
--- /dev/null
+++ b/packages/docs-ui/src/controllers/menu/context-menu.ts
@@ -0,0 +1,90 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { IAccessor } from '@univerjs/core';
+import { UniverInstanceType } from '@univerjs/core';
+import type { IMenuButtonItem } from '@univerjs/ui';
+import { getMenuHiddenObservable, MenuGroup, MenuItemType, MenuPosition } from '@univerjs/ui';
+import { Observable } from 'rxjs';
+import { DeleteLeftCommand, TextSelectionManagerService } from '@univerjs/docs';
+import { DocCopyCommand, DocCutCommand, DocPasteCommand } from '../../commands/commands/clipboard.command';
+
+const getDisableOnCollapsedObservable = (accessor: IAccessor) => {
+ const textSelectionManagerService = accessor.get(TextSelectionManagerService);
+ return new Observable<boolean>((subscriber) => {
+ const observable = textSelectionManagerService.textSelection$.subscribe((selections) => {
+ const range = textSelectionManagerService.getActiveRange();
+ if (range && !range.collapsed) {
+ subscriber.next(false);
+ } else {
+ subscriber.next(true);
+ }
+ });
+
+ return () => observable.unsubscribe();
+ });
+};
+
+export const CopyMenuFactory = (accessor: IAccessor): IMenuButtonItem => {
+ return {
+ id: DocCopyCommand.id,
+ group: MenuGroup.CONTEXT_MENU_FORMAT,
+ type: MenuItemType.BUTTON,
+ icon: 'Copy',
+ title: 'rightClick.copy',
+ positions: [MenuPosition.CONTEXT_MENU],
+ disabled$: getDisableOnCollapsedObservable(accessor),
+ hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_DOC),
+ };
+};
+
+export const CutMenuFactory = (accessor: IAccessor): IMenuButtonItem => {
+ return {
+ id: DocCutCommand.id,
+ group: MenuGroup.CONTEXT_MENU_FORMAT,
+ type: MenuItemType.BUTTON,
+ icon: 'Copy',
+ title: 'rightClick.cut',
+ positions: [MenuPosition.CONTEXT_MENU],
+ disabled$: getDisableOnCollapsedObservable(accessor),
+ hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_DOC),
+ };
+};
+
+export const PasteMenuFactory = (accessor: IAccessor): IMenuButtonItem => {
+ return {
+ id: DocPasteCommand.id,
+ group: MenuGroup.CONTEXT_MENU_FORMAT,
+ type: MenuItemType.BUTTON,
+ icon: 'PasteSpecial',
+ title: 'rightClick.paste',
+ positions: [MenuPosition.CONTEXT_MENU],
+ hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_DOC),
+ };
+};
+
+export const DeleteMenuFactory = (accessor: IAccessor): IMenuButtonItem => {
+ return {
+ id: DeleteLeftCommand.id,
+ group: MenuGroup.CONTEXT_MENU_FORMAT,
+ type: MenuItemType.BUTTON,
+ icon: 'PasteSpecial',
+ title: 'rightClick.delete',
+ positions: [MenuPosition.CONTEXT_MENU],
+ disabled$: getDisableOnCollapsedObservable(accessor),
+ hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_DOC),
+ };
+};
diff --git a/packages/docs-ui/src/controllers/render-controllers/contextmenu.render-controller.ts b/packages/docs-ui/src/controllers/render-controllers/contextmenu.render-controller.ts
new file mode 100644
index 0000000000..7c40812bf0
--- /dev/null
+++ b/packages/docs-ui/src/controllers/render-controllers/contextmenu.render-controller.ts
@@ -0,0 +1,79 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { Workbook } from '@univerjs/core';
+import {
+ DEFAULT_EMPTY_DOCUMENT_VALUE,
+ Disposable,
+ DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
+ DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
+ DOCS_ZEN_EDITOR_UNIT_ID_KEY,
+ ICommandService,
+ Inject,
+} from '@univerjs/core';
+import { RichTextEditingMutation, TextSelectionManagerService } from '@univerjs/docs';
+import type { Documents, IRenderContext, IRenderModule } from '@univerjs/engine-render';
+import { IContextMenuService, MenuPosition } from '@univerjs/ui';
+
+const SKIP_UNIT_IDS = [
+ DEFAULT_EMPTY_DOCUMENT_VALUE,
+ DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
+ DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
+ DOCS_ZEN_EDITOR_UNIT_ID_KEY,
+];
+
+/**
+ * This controller subscribe to context menu events in sheet rendering views and invoke context menu at a correct
+ * position and with correct menu type.
+ */
+export class DocContextMenuRenderController extends Disposable implements IRenderModule {
+ constructor(
+ private readonly _context: IRenderContext<Workbook>,
+ @IContextMenuService private readonly _contextMenuService: IContextMenuService,
+ @Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService,
+ @ICommandService private readonly _commandService: ICommandService
+ ) {
+ super();
+
+ if (!SKIP_UNIT_IDS.includes(this._context.unitId)) {
+ this._initPointerDown();
+ this._initEditChange();
+ }
+ }
+
+ private _initPointerDown(): void {
+ const documentsPointerDownObserver = (this._context?.mainComponent as Documents)?.onPointerDown$;
+ // Content range context menu
+ const documentsSubscription = documentsPointerDownObserver.subscribeEvent((event) => {
+ if (event.button === 2) {
+ this._contextMenuService.triggerContextMenu(event, MenuPosition.CONTEXT_MENU);
+ }
+ });
+ this.disposeWithMe(documentsSubscription);
+ }
+
+ private _initEditChange(): void {
+ this.disposeWithMe(
+ this._commandService.onCommandExecuted((command) => {
+ if (command.id === RichTextEditingMutation.id) {
+ if (this._contextMenuService.visible) {
+ this._contextMenuService.hideContextMenu();
+ }
+ }
+ })
+ );
+ }
+}
diff --git a/packages/docs-ui/src/controllers/render-controllers/doc-resize.render-controller.ts b/packages/docs-ui/src/controllers/render-controllers/doc-resize.render-controller.ts
new file mode 100644
index 0000000000..45e4dc2bb8
--- /dev/null
+++ b/packages/docs-ui/src/controllers/render-controllers/doc-resize.render-controller.ts
@@ -0,0 +1,46 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { Disposable, fromEventSubject, Inject } from '@univerjs/core';
+import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';
+import { TRANSFORM_CHANGE_OBSERVABLE_TYPE } from '@univerjs/engine-render';
+import { filter, throttleTime } from 'rxjs';
+import { TextSelectionManagerService } from '@univerjs/docs';
+import { DocPageLayoutService } from '../../services/doc-page-layout.service';
+
+export class DocResizeRenderController extends Disposable implements IRenderModule {
+ constructor(
+ private _context: IRenderContext,
+ @Inject(DocPageLayoutService) private readonly _docPageLayoutService: DocPageLayoutService,
+ @Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService
+ ) {
+ super();
+
+ this._initResize();
+ }
+
+ private _initResize() {
+ this.disposeWithMe(
+ fromEventSubject(this._context.engine.onTransformChange$).pipe(
+ filter((evt) => evt.type === TRANSFORM_CHANGE_OBSERVABLE_TYPE.resize),
+ throttleTime(16)
+ ).subscribe(() => {
+ this._docPageLayoutService.calculatePagePosition();
+ this._textSelectionManagerService.refreshSelection();
+ })
+ );
+ }
+}
diff --git a/packages/docs-ui/src/controllers/render-controllers/zoom.render-controller.ts b/packages/docs-ui/src/controllers/render-controllers/zoom.render-controller.ts
index f547fc761d..28e48817c2 100644
--- a/packages/docs-ui/src/controllers/render-controllers/zoom.render-controller.ts
+++ b/packages/docs-ui/src/controllers/render-controllers/zoom.render-controller.ts
@@ -21,10 +21,10 @@ import {
Inject,
IUniverInstanceService,
} from '@univerjs/core';
-import type { IDocObjectParam } from '@univerjs/docs';
-import { DocSkeletonManagerService, neoGetDocObject, SetDocZoomRatioCommand, SetDocZoomRatioOperation, TextSelectionManagerService, VIEWPORT_KEY } from '@univerjs/docs';
+import { DocSkeletonManagerService, neoGetDocObject, SetDocZoomRatioCommand, SetDocZoomRatioOperation, TextSelectionManagerService } from '@univerjs/docs';
import type { IRenderContext, IRenderModule, IWheelEvent } from '@univerjs/engine-render';
import { IEditorService } from '@univerjs/ui';
+import { DocPageLayoutService } from '../../services/doc-page-layout.service';
interface ISetDocMutationParams {
unitId: string;
@@ -37,7 +37,8 @@ export class DocZoomRenderController extends Disposable implements IRenderModule
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@ICommandService private readonly _commandService: ICommandService,
@Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService,
- @IEditorService private readonly _editorService: IEditorService
+ @IEditorService private readonly _editorService: IEditorService,
+ @Inject(DocPageLayoutService) private readonly _docPageLayoutService: DocPageLayoutService
) {
super();
@@ -49,7 +50,7 @@ export class DocZoomRenderController extends Disposable implements IRenderModule
this._initCommandExecutedListener();
this._initRenderRefresher();
- setTimeout(() => this._updateViewZoom(1, true), 20);
+ setTimeout(() => this.updateViewZoom(1, true), 20);
}
private _initRenderRefresher() {
@@ -106,7 +107,7 @@ export class DocZoomRenderController extends Disposable implements IRenderModule
const zoomRatio = documentModel.zoomRatio || 1;
- this._updateViewZoom(zoomRatio, false);
+ this.updateViewZoom(zoomRatio, false);
}));
}
@@ -126,16 +127,16 @@ export class DocZoomRenderController extends Disposable implements IRenderModule
const zoomRatio = documentModel.zoomRatio || 1;
- this._updateViewZoom(zoomRatio);
+ this.updateViewZoom(zoomRatio);
}
}));
}
- private _updateViewZoom(zoomRatio: number, needRefreshSelection = true) {
+ updateViewZoom(zoomRatio: number, needRefreshSelection = true) {
const docObject = neoGetDocObject(this._context);
docObject.scene.scale(zoomRatio, zoomRatio);
- this._calculatePagePosition(docObject, zoomRatio);
+ this._docPageLayoutService.calculatePagePosition();
if (needRefreshSelection && !this._editorService.isEditor(this._context.unitId)) {
this._textSelectionManagerService.refreshSelection();
@@ -143,63 +144,4 @@ export class DocZoomRenderController extends Disposable implements IRenderModule
docObject.scene.getTransformer()?.clearSelectedObjects();
}
-
- private _calculatePagePosition(currentRender: IDocObjectParam, zoomRatio: number) {
- const { document: docsComponent, scene, docBackground } = currentRender;
-
- const parent = scene?.getParent();
-
- const { width: docsWidth, height: docsHeight, pageMarginLeft, pageMarginTop } = docsComponent;
- if (parent == null || docsWidth === Number.POSITIVE_INFINITY || docsHeight === Number.POSITIVE_INFINITY) {
- return;
- }
- const { width: engineWidth, height: engineHeight } = parent;
- let docsLeft = 0;
- let docsTop = 0;
-
- let sceneWidth = 0;
-
- let sceneHeight = 0;
-
- let scrollToX = Number.POSITIVE_INFINITY;
-
- if (engineWidth > (docsWidth + pageMarginLeft * 2) * zoomRatio) {
- docsLeft = engineWidth / 2 - (docsWidth * zoomRatio) / 2;
- docsLeft /= zoomRatio;
- sceneWidth = (engineWidth - pageMarginLeft * 2) / zoomRatio;
-
- scrollToX = 0;
- } else {
- docsLeft = pageMarginLeft;
- sceneWidth = docsWidth + pageMarginLeft * 2;
-
- scrollToX = (sceneWidth - engineWidth / zoomRatio) / 2;
- }
-
- if (engineHeight > docsHeight) {
- docsTop = engineHeight / 2 - docsHeight / 2;
- sceneHeight = (engineHeight - pageMarginTop * 2) / zoomRatio;
- } else {
- docsTop = pageMarginTop;
- sceneHeight = docsHeight + pageMarginTop * 2;
- }
-
- // this.docsLeft = docsLeft;
-
- // this.docsTop = docsTop;
-
- scene.resize(sceneWidth, sceneHeight + 200);
-
- docsComponent.translate(docsLeft, docsTop);
- docBackground.translate(docsLeft, docsTop);
-
- const viewport = scene.getViewport(VIEWPORT_KEY.VIEW_MAIN);
- if (scrollToX !== Number.POSITIVE_INFINITY && viewport != null) {
- viewport.scrollToViewportPos({
- viewportScrollX: scrollToX,
- });
- }
-
- return this;
- }
}
diff --git a/packages/docs-ui/src/controllers/shortcut/toolbar.shortcut.ts b/packages/docs-ui/src/controllers/shortcut/toolbar.shortcut.ts
new file mode 100644
index 0000000000..281465f545
--- /dev/null
+++ b/packages/docs-ui/src/controllers/shortcut/toolbar.shortcut.ts
@@ -0,0 +1,94 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { AlignCenterCommand, AlignJustifyCommand, AlignLeftCommand, AlignRightCommand, BulletListCommand, OrderListCommand, SetInlineFormatBoldCommand, SetInlineFormatItalicCommand, SetInlineFormatStrikethroughCommand, SetInlineFormatSubscriptCommand, SetInlineFormatSuperscriptCommand, SetInlineFormatUnderlineCommand } from '@univerjs/docs';
+import type { IShortcutItem } from '@univerjs/ui';
+import { KeyCode, MetaKeys } from '@univerjs/ui';
+import { whenDocAndEditorFocused } from '../../shortcuts/utils';
+
+export const BoldShortCut: IShortcutItem = {
+ id: SetInlineFormatBoldCommand.id,
+ binding: KeyCode.B | MetaKeys.CTRL_COMMAND,
+ preconditions: whenDocAndEditorFocused,
+};
+
+export const ItalicShortCut: IShortcutItem = {
+ id: SetInlineFormatItalicCommand.id,
+ binding: KeyCode.I | MetaKeys.CTRL_COMMAND,
+ preconditions: whenDocAndEditorFocused,
+};
+
+export const UnderlineShortCut: IShortcutItem = {
+ id: SetInlineFormatUnderlineCommand.id,
+ binding: KeyCode.U | MetaKeys.CTRL_COMMAND,
+ preconditions: whenDocAndEditorFocused,
+};
+
+export const StrikeThroughShortCut: IShortcutItem = {
+ id: SetInlineFormatStrikethroughCommand.id,
+ binding: KeyCode.X | MetaKeys.SHIFT | MetaKeys.CTRL_COMMAND,
+ preconditions: whenDocAndEditorFocused,
+};
+
+export const SubscriptShortCut: IShortcutItem = {
+ id: SetInlineFormatSubscriptCommand.id,
+ binding: KeyCode.COMMA | MetaKeys.CTRL_COMMAND,
+ preconditions: whenDocAndEditorFocused,
+};
+
+export const SuperscriptShortCut: IShortcutItem = {
+ id: SetInlineFormatSuperscriptCommand.id,
+ binding: KeyCode.PERIOD | MetaKeys.CTRL_COMMAND,
+ preconditions: whenDocAndEditorFocused,
+};
+
+// L E R J
+
+export const AlignLeftShortCut: IShortcutItem = {
+ id: AlignLeftCommand.id,
+ binding: KeyCode.L | MetaKeys.CTRL_COMMAND | MetaKeys.SHIFT,
+ preconditions: whenDocAndEditorFocused,
+};
+
+export const AlignRightShortCut: IShortcutItem = {
+ id: AlignRightCommand.id,
+ binding: KeyCode.R | MetaKeys.CTRL_COMMAND | MetaKeys.SHIFT,
+ preconditions: whenDocAndEditorFocused,
+};
+
+export const AlignCenterShortCut: IShortcutItem = {
+ id: AlignCenterCommand.id,
+ binding: KeyCode.E | MetaKeys.CTRL_COMMAND | MetaKeys.SHIFT,
+ preconditions: whenDocAndEditorFocused,
+};
+
+export const AlignJustifyShortCut: IShortcutItem = {
+ id: AlignJustifyCommand.id,
+ binding: KeyCode.J | MetaKeys.CTRL_COMMAND | MetaKeys.SHIFT,
+ preconditions: whenDocAndEditorFocused,
+};
+
+export const OrderListShortCut: IShortcutItem = {
+ id: OrderListCommand.id,
+ binding: KeyCode.Digit7 | MetaKeys.CTRL_COMMAND | MetaKeys.SHIFT,
+ preconditions: whenDocAndEditorFocused,
+};
+
+export const BulletListShortCut: IShortcutItem = {
+ id: BulletListCommand.id,
+ binding: KeyCode.Digit8 | MetaKeys.CTRL_COMMAND | MetaKeys.SHIFT,
+ preconditions: whenDocAndEditorFocused,
+};
diff --git a/packages/docs-ui/src/docs-ui-plugin.ts b/packages/docs-ui/src/docs-ui-plugin.ts
index a2d52520bd..c1ab963c97 100644
--- a/packages/docs-ui/src/docs-ui-plugin.ts
+++ b/packages/docs-ui/src/docs-ui-plugin.ts
@@ -54,6 +54,9 @@ import { DocBackScrollRenderController } from './controllers/render-controllers/
import { DocCanvasPopManagerService } from './services/doc-popup-manager.service';
import { DocsRenderService } from './services/docs-render.service';
import { DocHeaderFooterController } from './controllers/doc-header-footer.controller';
+import { DocContextMenuRenderController } from './controllers/render-controllers/contextmenu.render-controller';
+import { DocPageLayoutService } from './services/doc-page-layout.service';
+import { DocResizeRenderController } from './controllers/render-controllers/doc-resize.render-controller';
import { DocHoverManagerService } from './services/doc-hover-manager.service';
import { DocHoverRenderController } from './controllers/render-controllers/doc-hover.render-controller';
@@ -142,6 +145,7 @@ export class UniverDocsUIPlugin extends Plugin {
([
[DocSkeletonManagerService],
[DocInterceptorService],
+ [DocPageLayoutService],
[DocRenderController],
[DocZoomRenderController],
] as Dependency[]).forEach((m) => {
@@ -154,7 +158,9 @@ export class UniverDocsUIPlugin extends Plugin {
[DocBackScrollRenderController],
[DocTextSelectionRenderController],
[DocHeaderFooterController],
+ [DocResizeRenderController],
[DocHoverRenderController],
+ [DocContextMenuRenderController],
] as Dependency[]).forEach((m) => {
this._renderManagerSrv.registerRenderModule(UniverInstanceType.UNIVER_DOC, m);
});
diff --git a/packages/docs-ui/src/services/clipboard/clipboard.service.ts b/packages/docs-ui/src/services/clipboard/clipboard.service.ts
index cb58ca57ed..b4ab4b2256 100644
--- a/packages/docs-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/docs-ui/src/services/clipboard/clipboard.service.ts
@@ -323,11 +323,12 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ
if (!text.includes('\r') && Tools.isLegalUrl(text)) {
const id = Tools.generateRandomId();
const docDataModel = this._univerInstanceService.getCurrentUnitForType<DocumentDataModel>(UniverInstanceType.UNIVER_DOC)!;
+ const urlText = `${DataStreamTreeTokenType.CUSTOM_RANGE_START}${dataStream}${DataStreamTreeTokenType.CUSTOM_RANGE_END}`;
const range = this._docCustomRangeService.copyCustomRange(
docDataModel.getUnitId(),
{
startIndex: 0,
- endIndex: dataStream.length - 1,
+ endIndex: urlText.length - 1,
rangeId: id,
rangeType: CustomRangeType.HYPERLINK,
data: text,
@@ -335,7 +336,7 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ
);
return {
- dataStream: `${DataStreamTreeTokenType.CUSTOM_RANGE_START}${dataStream}${DataStreamTreeTokenType.CUSTOM_RANGE_END}`,
+ dataStream: urlText,
customRanges: [range],
};
}
diff --git a/packages/docs-ui/src/services/doc-hover-manager.service.ts b/packages/docs-ui/src/services/doc-hover-manager.service.ts
index 09af3e283a..946a583fa8 100644
--- a/packages/docs-ui/src/services/doc-hover-manager.service.ts
+++ b/packages/docs-ui/src/services/doc-hover-manager.service.ts
@@ -21,21 +21,6 @@ import type { Documents, IMouseEvent, IPointerEvent, Viewport } from '@univerjs/
import { IRenderManagerService, PageLayoutType, Vector2 } from '@univerjs/engine-render';
import { BehaviorSubject } from 'rxjs';
-/**
- * Copyright 2023-present DreamNum Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
export class DocHoverManagerService extends Disposable {
private readonly _activeCustomRanges$ = new BehaviorSubject<ICustomRange[]>([]);
readonly activeCustomRanges$ = this._activeCustomRanges$.asObservable();
diff --git a/packages/docs-ui/src/services/doc-page-layout.service.ts b/packages/docs-ui/src/services/doc-page-layout.service.ts
new file mode 100644
index 0000000000..5f3daa930d
--- /dev/null
+++ b/packages/docs-ui/src/services/doc-page-layout.service.ts
@@ -0,0 +1,86 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { DocumentDataModel } from '@univerjs/core';
+import { Disposable } from '@univerjs/core';
+import { neoGetDocObject, VIEWPORT_KEY } from '@univerjs/docs';
+import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';
+
+export class DocPageLayoutService extends Disposable implements IRenderModule {
+ constructor(
+ private _context: IRenderContext<DocumentDataModel>
+ ) {
+ super();
+ }
+
+ calculatePagePosition() {
+ const docObject = neoGetDocObject(this._context);
+ const docDataModel = this._context.unit;
+ const zoomRatio = docDataModel.getSettings()?.zoomRatio ?? 1;
+ const { document: docsComponent, scene, docBackground } = docObject;
+
+ const parent = scene?.getParent();
+
+ const { width: docsWidth, height: docsHeight, pageMarginLeft, pageMarginTop } = docsComponent;
+ if (parent == null || docsWidth === Number.POSITIVE_INFINITY || docsHeight === Number.POSITIVE_INFINITY) {
+ return;
+ }
+ const { width: engineWidth, height: engineHeight } = parent;
+ let docsLeft = 0;
+ let docsTop = 0;
+
+ let sceneWidth = 0;
+
+ let sceneHeight = 0;
+
+ let scrollToX = Number.POSITIVE_INFINITY;
+
+ if (engineWidth > (docsWidth + pageMarginLeft * 2) * zoomRatio) {
+ docsLeft = engineWidth / 2 - (docsWidth * zoomRatio) / 2;
+ docsLeft /= zoomRatio;
+ sceneWidth = (engineWidth - pageMarginLeft * 2) / zoomRatio;
+
+ scrollToX = 0;
+ } else {
+ docsLeft = pageMarginLeft;
+ sceneWidth = docsWidth + pageMarginLeft * 2;
+
+ scrollToX = (sceneWidth - engineWidth / zoomRatio) / 2;
+ }
+
+ if (engineHeight > docsHeight) {
+ docsTop = engineHeight / 2 - docsHeight / 2;
+ sceneHeight = (engineHeight - pageMarginTop * 2) / zoomRatio;
+ } else {
+ docsTop = pageMarginTop;
+ sceneHeight = docsHeight + pageMarginTop * 2;
+ }
+
+ scene.resize(sceneWidth, sceneHeight + 200);
+
+ docsComponent.translate(docsLeft, docsTop);
+ docBackground.translate(docsLeft, docsTop);
+
+ const viewport = scene.getViewport(VIEWPORT_KEY.VIEW_MAIN);
+ if (scrollToX !== Number.POSITIVE_INFINITY && viewport != null) {
+ viewport.scrollToViewportPos({
+ viewportScrollX: scrollToX,
+ });
+ }
+
+ return this;
+ }
+}
diff --git a/packages/docs-ui/src/services/doc-popup-manager.service.ts b/packages/docs-ui/src/services/doc-popup-manager.service.ts
index 13c1869542..ffcc404cfc 100644
--- a/packages/docs-ui/src/services/doc-popup-manager.service.ts
+++ b/packages/docs-ui/src/services/doc-popup-manager.service.ts
@@ -16,7 +16,7 @@
import type { IDisposable, ITextRange } from '@univerjs/core';
import { Disposable, DisposableCollection, ICommandService, Inject, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
-import { getLineBounding, IRenderManagerService, NodePositionConvertToCursor } from '@univerjs/engine-render';
+import { getLineBounding, IRenderManagerService, NodePositionConvertToCursor, pxToNum } from '@univerjs/engine-render';
import type { BaseObject, Documents, IBoundRectNoAngle, IRender, Scene } from '@univerjs/engine-render';
import type { IPopup } from '@univerjs/ui';
import { ICanvasPopupService } from '@univerjs/ui';
@@ -90,14 +90,19 @@ export class DocCanvasPopManagerService extends Disposable {
top,
bottom: top + height,
};
+ const canvasElement = engine.getCanvasElement();
+ const canvasClientRect = canvasElement.getBoundingClientRect();
+ const widthOfCanvas = pxToNum(canvasElement.style.width); // declared width
const offsetBound = transformBound2OffsetBound(bound, scene);
- const topOffset = engine.getCanvasElement().getBoundingClientRect().top;
+ const { top: topOffset, left: leftOffset, width: domWidth } = canvasClientRect;
+ const scaleAdjust = domWidth / widthOfCanvas;
+
const position = {
- left: offsetBound.left,
- right: offsetBound.right,
- top: offsetBound.top + topOffset,
- bottom: offsetBound.bottom + topOffset,
+ left: (offsetBound.left * scaleAdjust) + leftOffset,
+ right: (offsetBound.right * scaleAdjust) + leftOffset,
+ top: (offsetBound.top * scaleAdjust) + topOffset,
+ bottom: (offsetBound.bottom * scaleAdjust) + topOffset,
};
return position;
};
@@ -140,16 +145,23 @@ export class DocCanvasPopManagerService extends Disposable {
const documentOffsetConfig = document.getOffsetConfig();
const { docsLeft, docsTop } = documentOffsetConfig;
- const top = engine.getCanvasElement().getBoundingClientRect().top;
+ const canvasElement = engine.getCanvasElement();
+ const canvasClientRect = canvasElement.getBoundingClientRect();
+ const widthOfCanvas = pxToNum(canvasElement.style.width); // declared width
+ const { top, left, width } = canvasClientRect; // real width affected by scale
+ const scaleAdjust = width / widthOfCanvas;
+
+ const { scaleX, scaleY } = scene.getAncestorScale();
const convertor = new NodePositionConvertToCursor(documentOffsetConfig, skeleton);
const { borderBoxPointGroup } = convertor.getRangePointData(startPosition, endPosition);
const bounds = getLineBounding(borderBoxPointGroup);
const res = bounds.map((bound) => transformBound2OffsetBound(bound, scene)).map((i) => ({
- left: i.left + docsLeft,
- right: i.right + docsLeft,
- top: i.top + docsTop + top,
- bottom: i.bottom + docsTop + top,
+ left: (i.left + docsLeft * scaleX) * scaleAdjust + left,
+ right: (i.right + docsLeft * scaleX) * scaleAdjust + left,
+ top: (i.top + docsTop * scaleY) * scaleAdjust + top,
+ bottom: (i.bottom + docsTop * scaleY) * scaleAdjust + top,
}));
+
return res;
};
diff --git a/packages/docs-ui/src/views/count-bar/CountBar.tsx b/packages/docs-ui/src/views/count-bar/CountBar.tsx
new file mode 100644
index 0000000000..70683d37dd
--- /dev/null
+++ b/packages/docs-ui/src/views/count-bar/CountBar.tsx
@@ -0,0 +1,33 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import React from 'react';
+
+import styles from './index.module.less';
+import { ZoomSlider } from './ZoomSlider';
+
+interface ICountBarProps {
+ changeRatio?: (ratio: string) => void;
+ onChange?: (value: string) => void;
+}
+
+export function CountBar(props: ICountBarProps) {
+ return (
+ <section className={styles.docCountBar}>
+ <ZoomSlider />
+ </section>
+ );
+}
diff --git a/packages/docs-ui/src/views/count-bar/ZoomSlider.tsx b/packages/docs-ui/src/views/count-bar/ZoomSlider.tsx
new file mode 100644
index 0000000000..7f13bcada9
--- /dev/null
+++ b/packages/docs-ui/src/views/count-bar/ZoomSlider.tsx
@@ -0,0 +1,82 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { DocumentDataModel } from '@univerjs/core';
+import {
+ ICommandService,
+ IUniverInstanceService,
+ UniverInstanceType,
+ useDependency,
+ useObservable,
+} from '@univerjs/core';
+import { Slider } from '@univerjs/design';
+import { SetDocZoomRatioOperation } from '@univerjs/docs';
+import React, { useCallback, useEffect, useMemo, useState } from 'react';
+
+const ZOOM_MAP = [50, 80, 100, 130, 150, 170, 200, 400];
+const DOC_ZOOM_RANGE = [10, 400];
+
+export function ZoomSlider() {
+ const commandService = useDependency(ICommandService);
+ const univerInstanceService = useDependency(IUniverInstanceService);
+ const currentDoc$ = useMemo(() => univerInstanceService.getCurrentTypeOfUnit$<DocumentDataModel>(UniverInstanceType.UNIVER_DOC), []);
+ const documentDataModel = useObservable(currentDoc$);
+
+ const getCurrentZoom = useCallback(() => {
+ if (!documentDataModel) return 100;
+
+ const currentZoom = ((documentDataModel.getSettings()?.zoomRatio ?? 1) * 100);
+ return Math.round(currentZoom);
+ }, [documentDataModel]);
+
+ const [zoom, setZoom] = useState(() => getCurrentZoom());
+
+ useEffect(() => {
+ setZoom(getCurrentZoom());
+
+ const disposable = commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetDocZoomRatioOperation.id) {
+ const currentZoom = getCurrentZoom();
+ setZoom(currentZoom);
+ }
+ });
+
+ return disposable.dispose;
+ }, [commandService, getCurrentZoom]);
+
+ function handleChange(value: number) {
+ setZoom(value);
+ if (documentDataModel == null) {
+ return;
+ }
+
+ const zoomRatio = value / 100;
+
+ commandService.executeCommand(SetDocZoomRatioOperation.id, {
+ unitId: documentDataModel.getUnitId(),
+ zoomRatio,
+ });
+ }
+
+ return (
+ <Slider
+ min={DOC_ZOOM_RANGE[0]}
+ value={zoom}
+ shortcuts={ZOOM_MAP}
+ onChange={handleChange}
+ />
+ );
+}
diff --git a/packages/docs-ui/src/views/count-bar/index.module.less b/packages/docs-ui/src/views/count-bar/index.module.less
new file mode 100644
index 0000000000..83a71b7c79
--- /dev/null
+++ b/packages/docs-ui/src/views/count-bar/index.module.less
@@ -0,0 +1,5 @@
+.doc-count-bar {
+ display: flex;
+ flex: 0 0 260px;
+ justify-content: flex-end;
+}
diff --git a/packages/docs-ui/src/views/count-bar/index.ts b/packages/docs-ui/src/views/count-bar/index.ts
new file mode 100644
index 0000000000..b63b154722
--- /dev/null
+++ b/packages/docs-ui/src/views/count-bar/index.ts
@@ -0,0 +1,17 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+export * from './CountBar';
diff --git a/packages/docs-ui/src/views/doc-container/DocContainer.tsx b/packages/docs-ui/src/views/doc-container/DocContainer.tsx
index 7412ce56d8..789dd6bb7a 100644
--- a/packages/docs-ui/src/views/doc-container/DocContainer.tsx
+++ b/packages/docs-ui/src/views/doc-container/DocContainer.tsx
@@ -205,7 +205,8 @@ export class DocContainer extends Component<IBaseDocContainerProps> {
style={{
display: config.footer ? 'block' : 'none',
}}
- />
+ >
+ </footer>
</div>
<aside
style={{
diff --git a/packages/docs-ui/src/views/doc-footer/DocFooter.tsx b/packages/docs-ui/src/views/doc-footer/DocFooter.tsx
new file mode 100644
index 0000000000..3267ed6ff7
--- /dev/null
+++ b/packages/docs-ui/src/views/doc-footer/DocFooter.tsx
@@ -0,0 +1,28 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import React from 'react';
+import { CountBar } from '../count-bar';
+import styles from './index.module.less';
+
+export const DocFooter = () => {
+ return (
+ <div className={styles.docFooterContainer}>
+ <div />
+ <CountBar />
+ </div>
+ );
+};
diff --git a/packages/docs-ui/src/views/doc-footer/index.module.less b/packages/docs-ui/src/views/doc-footer/index.module.less
new file mode 100644
index 0000000000..edcafe7e76
--- /dev/null
+++ b/packages/docs-ui/src/views/doc-footer/index.module.less
@@ -0,0 +1,7 @@
+.doc-footer-container {
+ display: flex;
+ flex-direction: row;
+ align-items: center;
+ justify-content: space-between;
+ padding: 0 var(--padding-xl);
+}
diff --git a/packages/docs-ui/src/views/doc-footer/index.ts b/packages/docs-ui/src/views/doc-footer/index.ts
new file mode 100644
index 0000000000..edec653038
--- /dev/null
+++ b/packages/docs-ui/src/views/doc-footer/index.ts
@@ -0,0 +1,17 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+export { DocFooter } from './DocFooter';
diff --git a/packages/engine-render/src/components/docs/text-selection/text-selection-render-manager.ts b/packages/engine-render/src/components/docs/text-selection/text-selection-render-manager.ts
index 258b9cffae..08aca6073c 100644
--- a/packages/engine-render/src/components/docs/text-selection/text-selection-render-manager.ts
+++ b/packages/engine-render/src/components/docs/text-selection/text-selection-render-manager.ts
@@ -152,6 +152,9 @@ export interface ITextSelectionRenderManager {
hasFocus(): boolean;
focus(): void;
blur(): void;
+
+ focusEditor(): void;
+ blurEditor(): void;
changeRuntime(docSkeleton: DocumentSkeleton, scene: Scene, document: Documents): void;
dispose(): void;
handleDblClick(evt: IPointerEvent | IMouseEvent): void;
@@ -230,6 +233,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
private _document: Nullable<Documents>;
private _scenePointerMoveSubs: Array<Subscription> = [];
private _scenePointerUpSubs: Array<Subscription> = [];
+ private _editorFocusing = true;
constructor(@ILogService private readonly _logService: ILogService) {
super();
@@ -360,6 +364,9 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
}
focus(): void {
+ if (!this._editorFocusing) {
+ return;
+ }
this._input.focus();
}
@@ -367,6 +374,16 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
this._input.blur();
}
+ focusEditor(): void {
+ this._editorFocusing = true;
+ this.focus();
+ }
+
+ blurEditor(): void {
+ this._editorFocusing = false;
+ this.blur();
+ }
+
// FIXME: for editor cell editor we don't need to blur the input element
deactivate() {
this._container.style.left = '0px';
@@ -494,7 +511,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
if (!this._scene || !this._isSelectionEnabled) {
return;
}
-
+ this._editorFocusing = true;
const scene = this._scene;
const { offsetX: evtOffsetX, offsetY: evtOffsetY } = evt;
@@ -506,6 +523,13 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
});
const position = this._getNodePosition(startNode);
+ const textSelection = this._textSelectionInner$.value;
+ if (startNode && evt.button === 2 && textSelection) {
+ const index = this._getNodeIndex(startNode);
+ if (textSelection.textRanges.some((textRange) => textRange.startOffset! <= index && textRange.endOffset! > index)) {
+ return;
+ }
+ }
if (position == null || startNode == null) {
this._removeAllTextRanges();
@@ -1094,6 +1118,11 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
return nodeInfo;
}
+ private _getNodeIndex(node: INodeInfo) {
+ const index = node.node.parent!.st + node.node.parent!.glyphGroup.indexOf(node.node);
+ return index;
+ }
+
private _detachEvent() {
this._onInputBefore$.complete();
this._onKeydown$.complete();
diff --git a/packages/sheets-ui/src/services/cell-alert-manager.service.ts b/packages/sheets-ui/src/services/cell-alert-manager.service.ts
index 476fa68e6a..80b0b2050f 100644
--- a/packages/sheets-ui/src/services/cell-alert-manager.service.ts
+++ b/packages/sheets-ui/src/services/cell-alert-manager.service.ts
@@ -16,7 +16,7 @@
import { Subject } from 'rxjs';
import type { IDisposable } from '@univerjs/core';
-import { Inject } from '@univerjs/core';
+import { Disposable, Inject } from '@univerjs/core';
import type { ISheetLocation } from '@univerjs/sheets';
import { IRenderManagerService } from '@univerjs/engine-render';
import { CELL_ALERT_KEY } from '../views/cell-alert';
@@ -38,7 +38,7 @@ export interface ICellAlert {
key: string;
}
-export class CellAlertManagerService {
+export class CellAlertManagerService extends Disposable {
private _currentAlert$ = new Subject<[string, { alert: ICellAlert; dispose: IDisposable }][]>();
private _currentAlert: Map<string, { alert: ICellAlert; dispose: IDisposable }> = new Map();
@@ -52,7 +52,11 @@ export class CellAlertManagerService {
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
@Inject(SheetCanvasPopManagerService) private readonly _canvasPopManagerService: SheetCanvasPopManagerService
) {
- // empty
+ super();
+
+ this.disposeWithMe(() => {
+ this._currentAlert$.complete();
+ });
}
showAlert(alert: ICellAlert) {
diff --git a/packages/sheets-zen-editor/src/controllers/zen-editor.controller.ts b/packages/sheets-zen-editor/src/controllers/zen-editor.controller.ts
index 2fdfa38a18..de81eb670c 100644
--- a/packages/sheets-zen-editor/src/controllers/zen-editor.controller.ts
+++ b/packages/sheets-zen-editor/src/controllers/zen-editor.controller.ts
@@ -19,6 +19,7 @@ import {
DEFAULT_EMPTY_DOCUMENT_VALUE,
DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
+ DOCS_ZEN_EDITOR_UNIT_ID_KEY,
DocumentFlavor,
ICommandService,
Inject,
@@ -47,8 +48,6 @@ import { takeUntil } from 'rxjs';
import { OpenZenEditorOperation } from '../commands/operations/zen-editor.operation';
import { IZenEditorManagerService } from '../services/zen-editor.service';
-export const DOCS_ZEN_EDITOR_UNIT_ID_KEY = '__defaultDocumentZenEditorSpecialUnitId_20231218__';
-
@OnLifecycle(LifecycleStages.Steady, ZenEditorController)
export class ZenEditorController extends RxDisposable {
constructor(
diff --git a/packages/sheets-zen-editor/src/index.ts b/packages/sheets-zen-editor/src/index.ts
index 9652d5f235..053ff87bbc 100644
--- a/packages/sheets-zen-editor/src/index.ts
+++ b/packages/sheets-zen-editor/src/index.ts
@@ -15,7 +15,6 @@
*/
export { UniverSheetsZenEditorPlugin } from './plugin';
-export { DOCS_ZEN_EDITOR_UNIT_ID_KEY } from './controllers/zen-editor.controller';
// #region - all commands
diff --git a/packages/sheets-zen-editor/src/views/zen-editor/ZenEditor.tsx b/packages/sheets-zen-editor/src/views/zen-editor/ZenEditor.tsx
index 2efa7dd33b..8e9b55696a 100644
--- a/packages/sheets-zen-editor/src/views/zen-editor/ZenEditor.tsx
+++ b/packages/sheets-zen-editor/src/views/zen-editor/ZenEditor.tsx
@@ -14,14 +14,13 @@
* limitations under the License.
*/
-import { ICommandService, useDependency } from '@univerjs/core';
+import { DOCS_ZEN_EDITOR_UNIT_ID_KEY, ICommandService, useDependency } from '@univerjs/core';
import { IRenderManagerService } from '@univerjs/engine-render';
import { CheckMarkSingle, CloseSingle } from '@univerjs/icons';
import clsx from 'clsx';
import React, { useEffect, useRef } from 'react';
import { CancelZenEditCommand, ConfirmZenEditCommand } from '../../commands/commands/zen-editor.command';
-import { DOCS_ZEN_EDITOR_UNIT_ID_KEY } from '../../controllers/zen-editor.controller';
import { IZenEditorManagerService } from '../../services/zen-editor.service';
import styles from './index.module.less';
diff --git a/packages/ui/src/common/component-manager.ts b/packages/ui/src/common/component-manager.ts
index 8b9247157a..fd33eaf676 100644
--- a/packages/ui/src/common/component-manager.ts
+++ b/packages/ui/src/common/component-manager.ts
@@ -240,6 +240,7 @@ export class ComponentManager {
AddDigitsSingle,
ReduceDigitsSingle,
PercentSingle,
+
};
for (const k in iconList) {
diff --git a/packages/ui/src/services/contextmenu/contextmenu.service.ts b/packages/ui/src/services/contextmenu/contextmenu.service.ts
index 68d9ecb5f2..705d4e9a13 100644
--- a/packages/ui/src/services/contextmenu/contextmenu.service.ts
+++ b/packages/ui/src/services/contextmenu/contextmenu.service.ts
@@ -21,14 +21,19 @@ import type { IDisposable } from '@univerjs/core';
export interface IContextMenuHandler {
/** A callback to open context menu with given position and menu type. */
handleContextMenu(event: IPointerEvent | IMouseEvent, menuType: string): void;
+ hideContextMenu(): void;
+
+ get visible(): boolean;
}
export interface IContextMenuService {
disabled: boolean;
+ get visible(): boolean;
enable(): void;
disable(): void;
triggerContextMenu(event: IPointerEvent | IMouseEvent, menuType: string): void;
+ hideContextMenu(): void;
registerContextMenuHandler(handler: IContextMenuHandler): IDisposable;
}
@@ -40,6 +45,10 @@ export class ContextMenuService extends Disposable implements IContextMenuServic
disabled: boolean = false;
+ get visible(): boolean {
+ return this._currentHandler?.visible ?? false;
+ }
+
disable(): void {
this.disabled = true;
}
@@ -55,6 +64,10 @@ export class ContextMenuService extends Disposable implements IContextMenuServic
this._currentHandler?.handleContextMenu(event, menuType);
}
+ hideContextMenu(): void {
+ this._currentHandler?.hideContextMenu();
+ }
+
registerContextMenuHandler(handler: IContextMenuHandler): IDisposable {
if (this._currentHandler) {
throw new Error('There is already a context menu handler!');
diff --git a/packages/ui/src/services/shortcut/keycode.ts b/packages/ui/src/services/shortcut/keycode.ts
index c1e63b2489..7103bf785c 100644
--- a/packages/ui/src/services/shortcut/keycode.ts
+++ b/packages/ui/src/services/shortcut/keycode.ts
@@ -91,10 +91,13 @@ export enum KeyCode {
NUM_LOCK = 144,
SCROLL_LOCK = 145,
- MINUS = 189,
EQUAL = 187,
+ COMMA = 188,
+ MINUS = 189,
+ PERIOD = 190,
BACK_SLASH = 220,
+
}
export const KeyCodeToChar: { [key: number]: string } = {
@@ -162,6 +165,8 @@ export const KeyCodeToChar: { [key: number]: string } = {
[KeyCode.MINUS]: '-',
[KeyCode.EQUAL]: '=',
+ [KeyCode.PERIOD]: '.',
+ [KeyCode.COMMA]: ',',
[KeyCode.BACK_SLASH]: '\\',
};
diff --git a/packages/ui/src/views/components/context-menu/ContextMenu.tsx b/packages/ui/src/views/components/context-menu/ContextMenu.tsx
index 8ac7df28c3..3ad44cb8fe 100644
--- a/packages/ui/src/views/components/context-menu/ContextMenu.tsx
+++ b/packages/ui/src/views/components/context-menu/ContextMenu.tsx
@@ -28,14 +28,21 @@ export function DesktopContextMenu() {
const [visible, setVisible] = useState(false);
const [menuType, setMenuType] = useState('');
const [offset, setOffset] = useState<[number, number]>([0, 0]);
-
+ const visibleRef = useRef(visible);
const contextMenuService = useDependency(IContextMenuService);
const commandService = useDependency(ICommandService);
const injector = useInjector();
+ visibleRef.current = visible;
useEffect(() => {
const disposables = contextMenuService.registerContextMenuHandler({
handleContextMenu,
+ hideContextMenu() {
+ setVisible(false);
+ },
+ get visible() {
+ return visibleRef.current;
+ },
});
function handleClickOutside(event: MouseEvent) {
diff --git a/packages/ui/src/views/components/context-menu/MobileContextMenu.tsx b/packages/ui/src/views/components/context-menu/MobileContextMenu.tsx
index b13c7f594f..b578f0ba19 100644
--- a/packages/ui/src/views/components/context-menu/MobileContextMenu.tsx
+++ b/packages/ui/src/views/components/context-menu/MobileContextMenu.tsx
@@ -15,7 +15,7 @@
*/
import { ICommandService, useDependency } from '@univerjs/core';
-import React, { useEffect, useState } from 'react';
+import React, { useEffect, useRef, useState } from 'react';
import { Popup } from '@univerjs/design';
import type { IMouseEvent } from '@univerjs/engine-render';
import { IContextMenuService } from '../../../services/contextmenu/contextmenu.service';
@@ -25,13 +25,20 @@ export function MobileContextMenu() {
const [visible, setVisible] = useState(false);
const [menuType, setMenuType] = useState('');
const [offset, setOffset] = useState<[number, number]>([0, 0]);
-
+ const visibleRef = useRef(visible);
const contextMenuService = useDependency(IContextMenuService);
const commandService = useDependency(ICommandService);
+ visibleRef.current = visible;
useEffect(() => {
const disposables = contextMenuService.registerContextMenuHandler({
handleContextMenu,
+ hideContextMenu() {
+ setVisible(false);
+ },
+ get visible() {
+ return visibleRef.current;
+ },
});
document.addEventListener('pointerdown', handleClose);