scniro / react-codemirror2

Codemirror integrated components for React
MIT License
1.66k stars 193 forks source link

extraKeys: can't access all the CM apis? #221

Open andreasciamanna opened 3 years ago

andreasciamanna commented 3 years ago

I'm trying to port an old version of a JS component, into a React-based one.

That also means porting the implementation of an ancient version of CM into the one used in this repo.

In a function component, I have this callback:

    const completeIfInTag = (cm) => {
        return completeAfter(cm, function () {
            const tok = cm.getTokenAt(cm.getCursor());
            if (tok.type === "string" && (!/['"]/.test(tok.string.charAt(tok.string.length - 1)) || tok.string.length === 1)) {
                return false;
            }
            console.log('***** completeIfInTag_cm', cm)
            const inner = cm.innerMode(cm.getMode(), tok.state).state;
            return inner.tagName;
        });
    };

extraKeys is defined as such:

{
// some stuff ...

            "' '": completeIfInTag,
            "'='": completeIfInTag,

// some more stuff ...
}

As soon as I start typing, I get an error: cm.innerMode is not a function.

I've also tried to get the editor's instance using editorDidMount and use that instead of cm, but I don't see any difference.

This is just one example: I have other pieces of code where it seems I can't access some properties, methods, or helpers of CodeMirror.

Any help would be welcome.

Thanks!

andreasciamanna commented 3 years ago

I forgot to mention, I can't share a repo because this code is part of a bigger project which I can't share.

I may try to create a small project that replicates the issue, but I'm not so sure I can manage (all the legacy code that I'm trying to refactor would make quite hard to do so).