Skip to content

Commit

Permalink
refactor: transform in body
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs committed Oct 29, 2024
1 parent aeb987a commit 427e9ef
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe('transform paragraph in body', () => {
expect(composedAction1).toEqual(composedAction2);
});

it('should pass test when REPLACE + COVER, and REPLACE has high priority', () => {
it('should pass test when REPLACE + COVER', () => {
const actionsA: TextXAction[] = [
{
t: TextXActionType.RETAIN,
Expand Down Expand Up @@ -269,18 +269,40 @@ describe('transform paragraph in body', () => {
const doc3 = getDefaultDocWithParagraph();
const doc4 = getDefaultDocWithParagraph();

const resultA = TextX.apply(TextX.apply(doc1, actionsA), TextX.transform(actionsB, actionsA, 'left'));
const resultB = TextX.apply(TextX.apply(doc2, actionsB), TextX.transform(actionsA, actionsB, 'right'));
const resultA = TextX.apply(TextX.apply(doc1, actionsA), TextX.transform(actionsB, actionsA, 'right'));
const resultB = TextX.apply(TextX.apply(doc2, actionsB), TextX.transform(actionsA, actionsB, 'left'));

const composedAction1 = TextX.compose(actionsA, TextX.transform(actionsB, actionsA, 'left'));
const composedAction2 = TextX.compose(actionsB, TextX.transform(actionsA, actionsB, 'right'));
const composedAction1 = TextX.compose(actionsA, TextX.transform(actionsB, actionsA, 'right'));
const composedAction2 = TextX.compose(actionsB, TextX.transform(actionsA, actionsB, 'left'));

const resultC = TextX.apply(doc3, composedAction1);
const resultD = TextX.apply(doc4, composedAction2);

// console.log(JSON.stringify(resultA, null, 2));
// console.log(JSON.stringify(resultB, null, 2));

expect(resultA).toEqual(resultB);
expect(resultC).toEqual(resultD);
expect(resultA).toEqual(resultC);
expect(composedAction1).toEqual(composedAction2);

const doc5 = getDefaultDocWithParagraph();
const doc6 = getDefaultDocWithParagraph();
const doc7 = getDefaultDocWithParagraph();
const doc8 = getDefaultDocWithParagraph();

const resultE = TextX.apply(TextX.apply(doc5, actionsA), TextX.transform(actionsB, actionsA, 'left'));
const resultF = TextX.apply(TextX.apply(doc6, actionsB), TextX.transform(actionsA, actionsB, 'right'));

const composedAction3 = TextX.compose(actionsA, TextX.transform(actionsB, actionsA, 'left'));
const composedAction4 = TextX.compose(actionsB, TextX.transform(actionsA, actionsB, 'right'));

const resultG = TextX.apply(doc7, composedAction3);
const resultH = TextX.apply(doc8, composedAction4);

expect(resultE).toEqual(resultF);
expect(resultG).toEqual(resultH);
expect(resultE).toEqual(resultG);
expect(composedAction3).toEqual(composedAction4);
});
});
22 changes: 19 additions & 3 deletions packages/core/src/docs/data-model/text-x/transform-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function transformCustomRanges(
// paragraphStyle is treated separately, while bullet is treated as a whole because
// the properties in bullet are related

// eslint-disable-next-line complexity
// eslint-disable-next-line complexity, max-lines-per-function
function transformParagraph(
originParagraph: IParagraph,
targetParagraph: IParagraph,
Expand Down Expand Up @@ -206,8 +206,16 @@ function transformParagraph(
const keys = Object.keys(originParagraph.paragraphStyle);

for (const key of keys) {
if (paragraph.paragraphStyle[key as keyof IParagraphStyle]) {
delete paragraph.paragraphStyle[key as keyof IParagraphStyle];
if (originParagraph.paragraphStyle[key as keyof IParagraphStyle] !== undefined) {
paragraph.paragraphStyle[key as keyof IParagraphStyle] = originParagraph.paragraphStyle[key as keyof IParagraphStyle] as any;
}
}
} else {
const keys = Object.keys(originParagraph.paragraphStyle);

for (const key of keys) {
if (paragraph.paragraphStyle[key as keyof IParagraphStyle] === undefined) {
paragraph.paragraphStyle[key as keyof IParagraphStyle] = originParagraph.paragraphStyle[key as keyof IParagraphStyle] as any;
}
}
}
Expand All @@ -222,6 +230,14 @@ function transformParagraph(
paragraph.paragraphStyle[key as keyof IParagraphStyle] = originParagraph.paragraphStyle[key as keyof IParagraphStyle] as any;
}
}
} else {
const keys = Object.keys(originParagraph.paragraphStyle);

for (const key of keys) {
if (paragraph.paragraphStyle[key as keyof IParagraphStyle] === undefined) {
paragraph.paragraphStyle[key as keyof IParagraphStyle] = originParagraph.paragraphStyle[key as keyof IParagraphStyle] as any;
}
}
}
} else {
if (transformType === TextXTransformType.COVER_ONLY_NOT_EXISTED) {
Expand Down

0 comments on commit 427e9ef

Please sign in to comment.