Skip to content
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

Add a way to clear history #405

Open
afska opened this issue Nov 13, 2022 · 6 comments
Open

Add a way to clear history #405

afska opened this issue Nov 13, 2022 · 6 comments

Comments

@afska
Copy link

afska commented Nov 13, 2022

I cannot find any method to clear history (to prevent Ctrl+Z in some cases)
It seems that CodeMirror 6 suggests doing editor.setState(...) with a new state, but in this library states are created internally and there's no easy way to do it.

@jaywcjlove
Copy link
Member

@BenStorey
Copy link

BenStorey commented Dec 29, 2022

I don't understand how to re-use that code when we are using react-codemirror. If somewhere in my code I'm using react-codemirror like this:

<CodeMirror
    ref={codeRef}
    style={{ whatever }}
    className={{ whatever }}
    value={ code }
    theme={mytheme}
    placeholder={mytext}
    extensions={[StreamLanguage.define(q)]}
    onChange={(e) => console.log(e) }
/>

Then how am I supposed to recreate the Editor to keep absolutely everything the same, but only removing History? If I create a new instance without all the options set then it's just broken. But if I need to carefully craft the arguments to exactly match whatever react-codemirror is generating, then why use codemirror in the first place? It seems like I shouldn't....?

Just FYI, my attempt that is broken, is inside useEffect() I set state like this:

codeRef.current.view.setState(EditorState.create({ doc: newcode, extensions: [StreamLanguage.define(q)] };

I end up with a white box with zero formatting (no line numbers, highlighting etc). How can I call EditorState.create() with the same values that react-codemirror would have used?

@BenStorey
Copy link

Actually one hacky workaround that seems to work is by setting the history state to false then back to true again right after. So code like this appears to work fine:

const [ codeMirrorUseHistory, setCodeMirrorUseHistory ] = useState(true);

useEffect(() =>
{
    // unset history
    setCodeMirrorUseHistory(false);
    setTimeout(() => setCodeMirrorUseHistory(true), 1);
}, [deps]);

<CodeMirror
    basicSetup={{ history: codeMirrorUseHistory}}
/>

Dirty but somehow seems better than the other alternative...

@Leessonomy
Copy link

I think this problem needs to be resolved

@Leessonomy
Copy link

https://discuss.codemirror.net/t/codemirror-6-cm-clearhistory-equivalent/2851/7

@rodri042

it seems as view.setState will break functionality like this

const updateListener = EditorView.updateListener.of((vu: ViewUpdate) => {

@chenzhutian
Copy link

+1, if I reset state, all the internal extensions of react-codemirror will be lost

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants