CodeMirror 6 cm.clearHistory() equivalent

Hello! What is the cm.clearHistory() equivalent in CM6? Couldn’t find any info in the docs and in the sources.

Thanks in advance.

1 Like

There is no such thing, directly. If you’re loading a new document, the recommendation is to create a fresh EditorState, which will start with an empty history. If you have another use case, I’m curious to hear it.

Everything is clear, thank you.

Hi.
as Excel has a lot of cells - every one of them has its own functions.
When a user selects one of them - we can change the value and use one set for everyone.
In this case, when we can not clear the history - it is not possible - need every time to run a new code mirror.

No, just a new editor state, which, if I understand what you’re doing correctly, would be a reasonable thing to create anyway.

yes. you are right.
the first time i tired to do -

const insert = 'new text from props'
const cm = codeMirror.current;
 cm.dispatch({
   changes: {from: 0, to: cm.state.doc.length, insert},
 });

and now i am using this ( but need also verify changes inside function - dependency in memo) -


  EditorState.create({
      doc,
      extensions: [
        placeholder('Type formula here...'),
        defaultTheme,
        keyAction.of(dummyKeymap(isCodeEditorOpen)),
        myHighlightStyle,
        basicSetup,
        CLearLines(),
        EditorView.clickAddsSelectionRange.of(() => false),
        EditorState.allowMultipleSelections.of(true),
        EditorView.updateListener.of((mirror: ViewUpdate) => {
          widgetHighlighting(mirror, widgetsTitle, activePage.title);
          console.log(preservedValue, mirror.state.doc.toString());
          setDataStatus(preservedValue !== mirror.state.doc.toString());
        }),
        autocompletion({
          override: [widgetAutocomplete, myFunction],
          maxRenderedOptions: 8,
          defaultKeymap: true,
        }),
      ],
    });

but previously i liked idea to changing state settings in this way ( as in your documentation is ) -

    codeMirror.current.dispatch({
      effects: keyAction.reconfigure(dummyKeymap(isCodeEditorOpen)),
    });
1 Like

i don`t say - that it is bad or something else - it works fast and i am happy to use it.
but i thought that should use an easier way of updating the state.
but if you told me that it is okay - i am using it.

One use case is an external component keeping track of physical characteristics of the editor, such as its height, which is independent of the document being edited. For that, it’s useful to have an update listener that survives the loading of a new document.

I was able to achieve this using a Compartment

import { Compartment } from '@codemirror/state';

export const historyCompartment = new Compartment();

export function setup() {
  return [
     // ...other initial extensions
     historyCompartment.of(history())
  ];
}

export function someAction(view: EditorView) {
  // Clear history
  view.dispatch({
    effects: [historyCompartment.reconfigure([])],
  });

  // Add history back
  view.dispatch({
    effects: [historyCompartment.reconfigure([history()])],
  });
}
2 Likes

Thanks! that is exactly what i was looking for. worked like a charm :slight_smile: