Skip to content

Uncaught RangeError: Position -1 out of range when creating a new editor instance #1145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Akshdhiwar opened this issue Oct 13, 2024 · 7 comments · Fixed by #1196
Closed
Labels
bug Something isn't working

Comments

@Akshdhiwar
Copy link

Description:
I encountered an Uncaught RangeError: Position -1 out of range error while creating a new editor instance in my project. This happens when a new editor is initialized in "editing mode." Below is the stack trace:

chunk-LTEZDJSH.js?v=1bc2ccfc:7965 Uncaught RangeError: Position -1 out of range
    at _ResolvedPos.resolve (chunk-LTEZDJSH.js?v=1bc2ccfc:7965:13)
    at _ResolvedPos.resolveCached (chunk-LTEZDJSH.js?v=1bc2ccfc:7991:63)
    at _Node.resolve (chunk-LTEZDJSH.js?v=1bc2ccfc:8251:24)
    at new MouseDown (chunk-LTEZDJSH.js?v=1bc2ccfc:19892:33)
    at handlers.mousedown (chunk-LTEZDJSH.js?v=1bc2ccfc:19869:28)
    at view.dom.addEventListener.view.input.eventHandlers.<computed> (chunk-LTEZDJSH.js?v=1bc2ccfc:19660:9)

Steps to Reproduce:

Create a new editor instance using the following code:


// Editor instance
const editor = useMemo(() => {
    let editor: any
    if (isEditing) {
        const doc = new Y.Doc();
        const provider = new YPartyKitProvider(
            "blocknote-dev.yousefed.partykit.dev",
            project?.Id! + selectedFolder?.id,
            doc,
        );
        editor = BNE.create({
            collaboration: {
                provider,
                fragment: doc.getXmlFragment("document-store"),
                user: {
                    name: user?.GithubName!,
                    color: getRandomColor() // blue color
                }
            }
        });
    } else {
        editor = BNE.create({
            initialContent: pageContent
        });
    }

    return editor;
}, [selectedFolder, isEditing, pageContent]);

Expected Behavior:
The editor should initialize properly and allow user interaction without any errors.

Actual Behavior:
The error Position -1 out of range occurs when trying to resolve a position within the document, possibly related to mouse input handling.

It works normal after a re-render of component but on initialization it throws a error.
Please free to give your suggestion on this if I'm doing something wrong or so.

@Akshdhiwar Akshdhiwar added the bug Something isn't working label Oct 13, 2024
@Akshdhiwar
Copy link
Author

Hey @matthewlipski @YousefED can anyone give me some guidance on this bug ???

@trival
Copy link

trival commented Oct 31, 2024

I saw the same error with NextJS 15. Once I added reactStrictMode: false to my next.config.js, as advised in https://www.blocknotejs.org/docs/advanced/nextjs#react-19--next-15-strictmode, the error disappeared.

@Akshdhiwar
Copy link
Author

Hey @trival Thanks for comment but I'm using react. Do you know what same can be done in react ?

@trival
Copy link

trival commented Nov 2, 2024

Hey @trival Thanks for comment but I'm using react. Do you know what same can be done in react ?

I am not sure. It seems the error is raised somewhere in ProseMirror, because some event is not propagated correctly. In my application, NextJS 14 was working just fine. The error started after updating to Next 15 and dissappeared after the configuration change. Apparently the configuration is somehow related to StrictMode. Maybe you or some of your downstream dependencies are using it? Other than that I don't have more clues...

@trival
Copy link

trival commented Nov 2, 2024

Also from StrictMode Docs:

Strict Mode enables the following development-only behaviors:

Your components will re-render an extra time to find bugs caused by impure rendering.
Your components will re-run Effects an extra time to find bugs caused by missing Effect cleanup.
Your components will be checked for usage of deprecated APIs.

Maybe your code does something like that?

@Akshdhiwar
Copy link
Author

Also from StrictMode Docs:

Strict Mode enables the following development-only behaviors:
Your components will re-render an extra time to find bugs caused by impure rendering.
Your components will re-run Effects an extra time to find bugs caused by missing Effect cleanup.
Your components will be checked for usage of deprecated APIs.

Maybe your code does something like that?

Yes there is a re-render when switches into editing mode

// Editor instance
const editor = useMemo(() => {
    let editor: any
    if (isEditing) {
        const doc = new Y.Doc();
        const provider = new YPartyKitProvider(
            "blocknote-dev.yousefed.partykit.dev",
            project?.Id! + selectedFolder?.id,
            doc,
        );
        editor = BNE.create({
            collaboration: {
                provider,
                fragment: doc.getXmlFragment("document-store"),
                user: {
                    name: user?.GithubName!,
                    color: getRandomColor() // blue color
                }
            }
        });
    } else {
        editor = BNE.create({
            initialContent: pageContent
        });
    }

    return editor;
}, [selectedFolder, isEditing, pageContent]);

I think I should look into re-rendering part.
Thanks btw

@Akshdhiwar
Copy link
Author

Hey @trival Thanks for suggesting me the solution. Yes you were correct the thing was the editor was re-rendered multiple times and that caused this issue. Thanks for helping me out and I would like to connect with you on other platforms and lets be friend. Already followed you on X. Once again thank you mate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants