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: some case rich text style should convert to cell style (#3975)

ybzky47cae07809d9
diff --git a/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts b/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts
index 90236c6dc5..ba2d697217 100644
--- a/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts
+++ b/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts
@@ -28,7 +28,7 @@ import type { IAfterProcessRule, IPastePlugin } from './paste-plugins/type';
import { CustomRangeType, DEFAULT_WORKSHEET_ROW_HEIGHT, generateRandomId, ObjectMatrix, skipParseTagNames } from '@univerjs/core';
import { handleStringToStyle, textTrim } from '@univerjs/ui';
import { extractNodeStyle } from './parse-node-style';
-import parseToDom, { generateParagraphs } from './utils';
+import parseToDom, { convertToCellStyle, generateParagraphs } from './utils';
export interface IStyleRule {
filter: string | string[] | ((node: HTMLElement) => boolean);
@@ -107,7 +107,7 @@ export class HtmlToUSMService {
this._getCurrentSkeleton = props.getCurrentSkeleton;
}
- // eslint-disable-next-line max-lines-per-function, complexity
+ // eslint-disable-next-line max-lines-per-function
convert(html: string): IUniverSheetCopyDataModel {
const pastePlugin = HtmlToUSMService._pluginList.find((plugin) => plugin.checkPasteType(html));
if (pastePlugin) {
@@ -205,20 +205,13 @@ export class HtmlToUSMService {
customRanges,
};
- const dataStreamLength = dataStream.length;
- const textRunsLength = textRuns?.length ?? 0;
- if (!customRanges?.length && (!textRunsLength || (textRunsLength === 1 && textRuns![0].st === 0 && textRuns![0].ed === dataStreamLength))) {
- valueMatrix.setValue(0, 0, {
- v: dataStream,
- });
+ if (!customRanges?.length) {
+ valueMatrix.setValue(0, 0, convertToCellStyle({ v: dataStream }, dataStream, textRuns));
} else {
const p = this._generateDocumentDataModelSnapshot({
body: singleDocBody,
});
- valueMatrix.setValue(0, 0, {
- v: dataStream,
- p,
- });
+ valueMatrix.setValue(0, 0, convertToCellStyle({ v: dataStream, p }, dataStream, textRuns));
}
rowProperties.push({}); // TODO@yuhongz
diff --git a/packages/sheets-ui/src/services/clipboard/html-to-usm/utils.ts b/packages/sheets-ui/src/services/clipboard/html-to-usm/utils.ts
index cb97b07921..3bb5ba9723 100644
--- a/packages/sheets-ui/src/services/clipboard/html-to-usm/utils.ts
+++ b/packages/sheets-ui/src/services/clipboard/html-to-usm/utils.ts
@@ -14,7 +14,8 @@
* limitations under the License.
*/
-import type { IParagraph, IParagraphStyle, Nullable } from '@univerjs/core';
+import type { IParagraph, IParagraphStyle, ITextRun, Nullable } from '@univerjs/core';
+import type { ICellDataWithSpanInfo } from '../type';
import { DataStreamTreeTokenType, Tools } from '@univerjs/core';
import { ptToPixel } from '@univerjs/engine-render';
@@ -94,3 +95,32 @@ export function generateParagraphs(dataStream: string, prevParagraph?: IParagrap
return paragraphs;
}
+
+export function convertToCellStyle(cell: ICellDataWithSpanInfo, dataStream: string, textRuns: ITextRun[] | undefined) {
+ const dataStreamLength = dataStream.length;
+ const textRunsLength = textRuns?.length ?? 0;
+ const canConvertToCellStyle = !textRunsLength || (textRunsLength === 1 && textRuns![0].st === 0 && textRuns![0].ed === dataStreamLength);
+
+ if (cell.p) {
+ if (canConvertToCellStyle && cell.p.body?.textRuns?.length) {
+ cell.p.body.textRuns = [];
+ return {
+ ...cell,
+ s: textRuns![0].ts,
+ };
+ } else {
+ return cell;
+ }
+ } else {
+ if (canConvertToCellStyle) {
+ return {
+ ...cell,
+ s: textRuns![0].ts,
+ };
+ } else {
+ return cell;
+ }
+ }
+
+ return cell;
+}