[Bug] After switching the model, the placeholder does not disappear

Open
#5,380 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@hediet is already working on this.

Since Jun 26, 2026.

Assessment

This issue has not been assessed yet.

Description

Reproducible in vscode.dev or in VS Code Desktop?
  • Not reproducible in vscode.dev or VS Code Desktop
Reproducible in the monaco editor playground?
Monaco Editor Playground Link

Switching from an empty model [placeholder display] to a non-empty model will cause the non-empty model to display the placeholder of the empty model
When switching from a non-empty model to an empty model, the placeholder of the empty model will not be displayed

Monaco Editor Playground Code
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Monaco Tab</title>
  <style>
      .tab-bar {
          display: flex;
          gap: 4px;
          padding: 8px;
          background: #252526;
      }

      .tab {
          padding: 4px 12px;
          color: #ccc;
          cursor: pointer;
          border: 1px solid #444;
          border-bottom: none;
      }

      .tab.active {
          background: #1e1e1e;
          color: #fff;
      }

      .close {
          margin-left: 8px;
          cursor: pointer
      }

      #editor {
          width: 100%;
          height: 500px;
          border: 1px solid #333;
      }
  </style>
</head>
<body>
<div class="tab-bar" id="tabBar"></div>
<div id="editor"></div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/min/vs/loader.js"></script>
<script>
  require.config({paths: {vs: 'https://cdn.jsdelivr.net/npm/[email protected]/min/vs'}});
  require(['vs/editor/editor.main'], function (monaco) {
    const editor = monaco.editor.create(document.getElementById('editor'), {
      model: null,
      minimap: {enabled: false},
      placeholder: "placeholder"
    });

    let tabs = [];
    let activePath = null;

    function renderTabs() {
      const bar = document.getElementById('tabBar');
      bar.innerHTML = '';
      tabs.forEach(tab => {
        const el = document.createElement('div');
        el.className = 'tab ' + (tab.filePath === activePath ? 'active' : '');
        el.innerHTML = `${tab.name}<span class="close">×</span>`;
        el.onclick = (e) => {
          if (e.target.className === 'close') return;
          switchTab(tab.filePath);
        };
        el.querySelector('.close').onclick = (e) => {
          e.stopPropagation();
          closeTab(tab.filePath);
        };
        bar.appendChild(el);
      })
    }

    function openTab(filePath, fileName, content, lang) {
      const uri = monaco.Uri.file(filePath);
      const exist = tabs.find(t => t.filePath === filePath);
      if (exist) return switchTab(filePath);

      // 创建Model
      let model = monaco.editor.getModel(uri);
      if (!model) {
        model = monaco.editor.createModel(content, lang, uri);
      }

      tabs.push({
        filePath, name: fileName, uri, model, viewState: null, language: lang
      });
      switchTab(filePath);
      renderTabs();
    }

    function switchTab(targetPath) {
      if (activePath) {
        const curr = tabs.find(t => t.filePath === activePath);
        if (curr) curr.viewState = editor.saveViewState();
      }
      const target = tabs.find(t => t.filePath === targetPath);
      activePath = targetPath;
      // editor.setModel(null); //Release this area, it can be repaired
      editor.setModel(target.model);
      if (target.viewState) editor.restoreViewState(target.viewState);
      renderTabs();
    }

    function closeTab(path) {
      const idx = tabs.findIndex(t => t.filePath === path);
      if (idx === -1) return;
      tabs.splice(idx, 1);
      if (activePath === path) {
        const next = tabs.at(idx - 1) || tabs[0];
        activePath = next?.filePath || null;
        if (next) switchTab(activePath);
        else editor.setModel(null);
      }
      renderTabs();
    }

    editor.onDidChangeModelContent(() => {
    })

    openTab('/index.js', 'index.js', '', 'javascript');
    openTab('/index.html', 'index.html', '<div></div>', 'html');
  });
</script>
</body>
</html>
Reproduction Steps

No response

Actual (Problematic) Behavior

No response

Expected Behavior

No response

Additional Context

No response

Dominant language
JavaScript
Stars
47k
Forks
4.2k
Avg merge
3d 13h
Merged PRs (30d)
13

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/monaco-editor

All issues in microsoft/monaco-editor

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.