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-drawing-ui): use feature-provided popup icons (#8094)
白熱5b5f61cbd9d4
diff --git a/packages/drawing-ui/src/views/image-popup-menu/ImagePopupMenu.tsx b/packages/drawing-ui/src/views/image-popup-menu/ImagePopupMenu.tsxindex 1359e3f76a..92cf30b923 100644--- a/packages/drawing-ui/src/views/image-popup-menu/ImagePopupMenu.tsx+++ b/packages/drawing-ui/src/views/image-popup-menu/ImagePopupMenu.tsx@@ -17,7 +17,6 @@import type { DocumentDataModel } from '@univerjs/core';import type { IValueOption } from '@univerjs/ui';import type { ReactNode } from 'react';-import type { LocaleKey } from '../../locale/types';import {BooleanNumber,ICommandService,@@ -35,6 +34,7 @@ import {} from '@univerjs/icons';import { IconManager, useDependency } from '@univerjs/ui';import { useState } from 'react';+import type { LocaleKey } from '../../locale/types';export interface IImagePopupMenuItem {label: string;@@ -52,6 +52,7 @@ export interface IImagePopupMenuItem {export interface IImagePopupMenuExtraProps {menuItems: IImagePopupMenuItem[];+ icon?: string;variant?: 'doc-floating-toolbar' | 'doc-chart-floating-toolbar';unitId?: string;subUnitId?: string;@@ -70,10 +71,13 @@ export function ImagePopupMenu(props: IImagePopupMenuProps) {const menuItems = popup?.extraProps?.menuItems;const commandService = useDependency(ICommandService);const localeService = useDependency(LocaleService);+ const iconManager = useDependency(IconManager);const [visible, setVisible] = useState(false);const [isHovered, setIsHovered] = useState(false);- if (!menuItems) return null;+ if (!menuItems) {+ return null;+ }if (popup.extraProps?.variant === 'doc-floating-toolbar' &&@@ -113,6 +117,7 @@ export function ImagePopupMenu(props: IImagePopupMenuProps) {};const showMore = visible || isHovered;+ const Icon = popup.extraProps?.icon ? iconManager.get(popup.extraProps.icon) : AutofillDoubleIcon;return (<div@@ -140,7 +145,7 @@ export function ImagePopupMenu(props: IImagePopupMenuProps) {'univer-bg-gray-0 dark:!univer-bg-gray-900': !visible,})}>- <AutofillDoubleIcon+ <IconclassName={`univer-fill-primary-600 univer-text-gray-900dark:!univer-text-gray-0diff --git a/packages/drawing-ui/src/views/image-popup-menu/__tests__/ImagePopupMenu.spec.tsx b/packages/drawing-ui/src/views/image-popup-menu/__tests__/ImagePopupMenu.spec.tsxindex 7c2f988255..7312588833 100644--- a/packages/drawing-ui/src/views/image-popup-menu/__tests__/ImagePopupMenu.spec.tsx+++ b/packages/drawing-ui/src/views/image-popup-menu/__tests__/ImagePopupMenu.spec.tsx@@ -19,6 +19,7 @@*/import { CommandType, ICommandService, LocaleService, LocaleType, Univer } from '@univerjs/core';+import { AddImageIcon, ChartIcon } from '@univerjs/icons';import { IconManager, RediContext } from '@univerjs/ui';import { act } from 'react';import { createRoot } from 'react-dom/client';@@ -34,6 +35,35 @@ afterEach(async () => {await act(async () => new Promise((resolve) => setTimeout(resolve, 0)));});+it.each([+ { icon: 'ChartIcon', expectedIcon: 'chart-icon' },+ { icon: 'AddImageIcon', expectedIcon: 'add-image-icon' },+ { icon: undefined, expectedIcon: 'autofill-double-icon' },+])('renders the supplied icon or defaults to $expectedIcon', async ({ icon, expectedIcon }) => {+ const univer = new Univer();+ cleanups.push(() => univer.dispose());+ const injector = univer.__getInjector();+ injector.add([IconManager]);+ injector.get(IconManager).register({ ChartIcon, AddImageIcon });+ const locale = injector.get(LocaleService);+ locale.load({ [LocaleType.EN_US]: {} });+ locale.setLocale(LocaleType.EN_US);+ locale.setDirection('ltr');+ const container = document.createElement('div');+ document.body.appendChild(container);+ cleanups.push(() => container.remove());+ const root = createRoot(container);+ cleanups.push(() => act(() => root.unmount()));++ await act(async () => root.render(+ <RediContext.Provider value={{ injector }}>+ <ImagePopupMenu popup={{ extraProps: { menuItems: [], icon } }} />+ </RediContext.Provider>+ ));++ expect(container.querySelector(`svg.univerjs-icon-${expectedIcon}`)).not.toBeNull();+});+it('dismisses the chart type tooltip while the dropdown is open and routes the selected type', async () => {const univer = new Univer();cleanups.push(() => univer.dispose());diff --git a/packages/sheets-drawing-ui/src/menu/__tests__/drawing-popup-menu.controller.spec.ts b/packages/sheets-drawing-ui/src/menu/__tests__/drawing-popup-menu.controller.spec.tsindex e30c0d83eb..b7de928320 100644--- a/packages/sheets-drawing-ui/src/menu/__tests__/drawing-popup-menu.controller.spec.ts+++ b/packages/sheets-drawing-ui/src/menu/__tests__/drawing-popup-menu.controller.spec.ts@@ -24,6 +24,7 @@ import {IPermissionService,IUniverInstanceService,LocaleService,+ LocaleType,toDisposable,} from '@univerjs/core';import { IDrawingManagerService } from '@univerjs/drawing';@@ -35,7 +36,7 @@ import { Subject } from 'rxjs';import { describe, expect, it, vi } from 'vitest';import { DrawingPopupMenuController } from '../drawing-popup-menu.controller';-function createControllerHarness(initialDrawingType: DrawingTypeEnum) {+function createControllerHarness(initialDrawingType: DrawingTypeEnum, icon?: string) {const injector = new Injector();injector.add([IPermissionService, { useValue: { getPermissionPoint: () => undefined } as never }]);injector.add([OleUIService, { useValue: { registerHost: () => toDisposable(() => undefined) } as never }]);@@ -77,10 +78,12 @@ function createControllerHarness(initialDrawingType: DrawingTypeEnum) {}),} as never,}]);+ const getFeatureMenuIcon = vi.fn(() => icon);injector.add([SheetCanvasPopManagerService, {useValue: {attachPopupToObject,getFeatureMenu: () => undefined,+ getFeatureMenuIcon,} as never,}]);injector.add([IRenderManagerService, {@@ -120,7 +123,7 @@ function createControllerHarness(initialDrawingType: DrawingTypeEnum) {}],} as never,}]);- injector.add([IContextService, { useValue: new ContextService() }]);+ injector.add([IContextService, { useClass: ContextService }]);injector.add([IImageIoService, { useValue: { change$: imageIoChange$ } as never }]);injector.add([ICommandService, { useValue: { syncExecuteCommand: vi.fn() } as never }]);injector.add([DrawingPopupMenuController]);@@ -130,6 +133,7 @@ function createControllerHarness(initialDrawingType: DrawingTypeEnum) {createControl$,drawingObject,getAttachedPopup: () => attachedPopup,+ getFeatureMenuIcon,injector,popupDisposable,setDrawingType: (nextDrawingType: DrawingTypeEnum) => {@@ -140,19 +144,26 @@ function createControllerHarness(initialDrawingType: DrawingTypeEnum) {describe('DrawingPopupMenuController', () => {it.each([- { drawingType: DrawingTypeEnum.DRAWING_IMAGE, label: 'image' },- { drawingType: DrawingTypeEnum.DRAWING_DOM, label: 'Float DOM' },- ])('constrains the $label popup to the canvas and places it on the left in RTL', ({ drawingType }) => {- const { attachPopupToObject, createControl$, drawingObject, getAttachedPopup, injector } = createControllerHarness(drawingType);- injector.get(LocaleService).setDirection('rtl');+ { drawingType: DrawingTypeEnum.DRAWING_IMAGE, label: 'image', icon: 'AddImageIcon' },+ { drawingType: DrawingTypeEnum.DRAWING_DOM, label: 'Float DOM', icon: undefined },+ { drawingType: DrawingTypeEnum.DRAWING_CHART, label: 'chart', icon: 'CustomChartIcon' },+ { drawingType: DrawingTypeEnum.DRAWING_CHART, label: 'chart without an icon', icon: undefined },+ ])('passes the $label popup icon and constrains its placement in RTL', ({ drawingType, icon }) => {+ const { attachPopupToObject, createControl$, drawingObject, getAttachedPopup, getFeatureMenuIcon, injector } = createControllerHarness(drawingType, icon);+ const locale = injector.get(LocaleService);+ locale.load({ [LocaleType.EN_US]: {} });+ locale.setLocale(LocaleType.EN_US);+ locale.setDirection('rtl');const controller = injector.get(DrawingPopupMenuController);createControl$.next();+ expect(getFeatureMenuIcon).toHaveBeenCalledWith(drawingType);expect(attachPopupToObject).toHaveBeenCalledWith(drawingObject,expect.objectContaining({constrainToCanvas: true,direction: 'left',+ extraProps: expect.objectContaining({ icon }),}));expect(getAttachedPopup()?.extraProps?.menuItems).toEqual(expect.arrayContaining([@@ -174,6 +185,10 @@ describe('DrawingPopupMenuController', () => {popupDisposable,setDrawingType,} = createControllerHarness(DrawingTypeEnum.DRAWING_CHART);+ const locale = injector.get(LocaleService);+ locale.load({ [LocaleType.EN_US]: {} });+ locale.setLocale(LocaleType.EN_US);+ locale.setDirection('ltr');const controller = injector.get(DrawingPopupMenuController);createControl$.next();diff --git a/packages/sheets-drawing-ui/src/menu/drawing-popup-menu.controller.ts b/packages/sheets-drawing-ui/src/menu/drawing-popup-menu.controller.tsindex 675a754a00..bb3e6981a8 100644--- a/packages/sheets-drawing-ui/src/menu/drawing-popup-menu.controller.ts+++ b/packages/sheets-drawing-ui/src/menu/drawing-popup-menu.controller.ts@@ -224,6 +224,7 @@ export class DrawingPopupMenuController extends RxDisposable {offset: [2, 0],extraProps: {menuItems,+ icon: this._canvasPopManagerService.getFeatureMenuIcon(drawingType),},}));}diff --git a/packages/sheets-ui/src/services/__tests__/canvas-pop-manager.service.spec.ts b/packages/sheets-ui/src/services/__tests__/canvas-pop-manager.service.spec.tsindex 708b50d9ae..7920910b4e 100644--- a/packages/sheets-ui/src/services/__tests__/canvas-pop-manager.service.spec.ts+++ b/packages/sheets-ui/src/services/__tests__/canvas-pop-manager.service.spec.ts@@ -27,12 +27,19 @@ import {} from '@univerjs/sheets';import { ICanvasPopupService } from '@univerjs/ui';import { BehaviorSubject, Subject } from 'rxjs';-import { describe, expect, it, vi } from 'vitest';+import { afterEach, describe, expect, it, vi } from 'vitest';import { SetScrollOperation } from '../../commands/operations/scroll.operation';import { SheetCanvasPopManagerService } from '../canvas-pop-manager.service';import { ISheetSelectionRenderService } from '../selection/base-selection-render.service';import { SheetSkeletonManagerService } from '../sheet-skeleton-manager.service';+const testInjectors: Injector[] = [];++afterEach(() => {+ testInjectors.forEach((injector) => injector.dispose());+ testInjectors.length = 0;+});+function createEventSubject<T>() {const listeners = new Set<(event: T) => void>();return {@@ -161,6 +168,7 @@ function createWorkbook(worksheet: any) {function createSheetHarness() {const injector = new Injector();+ testInjectors.push(injector);injector.add([ICanvasPopupService, { useClass: TestCanvasPopupService as never }]);injector.add([IRenderManagerService, { useClass: TestRenderManagerService as never }]);injector.add([IUniverInstanceService, { useClass: TestUniverInstanceService as never }]);@@ -257,7 +265,7 @@ function createSheetHarness() {renderManagerService.render = render;return {- service: injector.get(SheetCanvasPopManagerService),+ injector,popupService: injector.get(ICanvasPopupService) as unknown as TestCanvasPopupService,commandService: injector.get(ICommandService) as unknown as TestCommandService,refRangeService: injector.get(RefRangeService) as unknown as TestRefRangeService,@@ -280,7 +288,8 @@ function createSheetHarness() {describe('SheetCanvasPopManagerService', () => {it('resolves registered feature menus for the drawing that opened the menu', () => {- const { service } = createSheetHarness();+ const { injector } = createSheetHarness();+ const service = injector.get(SheetCanvasPopManagerService);const callback = vi.fn((unitId: string, subUnitId: string, drawingId: string, drawingType: unknown) => [{label: `${unitId}/${subUnitId}/${drawingId}/${drawingType}`,index: 0,@@ -289,7 +298,7 @@ describe('SheetCanvasPopManagerService', () => {disable: false,}]);- service.registerFeatureMenu('image' as never, callback, 'feature.image.title');+ service.registerFeatureMenu('image' as never, callback, 'feature.image.title', 'FeatureIcon');expect(service.getFeatureMenu('unit-1', 'sheet-1', 'drawing-1', 'image' as never)).toEqual([{ label: 'unit-1/sheet-1/drawing-1/image', index: 0, commandId: 'open', commandParams: { drawingId: 'drawing-1' }, disable: false },@@ -297,14 +306,27 @@ describe('SheetCanvasPopManagerService', () => {expect(service.getFeatureMenu('unit-1', 'sheet-1', 'drawing-1', 'chart' as never)).toBeUndefined();expect(service.getFeatureMenuTitle('image' as never)).toBe('feature.image.title');expect(service.getFeatureMenuTitle('chart' as never)).toBeUndefined();+ expect(service.getFeatureMenuIcon('image' as never)).toBe('FeatureIcon');+ expect(service.getFeatureMenuIcon('chart' as never)).toBeUndefined();service.dispose();expect(service.getFeatureMenu('unit-1', 'sheet-1', 'drawing-1', 'image' as never)).toBeUndefined();expect(service.getFeatureMenuTitle('image' as never)).toBeUndefined();+ expect(service.getFeatureMenuIcon('image' as never)).toBeUndefined();+ });++ it('clears a previous icon when the feature menu is registered without one', () => {+ const { injector } = createSheetHarness();+ const service = injector.get(SheetCanvasPopManagerService);+ service.registerFeatureMenu('image' as never, () => [], undefined, 'FeatureIcon');+ service.registerFeatureMenu('image' as never, () => []);++ expect(service.getFeatureMenuIcon('image' as never)).toBeUndefined();});it('anchors a drawing popup with the registered menu offset and removes it when closed', () => {- const { service, popupService, canvas, viewport, viewMainScrollAfter$ } = createSheetHarness();+ const { injector, popupService, canvas, viewport, viewMainScrollAfter$ } = createSheetHarness();+ const service = injector.get(SheetCanvasPopManagerService);const targetObject = {left: 20,top: 10,@@ -339,7 +361,8 @@ describe('SheetCanvasPopManagerService', () => {});it('adds scaled sheet header insets to a canvas-constrained drawing popup', () => {- const { service, popupService, viewport } = createSheetHarness();+ const { injector, popupService, viewport } = createSheetHarness();+ const service = injector.get(SheetCanvasPopManagerService);const targetObject = {left: 20,top: 10,@@ -360,7 +383,8 @@ describe('SheetCanvasPopManagerService', () => {});it('includes frozen panes in the boundary insets of a canvas-constrained drawing popup', () => {- const { service, popupService, viewport } = createSheetHarness();+ const { injector, popupService, viewport } = createSheetHarness();+ const service = injector.get(SheetCanvasPopManagerService);const targetObject = {left: 80,top: 90,@@ -381,7 +405,8 @@ describe('SheetCanvasPopManagerService', () => {});it('keeps absolute-position popups hidden while a selection is being dragged unless the popup opts in', () => {- const { service, popupService, refSelectionService, sheetSelectionService } = createSheetHarness();+ const { injector, popupService, refSelectionService, sheetSelectionService } = createSheetHarness();+ const service = injector.get(SheetCanvasPopManagerService);const bound = { top: 10, left: 20, right: 30, bottom: 40 };refSelectionService.selectionMoving$.next();@@ -412,7 +437,8 @@ describe('SheetCanvasPopManagerService', () => {});it('keeps dynamic absolute-position popups subscribed to caller-provided anchors', () => {- const { service, popupService } = createSheetHarness();+ const { injector, popupService } = createSheetHarness();+ const service = injector.get(SheetCanvasPopManagerService);const bound = { top: 10, left: 20, right: 30, bottom: 40 };const anchorRect$ = new BehaviorSubject(bound);@@ -442,7 +468,8 @@ describe('SheetCanvasPopManagerService', () => {});it('uses a scoped popup injector only for embedded sheet render units', () => {- const { service, popupService, render, univerInstanceService } = createSheetHarness();+ const { injector, popupService, render, univerInstanceService } = createSheetHarness();+ const service = injector.get(SheetCanvasPopManagerService);const bound = { top: 10, left: 20, right: 30, bottom: 40 };service.attachPopupToAbsolutePosition(bound, { componentKey: 'normal-popup' } as never, 'unit-1', 'sheet-1');@@ -456,7 +483,8 @@ describe('SheetCanvasPopManagerService', () => {});it('anchors a popup to a sheet position and refreshes its hidden freeze area on viewport changes', () => {- const { service, popupService, commandService, transformChange$ } = createSheetHarness();+ const { injector, popupService, commandService, transformChange$ } = createSheetHarness();+ const service = injector.get(SheetCanvasPopManagerService);const disposable = service.attachPopupByPosition({ left: 20, top: 10, right: 30, bottom: 40 },@@ -487,7 +515,8 @@ describe('SheetCanvasPopManagerService', () => {});it('moves a cell popup when its watched range is remapped and removes it when the cell disappears', () => {- const { service, popupService, refRangeService, viewport } = createSheetHarness();+ const { injector, popupService, refRangeService, viewport } = createSheetHarness();+ const service = injector.get(SheetCanvasPopManagerService);const disposable = service.attachPopupToCell(1, 1, { componentKey: 'cell-action' } as never, 'unit-1', 'sheet-1', viewport as never);const popup = popupService.lastPopup()!;@@ -517,7 +546,8 @@ describe('SheetCanvasPopManagerService', () => {});it('disposes tracked cell popups when the manager is disposed', () => {- const { service, popupService, refRangeService, viewport } = createSheetHarness();+ const { injector, popupService, refRangeService, viewport } = createSheetHarness();+ const service = injector.get(SheetCanvasPopManagerService);service.attachPopupToCell(1, 1, { componentKey: 'cell-action' } as never, 'unit-1', 'sheet-1', viewport as never);@@ -531,7 +561,7 @@ describe('SheetCanvasPopManagerService', () => {it('updates cell and range anchors when sheet geometry changes', () => {const {- service,+ injector,popupService,commandService,clientRect$,@@ -540,6 +570,7 @@ describe('SheetCanvasPopManagerService', () => {viewport,viewMainScrollAfter$,} = createSheetHarness();+ const service = injector.get(SheetCanvasPopManagerService);service.attachPopupToCell(1, 1, { componentKey: 'cell-action' } as never, 'unit-1', 'sheet-1', viewport as never);const cellPopup = popupService.lastPopup()!;@@ -586,7 +617,8 @@ describe('SheetCanvasPopManagerService', () => {});it('does not open popups for a different sheet location or unavailable workbook data', () => {- const { service, popupService, univerInstanceService, renderManagerService } = createSheetHarness();+ const { injector, popupService, univerInstanceService, renderManagerService } = createSheetHarness();+ const service = injector.get(SheetCanvasPopManagerService);const bound = { top: 10, left: 20, right: 30, bottom: 40 };expect(service.attachPopupByPosition(bound, { componentKey: 'mismatch' } as never, {diff --git a/packages/sheets-ui/src/services/canvas-pop-manager.service.ts b/packages/sheets-ui/src/services/canvas-pop-manager.service.tsindex c7d689e097..63c63e9c5a 100644--- a/packages/sheets-ui/src/services/canvas-pop-manager.service.ts+++ b/packages/sheets-ui/src/services/canvas-pop-manager.service.ts@@ -25,7 +25,15 @@ import type {Workbook,Worksheet,} from '@univerjs/core';-import type { BaseObject, IBoundRectNoAngle, IRender, IShapeProps, Shape, SpreadsheetSkeleton, Viewport } from '@univerjs/engine-render';+import type {+ BaseObject,+ IBoundRectNoAngle,+ IRender,+ IShapeProps,+ Shape,+ SpreadsheetSkeleton,+ Viewport,+} from '@univerjs/engine-render';import type { ISetWorksheetRowAutoHeightMutationParams, ISheetLocationBase } from '@univerjs/sheets';import type { IPopup } from '@univerjs/ui';import type { Observable } from 'rxjs';@@ -73,9 +81,9 @@ interface IPopupMenuItem {type getPopupMenuItemCallback = (unitId: string, subUnitId: string, drawingId: string, drawingType: DrawingTypeEnum) => IPopupMenuItem[];export class SheetCanvasPopManagerService extends Disposable {- // the DrawingTypeEnum should refer from drawing package, here we just use type, so no need to import the drawing packageprivate _popupMenuFeatureMap = new Map<DrawingTypeEnum, getPopupMenuItemCallback>();private _popupMenuTitleMap = new Map<DrawingTypeEnum, string>();+ private _popupMenuIconMap = new Map<DrawingTypeEnum, string | undefined>();private _popupMenuOffsetMap = new Map<DrawingTypeEnum, { offsetX: number; offsetY: number }>();private readonly _popupDisposables = new Set<IDisposable>();@@ -122,8 +130,14 @@ export class SheetCanvasPopManagerService extends Disposable {/*** Register a feature menu callback for a specific drawing type.such as image, chart, etc.*/- registerFeatureMenu(type: DrawingTypeEnum, getPopupMenuCallBack: getPopupMenuItemCallback, title?: string) {+ registerFeatureMenu(+ type: DrawingTypeEnum,+ getPopupMenuCallBack: getPopupMenuItemCallback,+ title?: string,+ icon?: string+ ) {this._popupMenuFeatureMap.set(type, getPopupMenuCallBack);+ this._popupMenuIconMap.set(type, icon);if (title) {this._popupMenuTitleMap.set(type, title);}@@ -158,12 +172,17 @@ export class SheetCanvasPopManagerService extends Disposable {return this._popupMenuTitleMap.get(drawingType);}+ getFeatureMenuIcon(drawingType: DrawingTypeEnum): Nullable<string> {+ return this._popupMenuIconMap.get(drawingType);+ }+override dispose(): void {Array.from(this._popupDisposables).forEach((disposable) => disposable.dispose());this._popupDisposables.clear();super.dispose();this._popupMenuFeatureMap.clear();this._popupMenuTitleMap.clear();+ this._popupMenuIconMap.clear();this._popupMenuOffsetMap.clear();}@@ -182,7 +201,6 @@ export class SheetCanvasPopManagerService extends Disposable {// We should take the scale into account when canvas is scaled by CSS.const widthOfCanvas = pxToNum(canvasElement.style.width); // declared width- // const { top, left, width } = canvasClientRect; // real width affected by scaleconst scaleAdjust = canvasClientRect.width / widthOfCanvas;const canvasScale = currentRender.scene.getAncestorScale().scaleX;@@ -264,7 +282,6 @@ export class SheetCanvasPopManagerService extends Disposable {// We should take the scale into account when canvas is scaled by CSS.const widthOfCanvas = pxToNum(canvasElement.style.width); // declared width- // const { top, left, width } = canvasClientRect; // real width affected by scaleconst scaleAdjust = canvasClientRect.width / widthOfCanvas;const position = {left: (offsetBound.left * scaleAdjust) + canvasClientRect.left,@@ -902,7 +919,6 @@ export class SheetCanvasPopManagerService extends Disposable {updatePosition();};- // const position$ = combineLatest(topLeftPos$, rightBottomPos$);const position$ = topLeftPos$.pipe(map((topLeft) => {const rightBottomCoord = this._calcCellPositionByCell(endRow, endColumn, currentRender, skeleton, activeViewport);