editor.setHistory(), clearHistory() equivalent in Codemirror #6


I am working on porting the Svelte REPL to Codemirror 6 (Currently CM5). We have many different files and tabs available there, and each file has its own undo-redo history. We store history of each file by calling editor.getHistory() and storing in a Map. Whenever you switch the files, we load the history for that file and call editor.setHistory. This worked well in CM 5

Ofc, in CM 6, the history API changed, and I’m not sure how to do it anymore. Getting history is easy, I’m calling editor?.state.toJSON({ history: historyField }).history;. Setting and clearing, I’m not finding a way how to do it.

Thanks in advance

Usually, in situations like this, you just want to store the entire editor state (either as a JS object, or, if you need to serialize it, via EditorState.toJSON) rather than storing the document and history separately.

You can’t clear history. Just create a new state. When creating a new state with fromJSON, you can provide your serialized history in the JSON object.

I tried this approach. this works for a few clicks, but then errors out

	export function setHistory(history) {
		if (!editor) return;

		// Set editor's history to `history` parameter
		const new_state = EditorState.fromJSON(
				...editor.state.toJSON({ history: historyField }),
			{ history: historyField }