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
fix(sheets-ui): resolve global selection service for facade selection events (#8108)
wpxp1234563b6ae87df1cc
diff --git a/packages/sheets-ui/src/facade/__tests__/f-univer.spec.ts b/packages/sheets-ui/src/facade/__tests__/f-univer.spec.tsindex d8b7cd79ad..d5291f8b02 100644--- a/packages/sheets-ui/src/facade/__tests__/f-univer.spec.ts+++ b/packages/sheets-ui/src/facade/__tests__/f-univer.spec.ts@@ -36,6 +36,8 @@ import {INTERCEPTOR_POINT,IRefSelectionsService,RefSelectionsService,+ SelectionMoveType,+ SetSelectionsOperation,SetWorksheetRowHeightMutation,SheetInterceptorService,SheetsSelectionsService,@@ -864,4 +866,71 @@ describe('Test FUniver UI mixin', () => {disposables.forEach((disposable) => disposable.dispose());});++ it('should resolve a global selection service through the render unit injector', () => {+ const testBed = createFacadeTestBed(undefined, [+ [IEditorBridgeService, { useClass: EditorBridgeService }],+ [IEditorService, { useClass: EditorService }],+ [DocSelectionManagerService],+ [IDefinedNamesService, { useClass: DefinedNamesService }],+ [IRefSelectionsService, { useClass: RefSelectionsService }],+ [SheetsSelectionsService],+ ]);++ const workbook = testBed.univerAPI.getActiveWorkbook()!;+ const commandService = testBed.get(ICommandService);+ commandService.registerCommand(SetSelectionsOperation);++ const changedRows: number[] = [];+ const disposable = testBed.univerAPI.addEvent(testBed.univerAPI.Event.SelectionChanged, ({ selections }) => {+ changedRows.push(selections[0]?.startRow ?? -1);+ });++ const scroll$ = new Subject<unknown>();+ const renderInjector = testBed.injector.createChild();+ const render = {+ type: UniverInstanceType.UNIVER_SHEET,+ unitId: workbook.getId(),+ getInjector: () => renderInjector,+ with: (service: unknown) => {+ if (service === SheetScrollManagerService) {+ return { validViewportScrollInfo$: scroll$ };+ }++ // `RenderUnit.with` only resolves render modules from the unit's own injector.+ // Global services are registered on an ancestor injector instead.+ throw new Error(`[redi]: Cannot find "${String(service)}" registered by any injector.`);+ },+ };+ const renderManagerService = testBed.get(IRenderManagerService) as IRenderManagerService & {+ _renderCreated$: Subject<typeof render>;+ };+ renderManagerService._renderCreated$.next(render);+ testBed.get(LifecycleService).stage = LifecycleStages.Steady;++ const selection = [{+ range: { startRow: 4, endRow: 4, startColumn: 0, endColumn: 0 },+ primary: {+ actualRow: 4,+ actualColumn: 0,+ startRow: 4,+ startColumn: 0,+ endRow: 4,+ endColumn: 0,+ isMerged: false,+ isMergedMainCell: false,+ },+ style: null,+ }];+ commandService.syncExecuteCommand(SetSelectionsOperation.id, {+ unitId: workbook.getId(),+ subUnitId: workbook.getActiveSheet().getSheetId(),+ type: SelectionMoveType.MOVE_END,+ selections: selection,+ });++ expect(changedRows).toEqual([4]);++ disposable.dispose();+ });});diff --git a/packages/sheets-ui/src/facade/f-univer.ts b/packages/sheets-ui/src/facade/f-univer.tsindex 5e84962f99..5847b41867 100644--- a/packages/sheets-ui/src/facade/f-univer.ts+++ b/packages/sheets-ui/src/facade/f-univer.ts@@ -26,25 +26,6 @@ import type { CommandListenerSkeletonChange } from '@univerjs/sheets';import type { IEditorBridgeServiceVisibleParam, ISetZoomRatioCommandParams, ISheetPasteByShortKeyParams, IViewportScrollState } from '@univerjs/sheets-ui';import type { FRange } from '@univerjs/sheets/facade';import type { Observable } from 'rxjs';-import type {- IBeforeClipboardChangeEventParams,- IBeforeClipboardPasteEventParams,- IBeforeSheetEditEndEventParams,- IBeforeSheetEditStartEventParams,- ICellEventParams,- IClipboardChangedEventParams,- IClipboardPastedEventParams,- IDragEventParams,- IScrollEventParams,- ISelectionEventParams,- ISheetColumnHeaderEventParams,- ISheetEditChangingEventParams,- ISheetEditEndedEventParams,- ISheetEditStartedEventParams,- ISheetRowHeaderEventParams,- ISheetSkeletonChangedEventParams,- ISheetZoomEventParams,-} from './f-event';import { CanceledError, DisposableCollection, DOCS_NORMAL_EDITOR_UNIT_ID_KEY, ICommandService, ILogService, InterceptorEffectEnum, IPermissionService, IUniverInstanceService, LifecycleService, LifecycleStages, RichTextValue, toDisposable, UniverInstanceType } from '@univerjs/core';import { FUniver } from '@univerjs/core/facade';import { RichTextEditingMutation } from '@univerjs/docs';@@ -64,6 +45,25 @@ import {} from '@univerjs/sheets-ui';import { CopyCommand, CutCommand, HTML_CLIPBOARD_MIME_TYPE, IClipboardInterfaceService, KeyCode, PasteCommand, PLAIN_TEXT_CLIPBOARD_MIME_TYPE, supportClipboardAPI } from '@univerjs/ui';import { combineLatest, filter, of, take } from 'rxjs';+import type {+ IBeforeClipboardChangeEventParams,+ IBeforeClipboardPasteEventParams,+ IBeforeSheetEditEndEventParams,+ IBeforeSheetEditStartEventParams,+ ICellEventParams,+ IClipboardChangedEventParams,+ IClipboardPastedEventParams,+ IDragEventParams,+ IScrollEventParams,+ ISelectionEventParams,+ ISheetColumnHeaderEventParams,+ ISheetEditChangingEventParams,+ ISheetEditEndedEventParams,+ ISheetEditStartedEventParams,+ ISheetRowHeaderEventParams,+ ISheetSkeletonChangedEventParams,+ ISheetZoomEventParams,+} from './f-event';/*** @ignore@@ -887,6 +887,14 @@ export class FUniverSheetsUIMixin extends FUniver implements IFUniverSheetsUIMixprivate _tryGetRenderDependency<T>(render: IRender, dependency: DependencyIdentifier<T>): Nullable<T> {try {+ // `IRender.with` only resolves render modules registered on the render unit itself.+ // Global services such as `SheetsSelectionsService` live in an ancestor injector, so they+ // must be resolved through the render unit's injector lookup chain instead.+ const injector = render.getInjector?.();+ if (injector) {+ return injector.get(dependency);+ }+return render.with(dependency);} catch (error) {if (error instanceof Error && (error.message.includes('DependencyNotFoundError') || error.message.includes('Cannot find'))) {