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(sheet): overflow cache (#788)

Univerf39c7a2af67a
diff --git a/packages/engine-render/src/components/sheets/extensions/border-auxiliary.ts b/packages/engine-render/src/components/sheets/extensions/border-auxiliary.ts
index 0d72baaf46..b680d560e1 100644
--- a/packages/engine-render/src/components/sheets/extensions/border-auxiliary.ts
+++ b/packages/engine-render/src/components/sheets/extensions/border-auxiliary.ts
@@ -157,12 +157,14 @@ export class BorderAuxiliary extends SheetExtension {
this._clearRectangle(ctx, scale, rowHeightAccumulation, columnWidthAccumulation, dataMergeCache, diffRanges);
// overflow cell
- this._clearRectangle(
+ this._clearOverflow(
ctx,
scale,
rowHeightAccumulation,
columnWidthAccumulation,
- overflowCache.toNativeArray(),
+ startRow,
+ endRow,
+ overflowCache,
diffRanges
);
@@ -199,7 +201,7 @@ export class BorderAuxiliary extends SheetExtension {
!this.isRenderDiffRangesByRow(startRow, diffRanges) &&
!this.isRenderDiffRangesByRow(endRow, diffRanges)
) {
- return true;
+ continue;
}
ctx.clearRect(startX, startY, endX - startX, endY - startY);
@@ -216,6 +218,57 @@ export class BorderAuxiliary extends SheetExtension {
}
}
+ private _clearOverflow(
+ ctx: CanvasRenderingContext2D,
+ scale: number,
+ rowHeightAccumulation: number[],
+ columnWidthAccumulation: number[],
+ currentStartRow: number,
+ currentEndRow: number,
+ overflowCache?: ObjectMatrix<IRange>,
+ diffRanges?: IRange[]
+ ) {
+ if (overflowCache == null) {
+ return;
+ }
+
+ overflowCache.forEach((row, columnRange) => {
+ if (!(row >= currentStartRow && row <= currentEndRow)) {
+ return true;
+ }
+
+ columnRange.forEach((column, dataCache) => {
+ const { startRow, endRow, startColumn, endColumn } = dataCache;
+
+ const startY = rowHeightAccumulation[startRow - 1] || 0;
+ const endY = rowHeightAccumulation[endRow] || rowHeightAccumulation[rowHeightAccumulation.length - 1];
+
+ const startX = columnWidthAccumulation[startColumn - 1] || 0;
+ const endX =
+ columnWidthAccumulation[endColumn] || columnWidthAccumulation[columnWidthAccumulation.length - 1];
+
+ if (
+ !this.isRenderDiffRangesByRow(startRow, diffRanges) &&
+ !this.isRenderDiffRangesByRow(endRow, diffRanges)
+ ) {
+ return true;
+ }
+
+ ctx.clearRect(startX, startY, endX - startX, endY - startY);
+
+ // After ClearRect, the lines will become thinner, and the lines will be repaired below.
+ ctx.beginPath();
+ ctx.moveTo(startX, startY);
+ ctx.lineTo(endX, startY);
+ ctx.lineTo(endX, endY);
+ ctx.lineTo(startX, endY);
+ ctx.lineTo(startX, startY);
+ ctx.stroke();
+ ctx.closePath();
+ });
+ });
+ }
+
private _clearBackground(
ctx: CanvasRenderingContext2D,
scale: number,
diff --git a/packages/engine-render/src/components/sheets/extensions/font.ts b/packages/engine-render/src/components/sheets/extensions/font.ts
index 061ce75edb..afa7ab4b5f 100644
--- a/packages/engine-render/src/components/sheets/extensions/font.ts
+++ b/packages/engine-render/src/components/sheets/extensions/font.ts
@@ -104,12 +104,15 @@ export class Font extends SheetExtension {
return true;
}
- if (
- !this.isRenderDiffRangesByColumn(mergeInfo.startColumn, diffRanges) &&
- !this.isRenderDiffRangesByColumn(mergeInfo.endColumn, diffRanges)
- ) {
- return true;
- }
+ /**
+ * Overflow can cause text truncation, so columns are not rendered incrementally.
+ */
+ // if (
+ // !this.isRenderDiffRangesByColumn(mergeInfo.startColumn, diffRanges) &&
+ // !this.isRenderDiffRangesByColumn(mergeInfo.endColumn, diffRanges)
+ // ) {
+ // return true;
+ // }
const cellWidth = endX - startX;
const cellHeight = endY - startY;
@@ -123,6 +126,7 @@ export class Font extends SheetExtension {
const { startColumn, startRow, endColumn, endRow } = overflowRectangle;
if (startColumn === endColumn && startColumn === columnIndex) {
ctx.rect(startX, startY, cellWidth, cellHeight);
+ ctx.clearRect(startX + 1, startY + 1, cellWidth - 2, cellHeight - 2);
} else {
if (horizontalAlign === HorizontalAlign.CENTER) {
this._clipRectangle(
@@ -162,9 +166,10 @@ export class Font extends SheetExtension {
}
} else {
ctx.rect(startX, startY, cellWidth, cellHeight);
+ ctx.clearRect(startX + 1, startY + 1, cellWidth - 2, cellHeight - 2);
}
ctx.clip();
- ctx.clearRect(startX + 1, startY + 1, cellWidth - 2, cellHeight - 2);
+
ctx.translate(startX, startY);
this._renderDocuments(ctx, docsConfig, startX, startY, endX, endY, rowIndex, columnIndex);
ctx.restore();
@@ -225,6 +230,7 @@ export class Font extends SheetExtension {
const endX = columnWidthAccumulation[endColumn] || columnWidthAccumulation[columnWidthAccumulation.length - 1];
ctx.rect(startX, startY, endX - startX, endY - startY);
+ ctx.clearRect(startX, startY, endX - startX, endY - startY);
}
}
diff --git a/packages/engine-render/src/components/sheets/spreadsheet.ts b/packages/engine-render/src/components/sheets/spreadsheet.ts
index 2cbdf4351a..cff7d710d9 100644
--- a/packages/engine-render/src/components/sheets/spreadsheet.ts
+++ b/packages/engine-render/src/components/sheets/spreadsheet.ts
@@ -53,6 +53,12 @@ export class Spreadsheet extends SheetComponent {
private _forceDirty = false;
+ private _overflowCacheRuntime: { [row: number]: boolean } = {};
+
+ private _overflowCacheRuntimeRange = new ObjectMatrix<IRange>();
+
+ private _overflowCacheRuntimeTimeout: number | NodeJS.Timeout = -1;
+
private _documents: Documents = new Documents(OBJECT_KEY, undefined, {
pageMarginLeft: 0,
pageMarginTop: 0,
@@ -119,6 +125,15 @@ export class Spreadsheet extends SheetComponent {
for (const extension of extensions) {
extension.draw(ctx, parentScale, spreadsheetSkeleton, diffRanges);
}
+
+ /**
+ * Caching overflow during scrolling and clearing it after waiting for 1 second without scrolling can significantly improve performance.
+ */
+ clearTimeout(this._overflowCacheRuntimeTimeout);
+ this._overflowCacheRuntimeTimeout = setTimeout(() => {
+ this._overflowCacheRuntimeRange.reset();
+ this._overflowCacheRuntime = {};
+ }, 1000);
}
override isHit(coord: Vector2) {
@@ -445,7 +460,7 @@ export class Spreadsheet extends SheetComponent {
* Overflow on the left or right is aligned according to the text's horizontal alignment.
*/
private _calculateOverflow() {
- const overflowCache = new ObjectMatrix<IRange>();
+ // const overflowCache = new ObjectMatrix<IRange>();
const spreadsheetSkeleton = this.getSkeleton();
if (!spreadsheetSkeleton) {
return;
@@ -459,112 +474,132 @@ export class Spreadsheet extends SheetComponent {
Object.keys(fontList).forEach((fontFormat: string) => {
const fontObjectArray = fontList[fontFormat];
- fontObjectArray.forValue((row, column, docsConfig) => {
- // Merged cells do not support overflow.
- if (spreadsheetSkeleton.intersectMergeRange(row, column)) {
- return true;
+ fontObjectArray.forEach((row, fontCache) => {
+ if (this._overflowCacheRuntime[row] != null) {
+ return;
}
- // wrap and angle handler
- const { documentSkeleton, angle = 0, horizontalAlign, wrapStrategy } = docsConfig;
+ fontCache.forEach((column, docsConfig) => {
+ // wrap and angle handler
+ const { documentSkeleton, angle = 0, horizontalAlign, wrapStrategy } = docsConfig;
- const cell = spreadsheetSkeleton.getCellData().getValue(row, column);
+ const cell = spreadsheetSkeleton.getCellData().getValue(row, column);
- const sheetSkeleton = this.getSkeleton();
- if (!sheetSkeleton) {
- return true;
- }
+ const sheetSkeleton = this.getSkeleton();
+ if (!sheetSkeleton) {
+ return true;
+ }
+
+ const { t: cellValueType = CellValueType.STRING } = cell || {};
+
+ /**
+ * Numerical and Boolean values are not displayed with overflow.
+ */
+ if (
+ wrapStrategy === WrapStrategy.OVERFLOW &&
+ cellValueType !== CellValueType.NUMBER &&
+ cellValueType !== CellValueType.BOOLEAN &&
+ horizontalAlign !== HorizontalAlign.JUSTIFIED
+ ) {
+ // Merged cells do not support overflow.
+ if (spreadsheetSkeleton.intersectMergeRange(row, column)) {
+ return true;
+ }
- const { t: cellValueType = CellValueType.STRING } = cell || {};
+ let contentSize = getDocsSkeletonPageSize(documentSkeleton, angle);
- /**
- * Numerical and Boolean values are not displayed with overflow.
- */
- if (
- wrapStrategy === WrapStrategy.OVERFLOW &&
- cellValueType !== CellValueType.NUMBER &&
- cellValueType !== CellValueType.BOOLEAN &&
- horizontalAlign !== HorizontalAlign.JUSTIFIED
- ) {
- let contentSize = getDocsSkeletonPageSize(documentSkeleton, angle);
+ if (!contentSize) {
+ return true;
+ }
- if (!contentSize) {
- return true;
- }
+ if (angle !== 0) {
+ const { startY, endY, startX, endX } = getCellByIndex(
+ row,
+ column,
+ rowHeightAccumulation,
+ columnWidthAccumulation,
+ mergeData
+ );
+ const cellWidth = endX - startX;
+ const cellHeight = endY - startY;
+
+ if (contentSize.height > cellHeight) {
+ contentSize = {
+ width: cellHeight / Math.tan(Math.abs(angle)) + cellWidth,
+ height: cellHeight,
+ };
+ // if (angle > 0) {
+ // horizontalAlign = HorizontalAlign.LEFT;
+ // } else {
+ // horizontalAlign = HorizontalAlign.RIGHT;
+ // }
+ }
+ }
- if (angle !== 0) {
- const { startY, endY, startX, endX } = getCellByIndex(
+ const position = spreadsheetSkeleton.getOverflowPosition(
+ contentSize,
+ horizontalAlign,
+ row,
+ column,
+ columnCount
+ );
+
+ const { startColumn, endColumn } = position;
+
+ this._overflowCacheRuntimeRange.setValue(row, column, {
+ startRow: row,
+ endRow: row,
+ startColumn,
+ endColumn,
+ });
+ } else if (wrapStrategy === WrapStrategy.WRAP && angle !== 0) {
+ // Merged cells do not support overflow.
+ if (spreadsheetSkeleton.intersectMergeRange(row, column)) {
+ return true;
+ }
+
+ const { startY, endY } = getCellByIndex(
row,
column,
rowHeightAccumulation,
columnWidthAccumulation,
mergeData
);
- const cellWidth = endX - startX;
+
const cellHeight = endY - startY;
+ documentSkeleton.getViewModel().getDataModel().updateDocumentDataPageSize(cellHeight);
+ documentSkeleton.calculate();
+ const contentSize = getDocsSkeletonPageSize(documentSkeleton, angle);
- if (contentSize.height > cellHeight) {
- contentSize = {
- width: cellHeight / Math.tan(Math.abs(angle)) + cellWidth,
- height: cellHeight,
- };
- // if (angle > 0) {
- // horizontalAlign = HorizontalAlign.LEFT;
- // } else {
- // horizontalAlign = HorizontalAlign.RIGHT;
- // }
+ if (!contentSize) {
+ return true;
}
- }
-
- const position = spreadsheetSkeleton.getOverflowPosition(
- contentSize,
- horizontalAlign,
- row,
- column,
- columnCount
- );
-
- const { startColumn, endColumn } = position;
-
- overflowCache.setValue(row, column, {
- startRow: row,
- endRow: row,
- startColumn,
- endColumn,
- });
- } else if (wrapStrategy === WrapStrategy.WRAP && angle !== 0) {
- const { startY, endY } = getCellByIndex(
- row,
- column,
- rowHeightAccumulation,
- columnWidthAccumulation,
- mergeData
- );
-
- const cellHeight = endY - startY;
- documentSkeleton.getViewModel().getDataModel().updateDocumentDataPageSize(cellHeight);
- documentSkeleton.calculate();
- const contentSize = getDocsSkeletonPageSize(documentSkeleton, angle);
- if (!contentSize) {
- return true;
+ const { startColumn, endColumn } = sheetSkeleton.getOverflowPosition(
+ contentSize,
+ horizontalAlign,
+ row,
+ column,
+ sheetSkeleton.getColumnCount()
+ );
+ this._overflowCacheRuntimeRange.setValue(row, column, {
+ startRow: row,
+ endRow: row,
+ startColumn,
+ endColumn,
+ });
}
+ });
- const { startColumn, endColumn } = sheetSkeleton.getOverflowPosition(
- contentSize,
- horizontalAlign,
- row,
- column,
- sheetSkeleton.getColumnCount()
- );
- overflowCache.setValue(row, column, { startRow: row, endRow: row, startColumn, endColumn });
- }
+ this._overflowCacheRuntime[row] = true;
+ });
+ fontObjectArray.forValue((row, column, docsConfig) => {
// console.log('appendToOverflowCache', cellHeight, angle, contentSize, { rowIndex, columnIndex, startColumn, endColumn });
});
});
- spreadsheetSkeleton.setOverflowCache(overflowCache);
+ spreadsheetSkeleton.setOverflowCache(this._overflowCacheRuntimeRange);
}
private _drawAuxiliary(ctx: CanvasRenderingContext2D) {