Skip to content

Commit

Permalink
Merge pull request #10457 from ckeditor/ck/10302
Browse files Browse the repository at this point in the history
Fix (find-and-replace): Improved the performance of the find feature by reducing the number of `model.change()` calls. Closes #10302.
  • Loading branch information
oleq authored Aug 31, 2021
2 parents ca33e93 + 58ccc5a commit 8d425ed
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
30 changes: 15 additions & 15 deletions packages/ckeditor5-find-and-replace/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ import { escapeRegExp } from 'lodash-es';
export function updateFindResultFromRange( range, model, findCallback, startResults ) {
const results = startResults || new Collection();

[ ...range ].forEach( ( { type, item } ) => {
if ( type === 'elementStart' ) {
if ( model.schema.checkChild( item, '$text' ) ) {
const foundItems = findCallback( {
item,
text: rangeToText( model.createRangeIn( item ) )
} );

if ( !foundItems ) {
return;
}
model.change( writer => {
[ ...range ].forEach( ( { type, item } ) => {
if ( type === 'elementStart' ) {
if ( model.schema.checkChild( item, '$text' ) ) {
const foundItems = findCallback( {
item,
text: rangeToText( model.createRangeIn( item ) )
} );

if ( !foundItems ) {
return;
}

foundItems.forEach( foundItem => {
model.change( writer => {
foundItems.forEach( foundItem => {
const resultId = `findResult:${ uid() }`;
const marker = writer.addMarker( resultId, {
usingOperation: false,
Expand All @@ -68,9 +68,9 @@ export function updateFindResultFromRange( range, model, findCallback, startResu
index
);
} );
} );
}
}
}
} );
} );

return results;
Expand Down
11 changes: 11 additions & 0 deletions packages/ckeditor5-find-and-replace/tests/findcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ describe( 'FindCommand', () => {
);
} );

it( 'calls model.change() only once', () => {
setData( model, '<paragraph>[]Foo bar baz. Bam bar bar bar bar bom.</paragraph>' );
const spy = sinon.spy( model, 'change' );

command.execute( 'bar' );

// It's called two additional times
// from 'change:highlightedResult' handler in FindAndReplaceEditing.
expect( spy.callCount ).to.equal( 3 );
} );

it( 'returns no result if nothing matched', () => {
setData( model, '<paragraph>[]Foo bar baz. Bam bar bom.</paragraph>' );

Expand Down

0 comments on commit 8d425ed

Please sign in to comment.