Skip to content

Commit

Permalink
refactor: transform textRuns
Browse files Browse the repository at this point in the history
  • Loading branch information
Jocs committed Oct 30, 2024
1 parent ee65e42 commit eb2e068
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,241 @@ describe('transform textRun in body', () => {
expect(resultA).toEqual(resultC);
expect(composedAction1).toEqual(composedAction2);
});

it('should pass test when REPLACE + COVER', () => {
const actionsA: TextXAction[] = [
{
t: TextXActionType.RETAIN,
len: 1,
coverType: UpdateDocsAttributeType.REPLACE,
body: {
dataStream: '',
textRuns: [{
st: 0,
ed: 1,
ts: {
it: BooleanNumber.TRUE,
fs: 10,
},
}],
},
},
];

const actionsB: TextXAction[] = [
{
t: TextXActionType.RETAIN,
len: 1,
coverType: UpdateDocsAttributeType.COVER,
body: {
dataStream: '',
textRuns: [{
st: 0,
ed: 1,
ts: {
bl: BooleanNumber.TRUE,
it: BooleanNumber.FALSE,
fs: 14,
},
}],
},
},
];

const expectedTransformedActionFalse: TextXAction[] = [
{
t: TextXActionType.RETAIN,
len: 1,
coverType: UpdateDocsAttributeType.COVER,
body: {
dataStream: '',
textRuns: [{
st: 0,
ed: 1,
ts: {
bl: BooleanNumber.TRUE,
},
}],
},
},
];

const expectedTransformedActionTrue: TextXAction[] = [
{
t: TextXActionType.RETAIN,
len: 1,
coverType: UpdateDocsAttributeType.COVER,
body: {
dataStream: '',
textRuns: [{
st: 0,
ed: 1,
ts: {
bl: BooleanNumber.TRUE,
it: BooleanNumber.FALSE,
fs: 14,
},
}],
},
},
];

expect(TextX.transform(actionsB, actionsA, 'left')).toEqual(expectedTransformedActionTrue);
expect(TextX.transform(actionsB, actionsA, 'right')).toEqual(expectedTransformedActionFalse);

const doc1 = getDefaultDocWithTextRuns();
const doc2 = getDefaultDocWithTextRuns();
const doc3 = getDefaultDocWithTextRuns();
const doc4 = getDefaultDocWithTextRuns();

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 composedAction1 = TextX.compose(actionsA, TextX.transform(actionsB, actionsA, 'left'));
const composedAction2 = TextX.compose(actionsB, TextX.transform(actionsA, actionsB, 'right'));

// console.log(JSON.stringify(TextX.transform(actionsA, actionsB, 'right'), null, 2));
// console.log('resultA');
// console.log(JSON.stringify(resultA, null, 2));
// console.log('resultB');
// console.log(JSON.stringify(resultB, null, 2));
// console.log(JSON.stringify(composedAction1, null, 2));
// console.log(JSON.stringify(composedAction2, null, 2));

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

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

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

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

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

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);
});

it('should pass test when COVER + COVER', () => {
const actionsA: TextXAction[] = [
{
t: TextXActionType.RETAIN,
len: 1,
coverType: UpdateDocsAttributeType.COVER,
body: {
dataStream: '',
textRuns: [{
st: 0,
ed: 1,
ts: {
it: BooleanNumber.TRUE,
fs: 10,
ul: {
s: BooleanNumber.TRUE,
},
},
}],
},
},
];

const actionsB: TextXAction[] = [
{
t: TextXActionType.RETAIN,
len: 1,
coverType: UpdateDocsAttributeType.COVER,
body: {
dataStream: '',
textRuns: [{
st: 0,
ed: 1,
ts: {
bl: BooleanNumber.TRUE,
it: BooleanNumber.FALSE,
fs: 14,
},
}],
},
},
];

const expectedTransformedActionFalse: TextXAction[] = [
{
t: TextXActionType.RETAIN,
len: 1,
coverType: UpdateDocsAttributeType.COVER,
body: {
dataStream: '',
textRuns: [{
st: 0,
ed: 1,
ts: {
bl: BooleanNumber.TRUE,
},
}],
},
},
];

const expectedTransformedActionTrue: TextXAction[] = [
{
t: TextXActionType.RETAIN,
len: 1,
coverType: UpdateDocsAttributeType.COVER,
body: {
dataStream: '',
textRuns: [{
st: 0,
ed: 1,
ts: {
bl: BooleanNumber.TRUE,
it: BooleanNumber.FALSE,
fs: 14,
},
}],
},
},
];

expect(TextX.transform(actionsB, actionsA, 'left')).toEqual(expectedTransformedActionTrue);
expect(TextX.transform(actionsB, actionsA, 'right')).toEqual(expectedTransformedActionFalse);

const doc1 = getDefaultDocWithTextRuns();
const doc2 = getDefaultDocWithTextRuns();
const doc3 = getDefaultDocWithTextRuns();
const doc4 = getDefaultDocWithTextRuns();

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 composedAction1 = TextX.compose(actionsA, TextX.transform(actionsB, actionsA, 'left'));
const composedAction2 = TextX.compose(actionsB, TextX.transform(actionsA, actionsB, 'right'));

// console.log(JSON.stringify(TextX.transform(actionsB, actionsA, 'left'), null, 2));
// console.log(JSON.stringify(TextX.transform(actionsA, actionsB, 'right'), null, 2));
// console.log(JSON.stringify(composedAction1, null, 2));
// console.log(JSON.stringify(composedAction2, null, 2));

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

expect(resultA).toEqual(resultB);
expect(resultC).toEqual(resultD);
expect(resultA).toEqual(resultC);
expect(composedAction1).toEqual(composedAction2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class ActionIterator {
if (nextAction) {
const offset = this._offset;
const actionLength = nextAction.len;

if (length >= actionLength - offset) {
// Return the action if the length is great than action length, and reset offset to 0.
length = actionLength - offset;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/docs/data-model/text-x/transform-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function transformTextRuns(
const keys = Object.keys(originStyle);

for (const key of keys) {
if (originStyle[key as keyof ITextStyle]) {
if (originStyle[key as keyof ITextStyle] !== undefined) {
newTs[key as keyof ITextStyle] = originStyle[key as keyof ITextStyle] as any;
}
}
Expand All @@ -116,7 +116,7 @@ function transformTextRuns(
const keys = Object.keys(originStyle);

for (const key of keys) {
if (newTs[key as keyof ITextStyle]) {
if (newTs[key as keyof ITextStyle] !== undefined) {
delete newTs[key as keyof ITextStyle];
}
}
Expand Down

0 comments on commit eb2e068

Please sign in to comment.