Skip to content

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.

fix: padding rendering & not throw cancel error (#5406)

WEI ZHANG14345ec1d77a
diff --git a/packages/core/src/common/error.ts b/packages/core/src/common/error.ts
index c89f73ab44..def002f170 100644
--- a/packages/core/src/common/error.ts
+++ b/packages/core/src/common/error.ts
@@ -21,7 +21,7 @@ export class CustomCommandExecutionError extends Error {
}
}
-export class CanceledError extends Error {
+export class CanceledError extends CustomCommandExecutionError {
constructor() {
super('Canceled by facade');
this.name = 'CanceledError';
diff --git a/packages/engine-render/src/components/sheets/extensions/font.ts b/packages/engine-render/src/components/sheets/extensions/font.ts
index c3c44f2463..e9c472fa27 100644
--- a/packages/engine-render/src/components/sheets/extensions/font.ts
+++ b/packages/engine-render/src/components/sheets/extensions/font.ts
@@ -453,13 +453,17 @@ export class Font extends SheetExtension {
) {
const { fontCache } = renderFontCtx;
if (!fontCache) return;
-
+ const padding = fontCache.style?.pd ?? DEFAULT_PADDING_DATA;
+ const paddingLeft = padding.l ?? DEFAULT_PADDING_DATA.l;
+ const paddingRight = padding.r ?? DEFAULT_PADDING_DATA.r;
+ const paddingTop = padding.t ?? DEFAULT_PADDING_DATA.t;
+ const paddingBottom = padding.b ?? DEFAULT_PADDING_DATA.b;
const { vertexAngle = 0, wrapStrategy, cellData } = fontCache;
if (cellData?.v === undefined || cellData?.v === null) return;
const text = String(cellData.v) as string;
let { startX, startY, endX, endY } = renderFontCtx;
- let cellWidth = endX - startX - DEFAULT_PADDING_DATA.l - DEFAULT_PADDING_DATA.r;
- const cellHeight = endY - startY - DEFAULT_PADDING_DATA.t - DEFAULT_PADDING_DATA.b;
+ let cellWidth = endX - startX - paddingLeft - paddingRight;
+ const cellHeight = endY - startY - paddingTop - paddingBottom;
const overflowRectangle = overflowCache.getValue(row, col);
const isOverflow = !(wrapStrategy === WrapStrategy.WRAP && vertexAngle === 0);
@@ -475,10 +479,11 @@ export class Font extends SheetExtension {
const startCell = renderFontCtx.spreadsheetSkeleton.getCellWithCoordByIndex(startRow, startColumn);
startX = startCell.startX;
startY = startCell.startY;
- cellWidth = endX - startX - DEFAULT_PADDING_DATA.l - DEFAULT_PADDING_DATA.r;
+ cellWidth = endX - startX - paddingLeft - paddingRight;
}
const isNumber = cellData.t === CellValueType.NUMBER && typeof cellData.v === 'number';
+
Text.drawWith(ctx, {
text,
fontStyle: fontCache.fontString,
@@ -487,8 +492,8 @@ export class Font extends SheetExtension {
vAlign: fontCache.verticalAlign,
width: cellWidth,
height: cellHeight,
- left: DEFAULT_PADDING_DATA.l,
- top: DEFAULT_PADDING_DATA.t,
+ left: paddingLeft,
+ top: paddingTop,
color: fontCache.style?.cl?.rgb,
strokeLine: Boolean(fontCache.style?.st?.s),
underline: Boolean(fontCache.style?.ul?.s),
diff --git a/packages/engine-render/src/components/sheets/sheet.render-skeleton.ts b/packages/engine-render/src/components/sheets/sheet.render-skeleton.ts
index 6ab9699792..bf3c7ec8dc 100644
--- a/packages/engine-render/src/components/sheets/sheet.render-skeleton.ts
+++ b/packages/engine-render/src/components/sheets/sheet.render-skeleton.ts
@@ -562,13 +562,17 @@ export class SpreadsheetSkeleton extends SheetSkeleton {
if (cell?.v === undefined || cell?.v === null) {
return undefined;
}
+ const paddingLeft = style.pd?.l ?? DEFAULT_PADDING_DATA.l;
+ const paddingRight = style.pd?.r ?? DEFAULT_PADDING_DATA.r;
+ const paddingTop = style.pd?.t ?? DEFAULT_PADDING_DATA.t;
+ const paddingBottom = style.pd?.b ?? DEFAULT_PADDING_DATA.b;
if (style?.tb === WrapStrategy.WRAP) {
const skeleton = new DocSimpleSkeleton(
`${cell!.v!}`,
getFontStyleString(style).fontCache,
style?.tb === WrapStrategy.WRAP,
- colWidth - DEFAULT_PADDING_DATA.l - DEFAULT_PADDING_DATA.r,
+ colWidth - paddingLeft - paddingRight,
Infinity
);
skeleton.calculate();
@@ -577,7 +581,7 @@ export class SpreadsheetSkeleton extends SheetSkeleton {
// For same fontStyle, the height of the text is fixed.
// So we can use a fixed text to calculate the height to make a speed up.
const textSize = FontCache.getMeasureText('A', getFontStyleString(style).fontCache);
- return textSize.fontBoundingBoxAscent + textSize.fontBoundingBoxDescent + DEFAULT_PADDING_DATA.t + DEFAULT_PADDING_DATA.b;
+ return textSize.fontBoundingBoxAscent + textSize.fontBoundingBoxDescent + paddingTop + paddingBottom;
}
}
}
diff --git a/packages/sheets-drawing-ui/src/facade/f-univer.ts b/packages/sheets-drawing-ui/src/facade/f-univer.ts
index 540277659e..ae763a7d41 100644
--- a/packages/sheets-drawing-ui/src/facade/f-univer.ts
+++ b/packages/sheets-drawing-ui/src/facade/f-univer.ts
@@ -20,7 +20,7 @@ import type { ISheetImage } from '@univerjs/sheets-drawing';
import type { IDeleteDrawingCommandParams, IInsertDrawingCommandParams, ISetDrawingCommandParams } from '@univerjs/sheets-drawing-ui';
import type { FWorkbook } from '@univerjs/sheets/facade';
import type { IBeforeOverGridImageChangeParamObject } from './f-event';
-import { ICommandService } from '@univerjs/core';
+import { CanceledError, ICommandService } from '@univerjs/core';
import { FUniver } from '@univerjs/core/facade';
import { IDrawingManagerService, SetDrawingSelectedOperation } from '@univerjs/drawing';
import { InsertSheetDrawingCommand, RemoveSheetDrawingCommand, SetSheetDrawingCommand } from '@univerjs/sheets-drawing-ui';
@@ -78,7 +78,7 @@ export class FUniverDrawingMixin extends FUniver {
this.fireEvent(this.Event.BeforeOverGridImageInsert, eventParams);
if (eventParams.cancel) {
- throw new Error('Canceled by BeforeOverGridImageInsert event');
+ throw new CanceledError();
}
})
);
@@ -109,7 +109,7 @@ export class FUniverDrawingMixin extends FUniver {
this.fireEvent(this.Event.BeforeOverGridImageRemove, eventParams);
if (eventParams.cancel) {
- throw new Error('Canceled by BeforeOverGridImageRemove event');
+ throw new CanceledError();
}
})
);
@@ -150,7 +150,7 @@ export class FUniverDrawingMixin extends FUniver {
if (eventParams.cancel) {
drawingManagerService.updateNotification(drawings as IDrawingSearch[]);
- throw new Error('Canceled by BeforeOverGridImageChange event');
+ throw new CanceledError();
}
})
);
@@ -181,7 +181,7 @@ export class FUniverDrawingMixin extends FUniver {
this.fireEvent(this.Event.BeforeOverGridImageSelect, eventParams);
if (eventParams.cancel) {
- throw new Error('Canceled by BeforeOverGridImageSelect event');
+ throw new CanceledError();
}
})
);