How do I calculate the number of new words in the text?

I’m working on a text locking feature that allows me to exit if I don’t enter the word count I need, and now my difficulty is how to count the number of new text words in real time, including deletions and changes, but not the original existing text, how can I get this knowledge? I hope my description is clear, thanks for the excellent project :slightly_smiling_face:

if (tr.docChanged) {
                const doc = newState.doc;
                let totalCount = value.totalCount;
                if (tr.getMeta('paste')) {
                    return {totalCount };
                }
                const oldText = tr.before.content.textBetween(0, tr.before.content.size, ' ').replace(/[^\u4E00-\u9FA5]/g, '');
                const newText = doc.content.textBetween(0, newState.doc.content.size, ' ').replace(/[^\u4E00-\u9FA5]/g, '');
                const diff = newText.length - oldText.length;
                if (diff > 0) {
                    totalCount += diff;
                } else {
                    totalCount -= Math.abs(diff);
                    if (totalCount < 0) totalCount = 0;
                }
                return { totalCount };
            }

I solved it!!!