Reputation: 37
I'm trying to find an element in the model based on his id. Looking through the documentation, it seems that the Matcher class can do what I need.
However, I'm unable to create a Matcher instance, and all the examples there show only how to use Matcher, and not how to create the instance.
Sorry if it is a dumb question, but how do I create a new Matcher instance?
Upvotes: 1
Views: 295
Reputation: 683
If you want to operate over a model, then you should use classes related to engine/model
. Mentioned by you Matcher
belongs to engine/view
so it doesn't fit to model operation.
If you want walk through model and operate over it, then you can use TreeWalker class. You just need to create range over entire model, or create start position at the beginning.
Another option could be obtaining root element and iterate over its children recursively.
You can utilize tree walker for example in such way:
const txt = ( editor => {
const position = new Position( editor.model.document.getRoot(), [ 0 ] );
const walker = new TreeWalker( { startPosition: position } );
for ( const element of walker ) {
// do sth with 'element'
// but be careful on element boundaries
}
return outputText;
} )( this.editor );
You can recursive walking over children here, where rootElement
can be used as an entry element.
function getAllTextFromElementAndChildren( element ) {
if( element.is( 'text' ) ){
return elemen.data;
}
let text = '';
for ( const child of element.getChildren() ) {
const childText = getAllTextFromElementAndChildren( child );
text += childText;
}
return text;
}
Upvotes: 1