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): chart and float-dom not all collected on print (#5024)
ZhangWei9d163b8b0dbe
diff --git a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-printing.controller.tsx b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-printing.controller.tsxindex c4d1567dd3..99d5d632ac 100644--- a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-printing.controller.tsx+++ b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-printing.controller.tsx@@ -54,7 +54,10 @@ export class SheetDrawingPrintingController extends Disposable {const subUnitData = unitData?.[subUnitId];if (subUnitData) {subUnitData.order.forEach((id) => {- this._drawingRenderService.renderDrawing(subUnitData.data[id], scene);+ const drawing = subUnitData.data[id];+ if (drawing.drawingType !== DrawingTypeEnum.DRAWING_CHART && drawing.drawingType !== DrawingTypeEnum.DRAWING_DOM) {+ this._drawingRenderService.renderDrawing(drawing, scene);+ }});}@@ -127,28 +130,40 @@ export class SheetDrawingPrintingController extends Disposable {{handler: (disposableCollection, pos, next) => {const { unitId, subUnitId } = pos;- const floatDomInfos = this._canvasFloatDomManagerService.getFloatDomsBySubUnitId(unitId, subUnitId)- .map((i) => {- const drawing = this._drawingManagerService.getDrawingByParam({ drawingId: i.id, unitId, subUnitId }) as IFloatDomData;-- return drawing && {- ...drawing,- componentKey: this._componetManager.get(drawing.drawingType === DrawingTypeEnum.DRAWING_CHART ? PRINT_CHART_COMPONENT_KEY : drawing.componentKey) as any,- };- })- .filter(Boolean) as IFloatDomData[];- const PrintingFloatDomInjector = connectInjector(PrintingFloatDom, this._injector) as React.ComponentType<IPrintingFloatDomProps>;-- render(- <PrintingFloatDomInjector floatDomInfos={floatDomInfos} scene={pos.scene} skeleton={pos.skeleton} worksheet={pos.worksheet} />,- pos.root- );-- disposableCollection?.add(() => {- unmount(pos.root);- });-- return next(disposableCollection);+ const unitData = this._drawingManagerService.getDrawingDataForUnit(unitId);+ const subUnitData = unitData?.[subUnitId];+ if (subUnitData) {+ const floatDomInfos = subUnitData.order.map((id) => {+ const drawing = subUnitData.data[id];+ if (drawing.drawingType === DrawingTypeEnum.DRAWING_CHART) {+ return {+ ...drawing,+ componentKey: this._componetManager.get(PRINT_CHART_COMPONENT_KEY) as any,+ };+ }++ if (drawing.drawingType === DrawingTypeEnum.DRAWING_DOM) {+ return {+ ...drawing,+ componentKey: this._componetManager.get((drawing as IFloatDomData).componentKey) as any,+ };+ }++ return null;+ }).filter(Boolean) as IFloatDomData[];+ const PrintingFloatDomInjector = connectInjector(PrintingFloatDom, this._injector) as React.ComponentType<IPrintingFloatDomProps>;++ render(+ <PrintingFloatDomInjector floatDomInfos={floatDomInfos} scene={pos.scene} skeleton={pos.skeleton} worksheet={pos.worksheet} />,+ pos.root+ );++ disposableCollection?.add(() => {+ unmount(pos.root);+ });++ return next(disposableCollection);+ }},})diff --git a/packages/sheets-drawing-ui/src/views/printing-float-dom/index.tsx b/packages/sheets-drawing-ui/src/views/printing-float-dom/index.tsxindex 5fd3b30952..231e1443eb 100644--- a/packages/sheets-drawing-ui/src/views/printing-float-dom/index.tsx+++ b/packages/sheets-drawing-ui/src/views/printing-float-dom/index.tsx@@ -18,8 +18,9 @@ import type { Worksheet } from '@univerjs/core';import type { Scene, SpreadsheetSkeleton } from '@univerjs/engine-render';import type { IFloatDomData } from '@univerjs/sheets-drawing';import type { IFloatDom, IFloatDomLayout } from '@univerjs/ui';-import { FloatDomSingle } from '@univerjs/ui';-import { useMemo } from 'react';+import { IUniverInstanceService } from '@univerjs/core';+import { ComponentManager, useDependency } from '@univerjs/ui';+import { memo, useMemo, useRef } from 'react';import { BehaviorSubject } from 'rxjs';import { transformBound2DOMBound } from '../../services/canvas-float-dom-manager.service';@@ -30,6 +31,93 @@ export interface IPrintingFloatDomProps {worksheet: Worksheet;};+export const FloatDomSingle = memo((props: { layer: IFloatDom; id: string; position: IFloatDomLayout }) => {+ const { layer, id, position } = props;+ const univerInstanceService = useDependency(IUniverInstanceService);+ const domRef = useRef<HTMLDivElement>(null);+ const innerDomRef = useRef<HTMLDivElement>(null);+ const transformRef = useRef<string>(`transform: rotate(${position?.rotate}deg) translate(${position?.startX}px, ${position?.startY}px)`);+ const topRef = useRef<number>(position?.startY ?? 0);+ const leftRef = useRef<number>(position?.startX ?? 0);++ const Component = typeof layer.componentKey === 'string' ? useDependency(ComponentManager).get(layer.componentKey) : layer.componentKey;+ const layerProps: any = useMemo(() => ({+ data: layer.data,+ ...layer.props,+ }), [layer.data, layer.props]);++ const innerStyle = {+ width: `${position.width - 4}px`,+ height: `${position.height - 4}px`,+ left: `${position.absolute.left ? 0 : 'auto'}`,+ top: `${position.absolute.top ? 0 : 'auto'}`,+ right: `${position.absolute.left ? 'auto' : 0}`,+ bottom: `${position.absolute.top ? 'auto' : 0}`,+ };+ transformRef.current = `rotate(${position.rotate}deg)`;+ topRef.current = position.startY;+ leftRef.current = position.startX;++ const instance = univerInstanceService.getUnit(layer.unitId);+ const component = useMemo(() => Component+ ? (+ <Component+ {...layerProps}+ unitId={layer.unitId}+ unit={instance}+ floatDomId={layer.id}+ context={{+ root: innerDomRef,+ }}+ />+ )+ : null, [Component, layerProps]);++ if (!position) {+ return null;+ }++ //domRef univer-float-dom-wrapper+ //innerDomRef univer-float-dom+ return (+ <div+ ref={domRef}+ className="univer-z-10"+ style={{+ position: 'absolute',+ top: topRef.current,+ left: leftRef.current,+ width: Math.max(position.endX - position.startX - 2, 0),+ height: Math.max(position.endY - position.startY - 2, 0),+ transform: transformRef.current,+ overflow: 'hidden',+ transformOrigin: 'center center',+ }}+ onPointerMove={(e) => {+ layer.onPointerMove(e.nativeEvent);+ }}+ onPointerDown={(e) => {+ layer.onPointerDown(e.nativeEvent);+ }}+ onPointerUp={(e) => {+ layer.onPointerUp(e.nativeEvent);+ }}+ onWheel={(e) => {+ layer.onWheel(e.nativeEvent);+ }}+ >+ <div+ id={id}+ ref={innerDomRef}+ className="univer-overflow-hidden"+ style={{ position: 'absolute', ...innerStyle }}+ >+ {component}+ </div>+ </div>+ );+});+export const PrintingFloatDom = (props: IPrintingFloatDomProps) => {const { floatDomInfos, scene, skeleton, worksheet } = props;const floatDomParams = useMemo(() => floatDomInfos.map((info) => {@@ -60,8 +148,9 @@ export const PrintingFloatDom = (props: IPrintingFloatDomProps) => {absolute: offsetBound.absolute,};- const floatDom: IFloatDom = {+ const floatDom: IFloatDom & { position: IFloatDomLayout } = {position$: new BehaviorSubject<IFloatDomLayout>(domPos),+ position: domPos,id: info.drawingId,componentKey: info.componentKey,onPointerMove: () => { },@@ -77,7 +166,7 @@ export const PrintingFloatDom = (props: IPrintingFloatDomProps) => {return (<div style={{ position: 'absolute', top: 0, left: 0 }}>{floatDomParams.map(([id, floatDom]) => (- <FloatDomSingle key={id} layer={floatDom} id={id} />+ <FloatDomSingle key={id} layer={floatDom} id={id} position={floatDom.position} />))}</div>);