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 float menu on doc (#4842)

ZhangWei6c14ea16bd6c
diff --git a/packages/docs-ui/src/components/float-toolbar/FloatToolbar.tsx b/packages/docs-ui/src/components/float-toolbar/FloatToolbar.tsx
index 6f8e0ac66c..ff9edfbd04 100644
--- a/packages/docs-ui/src/components/float-toolbar/FloatToolbar.tsx
+++ b/packages/docs-ui/src/components/float-toolbar/FloatToolbar.tsx
@@ -17,21 +17,28 @@
import type { IMenuSchema } from '@univerjs/ui';
import { IMenuManagerService, MenuManagerPosition, ToolbarItem, useDependency } from '@univerjs/ui';
import { useEffect, useState } from 'react';
+import { SetInlineFormatBoldCommand, SetInlineFormatFontFamilyCommand, SetInlineFormatFontSizeCommand, SetInlineFormatItalicCommand, SetInlineFormatStrikethroughCommand, SetInlineFormatSubscriptCommand, SetInlineFormatSuperscriptCommand, SetInlineFormatTextBackgroundColorCommand, SetInlineFormatTextColorCommand, SetInlineFormatUnderlineCommand } from '../../commands/commands/inline-format.command';
interface IFloatToolbarProps {
avaliableMenus?: string[];
- position?: {
- top?: number;
- right?: number;
- bottom?: number;
- left?: number;
- };
+
}
-export function FloatToolbar(props: IFloatToolbarProps) {
- const { avaliableMenus = [], position = { top: 0, left: 0 } } = props;
+const DEFAULT_AVALIABLE_MENUS: string[] = [
+ SetInlineFormatFontFamilyCommand.id,
+ SetInlineFormatFontSizeCommand.id,
+ SetInlineFormatBoldCommand.id,
+ SetInlineFormatItalicCommand.id,
+ SetInlineFormatUnderlineCommand.id,
+ SetInlineFormatStrikethroughCommand.id,
+ SetInlineFormatSubscriptCommand.id,
+ SetInlineFormatSuperscriptCommand.id,
+ SetInlineFormatTextColorCommand.id,
+ SetInlineFormatTextBackgroundColorCommand.id,
+];
- const { top, right, bottom, left } = position;
+export function FloatToolbar(props: IFloatToolbarProps) {
+ const { avaliableMenus = DEFAULT_AVALIABLE_MENUS } = props;
const menuManagerService = useDependency(IMenuManagerService);
@@ -66,16 +73,10 @@ export function FloatToolbar(props: IFloatToolbarProps) {
return (
<div
className={`
- univer-fixed univer-box-border univer-flex univer-rounded-xl univer-border univer-border-solid
- univer-border-gray-200 univer-bg-white univer-p-2
+ univer-box-border univer-flex univer-rounded-xl univer-border univer-border-solid univer-border-gray-200
+ univer-bg-white univer-p-2
univer-shadow-[0_1px_6px_-2px_rgba(30,40,77,0.08),0_2px_6px_-1px_rgba(30,40,77,0.10)]
`}
- style={{
- top: `${top}px`,
- right: `${right}px`,
- bottom: `${bottom}px`,
- left: `${left}px`,
- }}
>
{menus.map((groupItem) => groupItem.item && (
<div key={groupItem.key} className="univer-flex univer-flex-nowrap univer-gap-2 univer-px-2">
diff --git a/packages/docs-ui/src/controllers/float-menu.controller.ts b/packages/docs-ui/src/controllers/float-menu.controller.ts
new file mode 100644
index 0000000000..9df031947d
--- /dev/null
+++ b/packages/docs-ui/src/controllers/float-menu.controller.ts
@@ -0,0 +1,78 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * 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 { IDisposable, ITextRangeParam, Nullable } from '@univerjs/core';
+import { Disposable, Inject, isInternalEditorID, toDisposable } from '@univerjs/core';
+import { DocSelectionManagerService } from '@univerjs/docs';
+import { ComponentManager } from '@univerjs/ui';
+import { FloatToolbar } from '../components/float-toolbar/FloatToolbar';
+import { DocCanvasPopManagerService } from '../services/doc-popup-manager.service';
+
+const FLOAT_MENU_COMPONENT_KEY = 'univer.doc.float-menu';
+
+export class FloatMenuController extends Disposable {
+ private _floatMenu: Nullable<IDisposable> = null;
+
+ constructor(
+ @Inject(DocSelectionManagerService) private readonly _docSelectionManagerService: DocSelectionManagerService,
+ @Inject(DocCanvasPopManagerService) private readonly _docCanvasPopManagerService: DocCanvasPopManagerService,
+ @Inject(ComponentManager) private readonly _componentManager: ComponentManager
+ ) {
+ super();
+ this._registerFloatMenu();
+ this._initSelectionChange();
+ }
+
+ private _registerFloatMenu() {
+ this.disposeWithMe(this._componentManager.register(FLOAT_MENU_COMPONENT_KEY, FloatToolbar));
+ }
+
+ private _initSelectionChange() {
+ this.disposeWithMe(this._docSelectionManagerService.textSelection$.subscribe((selection) => {
+ const { unitId, textRanges } = selection;
+ if (isInternalEditorID(unitId)) {
+ this.hideFloatMenu();
+ return;
+ }
+
+ if ((textRanges.length > 0) && textRanges.some((range) => !range.collapsed)) {
+ this.hideFloatMenu();
+ this.showFloatMenu(unitId, textRanges.find((range) => !range.collapsed)!);
+ } else {
+ this.hideFloatMenu();
+ }
+ }));
+ }
+
+ hideFloatMenu() {
+ this._floatMenu?.dispose();
+ this._floatMenu = null;
+ }
+
+ showFloatMenu(unitId: string, range: ITextRangeParam) {
+ this._floatMenu = this._docCanvasPopManagerService.attachPopupToRange(
+ range,
+ {
+ componentKey: FLOAT_MENU_COMPONENT_KEY,
+ direction: range.direction === 'backward' ? 'top-center' : 'bottom-center',
+ offset: [0, 4],
+ },
+ unitId
+ );
+
+ return toDisposable(() => this.hideFloatMenu());
+ }
+}
diff --git a/packages/docs-ui/src/docs-ui-plugin.ts b/packages/docs-ui/src/docs-ui-plugin.ts
index 679d66f0da..b7dad856fc 100644
--- a/packages/docs-ui/src/docs-ui-plugin.ts
+++ b/packages/docs-ui/src/docs-ui-plugin.ts
@@ -95,6 +95,7 @@ import { DocMoveCursorController } from './controllers/doc-move-cursor.controlle
import { DocParagraphSettingController } from './controllers/doc-paragraph-setting.controller';
import { DocTableController } from './controllers/doc-table.controller';
import { DocUIController } from './controllers/doc-ui.controller';
+import { FloatMenuController } from './controllers/float-menu.controller';
import { DocBackScrollRenderController } from './controllers/render-controllers/back-scroll.render-controller';
import { DocChecklistRenderController } from './controllers/render-controllers/doc-checklist.render-controller';
import { DocClipboardController } from './controllers/render-controllers/doc-clipboard.controller';
@@ -170,6 +171,7 @@ export class UniverDocsUIPlugin extends Plugin {
touchDependencies(this._injector, [
[DocStateChangeManagerService],
[DocsRenderService],
+
]);
}
@@ -186,6 +188,7 @@ export class UniverDocsUIPlugin extends Plugin {
// FIXME: LifecycleStages.Rendered must be used, otherwise the menu cannot be added to the DOM, but the sheet ui
// plugin can be added in LifecycleStages.Ready
[DocUIController],
+ [FloatMenuController],
]);
}
@@ -301,6 +304,7 @@ export class UniverDocsUIPlugin extends Plugin {
[DocStateChangeManagerService],
[DocAutoFormatService],
[DocMenuStyleService],
+ [FloatMenuController],
], this._config.override);
dependencies.forEach((d) => injector.add(d));
}
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 fdd3f4fd9b..a0446ffb80 100644
--- a/packages/docs-ui/src/services/doc-popup-manager.service.ts
+++ b/packages/docs-ui/src/services/doc-popup-manager.service.ts
@@ -289,7 +289,7 @@ export class DocCanvasPopManagerService extends Disposable {
excludeRects$: bounds$,
direction:
(direction.includes('top') || direction.includes('bottom')) ?
- bounds.length > 1 ? multipleDirection : direction
+ bounds.length > 1 ? multipleDirection ?? direction : direction
: 'bottom',
canvasElement: currentRender.engine.getCanvasElement(),
});