Skip to content

Commit

Permalink
1. convert with editingDowncast;
Browse files Browse the repository at this point in the history
2. get correct position of inline widget;
  • Loading branch information
wisewrong committed Jun 6, 2021
1 parent bf56805 commit 4bd4d44
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
21 changes: 12 additions & 9 deletions src/findCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,18 @@ export default class FindCommand extends Command {
let counter = 0;
model.change( writer => {
for ( const element of root.getChildren() ) {
const text = getText( element );
const indices = getIndicesOf( searchText, text, matchCase );
for ( const index of indices ) {
const label = SEARCH_MARKER + ':' + searchText + ':' + counter++;
const start = writer.createPositionAt( element, index );
const end = writer.createPositionAt( element, index + searchText.length );
const range = writer.createRange( start, end );
writer.addMarker( label, { range, usingOperation: false } );
}
getText( element, ( textNode ) => {
// get correct position of inline widget
const { parent, startOffset, data } = textNode;
const indices = getIndicesOf( searchText, data, matchCase );
for ( const index of indices ) {
const label = SEARCH_MARKER + ':' + searchText + ':' + counter++;
const start = writer.createPositionAt( parent, index + startOffset );
const end = writer.createPositionAt( parent, index + startOffset + searchText.length );
const range = writer.createRange( start, end );
writer.addMarker( label, { range, usingOperation: false } );
}
});
}
// update markers variable after search
markers = Array.from( model.markers.getMarkersGroup( SEARCH_MARKER ) );
Expand Down
4 changes: 2 additions & 2 deletions src/findReplaceEditing.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export default class FindReplaceEditing extends Plugin {
const editor = this.editor;

// conversion between model and view
editor.conversion.for( 'downcast' ).markerToHighlight(
editor.conversion.for( 'editingDowncast' ).markerToHighlight(
{ model: SEARCH_MARKER, view: () => ( { classes: 'search-item' } ) } );
editor.conversion.for( 'downcast' ).markerToHighlight(
editor.conversion.for( 'editingDowncast' ).markerToHighlight(
{ model: CURRENT_SEARCH_MARKER, view: () => ( { classes: 'current', priority: 99 } ) } );

// add command
Expand Down
5 changes: 3 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ export function removeCurrentSearchMarker( model, writer ) {
* @param {*} node model node
* @returns {string} the whole text of the node
*/
export function getText( node ) {
export function getText( node, callback ) {
let str = '';
if ( node.is( 'text' ) ) {
str += node.data;
typeof callback === 'function' && callback( node );
} else {
const children = Array.from( node.getChildren() );
for ( const child of children ) {
str += getText( child );
str += getText( child, callback );
}
}
return str;
Expand Down

0 comments on commit 4bd4d44

Please sign in to comment.