Skip to content

Commit

Permalink
add tests for embeds in getLastChangeIndex tests
Browse files Browse the repository at this point in the history
  • Loading branch information
siltomato committed Feb 25, 2025
1 parent c12662c commit dabfda4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,36 @@ describe('Quill history', () => {
name: 'retain followed by insert',
delta: new Delta().retain(3).insert('test'),
expected: 7
},
{
name: 'single embed insert',
delta: new Delta().insert({ image: 'test.jpg' }),
expected: 0
},
{
name: 'text followed by embed',
delta: new Delta().insert('hello').insert({ image: 'test.jpg' }),
expected: 5
},
{
name: 'embed followed by text',
delta: new Delta().insert({ image: 'test.jpg' }).insert('hello'),
expected: 6
},
{
name: 'mixed text and embeds with trailing text',
delta: new Delta().insert({ blank: true }).insert('text').insert({ image: 'test.jpg' }).insert('more'),
expected: 10
},
{
name: 'mixed text and embeds with trailing embed',
delta: new Delta().insert({ blank: true }).insert('text').insert({ image: 'test.jpg' }),
expected: 5
},
{
name: 'retain followed by embed',
delta: new Delta().retain(3).insert({ image: 'test.jpg' }),
expected: 3
}
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function removeObsoleteSegmentAttrs(delta: Delta): Delta {

/**
* Finds the index where the last insert/delete occurs in the delta. This function has been modified from the
* original in the Quill history module.
* original in the Quill history module. Trailing inserted embeds ops are not counted when determining the last edit.
*
* @param {QuillScrollBlot} scroll The Quill scroll.
* @param {Delta} delta The undo/redo delta.
Expand All @@ -102,7 +102,7 @@ export function getLastChangeIndex(scroll: QuillScrollBlot, delta: Delta): numbe
if (delta.ops == null) {
return 0;
}
// skip inserted embeds when determining last edit
// Skip trailing inserted embed ops when determining last edit
let changeIndex = 0;
let curIndex = 0;
for (const op of delta.ops) {
Expand All @@ -111,7 +111,7 @@ export function getLastChangeIndex(scroll: QuillScrollBlot, delta: Delta): numbe
curIndex += op.insert.length;
changeIndex = curIndex;
} else {
curIndex++;
curIndex++; // Won't be assigned to 'changeIndex' if it is a trailing embed op
}
} else if (op.retain != null) {
const retainCount: number = getRetainCount(op)!;
Expand Down

0 comments on commit dabfda4

Please sign in to comment.