Skip to content

Commit

Permalink
fix: some case rich text style should convert to cell style (#3975)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybzky authored Nov 6, 2024
1 parent 2272eae commit 47cae07
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
17 changes: 5 additions & 12 deletions packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
32 changes: 31 additions & 1 deletion packages/sheets-ui/src/services/clipboard/html-to-usm/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
}

0 comments on commit 47cae07

Please sign in to comment.