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.

style(base-ui): cell.ts eslint

Dushusir965f509f2820
diff --git a/packages/base-ui/src/Helpers/Cell.ts b/packages/base-ui/src/Helpers/Cell.ts
index a1826c66d8..fbba4d9a3a 100644
--- a/packages/base-ui/src/Helpers/Cell.ts
+++ b/packages/base-ui/src/Helpers/Cell.ts
@@ -1,4 +1,4 @@
-import { BlockType, getBorderStyleType, ICellData, IDocumentData, IElement, IRangeData, IStyleData, ITextDecoration, Tools } from '@univerjs/core';
+import { getBorderStyleType, IBorderData, ICellData, IDocumentData, IKeyValue, IRangeData, IStyleData, ITextDecoration, ITextRun, Tools } from '@univerjs/core';
import { ptToPx, pxToPt } from '@univerjs/base-render';
import { textTrim } from '../Utils';
@@ -25,8 +25,10 @@ export function handleDomToJson($dom: HTMLElement): IDocumentData | string {
if (nodeList.length === 1 && nodeList[0].nodeName === '#text') {
return nodeList[0].textContent as string;
}
- const elements: IElement[] = [];
+ const textRuns: ITextRun[] = [];
let ed = 0;
+ let dataStream = $dom.textContent || '';
+ dataStream += '\r\n';
for (let i = 0; i < nodeList.length; i++) {
let span = nodeList[`${i}`] as HTMLElement;
@@ -44,17 +46,13 @@ export function handleDomToJson($dom: HTMLElement): IDocumentData | string {
spanTexts.forEach((item) => {
ed = +item.length;
- let eId = Tools.generateRandomId(6);
+ let sId = Tools.generateRandomId(6);
- elements.push({
- eId,
+ textRuns.push({
+ sId,
st: 0,
ed: item.length - 1,
- et: 0,
- tr: {
- ct: item,
- ts: textStyle,
- },
+ ts: textStyle,
});
// // 如果有 \n 说明有换行,另起一段
// if (item.includes('\n')) {
@@ -87,31 +85,11 @@ export function handleDomToJson($dom: HTMLElement): IDocumentData | string {
}
const blockId = Tools.generateRandomId(6);
- let p = {
+ let p: IDocumentData = {
id: Tools.generateRandomId(6),
body: {
- blockElements: [
- {
- blockId,
- st: 0,
- ed,
- blockType: 0,
- paragraph: {
- elements,
- },
- },
- {
- blockId: 'b',
- st: 0,
- ed: 0,
- blockType: BlockType.SECTION_BREAK,
- sectionBreak: {
- columnProperties: [],
- columnSeparatorType: 1,
- sectionType: 0,
- },
- },
- ],
+ dataStream,
+ textRuns,
},
documentStyle: {},
};
@@ -371,11 +349,11 @@ export function handleStringToStyle($dom: HTMLElement, cssStyle: string = '') {
},
};
for (let k in colors) {
- styleList.bd[k].cl.rgb = colors[k];
+ (styleList.bd as IKeyValue)[k].cl.rgb = colors[k as keyof IBorderData];
}
} else {
for (let k in colors) {
- styleList.bd[k].cl.rgb = colors[k];
+ (styleList.bd as IKeyValue)[k].cl.rgb = colors[k as keyof IBorderData];
}
}
}
@@ -383,7 +361,7 @@ export function handleStringToStyle($dom: HTMLElement, cssStyle: string = '') {
if (key === 'border-width' || key === 'border-style') {
const width = handleBorder(value, ' ');
for (let k in width) {
- borderInfo[k] += ` ${width[k]}`;
+ (borderInfo as IKeyValue)[k] += ` ${width[k as keyof IBorderData]}`;
}
if (!styleList.bd) {
styleList.bd = {
@@ -464,7 +442,7 @@ export function handleStringToStyle($dom: HTMLElement, cssStyle: string = '') {
return styleList;
}
-function handleBorder(border: string, param: string) {
+function handleBorder(border: string, param: string): IBorderData {
let arr;
if (param === ' ') {
arr = border.trim().split(param);
@@ -639,8 +617,8 @@ export function handelTableToJson(table: string) {
}
if (data[r][c] == null) {
data[r][c] = cell;
- let rowSpan = td.getAttribute('rowSpan') ?? 1;
- let colSpan = td.getAttribute('colSpan') ?? 1;
+ let rowSpan = Number(td.getAttribute('rowSpan')) ?? 1;
+ let colSpan = Number(td.getAttribute('colSpan')) ?? 1;
if (rowSpan > 1 || colSpan > 1) {
let first = { rs: +rowSpan - 1, cs: +colSpan - 1, r, c };
data[r][c].mc = first;
@@ -800,8 +778,8 @@ export function handelExcelToJson(html: string) {
}
if (data[r][c] == null) {
data[r][c] = cell;
- let rowSpan = td.getAttribute('rowSpan') ?? 1;
- let colSpan = td.getAttribute('colSpan') ?? 1;
+ let rowSpan = Number(td.getAttribute('rowSpan')) ?? 1;
+ let colSpan = Number(td.getAttribute('colSpan')) ?? 1;
if (rowSpan > 1 || colSpan > 1) {
let first = { rs: +rowSpan - 1, cs: +colSpan - 1, r, c };
@@ -824,8 +802,8 @@ export function handelExcelToJson(html: string) {
return data;
}
-function getStyles(styleText: string) {
- let output = {};
+function getStyles(styleText: string): IKeyValue {
+ let output: IKeyValue = {};
const string = styleText.replaceAll('<!--', '').replaceAll('-->', '').trim();
const style = string?.replaceAll('\t', '').replaceAll('\n', '').split('}');
for (let i = 0; i < style.length; i++) {
diff --git a/packages/core/src/Shared/Migrate/Migrate.ts b/packages/core/src/Shared/Migrate/Migrate.ts
index c780d82b78..836a57838b 100644
--- a/packages/core/src/Shared/Migrate/Migrate.ts
+++ b/packages/core/src/Shared/Migrate/Migrate.ts
@@ -10,9 +10,9 @@ import {
ICellData,
ITextStyle,
IStyleData,
- IElement,
IRowData,
IColumnData,
+ ITextRun,
} from '../../Interfaces';
import { ObjectArrayPrimitiveType } from '../ObjectArray';
import { Tools } from '../Tools';
@@ -188,7 +188,8 @@ export function migrate(config: any): Partial<IWorkbookConfig> {
newSheet.cellData[cellItem.r][cellItem.c];
if (cell?.ct?.t === 'inlineStr') {
- const elements: IElement[] = [];
+ const textRuns: ITextRun[] = [];
+ let dataStream = '';
cell.ct.s.forEach((element: any) => {
const textStyle: ITextStyle = {};
@@ -241,33 +242,23 @@ export function migrate(config: any): Partial<IWorkbookConfig> {
};
}
- const eId = Tools.generateRandomId(6);
- elements.push({
- eId,
+ const sId = Tools.generateRandomId(6);
+ dataStream += element.v;
+ textRuns.push({
+ sId,
st: 0,
ed: element.v.length - 1,
- et: 0,
- tr: {
- ct: element.v,
- ts: textStyle,
- },
+ ts: textStyle,
});
});
+ dataStream += '\r\n';
+
newCell.p = {
id: Tools.generateRandomId(6),
body: {
- blockElements: [
- {
- blockId: 'p1',
- st: 0,
- ed: cell.ct.s.length - 1,
- blockType: 0,
- paragraph: {
- elements,
- },
- },
- ],
+ dataStream,
+ textRuns,
},
documentStyle: {},
};
diff --git a/packages/core/src/Sheets/Apply/SetRangeStyle.ts b/packages/core/src/Sheets/Apply/SetRangeStyle.ts
index 09c5ed61ce..58d16f2ba8 100644
--- a/packages/core/src/Sheets/Apply/SetRangeStyle.ts
+++ b/packages/core/src/Sheets/Apply/SetRangeStyle.ts
@@ -1,6 +1,6 @@
import { ITextStyle } from '../../Interfaces/IDocumentData';
import { Styles } from '../Domain';
-import { IRangeData, ICellData, IDocumentData, ITextRun } from '../../Interfaces';
+import { IRangeData, ICellData, IDocumentData } from '../../Interfaces';
import { IBorderData, IStyleData } from '../../Interfaces/IStyleData';
import {
IKeyValue,
@@ -193,37 +193,55 @@ export function mergeRichTextStyle(
p: IDocumentData,
newStyle: Nullable<IStyleData>
) {
- p.body?.blockElements.forEach((blockElement) => {
- if (blockElement.blockType === 0) {
- const paragraph = blockElement.paragraph;
- paragraph?.elements.forEach((element) => {
- if (!element.tr) {
- element.tr = {};
- }
-
- const textRun = element.tr as ITextRun;
-
- if (!textRun.ts) {
- textRun.ts = {};
- }
+ // p.body?.blockElements.forEach((blockElement) => {
+ // if (blockElement.blockType === 0) {
+ // const paragraph = blockElement.paragraph;
+ // paragraph?.elements.forEach((element) => {
+ // if (!element.tr) {
+ // element.tr = {};
+ // }
+
+ // const textRun = element.tr as ITextRun;
+
+ // if (!textRun.ts) {
+ // textRun.ts = {};
+ // }
+
+ // const oldStyle = textRun.ts;
+
+ // const merge = mergeStyle(
+ // oldStyle as Nullable<IStyleData>,
+ // newStyle,
+ // true
+ // );
+
+ // // then remove null
+ // merge && Tools.removeNull(merge);
+
+ // if (Tools.isEmptyObject(merge)) {
+ // delete textRun.ts;
+ // } else {
+ // textRun.ts = merge as ITextStyle;
+ // }
+ // });
+ // }
+ // });
+ p.body?.textRuns?.forEach((textRun) => {
+ if (!textRun.ts) {
+ textRun.ts = {};
+ }
- const oldStyle = textRun.ts;
+ const oldStyle = textRun.ts || {};
- const merge = mergeStyle(
- oldStyle as Nullable<IStyleData>,
- newStyle,
- true
- );
+ const merge = mergeStyle(oldStyle as Nullable<IStyleData>, newStyle, true);
- // then remove null
- merge && Tools.removeNull(merge);
+ // then remove null
+ merge && Tools.removeNull(merge);
- if (Tools.isEmptyObject(merge)) {
- delete textRun.ts;
- } else {
- textRun.ts = merge as ITextStyle;
- }
- });
+ if (Tools.isEmptyObject(merge)) {
+ delete textRun.ts;
+ } else {
+ textRun.ts = merge as ITextStyle;
}
});
}