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
feat(docs-quick-insert): show all menus at quick insert button (#4984)
siamc8896a6955f3
diff --git a/packages/docs-quick-insert-ui/src/controllers/doc-quick-insert-menu.controller.ts b/packages/docs-quick-insert-ui/src/controllers/doc-quick-insert-menu.controller.tsindex 4f5c7b8353..b72a440bac 100644--- a/packages/docs-quick-insert-ui/src/controllers/doc-quick-insert-menu.controller.ts+++ b/packages/docs-quick-insert-ui/src/controllers/doc-quick-insert-menu.controller.ts@@ -18,12 +18,17 @@ import type { DocumentDataModel, INeedCheckDisposable, Nullable } from '@univerjimport type { IRenderContext, IRenderModule } from '@univerjs/engine-render';import { Disposable, Inject } from '@univerjs/core';import { DocCanvasPopManagerService, DocEventManagerService } from '@univerjs/docs-ui';-import { combineLatest } from 'rxjs';+import { BehaviorSubject, combineLatest } from 'rxjs';import { DocQuickInsertPopupService } from '../services/doc-quick-insert-popup.service';-import { QuickInsertButton } from '../views/menu';+import { QuickInsertButtonComponentKey } from '../views/menu/const';export class DocQuickInsertMenuController extends Disposable implements IRenderModule {- private _popup: Nullable<{ startIndex: number; disposable: INeedCheckDisposable }> = null;+ private _popup$ = new BehaviorSubject<Nullable<{ startIndex: number; disposable: INeedCheckDisposable }>>(null);+ readonly popup$ = this._popup$.asObservable();+ get popup() {+ return this._popup$.value;+ }+constructor(private _context: IRenderContext<DocumentDataModel>,@Inject(DocEventManagerService) private _docEventManagerService: DocEventManagerService,@@ -36,24 +41,24 @@ export class DocQuickInsertMenuController extends Disposable implements IRenderM}private _init() {- this.disposeWithMe(combineLatest([this._docEventManagerService.hoverParagraphLeft$, this._docEventManagerService.hoverParagraph$]).subscribe(([left, paragraph]) => {+ this.disposeWithMe(combineLatest([this._docEventManagerService.hoverParagraphLeftRealTime$, this._docEventManagerService.hoverParagraphRealTime$]).subscribe(([left, paragraph]) => {const p = left ?? paragraph;if (!p) {this._hideMenu(true);return;}-- if (p.paragraphStart === p.paragraphEnd && p.startIndex !== this._popup?.startIndex) {+ if (p.paragraphStart === p.paragraphEnd) {+ if (this._docQuickInsertPopupService.editPopup || p.startIndex === this.popup?.startIndex) return;this._hideMenu(true);const disposable = this._docCanvasPopManagerService.attachPopupToRect(p.firstLine, {- componentKey: QuickInsertButton.componentKey,+ componentKey: QuickInsertButtonComponentKey,direction: 'left-center',}, this._context.unit.getUnitId());- this._popup = {+ this._popup$.next({startIndex: p.startIndex,disposable,- };+ });} else {this._hideMenu(true);}@@ -61,9 +66,10 @@ export class DocQuickInsertMenuController extends Disposable implements IRenderM}private _hideMenu(force: boolean) {- if (this._popup && (force || this._popup.disposable.canDispose())) {- this._popup.disposable.dispose();- this._popup = null;+ if (this._docQuickInsertPopupService.editPopup) return;+ if (this.popup && (force || this.popup.disposable.canDispose())) {+ this.popup.disposable.dispose();+ this._popup$.next(null);}}}diff --git a/packages/docs-quick-insert-ui/src/services/doc-quick-insert-popup.service.ts b/packages/docs-quick-insert-ui/src/services/doc-quick-insert-popup.service.tsindex a7d8f7cf6b..1c5e25a54a 100644--- a/packages/docs-quick-insert-ui/src/services/doc-quick-insert-popup.service.ts+++ b/packages/docs-quick-insert-ui/src/services/doc-quick-insert-popup.service.ts@@ -54,6 +54,9 @@ const noopDisposable = {};export class DocQuickInsertPopupService extends Disposable {private readonly _popups: Set<IDocPopup> = new Set();+ get popups() {+ return Array.from(this._popups);+ }private readonly _editPopup$ = new BehaviorSubject<Nullable<{popup: IDocPopup;diff --git a/packages/docs-quick-insert-ui/src/views/QuickInsertPopup.tsx b/packages/docs-quick-insert-ui/src/views/QuickInsertPopup.tsxindex 508b99b35b..4600884917 100644--- a/packages/docs-quick-insert-ui/src/views/QuickInsertPopup.tsx+++ b/packages/docs-quick-insert-ui/src/views/QuickInsertPopup.tsx@@ -283,7 +283,7 @@ export const QuickInsertPopup = () => {<div className={clsx('univer-mt-2')}>{hasMenus? (- <Menu wrapperClass="univer-max-h-[360px] univer-w-[220px]">+ <Menu wrapperClass="univer-max-h-[360px] univer-w-[220px] univer-overflow-y-auto univer-overflow-x-hidden">{renderMenus(filteredMenus)}</Menu>)diff --git a/packages/docs-quick-insert-ui/src/views/menu/const.ts b/packages/docs-quick-insert-ui/src/views/menu/const.tsnew file mode 100644index 0000000000..16962a68a5--- /dev/null+++ b/packages/docs-quick-insert-ui/src/views/menu/const.ts@@ -0,0 +1,17 @@+/**+ * 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.+ */++export const QuickInsertButtonComponentKey = 'doc.quick-insert.button';diff --git a/packages/docs-quick-insert-ui/src/views/menu/index.tsx b/packages/docs-quick-insert-ui/src/views/menu/index.tsxindex 0cd60c2b3e..595ff502f9 100644--- a/packages/docs-quick-insert-ui/src/views/menu/index.tsx+++ b/packages/docs-quick-insert-ui/src/views/menu/index.tsx@@ -15,52 +15,71 @@*/import type { DocumentDataModel } from '@univerjs/core';+import type { IDocPopup } from '../../services/doc-quick-insert-popup.service';import { IUniverInstanceService, UniverInstanceType } from '@univerjs/core';-import { DocEventManagerService } from '@univerjs/docs-ui';+import { DocSelectionManagerService } from '@univerjs/docs';import { IRenderManagerService } from '@univerjs/engine-render';import { PlusSingle } from '@univerjs/icons';-import { useDependency, useObservable } from '@univerjs/ui';+import { ILayoutService, useDependency, useEvent, useObservable } from '@univerjs/ui';import { useMemo } from 'react';+import { combineLatest, map } from 'rxjs';+import { DocQuickInsertMenuController } from '../../controllers/doc-quick-insert-menu.controller';import { DocQuickInsertPopupService } from '../../services/doc-quick-insert-popup.service';+import { QuickInsertButtonComponentKey } from './const';interface IQuickInsertButtonProps {className?: string;}-export const QuickInsertButton = ({- className = '',-}: IQuickInsertButtonProps) => {+export const QuickInsertButton = ({ className = '' }: IQuickInsertButtonProps) => {const docQuickInsertPopupService = useDependency(DocQuickInsertPopupService);const univerInstanceService = useDependency(IUniverInstanceService);const renderManagerService = useDependency(IRenderManagerService);const currentDoc = useObservable(useMemo(() => univerInstanceService.getCurrentTypeOfUnit$<DocumentDataModel>(UniverInstanceType.UNIVER_DOC), [univerInstanceService]));const currentUnit = currentDoc && renderManagerService.getRenderById(currentDoc.getUnitId());- const docEventManagerService = currentUnit?.with(DocEventManagerService);- const paragraph = useObservable(docEventManagerService?.hoverParagraph$);- const paragraphLeft = useObservable(docEventManagerService?.hoverParagraphLeft$);+ const docQuickInsertMenuController = currentUnit?.with(DocQuickInsertMenuController);+ const layoutService = useDependency(ILayoutService);+ const docSelectionManagerService = useDependency(DocSelectionManagerService);+ const editPopup = useObservable(docQuickInsertPopupService.editPopup$);- const onClick = () => {- const p = paragraph ?? paragraphLeft;+ const onClick: React.MouseEventHandler<HTMLDivElement> = useEvent((event) => {+ const p = docQuickInsertMenuController?.popup;if (!p) {return;}- const popup = docQuickInsertPopupService.resolvePopup('/');- if (!popup) {- return;- }++ const allPopups = docQuickInsertPopupService.popups;+ // combine all popups into one+ const popup: IDocPopup = {+ keyword: '',+ menus$: combineLatest(allPopups.map((p) => p.menus$))+ .pipe(+ map((menusCollection) => menusCollection.flat())+ ),+ };++ docSelectionManagerService.replaceDocRanges([{+ startOffset: p.startIndex,+ endOffset: p.startIndex,+ }]);docQuickInsertPopupService.showPopup({popup,index: p.startIndex - 1,unitId: currentDoc?.getUnitId() ?? '',});- };+ setTimeout(() => {+ // keep the cursor in doc+ layoutService.focus();+ });+ });return (<divclassName={`univer-mr-1 univer-flex univer-cursor-pointer univer-items-center univer-gap-2.5 univer-rounded-full- univer-border univer-border-gray-200 univer-bg-white univer-p-1.5 univer-shadow-sm+ univer-border univer-border-solid univer-border-gray-200 univer-p-1.5 univer-shadow-smhover:univer-bg-gray-100${className}+ ${editPopup ? 'univer-bg-gray-100' : 'univer-bg-white'}`}onClick={onClick}role="button"@@ -71,4 +90,4 @@ export const QuickInsertButton = ({);};-QuickInsertButton.componentKey = 'doc.quick-insert.button';+QuickInsertButton.componentKey = QuickInsertButtonComponentKey;diff --git a/packages/docs-ui/src/services/doc-event-manager.service.ts b/packages/docs-ui/src/services/doc-event-manager.service.tsindex 81da74b4d6..a4a532b9e6 100644--- a/packages/docs-ui/src/services/doc-event-manager.service.ts+++ b/packages/docs-ui/src/services/doc-event-manager.service.ts@@ -218,14 +218,22 @@ export class DocEventManagerService extends Disposable implements IRenderModuleprivate readonly _clickBullet$ = new Subject<IBulletActive>();readonly clickBullets$ = this._clickBullet$.asObservable();- private readonly _hoverParagraph$ = new Subject<Nullable<IMutiPageParagraphBound>>();+ private readonly _hoverParagraph$ = new BehaviorSubject<Nullable<IMutiPageParagraphBound>>(null);readonly hoverParagraph$ = this._hoverParagraph$.pipe(distinctUntilChanged((pre, aft) => pre?.startIndex === aft?.startIndex && pre?.segmentId === aft?.segmentId && pre?.pageIndex === aft?.pageIndex));readonly hoverParagraphRealTime$ = this._hoverParagraph$.asObservable();- private readonly _hoverParagraphLeft$ = new Subject<Nullable<IMutiPageParagraphBound>>();+ get hoverParagraph() {+ return this._hoverParagraph$.value;+ }++ private readonly _hoverParagraphLeft$ = new BehaviorSubject<Nullable<IMutiPageParagraphBound>>(null);readonly hoverParagraphLeft$ = this._hoverParagraphLeft$.pipe(distinctUntilChanged((pre, aft) => pre?.startIndex === aft?.startIndex && pre?.segmentId === aft?.segmentId && pre?.pageIndex === aft?.pageIndex));readonly hoverParagraphLeftRealTime$ = this._hoverParagraphLeft$.asObservable();+ get hoverParagraphLeft() {+ return this._hoverParagraphLeft$.value;+ }+private readonly _hoverTableCell$ = new Subject<Nullable<ITableCellBound>>();readonly hoverTableCell$ = this._hoverTableCell$.pipe(distinctUntilChanged((pre, aft) => pre?.rowIndex === aft?.rowIndex && pre?.colIndex === aft?.colIndex && pre?.tableId === aft?.tableId && pre?.pageIndex === aft?.pageIndex));readonly hoverTableCellRealTime$ = this._hoverTableCell$.asObservable();