Currently, you can only listen to the #update. It'd also be cool if you could just retrieve the same information on demand.
Use case – you want to access that information on form submit to validate those values.
@Reinmar there are already two observable properties for WordCount plugin so you can:
const wordCount = editor.plugins.get( 'WordCount' );
console.log( wordCount.words, wordCount.characters );
Nice! I've just stumbled upon them (cause they are not mentioned in the docs ;)).
However, there's a problem with them – they can be outdated. They should be implemented as getters which calculate the character count if the model changed since the last retrieval.
And once we fix the above, let's document them in the feature guide.
Do they need to be getters if you would update after every change?
They are now calculated in a throttled event. So, not after every change.
However, there's a problem with them – they can be outdated. They should be implemented as getters which calculate the character count if the model changed since the last retrieval.
Quick idea: cache them. As we already calculate them in a throttled event we could have these values either taken directly or calculate them if the throttled event is not finished.
So the idea is that a change event listener would set some internal flag, ie this._freshData to false and the throttled method would set this._freshData to true once resolved. This way the getters could either take the words and characters directly or calculate them if needed.
But OTOH - this might be not needed as the getters are rather less frequent use and the calculate method is fast enough.
But OTOH - this might be not needed as the getters are rather less frequent use and the calculate method is fast enough.
Good point. It doesn't seem necessary to cache them. If anyone wants to access them frequently, then they should use the event.
Definitely getters will be needed, otherwise we have a risk of edge case when validation based on our word count will pass content longer than expected.
FYI: I smuggled a new #update event demo in the PR

FYI: I smuggled a new
#updateevent demo in the PR
:thinking: -2 might be confusing there :D but I don't have better idea ;)
Most helpful comment
FYI: I smuggled a new
#updateevent demo in the PR