CodeMirror6 - how to save history filed when set diffent content

I’m a codemirror newbie.
I want to save history filed when change the content.
see the image.
tab1 content is “tab1”.
tab2 content is “tab2”.
when i change tab1 to tab2,the tab1 history filed should be saved,
becase next time when i change tab2 to tab1,it should undo tab1 history filed.
codemirror

change content like this:

view.dispatch({
    changes: { from: 0, to: view.state.doc.length, insert: text },
    selection: { anchor: text.length },
    annotations: Transaction.addToHistory.of(false),
  })

The page only have one view instance.
i tried view.setState when setContent.but it doesn’t work.
what should i write the code?
thanks you very mush!

You could create a fresh editor state (and switch to it with view.setState), keeping the editor state for the other tab around to switch back to that when the user goes back to that tab.