diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts index a6d997c3619..78936c76756 100644 --- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts +++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts @@ -124,10 +124,11 @@ export class SheetClipboardController extends RxDisposable { super(); this._init(); this._initCommandListener(); + this._pasteWithDoc(); + } - const docSelectionRenderService = this._renderManagerService.getRenderById(DOCS_NORMAL_EDITOR_UNIT_ID_KEY)?.with(DocSelectionRenderService); - - if (docSelectionRenderService) { + private _pasteWithDoc() { + const sheetPasteShortKeyFn = (docSelectionRenderService: DocSelectionRenderService) => { docSelectionRenderService.onPaste$.pipe(takeUntil(this.dispose$)).subscribe((config) => { if (!whenSheetEditorFocused(this._contextService)) { return; @@ -141,7 +142,25 @@ export class SheetClipboardController extends RxDisposable { const textContent = clipboardEvent.clipboardData?.getData('text/plain'); this._commandService.executeCommand(SheetPasteShortKeyCommand.id, { htmlContent, textContent }); }); + }; + + // docSelectionRenderService would init before clipboardService controller when creating a univer. + // But when creating a sheet unit again after the previous sheet unit has been disposed, clipboard controller would init before docSelectionRenderService. + // In this case, DocSelectionRenderService isn't ready when clipboardService controller init. + // So better listening to the created$ event of the renderManagerService to get the DocSelectionRenderService instance. + let docSelectionRenderService = this._renderManagerService.getRenderById(DOCS_NORMAL_EDITOR_UNIT_ID_KEY)?.with(DocSelectionRenderService); + + if (docSelectionRenderService) { + sheetPasteShortKeyFn(docSelectionRenderService); } + this._renderManagerService.created$.subscribe((renderer) => { + if (renderer.unitId === DOCS_NORMAL_EDITOR_UNIT_ID_KEY) { + docSelectionRenderService = this._renderManagerService.getRenderById(DOCS_NORMAL_EDITOR_UNIT_ID_KEY)?.with(DocSelectionRenderService); + if (docSelectionRenderService) { + sheetPasteShortKeyFn(docSelectionRenderService); + } + } + }); } private _init(): void { diff --git a/packages/sheets-ui/src/plugin.ts b/packages/sheets-ui/src/plugin.ts index bf97200caf2..2e94505b66f 100644 --- a/packages/sheets-ui/src/plugin.ts +++ b/packages/sheets-ui/src/plugin.ts @@ -139,7 +139,6 @@ export class UniverSheetsUIPlugin extends Plugin { [AutoHeightController], [AutoWidthController], [FormulaEditorController], - [SheetClipboardController], [SheetsRenderService], [SheetUIController], [StatusBarController], @@ -170,6 +169,10 @@ export class UniverSheetsUIPlugin extends Plugin { this._initAutoFocus(); } + registerDependencies(this._injector, [ + [SheetClipboardController], + ]); + this._registerRenderBasics(); touchDependencies(this._injector, [