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

refactor: remove endIndex in ICustomTable #4137

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ function getDefaultDocWithCustomRange() {
return doc;
}

function getEmptyDoc() {
const doc: IDocumentBody = {
dataStream: '\r\n',
textRuns: [],
tables: [],
paragraphs: [
{
startIndex: 0,
},
],
sectionBreaks: [
{
startIndex: 1,
},
],
};

return doc;
}

describe('apply consistency', () => {
it('should get the same result when compose delete action with body', () => {
const actionsA: TextXAction[] = [
Expand Down Expand Up @@ -151,4 +171,126 @@ describe('apply consistency', () => {
expect(resultA).toEqual(resultC);
expect(composedAction1).toEqual(composedAction2);
});

it('should pass test when compose inert table and insert text', () => {
const actionA: TextXAction[] = [{
t: TextXActionType.INSERT,
len: 1,
body: {
dataStream: '\r',
paragraphs: [
{
startIndex: 0,
paragraphStyle: {
spaceAbove: {
v: 10,
},
lineSpacing: 2,
spaceBelow: {
v: 0,
},
},
},
],
},
}, {
t: TextXActionType.INSERT,
body: {
dataStream: '\u001A\u001B\u001C\r\n\u001D\u001C\r\n\u001D\u000E\u000F',
paragraphs: [
{
startIndex: 3,
paragraphStyle: {
spaceAbove: {
v: 3,
},
lineSpacing: 2,
spaceBelow: {
v: 0,
},
},
},
{
startIndex: 7,
paragraphStyle: {
spaceAbove: {
v: 3,
},
lineSpacing: 2,
spaceBelow: {
v: 0,
},
},
},
],
sectionBreaks: [
{
startIndex: 4,
},
{
startIndex: 8,
},
],
textRuns: [
{
st: 0,
ed: 12,
ts: {
ff: 'Arial',
fs: 11,
},
},
],
tables: [
{
startIndex: 0,
endIndex: 12,
tableId: 'JfhiKo',
},
],
},
len: 12,
}];
const actionB: TextXAction[] = [{
t: TextXActionType.RETAIN,
len: 4,
}, {
t: TextXActionType.INSERT,
len: 1,
body: {
dataStream: 'w',
textRuns: [
{
st: 0,
ed: 1,
ts: {
ff: 'Arial',
fs: 11,
},
},
],
tables: [{
startIndex: 0,
endIndex: 1,
tableId: 'JfhiKo',
}],
customRanges: [],
customDecorations: [],
},
}];

const doc1 = getEmptyDoc();
const doc2 = getEmptyDoc();

// console.log(JSON.stringify(TextX.compose(actionA, actionB), null, 2));

const resultA = TextX.apply(TextX.apply(doc1, actionA), actionB);
const resultB = TextX.apply(doc2, TextX.compose(actionA, actionB));
// console.log('result 1');
// console.log(JSON.stringify(resultA, null, 2));
// console.log('result 2');
// console.log(JSON.stringify(resultB, null, 2));

expect(resultA).toEqual(resultB);
});
});
30 changes: 23 additions & 7 deletions packages/core/src/docs/data-model/text-x/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,33 @@ export function getBodySlice(
const clonedTable = Tools.deepClone(table);
const { startIndex, endIndex } = clonedTable;

if (startIndex >= startOffset && endIndex <= endOffset) {
newTables.push({
...clonedTable,
startIndex: startIndex - startOffset,
endIndex: endIndex - startOffset,
});
if (Tools.hasIntersectionBetweenTwoRanges(startIndex, endIndex, startOffset, endOffset)) {
if (startOffset >= startIndex && startOffset <= endIndex) {
newTables.push({
...clonedTable,
startIndex: startOffset,
endIndex: Math.min(endOffset, endIndex),
});
} else if (endOffset >= startIndex && endOffset <= endIndex) {
newTables.push({
...clonedTable,
startIndex: Math.max(startOffset, startIndex),
endIndex: endOffset,
});
} else {
newTables.push(clonedTable);
}
}
}

if (newTables.length) {
docBody.tables = newTables;
docBody.tables = newTables.map((t) => {
return {
...t,
startIndex: t.startIndex - startOffset,
endIndex: t.endIndex - startOffset,
};
});
}

const newParagraphs: IParagraph[] = [];
Expand Down
Loading