From 5ec43b271551f7b2d1db7f9b01ebd24c95932135 Mon Sep 17 00:00:00 2001 From: Rui-Sun Date: Mon, 3 Jun 2024 23:12:23 +0800 Subject: [PATCH] fix: fix getCellOverflowText --- .../vtable/src/scenegraph/group-creater/cell-helper.ts | 3 ++- .../vtable/src/scenegraph/layout/compute-col-width.ts | 2 +- .../vtable/src/scenegraph/layout/compute-row-height.ts | 2 +- packages/vtable/src/scenegraph/scenegraph.ts | 9 +++++++-- packages/vtable/src/scenegraph/utils/break-string.ts | 9 +++++++-- packages/vtable/src/scenegraph/utils/text-icon-layout.ts | 6 ++++-- 6 files changed, 22 insertions(+), 9 deletions(-) diff --git a/packages/vtable/src/scenegraph/group-creater/cell-helper.ts b/packages/vtable/src/scenegraph/group-creater/cell-helper.ts index 67ba4a622..23e990383 100644 --- a/packages/vtable/src/scenegraph/group-creater/cell-helper.ts +++ b/packages/vtable/src/scenegraph/group-creater/cell-helper.ts @@ -471,7 +471,7 @@ export function updateCell(col: number, row: number, table: BaseTableAPI, addNew const textMark = oldCellGroup.getChildByName('text'); if (textMark) { const text = table.getCellValue(col, row); - const textArr = breakString(text, table); + const { text: textArr, moreThanMaxCharacters } = breakString(text, table); const hierarchyOffset = getHierarchyOffset(col, row, table); const lineClamp = cellStyle.lineClamp; @@ -489,6 +489,7 @@ export function updateCell(col: number, row: number, table: BaseTableAPI, addNew const attribute = { text: textArr.length === 1 && !autoWrapText ? textArr[0] : textArr, // 单行(no-autoWrapText)为字符串,多行(autoWrapText)为字符串数组 + moreThanMaxCharacters, maxLineWidth: cellWidth - (padding[1] + padding[3] + hierarchyOffset), // fill: true, // textAlign: 'left', diff --git a/packages/vtable/src/scenegraph/layout/compute-col-width.ts b/packages/vtable/src/scenegraph/layout/compute-col-width.ts index 5503a84a2..2f85500d9 100644 --- a/packages/vtable/src/scenegraph/layout/compute-col-width.ts +++ b/packages/vtable/src/scenegraph/layout/compute-col-width.ts @@ -590,7 +590,7 @@ function computeTextWidth(col: number, row: number, cellType: ColumnTypeOption, } else { text = cellValue; } - const lines = breakString(text, table); + const lines = breakString(text, table).text; if (lines.length >= 1) { // eslint-disable-next-line no-loop-func lines.forEach((line: string) => { diff --git a/packages/vtable/src/scenegraph/layout/compute-row-height.ts b/packages/vtable/src/scenegraph/layout/compute-row-height.ts index 4dc192f16..5484a4b8d 100644 --- a/packages/vtable/src/scenegraph/layout/compute-row-height.ts +++ b/packages/vtable/src/scenegraph/layout/compute-row-height.ts @@ -700,7 +700,7 @@ function computeTextHeight(col: number, row: number, cellType: ColumnTypeOption, } else { // text text = cellValue; - const lines = breakString(text, table); + const lines = breakString(text, table).text; const cellWidth = table.getColsWidth(col, endCol); if (iconInlineFront.length || iconInlineEnd.length) { diff --git a/packages/vtable/src/scenegraph/scenegraph.ts b/packages/vtable/src/scenegraph/scenegraph.ts index 7fec67657..c7de79042 100644 --- a/packages/vtable/src/scenegraph/scenegraph.ts +++ b/packages/vtable/src/scenegraph/scenegraph.ts @@ -1738,6 +1738,9 @@ export class Scenegraph { const text = cellGroup.getChildByName('text', true) as unknown as Text | RichText; if (text && text.type === 'text') { + if ((text.attribute as any).moreThanMaxCharacters) { + return this.table.getCellValue(col, row); + } const textAttributeStr = isArray(text.attribute.text) ? text.attribute.text.join('') : (text.attribute.text as string); @@ -1750,7 +1753,8 @@ export class Scenegraph { }); } if (cacheStr !== textAttributeStr) { - return textAttributeStr; + // return textAttributeStr; + return this.table.getCellValue(col, row); } } else if (text && text.type === 'richtext') { const richtext = text; @@ -1760,7 +1764,8 @@ export class Scenegraph { richtext.attribute.height < richtext._frameCache.actualHeight ) { const textConfig = richtext.attribute.textConfig.find((item: any) => item.text); - return (textConfig as any).text as string; + // return (textConfig as any).text as string; + return this.table.getCellValue(col, row); } } return null; diff --git a/packages/vtable/src/scenegraph/utils/break-string.ts b/packages/vtable/src/scenegraph/utils/break-string.ts index 678cae36d..11350ca34 100644 --- a/packages/vtable/src/scenegraph/utils/break-string.ts +++ b/packages/vtable/src/scenegraph/utils/break-string.ts @@ -2,10 +2,12 @@ import { isString } from '@visactor/vutils'; import { convertInternal } from '../../tools/util'; import type { BaseTableAPI } from '../../ts-types/base-table'; -export function breakString(textStr: string, table: BaseTableAPI): string[] { +export function breakString(textStr: string, table: BaseTableAPI) { + let moreThanMaxCharacters = false; if (isString(textStr) && textStr.length > (table.options.maxCharactersNumber || 200)) { textStr = textStr.slice(0, table.options.maxCharactersNumber || 200); textStr += '\u2026'; + moreThanMaxCharacters = true; } let text; if (!table.internalProps.enableLineBreak && !table.options.customConfig?.multilinesForXTable) { @@ -19,5 +21,8 @@ export function breakString(textStr: string, table: BaseTableAPI): string[] { text.pop(); } - return text; + return { + text, + moreThanMaxCharacters + }; } diff --git a/packages/vtable/src/scenegraph/utils/text-icon-layout.ts b/packages/vtable/src/scenegraph/utils/text-icon-layout.ts index e451acc0c..560006c60 100644 --- a/packages/vtable/src/scenegraph/utils/text-icon-layout.ts +++ b/packages/vtable/src/scenegraph/utils/text-icon-layout.ts @@ -75,7 +75,7 @@ export function createCellContent( if (!Array.isArray(icons) || icons.length === 0) { if (isValid(textStr)) { // 没有icon,cellGroup只添加WrapText - const text = breakString(textStr, table); + const { text, moreThanMaxCharacters } = breakString(textStr, table); const hierarchyOffset = range ? getHierarchyOffset(range.start.col, range.start.row, table) @@ -91,6 +91,7 @@ export function createCellContent( } const attribute = { text: text.length === 1 ? text[0] : text, + moreThanMaxCharacters, maxLineWidth: autoColWidth ? Infinity : cellWidth - (padding[1] + padding[3] + hierarchyOffset), // fill: true, // textAlign: 'left', @@ -205,10 +206,11 @@ export function createCellContent( const hierarchyOffset = range ? getHierarchyOffset(range.start.col, range.start.row, table) : getHierarchyOffset(cellGroup.col, cellGroup.row, table); - const text = breakString(textStr, table); + const { text, moreThanMaxCharacters } = breakString(textStr, table); const attribute = { text: text.length === 1 ? text[0] : text, + moreThanMaxCharacters, maxLineWidth: autoColWidth ? Infinity : cellWidth - (padding[1] + padding[3]) - leftIconWidth - rightIconWidth - hierarchyOffset,