From 5ab492fd78f5e09188ff4f03456c4ce5d8ed15d1 Mon Sep 17 00:00:00 2001 From: zhangw Date: Thu, 5 Sep 2024 14:58:45 +0800 Subject: [PATCH] fix(uni-formula-ui): uni formula deps error (#3302) --- .../doc-formula-input.controller.ts | 45 ++++++++++++++++--- .../src/services/formula-popup.service.ts | 15 ++++--- .../drawing-popup-menu.controller.ts | 21 +++++---- .../commands/operations/popup.operation.ts | 8 +++- .../src/services/hyper-link-popup.service.ts | 9 ++-- .../operations/mention-popup.operation.ts | 3 +- .../doc-mention-trigger.controller.ts | 1 + .../src/services/doc-mention-popup.service.ts | 7 +-- .../src/services/doc-mention.service.ts | 6 +-- .../src/services/doc-popup-manager.service.ts | 35 +++++++-------- 10 files changed, 100 insertions(+), 50 deletions(-) diff --git a/packages-experimental/uni-formula-ui/src/controllers/doc-formula-input.controller.ts b/packages-experimental/uni-formula-ui/src/controllers/doc-formula-input.controller.ts index 456fb6a434..fe325e4d16 100644 --- a/packages-experimental/uni-formula-ui/src/controllers/doc-formula-input.controller.ts +++ b/packages-experimental/uni-formula-ui/src/controllers/doc-formula-input.controller.ts @@ -14,12 +14,15 @@ * limitations under the License. */ +import type { DocumentDataModel } from '@univerjs/core'; import { CustomRangeType, Disposable, ICommandService, ILogService, Inject, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core'; import type { IInsertCommandParams } from '@univerjs/docs'; import { DeleteLeftCommand, InsertCommand, MoveCursorOperation, TextSelectionManagerService } from '@univerjs/docs'; import { IEditorService } from '@univerjs/ui'; import { DocEventManagerService } from '@univerjs/docs-ui'; +import { filter, map, mergeMap } from 'rxjs'; +import { IRenderManagerService } from '@univerjs/engine-render'; import { AddDocUniFormulaCommand, RemoveDocUniFormulaCommand, UpdateDocUniFormulaCommand } from '../commands/commands/doc.command'; import type { IShowFormulaPopupOperationParams } from '../commands/operations/operation'; import { CloseFormulaPopupOperation, ShowFormulaPopupOperation } from '../commands/operations/operation'; @@ -35,9 +38,9 @@ export class DocUniFormulaInputController extends Disposable { @IUniverInstanceService private readonly _instanceSrv: IUniverInstanceService, @IEditorService private readonly _editorService: IEditorService, @ILogService private readonly _logService: ILogService, - @Inject(DocEventManagerService) private readonly _docEventManagerService: DocEventManagerService, @Inject(UniFormulaPopupService) private readonly _formulaPopupSrv: UniFormulaPopupService, - @Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService + @Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService, + @IRenderManagerService private readonly _renderManagerService: IRenderManagerService ) { super(); @@ -91,11 +94,16 @@ export class DocUniFormulaInputController extends Disposable { } private _initHoverListener(): void { - this.disposeWithMe(this._docEventManagerService.hoverCustomRanges$.subscribe((customRanges) => { - const focusedUnit = this._instanceSrv.getFocusedUnit(); + const rangesWithDoc$ = this._instanceSrv.focused$.pipe( + map((focused) => focused ? this._instanceSrv.getUnit(focused, UniverInstanceType.UNIVER_DOC) : null), + map((doc) => doc && { doc, docEventManagerService: this._renderManagerService.getRenderById(doc!.getUnitId())?.with(DocEventManagerService) }), + filter((info) => !!info), + mergeMap((info) => info.docEventManagerService!.hoverCustomRanges$.pipe(map((ranges) => ({ doc: info.doc, ranges })))) + ); + this.disposeWithMe(rangesWithDoc$.subscribe(({ doc, ranges: customRanges }) => { if ( - !focusedUnit || + !doc || this._formulaPopupSrv.popupInfo?.type === 'new' || this._formulaPopupSrv.popupLocked ) { @@ -108,7 +116,32 @@ export class DocUniFormulaInputController extends Disposable { this._logService.debug('[DocUniFormulaController]: activeCustomRanges', customRanges); this._showPopup({ startIndex, - unitId: focusedUnit.getUnitId(), + unitId: doc.getUnitId(), + position: { rangeId }, + type: 'existing', + }); + } else { + if (!this._hovered) { + this._closePopup(500); + } + } + })); + this.disposeWithMe(rangesWithDoc$.subscribe(({ doc, ranges: customRanges }) => { + if ( + !doc || + this._formulaPopupSrv.popupInfo?.type === 'new' || + this._formulaPopupSrv.popupLocked + ) { + return; + } + + const formulaCustomRange = customRanges.find((range) => range.range.rangeType === CustomRangeType.UNI_FORMULA)?.range; + if (formulaCustomRange) { + const { startIndex, rangeId } = formulaCustomRange; + this._logService.debug('[DocUniFormulaController]: activeCustomRanges', customRanges); + this._showPopup({ + startIndex, + unitId: doc.getUnitId(), position: { rangeId }, type: 'existing', }); diff --git a/packages-experimental/uni-formula-ui/src/services/formula-popup.service.ts b/packages-experimental/uni-formula-ui/src/services/formula-popup.service.ts index 7ab1d5e769..feecdebadd 100644 --- a/packages-experimental/uni-formula-ui/src/services/formula-popup.service.ts +++ b/packages-experimental/uni-formula-ui/src/services/formula-popup.service.ts @@ -104,12 +104,15 @@ export class UniFormulaPopupService extends Disposable { ? this._uniFormulaService.getDocFormula(unitId, position.rangeId)?.f ?? '=' : '='; - const disposable = this._docCanvasPopupManagerService.attachPopupToRange(makeSelection(startIndex), { - componentKey: DOC_FORMULA_POPUP_KEY, - onClickOutside: () => this.closePopup(), // user may update ref range selections - direction: 'top', - }); - + const disposable = this._docCanvasPopupManagerService.attachPopupToRange( + makeSelection(startIndex), + { + componentKey: DOC_FORMULA_POPUP_KEY, + onClickOutside: () => this.closePopup(), // user may update ref range selections + direction: 'top', + }, + unitId + ); this._popupInfo$.next({ unitId, disposable, type, f, startIndex, position }); return true; } diff --git a/packages/docs-drawing-ui/src/controllers/drawing-popup-menu.controller.ts b/packages/docs-drawing-ui/src/controllers/drawing-popup-menu.controller.ts index fd6b72305f..65ffa69e7f 100644 --- a/packages/docs-drawing-ui/src/controllers/drawing-popup-menu.controller.ts +++ b/packages/docs-drawing-ui/src/controllers/drawing-popup-menu.controller.ts @@ -116,15 +116,20 @@ export class DocDrawingPopupMenuController extends RxDisposable { } const { unitId, subUnitId, drawingId } = drawingParam; - - disposePopups.push(this.disposeWithMe(this._canvasPopManagerService.attachPopupToObject(object, { - componentKey: COMPONENT_IMAGE_POPUP_MENU, - direction: 'horizontal', - offset: [2, 0], - extraProps: { - menuItems: this._getImageMenuItems(unitId, subUnitId, drawingId), + const popup = this._canvasPopManagerService.attachPopupToObject( + object, + { + componentKey: COMPONENT_IMAGE_POPUP_MENU, + direction: 'horizontal', + offset: [2, 0], + extraProps: { + menuItems: this._getImageMenuItems(unitId, subUnitId, drawingId), + }, }, - }))); + unitId + ); + + disposePopups.push(this.disposeWithMe(popup)); const focusDrawings = this._drawingManagerService.getFocusDrawings(); diff --git a/packages/docs-hyper-link-ui/src/commands/operations/popup.operation.ts b/packages/docs-hyper-link-ui/src/commands/operations/popup.operation.ts index 1d9b4c24d4..18275a46e4 100644 --- a/packages/docs-hyper-link-ui/src/commands/operations/popup.operation.ts +++ b/packages/docs-hyper-link-ui/src/commands/operations/popup.operation.ts @@ -69,11 +69,17 @@ export const ShowDocHyperLinkEditPopupOperation: ICommand): Nullable { + showEditPopup(unitId: string, linkInfo: Nullable): Nullable { if (this._editPopup) { this._editPopup.dispose(); } @@ -89,13 +89,15 @@ export class DocHyperLinkPopupService extends Disposable { }]); } } + if (activeRange) { this._editPopup = this._docCanvasPopupManagerService.attachPopupToRange( activeRange, { componentKey: DocHyperLinkEdit.componentKey, direction: 'bottom', - } + }, + unitId ); return this._editPopup; } @@ -148,7 +150,8 @@ export class DocHyperLinkPopupService extends Disposable { onClickOutside: () => { this.hideInfoPopup(); }, - } + }, + info.unitId ); return this._infoPopup; } diff --git a/packages/docs-mention-ui/src/commands/operations/mention-popup.operation.ts b/packages/docs-mention-ui/src/commands/operations/mention-popup.operation.ts index 6e0b5f701a..81a60ff1a2 100644 --- a/packages/docs-mention-ui/src/commands/operations/mention-popup.operation.ts +++ b/packages/docs-mention-ui/src/commands/operations/mention-popup.operation.ts @@ -41,6 +41,7 @@ export const CloseMentionInfoPopupOperation: ICommand = { export interface IShowMentionEditPopupOperationParams { startIndex: number; + unitId: string; } export const ShowMentionEditPopupOperation: ICommand = { @@ -51,7 +52,7 @@ export const ShowMentionEditPopupOperation: ICommand { if (editing !== undefined && editing !== null) { - this.showEditPopup(editing); + this.showEditPopup(editing.unitId, editing.index); } else { this.closeEditPopup(); } @@ -54,7 +54,7 @@ export class DocMentionPopupService extends Disposable { closeInfoPopup() {} - showEditPopup(index: number) { + showEditPopup(unitId: string, index: number) { this.closeEditPopup(); const dispose = this._docCanvasPopupManagerService.attachPopupToRange( { startOffset: index, endOffset: index, collapsed: true }, @@ -64,7 +64,8 @@ export class DocMentionPopupService extends Disposable { this.closeEditPopup(); }, direction: 'bottom', - } + }, + unitId ); this._editPopup$.next({ popup: dispose, anchor: index }); } diff --git a/packages/docs-mention-ui/src/services/doc-mention.service.ts b/packages/docs-mention-ui/src/services/doc-mention.service.ts index 8915880bb0..7b5265c5ce 100644 --- a/packages/docs-mention-ui/src/services/doc-mention.service.ts +++ b/packages/docs-mention-ui/src/services/doc-mention.service.ts @@ -19,7 +19,7 @@ import { Disposable } from '@univerjs/core'; import { BehaviorSubject } from 'rxjs'; export class DocMentionService extends Disposable { - private readonly _editing$ = new BehaviorSubject>(undefined); + private readonly _editing$ = new BehaviorSubject>(undefined); readonly editing$ = this._editing$.asObservable(); get editing() { @@ -34,8 +34,8 @@ export class DocMentionService extends Disposable { }); } - startEditing(index: number) { - this._editing$.next(index); + startEditing(item: { unitId: string; index: number }) { + this._editing$.next(item); } endEditing() { 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 60e9b24552..9432fe1650 100644 --- a/packages/docs-ui/src/services/doc-popup-manager.service.ts +++ b/packages/docs-ui/src/services/doc-popup-manager.service.ts @@ -15,7 +15,7 @@ */ import type { IDisposable, ITextRangeParam } from '@univerjs/core'; -import { Disposable, DisposableCollection, ICommandService, Inject, IUniverInstanceService, UniverInstanceType } from '@univerjs/core'; +import { Disposable, DisposableCollection, ICommandService, Inject, IUniverInstanceService } from '@univerjs/core'; 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'; @@ -222,18 +222,10 @@ export class DocCanvasPopManagerService extends Disposable { * @param popup popup item * @returns disposable */ - attachPopupToObject(targetObject: BaseObject, popup: IDocCanvasPopup): IDisposable { - const workbook = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_DOC)!; - const unitId = workbook.getUnitId(); - // const subUnitId = - + attachPopupToObject(targetObject: BaseObject, popup: IDocCanvasPopup, unitId: string): IDisposable { const currentRender = this._renderManagerService.getRenderById(unitId); if (!currentRender) { - return { - dispose: () => { - // empty - }, - }; + throw new Error(`Current render not found, unitId: ${unitId}`); } const { position, position$, disposable } = this._createObjectPositionObserver(targetObject, currentRender); @@ -256,17 +248,22 @@ export class DocCanvasPopManagerService extends Disposable { }; } - attachPopupToRange(range: ITextRangeParam, popup: IDocCanvasPopup): IDisposable { - const workbook = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_DOC)!; - const unitId = workbook.getUnitId(); + /** + * attach a popup to doc range + * @param range doc range + * @param popup popup item + * @param unitId unit id + * @returns disposable + */ + attachPopupToRange(range: ITextRangeParam, popup: IDocCanvasPopup, unitId: string): IDisposable { + const doc = this._univerInstanceService.getUnit(unitId); + if (!doc) { + throw new Error(`Document not found, unitId: ${unitId}`); + } const { direction = 'top', multipleDirection } = popup; const currentRender = this._renderManagerService.getRenderById(unitId); if (!currentRender) { - return { - dispose: () => { - // empty - }, - }; + throw new Error(`Current render not found, unitId: ${unitId}`); } const { positions: bounds, positions$: bounds$, disposable } = this._createRangePositionObserver(range, currentRender);