From 3581801e5de36bea86ab6dd56fb902399f38bb40 Mon Sep 17 00:00:00 2001 From: lumixraku Date: Thu, 21 Nov 2024 15:16:52 +0800 Subject: [PATCH 1/3] fix: docSelectionRenderService listening to _renderManagerService.created$ --- .../clipboard/clipboard.controller.ts | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts index a6d997c3619..e53cc4c75ec 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,6 +142,29 @@ 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) { + // console.log('SheetClipboardController init doc selection service', docSelectionRenderService); + sheetPasteShortKeyFn(docSelectionRenderService); + } else { + // console.log('SheetClipboardController init doc selection service DOCS_NORMAL_EDITOR_UNIT_ID_KEY', DOCS_NORMAL_EDITOR_UNIT_ID_KEY); + this._renderManagerService.created$.subscribe((renderer) => { + if (renderer.unitId === DOCS_NORMAL_EDITOR_UNIT_ID_KEY) { + console.log('SheetClipboardController init doc selection service $$$$', renderer); + docSelectionRenderService = this._renderManagerService.getRenderById(DOCS_NORMAL_EDITOR_UNIT_ID_KEY)?.with(DocSelectionRenderService); + console.log('SheetClipboardController init doc selection service $$$$$', docSelectionRenderService); + if (docSelectionRenderService) { + sheetPasteShortKeyFn(docSelectionRenderService); + } + } + }); } } From f08406f3f52303f6b7a59cdadc6286e8bfb1c8b5 Mon Sep 17 00:00:00 2001 From: lumixraku Date: Thu, 21 Nov 2024 15:18:25 +0800 Subject: [PATCH 2/3] chore: better code --- .../src/controllers/clipboard/clipboard.controller.ts | 4 ---- packages/sheets-ui/src/plugin.ts | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts index e53cc4c75ec..98f5c04b77d 100644 --- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts +++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts @@ -151,15 +151,11 @@ export class SheetClipboardController extends RxDisposable { let docSelectionRenderService = this._renderManagerService.getRenderById(DOCS_NORMAL_EDITOR_UNIT_ID_KEY)?.with(DocSelectionRenderService); if (docSelectionRenderService) { - // console.log('SheetClipboardController init doc selection service', docSelectionRenderService); sheetPasteShortKeyFn(docSelectionRenderService); } else { - // console.log('SheetClipboardController init doc selection service DOCS_NORMAL_EDITOR_UNIT_ID_KEY', DOCS_NORMAL_EDITOR_UNIT_ID_KEY); this._renderManagerService.created$.subscribe((renderer) => { if (renderer.unitId === DOCS_NORMAL_EDITOR_UNIT_ID_KEY) { - console.log('SheetClipboardController init doc selection service $$$$', renderer); docSelectionRenderService = this._renderManagerService.getRenderById(DOCS_NORMAL_EDITOR_UNIT_ID_KEY)?.with(DocSelectionRenderService); - console.log('SheetClipboardController init doc selection service $$$$$', docSelectionRenderService); if (docSelectionRenderService) { sheetPasteShortKeyFn(docSelectionRenderService); } diff --git a/packages/sheets-ui/src/plugin.ts b/packages/sheets-ui/src/plugin.ts index bf97200caf2..a6e4b8758ba 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], @@ -241,6 +240,7 @@ export class UniverSheetsUIPlugin extends Plugin { [MoveRangeRenderController], // editor + [SheetClipboardController], [EditorBridgeRenderController], [EditingRenderController], From 1a50e6a71a78c47de195bd203cf2cb6c6e2d6dd1 Mon Sep 17 00:00:00 2001 From: lumixraku Date: Sat, 23 Nov 2024 17:45:13 +0800 Subject: [PATCH 3/3] fix: mv SheetClipboardController out of render module --- .../clipboard/clipboard.controller.ts | 17 ++++++++--------- packages/sheets-ui/src/plugin.ts | 5 ++++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts index 98f5c04b77d..78936c76756 100644 --- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts +++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts @@ -152,16 +152,15 @@ export class SheetClipboardController extends RxDisposable { if (docSelectionRenderService) { sheetPasteShortKeyFn(docSelectionRenderService); - } else { - 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); - } - } - }); } + 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 a6e4b8758ba..2e94505b66f 100644 --- a/packages/sheets-ui/src/plugin.ts +++ b/packages/sheets-ui/src/plugin.ts @@ -169,6 +169,10 @@ export class UniverSheetsUIPlugin extends Plugin { this._initAutoFocus(); } + registerDependencies(this._injector, [ + [SheetClipboardController], + ]); + this._registerRenderBasics(); touchDependencies(this._injector, [ @@ -240,7 +244,6 @@ export class UniverSheetsUIPlugin extends Plugin { [MoveRangeRenderController], // editor - [SheetClipboardController], [EditorBridgeRenderController], [EditingRenderController],