Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support sheet paste customRange #3537

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion packages/core/src/shared/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,40 @@
*/

import { BaselineOffset, BooleanNumber } from '../types/enum';
import type { IDocumentBody, ITextRun } from '../types/interfaces';
import { CustomRangeType, type IDocumentBody, type ITextRun } from '../types/interfaces';
import { Tools } from './tools';

export function getBodySliceHtml(body: IDocumentBody, startIndex: number, endIndex: number) {
const { customRanges = [] } = body;
const customRangesInRange = customRanges.filter((range) => range.startIndex >= startIndex && range.endIndex <= endIndex);

let cursorIndex = startIndex;
let html = '';
customRangesInRange.forEach((range) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about extensibility, what features should be builtin here? What about other features like mentions, extensions etc.?

const { startIndex, endIndex, rangeType, rangeId } = range;
const preHtml = getBodyInlineSlice(body, cursorIndex, startIndex);
html += preHtml;
const sliceHtml = getBodyInlineSlice(body, startIndex + 1, endIndex);
switch (rangeType) {
case CustomRangeType.HYPERLINK: {
html += `<a data-rangeid="${rangeId}" href="${range.properties?.url ?? ''}">${sliceHtml}</a>`;
break;
}

default: {
html += sliceHtml;
break;
}
}
cursorIndex = endIndex + 1;
});
const endHtml = getBodyInlineSlice(body, cursorIndex, endIndex);
html += endHtml;
return html;
}

// TODO @ybzky Both sheet and doc need to use the same rich text parsing logic.
function getBodyInlineSlice(body: IDocumentBody, startIndex: number, endIndex: number): string {
const { dataStream, textRuns = [] } = body;
let cursorIndex = startIndex;
const spanList: string[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ exports[`Test clipboard > Test paste > test convert util func from excel 1`] = `
"colSpan": 1,
"p": {
"body": {
"customRanges": [],
"dataStream": " background color
",
"paragraphs": [
Expand Down Expand Up @@ -532,6 +533,7 @@ exports[`Test clipboard > Test paste > test convert util func from excel 1`] = `
"colSpan": 1,
"p": {
"body": {
"customRanges": [],
"dataStream": " U N I V ER UP UP UP
",
"paragraphs": [
Expand Down Expand Up @@ -3277,6 +3279,7 @@ exports[`Test clipboard > Test paste > test convert util func from google 1`] =
"colSpan": 1,
"p": {
"body": {
"customRanges": [],
"dataStream": " UNIVER UP UP UP
",
"paragraphs": [
Expand Down Expand Up @@ -3564,6 +3567,7 @@ exports[`Test clipboard > Test paste > test convert util func from google 1`] =
"colSpan": 1,
"p": {
"body": {
"customRanges": [],
"dataStream": " text wrap wrap wrap wrap wrap wrap wrap wrap wrap
",
"paragraphs": [
Expand Down Expand Up @@ -4124,6 +4128,7 @@ exports[`Test clipboard > Test paste > test convert util func from google 1`] =
"colSpan": 1,
"p": {
"body": {
"customRanges": [],
"dataStream": "
",
"paragraphs": [
Expand Down Expand Up @@ -4189,6 +4194,7 @@ exports[`Test clipboard > Test paste > test convert util func from google 1`] =
"colSpan": 1,
"p": {
"body": {
"customRanges": [],
"dataStream": " univer
",
"paragraphs": [
Expand Down Expand Up @@ -4668,6 +4674,7 @@ exports[`Test clipboard > Test paste > test convert util func from google 1`] =
"colSpan": 2,
"p": {
"body": {
"customRanges": [],
"dataStream": "
",
"paragraphs": [
Expand Down Expand Up @@ -4733,6 +4740,7 @@ exports[`Test clipboard > Test paste > test convert util func from google 1`] =
"colSpan": 2,
"p": {
"body": {
"customRanges": [],
"dataStream": " univer
",
"paragraphs": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@ export class HtmlToUSMService {
} else if (node.nodeType === Node.COMMENT_NODE || node.nodeName === 'STYLE') {
continue;
} else if (node.nodeType === Node.ELEMENT_NODE) {
const element = node as HTMLElement;
const linkStart = this._processBeforeLink(element, { body: doc });
const currentNodeStyle = this._getStyle(node as HTMLElement, styleStr);
const parentStyles = parent ? styleCache.get(parent) : {};
const predefinedStyles = turnToStyleObject(currentNodeStyle);
Expand All @@ -462,6 +464,7 @@ export class HtmlToUSMService {
styleCache.set(node, { ...parentStyles, ...nodeStyles });
const { childNodes } = node;
this._parseCellHtml(node, childNodes, doc, styleCache, currentNodeStyle);
this._processAfterLink(element, { body: doc }, linkStart);
}
}
}
Expand All @@ -476,16 +479,26 @@ export class HtmlToUSMService {
textRuns: [],
};
// Rich text parsing method, refer to the doc
// this.process(null, cell.childNodes!, newDocBody, []);
this._parseCellHtml(null, cell.childNodes, newDocBody, undefined, styleStr);
const documentModel = skeleton.getBlankCellDocumentModel()?.documentModel;
const p = documentModel?.getSnapshot();
const cellCustomRanges: ICustomRange[] = [];
newDocBody?.customRanges?.forEach((c) => {
cellCustomRanges.push({
...c,
startIndex: c.startIndex,
endIndex: c.endIndex,
});
});
const singleDataStream = `${newDocBody.dataStream}\r\n`;
const documentData = {
...p, ...{
body: {
dataStream: singleDataStream,
textRuns: newDocBody.textRuns,
paragraphs: generateParagraphs(singleDataStream),
customRanges: cellCustomRanges,
},
},
};
Expand Down
Loading