Skip to content

Ckeditor select all occurrences of the word given #4476

Description

@itg-assistant

Eray Yang asked 2024-02-15 14:32:39 UTC

First of all, I tried to use the Find and Replace plugin from the link below  and attempted to find it using the command

editor.execute('find', 'steam');

but I couldn't succeed.

https://ckeditor.com/docs/ckeditor5/latest/features/find-and-replace.html

Secondly, when I tried the following code from this link with

ckeditor.eval

, it only finds the first match but cannot find the other matches."

string eval = @"// Note: your instance name may differ.
var editor = this.editor,
selection = editor.getSelection(),
root = selection.root,
textNodes = [],
ranges = [],
range, text, index;

function getTextNodes( element ) {
var children = element.getChildren(),
child;

for ( var i = children.count(); i--; ) {
child = children.getItem( i );
if ( child.type == CKEDITOR.NODE_ELEMENT )
getTextNodes( child );
else if ( child.type == CKEDITOR.NODE_TEXT )
textNodes.push( child );
}
}

// Recursively search for text nodes starting from root.
// You may want to search a specific branch starting from other element.
getTextNodes( root );

// Iterate over found text nodes. If some contains
// phrase ""the"", create a range that selects this word.
for ( i = textNodes.length; i--; ) {
text = textNodes[ i ];
index = text.getText().indexOf( 'erol' );
if ( index > -1 ) {
range = editor.createRange();
range.setStart( text, index );
// Note: 3 is fixed length of ""the"". You may want to change it.
range.setEnd( text, index + 4 );
ranges.push( range );
}
}

// Select all ranges ""containing"" phrase ""the"".
selection.selectRanges( ranges );";
ckEditor1.Eval(eval);

Is there an easier way to find all the matches?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    GeneralQuestions, how-tos and everything else.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions