Markpad icon indicating copy to clipboard operation
Markpad copied to clipboard

Undo history is destroyed on every tab switch (one Monaco model shared by all tabs)

Open PathGao opened this issue 4 months ago • 0 comments

Switch to another tab and back, then press Ctrl/⌘+Z — the document's undo history is gone.

Why

There is one editor and one implicit model; Editor.svelte never calls monaco.editor.createModel. Switching tabs calls editor.setValue(content).

In the bundled monaco, TextModel.setValue → _setValueFromTextBuffer → this._commandManager.clear(). Clearing the undo stack is what setValue is defined to do.

The same call is also how external writes reach the buffer, so a Live Mode reload, a task-checkbox toggle, front matter editing, or a format action clears undo without leaving the tab.

The suggestion

Give each tab its own ITextModel — Monaco's intended model, and how VS Code itself uses it. setModel(tab.model) on switch, dispose() on close, pushEditOperation() for external writes (which makes them undoable).

Undo, folding, decorations, language and dirty state then follow the document because they live on it, rather than because something remembered to sync them. The setValue dance in the tab-switch effect goes away.

Why I'm asking first

  • It changes observable behaviour: undo starts surviving tab switches.
  • Model lifetime becomes real work — dispose on close, and cross-window tab transfer needs a defined owner.
  • Untitled tabs and the same file open in two windows need unique URIs.
  • monaco-vim binds to the editor; it needs checking that it holds no model reference.

Happy to do it if you want it; equally happy to leave it if you'd rather not take on model lifetime.

PathGao avatar Aug 02 '26 21:08 PathGao