Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/plain-ads-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sillsdev/lynx-delta': patch
---

Fix crash when adding a paragraph to the end of a ScriptureDeltaDocument
36 changes: 36 additions & 0 deletions packages/delta/src/scripture-delta-document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,40 @@ describe('ScriptureDeltaDocument', () => {
expect(paragraph1.getText()).toEqual(`\ufffcVerse one.\ufffcVerse two.\n`);
expect(paragraph1.range).toEqual({ start: { line: 1, character: 0 }, end: { line: 2, character: 0 } });
});

it('add paragraph at end of document', () => {
const document = new ScriptureDeltaDocument(
'uri',
'scr-delta',
1,
new Delta()
.insert({ chapter: { number: '1', style: 'c' } })
.insert({ verse: { number: '1', style: 'v' } })
.insert('This is a test.', { segment: 'verse_1_1' })
.insert('\n', { para: { style: 'p' } })
.insert({ verse: { number: '2', style: 'v' } })
.insert('This is a test.', { segment: 'verse_1_2' })
.insert('\n', { para: { style: 'p' } }),
);

expect(document.children.length).toEqual(3);

document.update(
new Delta()
.retain(35)
.insert('section header.', { segment: 's_1' })
.insert('\n', { para: { style: 's' } }),
2,
);

expect(document.children.length).toEqual(4);

const paragraph2 = document.children[2];
expect(paragraph2.getText()).toEqual(`\ufffcThis is a test.\n`);
expect(paragraph2.range).toEqual({ start: { line: 2, character: 0 }, end: { line: 3, character: 0 } });

const paragraph3 = document.children[3];
expect(paragraph3.getText()).toEqual(`section header.\n`);
expect(paragraph3.range).toEqual({ start: { line: 3, character: 0 }, end: { line: 4, character: 0 } });
});
});
15 changes: 10 additions & 5 deletions packages/delta/src/scripture-delta-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,14 @@ export class ScriptureDeltaDocument extends ScriptureDocumentMixin(DeltaDocument
let lineChildren = this.lineChildren!;
const childStartIndex = lineChildren[changeStartLine];
const childEndIndex = lineChildren[changeEndLine];
const childStart = this.children[childStartIndex];
const childStartLine = childStart.range.start.line;
// const childEndLine = this.children[childEndIndex].range.end.line;
const childStartLine =
childStartIndex < this.children.length
? this.children[childStartIndex].range.start.line
: lineChildren.length - 1;
const childStartPosition =
childStartIndex < this.children.length
? this.children[childStartIndex].range.start
: this.children[this.children.length - 1].range.end;
const childEndLine =
childEndIndex < this.children.length ? this.children[childEndIndex].range.end.line : lineChildren.length - 1;

Expand All @@ -69,8 +74,8 @@ export class ScriptureDeltaDocument extends ScriptureDocumentMixin(DeltaDocument
addedLineOffsets,
addedLineChildren,
addedLineOps,
childStart.range.start,
this.offsetAt(childStart.range.start),
childStartPosition,
this.offsetAt(childStartPosition),
childStartIndex,
opStartIndex,
);
Expand Down