GithubHelp home page GithubHelp logo

Example with Multiple Buffers about dev HOT 6 CLOSED

codemirror avatar codemirror commented on July 16, 2024
Example with Multiple Buffers

from dev.

Comments (6)

marijnh avatar marijnh commented on July 16, 2024 1

The setState method will do that—but it always forces a full redraw of the document, so it may not be more efficient. (Though I guess buffer changes don't happen with a high enough frequency for that to matter terribly much.)

from dev.

marijnh avatar marijnh commented on July 16, 2024

At some point, I'll write docs with a number of examples, like these, but that's quite a bit off now because the API is entirely unstable for the near future.

Having multiple views on the same 'buffer' would work quite different in CM6 than it does in 5—the basic idea is to have separate editor states, but to apply changes made in one editor state to the other states that refer to the same document, so that the states' documents stay in sync.

from dev.

curran avatar curran commented on July 16, 2024

Thanks for outlining the basic idea of how it could work. That is very useful.

from dev.

curran avatar curran commented on July 16, 2024

FWIW I got this working. The solution I used keeps multiple views around, and just detaches and reattaches their DOM nodes as the "selected file" changes. The code looks something like this:

...
  let selectedFileName;
  const editorDiv = document.querySelector('#editor');

  const setSelectedFileName = newSelectedFileName => {
    if (selectedFileName) {
      const oldView = viewForFileName(selectedFileName);
      oldView.dom.remove();
    }
    const newView = viewForFileName(newSelectedFileName);
    editorDiv.appendChild(newView.dom);
    newView.dispatch(newView.state.transaction.scrollIntoView());
    selectedFileName = newSelectedFileName;
  };
...

full code example.

This works pretty well, and it's fine for my particular use case of simulating and IDE sort of experience, but there may be a better way to do this. It may be possible to find a solution where the state instances for each "file" are tracked, while a single view instance gets re-used. I'm not sure how one might accomplish this with the new API.

from dev.

marijnh avatar marijnh commented on July 16, 2024

See codemirror/website@7f7f00e

from dev.

curran avatar curran commented on July 16, 2024

Awesome example! However it lacks the feature of selecting buffers. Is there any example that has this?

image

from dev.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.