Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(sheets-ui): can't edit after dispose (#3788)

zhangw77b39cda6286
diff --git a/packages/sheets-ui/src/controllers/render-controllers/editor-bridge.render-controller.ts b/packages/sheets-ui/src/controllers/render-controllers/editor-bridge.render-controller.ts
index 5f5877fb47..90433f03e3 100644
--- a/packages/sheets-ui/src/controllers/render-controllers/editor-bridge.render-controller.ts
+++ b/packages/sheets-ui/src/controllers/render-controllers/editor-bridge.render-controller.ts
@@ -16,10 +16,10 @@
import type { ICommandInfo, IDisposable, IExecutionOptions, ISelectionCell, Nullable, Workbook } from '@univerjs/core';
import type { IEditorInputConfig } from '@univerjs/docs-ui';
-import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';
+import type { IRender, IRenderContext, IRenderModule } from '@univerjs/engine-render';
import type { ISelectionWithStyle } from '@univerjs/sheets';
import type { ICurrentEditCellParam, IEditorBridgeServiceVisibleParam } from '../../services/editor-bridge.service';
-import { DisposableCollection, FOCUSING_FX_BAR_EDITOR, FOCUSING_SHEET, ICommandService, IContextService, Inject, IUniverInstanceService, RxDisposable, UniverInstanceType } from '@univerjs/core';
+import { DisposableCollection, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, FOCUSING_FX_BAR_EDITOR, FOCUSING_SHEET, ICommandService, IContextService, Inject, IUniverInstanceService, RxDisposable, toDisposable, UniverInstanceType } from '@univerjs/core';
import { DocSelectionRenderService, IEditorService, IRangeSelectorService } from '@univerjs/docs-ui';
import { DeviceInputEventType, IRenderManagerService } from '@univerjs/engine-render';
import {
@@ -28,7 +28,7 @@ import {
SetWorksheetActiveOperation,
SheetsSelectionsService,
} from '@univerjs/sheets';
-import { merge } from 'rxjs';
+import { filter, merge } from 'rxjs';
import { SetZoomRatioCommand } from '../../commands/commands/set-zoom-ratio.command';
import { SetActivateCellEditOperation } from '../../commands/operations/activate-cell-edit.operation';
import { SetCellEditVisibleOperation } from '../../commands/operations/cell-edit.operation';
@@ -163,23 +163,36 @@ export class EditorBridgeRenderController extends RxDisposable implements IRende
* @param d DisposableCollection
*/
private _initialKeyboardListener(d: DisposableCollection) {
- const docSelectionRenderService = this._renderManagerService.getCurrentTypeOfRenderer(UniverInstanceType.UNIVER_DOC)?.with(DocSelectionRenderService);
-
- if (docSelectionRenderService) {
- d.add(docSelectionRenderService.onInputBefore$.subscribe((config) => {
- if (!this._isCurrentSheetFocused()) {
- return;
- }
-
- const isFocusFormulaEditor = this._contextService.getContextValue(FOCUSING_FX_BAR_EDITOR);
- const isFocusSheets = this._contextService.getContextValue(FOCUSING_SHEET);
- // TODO@Jocs: should get editor instead of current doc
- const unitId = this._instanceSrv.getCurrentUniverDocInstance()?.getUnitId();
- if (this._editorBridgeService.isVisible().visible) return;
+ let disposable: Nullable<IDisposable> = null;
+ const addEvent = (render: IRender) => {
+ const docSelectionRenderService = render.with(DocSelectionRenderService);
+ if (docSelectionRenderService) {
+ disposable = toDisposable(docSelectionRenderService.onInputBefore$.subscribe((config) => {
+ if (!this._isCurrentSheetFocused()) {
+ return;
+ }
+
+ const isFocusFormulaEditor = this._contextService.getContextValue(FOCUSING_FX_BAR_EDITOR);
+ const isFocusSheets = this._contextService.getContextValue(FOCUSING_SHEET);
+ const unitId = render.unitId;
+ if (this._editorBridgeService.isVisible().visible) return;
+
+ if (unitId && isFocusSheets && !isFocusFormulaEditor && this._editorService.isSheetEditor(unitId)) {
+ this._showEditorByKeyboard(config);
+ }
+ }));
+
+ d.add(disposable);
+ }
+ };
- if (unitId && isFocusSheets && !isFocusFormulaEditor && this._editorService.isSheetEditor(unitId)) {
- this._showEditorByKeyboard(config);
- }
+ const render = this._renderManagerService.getRenderById(DOCS_NORMAL_EDITOR_UNIT_ID_KEY);
+ if (render) {
+ addEvent(render);
+ } else {
+ this.disposeWithMe(this._renderManagerService.created$.pipe(filter((render) => render.unitId === DOCS_NORMAL_EDITOR_UNIT_ID_KEY)).subscribe((render) => {
+ disposable?.dispose();
+ addEvent(render);
}));
}
}