Prevent users from adding new content to input after they reached the limit

My task is prevent users from adding new content to input after they reached the limit that I provide.

I’ve tried to create plugin using filterTransaction:

function makePreventTypingPlugin(hard_limit) {
        return new _prosemirrorState.Plugin({
            filterTransaction: function (transaction, state) {
                const new_state = state.apply(transaction);
                const fragment = ProseMirror.modelModule.DOMSerializer.fromSchema(new_state.schema).serializeFragment(new_state.doc);
                // here i can calculate length of content using fragment and return true or false
            }
        });
    }

But (as i understood) “state.apply(transaction)” will call filterTransaction and it will cause “Maximum call stack size exceeded”

Could you please push me in the right direction, how to calculate whole input content length every time when user doing something and prevent user from adding new content when hard_limit of content is reached?

Thank you

You could iterate over the changes the transaction, applying them to the document (not to the whole state). That’s still pretty inefficient, but should work at least.