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: defer drawing service lookup and anchor autofill overlay (#7521)
白熱cade815f9d0a
diff --git a/packages/sheets-drawing/src/facade/__tests__/f-worksheet.spec.ts b/packages/sheets-drawing/src/facade/__tests__/f-worksheet.spec.tsindex 8d38183c60..0e52fdaf14 100644--- a/packages/sheets-drawing/src/facade/__tests__/f-worksheet.spec.ts+++ b/packages/sheets-drawing/src/facade/__tests__/f-worksheet.spec.ts@@ -16,7 +16,6 @@import type {Injector,- Univer,Workbook,} from '@univerjs/core';import type { FWorkbook } from '@univerjs/sheets/facade';@@ -29,9 +28,10 @@ import {IUniverInstanceService,RedoCommand,UndoCommand,+ Univer,UniverInstanceType,} from '@univerjs/core';-import { SheetSkeletonService } from '@univerjs/sheets';+import { SheetSkeletonService, UniverSheetsPlugin } from '@univerjs/sheets';import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { createSheetsDrawingTestBed } from '../../__tests__/create-sheets-drawing-test-bed';import { InsertSheetDrawingCommand } from '../../commands/commands/insert-sheet-drawing.command';@@ -54,6 +54,27 @@ describe('FWorksheetDrawingMixin group drawings', () => {univer.dispose();});+ it('uses non-drawing worksheet APIs before drawing services are registered', () => {+ univer.dispose();+ univer = new Univer();+ univer.registerPlugin(UniverSheetsPlugin);+ univer.createUnit(UniverInstanceType.UNIVER_SHEET, {+ id: 'test',+ sheetOrder: ['sheet1'],+ sheets: {+ sheet1: {+ id: 'sheet1',+ name: 'Sheet1',+ rowCount: 20,+ columnCount: 20,+ cellData: {},+ },+ },+ });++ expect(createFacade(univer.__getInjector()).getSheetName()).toBe('Sheet1');+ });+it('groups drawings and exposes parent and child relationships', () => {const drawings = [createDrawing('drawing-1', 10),diff --git a/packages/sheets-drawing/src/facade/f-worksheet.ts b/packages/sheets-drawing/src/facade/f-worksheet.tsindex 4cbf187311..279363d758 100644--- a/packages/sheets-drawing/src/facade/f-worksheet.ts+++ b/packages/sheets-drawing/src/facade/f-worksheet.ts@@ -14,7 +14,7 @@* limitations under the License.*/-import type { IDrawingParam, IGroupBaseBound, Injector } from '@univerjs/core';+import type { IDrawingParam, IGroupBaseBound } from '@univerjs/core';import type { IFBlobSource } from '@univerjs/core/facade';import type { IDrawingGroupUpdateParam, IDrawingJsonUndo1 } from '@univerjs/drawing';import type { ISheetDrawing, ISheetDrawingPlacement, ISheetDrawingPlacementInput, ISheetImage } from '@univerjs/sheets-drawing';@@ -509,14 +509,16 @@ export interface IFWorksheetDrawingMixin {}export class FWorksheetDrawingMixin extends FWorksheet implements IFWorksheetDrawingMixin {- declare private _sheetDrawingService: ISheetDrawingService;- declare private _sheetSkeletonService: SheetSkeletonService;- declare private _undoRedoService: IUndoRedoService;-- override _initialize(injector: Injector): void {- this._sheetDrawingService = injector.get(ISheetDrawingService);- this._sheetSkeletonService = injector.get(SheetSkeletonService);- this._undoRedoService = injector.get(IUndoRedoService);+ private get _sheetDrawingService(): ISheetDrawingService {+ return this._injector.get(ISheetDrawingService);+ }++ private get _sheetSkeletonService(): SheetSkeletonService {+ return this._injector.get(SheetSkeletonService);+ }++ private get _undoRedoService(): IUndoRedoService {+ return this._injector.get(IUndoRedoService);}override async insertImage(url: IFBlobSource | string, column?: number, row?: number, offsetX?: number, offsetY?: number): Promise<boolean> {diff --git a/packages/sheets-ui/src/views/auto-fill-popup-menu/AutoFillPopupMenu.tsx b/packages/sheets-ui/src/views/auto-fill-popup-menu/AutoFillPopupMenu.tsxindex 17ee48cf08..fdb89b9804 100644--- a/packages/sheets-ui/src/views/auto-fill-popup-menu/AutoFillPopupMenu.tsx+++ b/packages/sheets-ui/src/views/auto-fill-popup-menu/AutoFillPopupMenu.tsx@@ -14,14 +14,14 @@* limitations under the License.*/-import type { ICommandInfo, IExecutionOptions } from '@univerjs/core';+import type { ICommandInfo } from '@univerjs/core';import { ICommandService, IUniverInstanceService, LocaleService, toDisposable } from '@univerjs/core';import { borderClassName, clsx, DropdownMenu } from '@univerjs/design';import { convertTransformToOffsetX, convertTransformToOffsetY, IRenderManagerService } from '@univerjs/engine-render';import { AutofillDoubleIcon, MoreDownIcon } from '@univerjs/icons';import { AUTO_FILL_APPLY_TYPE, IAutoFillService, RefillCommand } from '@univerjs/sheets';import { useDependency, useObservable } from '@univerjs/ui';-import { useCallback, useEffect, useMemo, useState } from 'react';+import { useEffect, useMemo, useReducer, useState } from 'react';import { map } from 'rxjs';import { SetScrollOperation } from '../../commands/operations/scroll.operation';import { getViewportByCell } from '../../common/utils';@@ -41,11 +41,6 @@ export interface IAutoFillPopupMenuItem {disable: boolean;}-const useUpdate = () => {- const [, setState] = useState({});- return useCallback(() => setState((prevState) => !prevState), []);-};-export function AutoFillPopupMenu() {const commandService = useDependency(ICommandService);const univerInstanceService = useDependency(IUniverInstanceService);@@ -79,7 +74,8 @@ export function AutoFillPopupMenu() {[autoFillService]);const selected = useObservable(autoFillService.applyType$, AUTO_FILL_APPLY_TYPE.SERIES);- const [isHovered, setHovered] = useState(false);+ const [isHovered, setIsHovered] = useState(false);+ const [, forceUpdate] = useReducer((version: number) => version + 1, 0);const workbook = useActiveWorkbook();const sheetSkeletonManagerService = useMemo(() => {if (workbook) {@@ -91,16 +87,15 @@ export function AutoFillPopupMenu() {}, [workbook, renderManagerService]);const handleMouseEnter = () => {- setHovered(true);+ setIsHovered(true);};const handleMouseLeave = () => {- setHovered(false);+ setIsHovered(false);};- const forceUpdate = useUpdate();useEffect(() => {- const disposable = commandService.onCommandExecuted((command: ICommandInfo, options?: IExecutionOptions) => {+ const disposable = commandService.onCommandExecuted((command: ICommandInfo) => {if (command.id === SetScrollOperation.id) {forceUpdate();}@@ -166,7 +161,7 @@ export function AutoFillPopupMenu() {const availableMenu = menu.filter((item) => !item.disable);return (- <div className="univer-absolute univer-inset-0 univer-z-10 univer-size-0">+ <div className="univer-absolute univer-z-10 univer-size-0" style={{ left: 0, top: 0 }}><divclassName="univer-absolute"style={{ left: `${relativeX + 2}px`, top: `${relativeY + 2}px` }}diff --git a/packages/sheets-ui/src/views/auto-fill-popup-menu/__tests__/AutoFillPopupMenu.spec.tsx b/packages/sheets-ui/src/views/auto-fill-popup-menu/__tests__/AutoFillPopupMenu.spec.tsxindex 7d371b0327..19859b7c92 100644--- a/packages/sheets-ui/src/views/auto-fill-popup-menu/__tests__/AutoFillPopupMenu.spec.tsx+++ b/packages/sheets-ui/src/views/auto-fill-popup-menu/__tests__/AutoFillPopupMenu.spec.tsx@@ -231,4 +231,30 @@ describe('AutoFillPopupMenu', () => {expect(autoFillService.refillTypes).toEqual([AUTO_FILL_APPLY_TYPE.COPY]);});++ it('keeps the overlay origin on the physical left edge in rtl', () => {+ currentBed = createAutoFillPopupTestBed();+ currentBed.injector.get(LocaleService).setDirection('rtl');+ const autoFillService = currentBed.injector.get(IAutoFillService) as unknown as TestAutoFillService;+ const rendered = renderWithDependencies(<AutoFillPopupMenu />, currentBed.injector);+ root = rendered.root;+ container = rendered.container;++ act(() => {+ autoFillService.setShowMenu(true);+ });++ const trigger = rendered.container.querySelector('[data-slot="dropdown-menu-trigger"]');+ if (!(trigger instanceof HTMLElement)) {+ throw new TypeError('Auto fill menu trigger not found');+ }++ const overlay = trigger.parentElement?.parentElement;+ if (!(overlay instanceof HTMLElement)) {+ throw new TypeError('Auto fill menu overlay not found');+ }++ expect(overlay.style.left).toBe('0px');+ expect(overlay.style.right).toBe('');+ });});