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): prevent drawings flashing on sheet switch (#7900)
wpxp123456e5c98bb6f338
diff --git a/packages/sheets-drawing-ui/src/controllers/__tests__/sheet-drawing-active-render.controller.spec.ts b/packages/sheets-drawing-ui/src/controllers/__tests__/sheet-drawing-active-render.controller.spec.tsindex 307f37fc8e..4e9fb90320 100644--- a/packages/sheets-drawing-ui/src/controllers/__tests__/sheet-drawing-active-render.controller.spec.ts+++ b/packages/sheets-drawing-ui/src/controllers/__tests__/sheet-drawing-active-render.controller.spec.ts@@ -25,12 +25,20 @@ import {ImageSourceType,IUndoRedoService,LogLevel,+ mergeWorksheetSnapshotWithDefault,RedoCommandId,Tools,UndoCommandId,+ UniverInstanceType,} from '@univerjs/core';import { IDrawingManagerService } from '@univerjs/drawing';-import { RemoveColByRangeCommand, RemoveRowByRangeCommand, SheetSkeletonService } from '@univerjs/sheets';+import {+ InsertSheetCommand,+ RemoveColByRangeCommand,+ RemoveRowByRangeCommand,+ SetWorksheetActiveOperation,+ SheetSkeletonService,+} from '@univerjs/sheets';import {applySheetDrawingPlacement,drawingPositionToTransform,@@ -88,6 +96,122 @@ describe('SheetDrawingActiveRenderController', () => {beforeEach(() => vi.useFakeTimers());afterEach(() => vi.useRealTimers());+ it.each([DrawingTypeEnum.DRAWING_IMAGE, DrawingTypeEnum.DRAWING_CHART])(+ 'removes old drawings before the target sheet can restore its viewport (%s)',+ async (drawingType) => {+ const bed = createTestBed();+ const subscriptions = new Disposable();+ try {+ expect(bed.commandService.syncExecuteCommand(InsertSheetCommand.id, {+ unitId: bed.unitId,+ sheet: mergeWorksheetSnapshotWithDefault({ id: 'sheet2', name: 'Sheet2' }),+ })).toBe(true);+ expect(bed.commandService.syncExecuteCommand(SetWorksheetActiveOperation.id, {+ unitId: bed.unitId,+ subUnitId: bed.subUnitId,+ })).toBe(true);+ const image = await insertDrawing(bed, SheetDrawingAnchorType.None);+ const drawing: ISheetDrawing = { ...image, drawingType };+ const sheetService = bed.get(ISheetDrawingService);+ const manager = bed.get(IDrawingManagerService);+ const store = {+ [bed.subUnitId]: { data: { [drawing.drawingId]: drawing }, order: [drawing.drawingId] },+ };+ sheetService.registerDrawingData(bed.unitId, Tools.deepClone(store));+ manager.registerDrawingData(bed.unitId, Tools.deepClone(store));+ bed.get(SheetDrawingActiveRenderController);+ bed.activated$.next(true);+ vi.runOnlyPendingTimers();+ const events: string[] = [];+ subscriptions.disposeWithMe(sheetService.remove$.subscribe((drawings) => {+ if (drawings.some(({ drawingId }) => drawingId === drawing.drawingId)) {+ events.push('sheet-remove');+ }+ }));+ subscriptions.disposeWithMe(manager.remove$.subscribe((drawings) => {+ if (drawings.some(({ drawingId }) => drawingId === drawing.drawingId)) {+ events.push('manager-remove');+ }+ }));+ subscriptions.disposeWithMe(bed.workbook.activeSheet$.subscribe((sheet) => {+ if (sheet?.getSheetId() === 'sheet2') {+ events.push('target-sheet');+ }+ }));++ expect(bed.commandService.syncExecuteCommand(SetWorksheetActiveOperation.id, {+ unitId: bed.unitId,+ subUnitId: 'sheet2',+ })).toBe(true);+ expect(events).toEqual(['sheet-remove', 'manager-remove', 'target-sheet']);+ vi.runOnlyPendingTimers();+ expect(events).toEqual(['sheet-remove', 'manager-remove', 'target-sheet']);+ expect(manager.getDrawingByParam(drawing)).toEqual(drawing);+ expect(sheetService.getDrawingByParam(drawing)).toEqual(drawing);++ const added = vi.fn();+ subscriptions.disposeWithMe(manager.add$.subscribe(added));+ expect(bed.commandService.syncExecuteCommand(SetWorksheetActiveOperation.id, {+ unitId: bed.unitId,+ subUnitId: bed.subUnitId,+ })).toBe(true);+ expect(bed.commandService.syncExecuteCommand(SetWorksheetActiveOperation.id, {+ unitId: bed.unitId,+ subUnitId: 'sheet2',+ })).toBe(true);+ vi.runOnlyPendingTimers();+ expect(added).toHaveBeenCalledExactlyOnceWith([]);+ expect(bed.commandService.syncExecuteCommand(SetWorksheetActiveOperation.id, {+ unitId: bed.unitId,+ subUnitId: bed.subUnitId,+ })).toBe(true);+ vi.runOnlyPendingTimers();+ expect(added).toHaveBeenLastCalledWith([drawing]);+ } finally {+ subscriptions.dispose();+ bed.univer.dispose();+ bed.activated$.complete();+ }+ }+ );++ it('preserves pending activation for invalid targets and other workbooks', async () => {+ const bed = createTestBed();+ const subscriptions = new Disposable();+ try {+ const drawing = await insertDrawing(bed, SheetDrawingAnchorType.None);+ bed.univer.createUnit(UniverInstanceType.UNIVER_SHEET, {+ ...Tools.deepClone(bed.workbook.getSnapshot()),+ id: 'other-workbook',+ });+ const manager = bed.get(IDrawingManagerService);+ manager.registerDrawingData(bed.unitId, Tools.deepClone(+ bed.get(ISheetDrawingService).getDrawingDataForUnit(bed.unitId)+ ));+ bed.get(SheetDrawingActiveRenderController);+ bed.activated$.next(true);+ const removed = vi.fn();+ const added = vi.fn();+ subscriptions.disposeWithMe(manager.remove$.subscribe(removed));+ subscriptions.disposeWithMe(manager.add$.subscribe(added));+ expect(bed.commandService.syncExecuteCommand(SetWorksheetActiveOperation.id, {+ unitId: bed.unitId,+ subUnitId: 'missing-sheet',+ })).toBe(false);+ expect(bed.commandService.syncExecuteCommand(SetWorksheetActiveOperation.id, {+ unitId: 'other-workbook',+ subUnitId: bed.subUnitId,+ })).toBe(true);+ expect(removed).not.toHaveBeenCalled();+ vi.runOnlyPendingTimers();+ expect(added).toHaveBeenCalledExactlyOnceWith([drawing]);+ } finally {+ subscriptions.dispose();+ bed.univer.dispose();+ bed.activated$.complete();+ }+ });+it.each([SheetDrawingAnchorType.Position, SheetDrawingAnchorType.Both])('isolates failed geometry and retries repaired drawings on activation (%s)',async (anchorType) => {diff --git a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-active-render.controller.ts b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-active-render.controller.tsindex 8d630b205c..4e540ed537 100644--- a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-active-render.controller.ts+++ b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-active-render.controller.ts@@ -64,11 +64,32 @@ export class SheetDrawingActiveRenderController extends Disposable implements IR}private _commandListener() {+ this.disposeWithMe(+ this._commandService.beforeCommandExecuted((command: ICommandInfo) => {+ if (command.id !== SetWorksheetActiveOperation.id) {+ return;+ }+ const { unitId, subUnitId } = command.params as ISetWorksheetActiveOperationParams;+ const { unit, unitId: contextUnitId } = this._context;+ if (+ unitId !== contextUnitId || !unit.getSheetBySheetId(subUnitId) ||+ unit.getActiveSheet().getSheetId() === subUnitId+ ) {+ return;+ }++ // Restoring the next sheet's viewport can expose offscreen drawings from the previous sheet.+ this._clearDrawings(unitId, subUnitId);+ })+ );+this.disposeWithMe(this._commandService.onCommandExecuted((command: ICommandInfo) => {if (command.id === SetWorksheetActiveOperation.id) {const { unitId, subUnitId } = command.params as ISetWorksheetActiveOperationParams;- this._updateDrawings(unitId, subUnitId);+ if (unitId === this._context.unitId && subUnitId === this._context.unit.getActiveSheet().getSheetId()) {+ this._updateDrawings(unitId, subUnitId);+ }}}));@@ -77,7 +98,9 @@ export class SheetDrawingActiveRenderController extends Disposable implements IRthis._context.activated$.subscribe((activated) => {const { unit, unitId } = this._context;if (activated) {- this._updateDrawings(unitId, unit.getActiveSheet().getSheetId());+ const subUnitId = unit.getActiveSheet().getSheetId();+ this._clearDrawings(unitId, subUnitId);+ this._updateDrawings(unitId, subUnitId);} else {this._clearDrawings(unitId);}@@ -85,26 +108,21 @@ export class SheetDrawingActiveRenderController extends Disposable implements IR);}- private _clearDrawings(selfUnitId: string): void {- this._scheduleRender(() => {- const drawingMap = this._drawingManagerService.drawingManagerData;- const removeDrawings: IDrawingParam[] = [];-- Object.keys(drawingMap ?? {}).forEach((unitId) => {- const subUnitMap = drawingMap[unitId] ?? {};- Object.keys(subUnitMap).forEach((subUnitId) => {- const drawingData = subUnitMap[subUnitId]?.data ?? {};- Object.keys(drawingData).forEach((drawingId) => {- if (unitId === selfUnitId) {- removeDrawings.push(drawingData[drawingId]);- }- });- });- });+ private _clearDrawings(unitId: string, keepSubUnitId?: string): void {+ this._cancelPendingRender();+ const subUnitMap = this._drawingManagerService.drawingManagerData[unitId] ?? {};+ const removeDrawings: IDrawingParam[] = [];+ for (const subUnitId of Object.keys(subUnitMap)) {+ if (subUnitId === keepSubUnitId) {+ continue;+ }+ for (const drawing of Object.values(subUnitMap[subUnitId]?.data ?? {})) {+ removeDrawings.push(drawing);+ }+ }- this._sheetDrawingService.removeNotification(removeDrawings);- this._drawingManagerService.removeNotification(removeDrawings);- });+ this._sheetDrawingService.removeNotification(removeDrawings);+ this._drawingManagerService.removeNotification(removeDrawings);}private _updateDrawings(showUnitId: string, showSubunitId: string): void {@@ -117,14 +135,6 @@ export class SheetDrawingActiveRenderController extends Disposable implements IRsheetSkeletonParam?.skeleton);const removeDrawings: IDrawingParam[] = failures.map(({ drawingId }) => drawingData[drawingId]);- for (const subUnitId of Object.keys(subUnitMap)) {- if (subUnitId !== showSubunitId) {- for (const drawing of Object.values(subUnitMap[subUnitId]?.data ?? {})) {- removeDrawings.push(drawing);- }- }- }-// JSON1 drawing mutations are applied to both stores, so derived geometry must agree.this._sheetDrawingService.refreshTransform(refreshedDrawings);this._drawingManagerService.refreshTransform(refreshedDrawings);