|
| 1 | +--- |
| 2 | +slug: codemirror-5-multi-file-editor-swapdoc-autosave-discipline |
| 3 | +title: How do I implement a multi-file editor in CodeMirror 5 with per-tab undo history and localStorage autosave? |
| 4 | +created: 2026-05-13 |
| 5 | +last_verified: 2026-05-13 |
| 6 | +links: [] |
| 7 | +--- |
| 8 | + |
| 9 | +**Core pattern: one `CodeMirror.Doc` per file, `editor.swapDoc(doc)` to switch the visible buffer. The editor instance is shared; each Doc carries its own text, undo stack, and scroll position.** |
| 10 | + |
| 11 | +```js |
| 12 | +const editor = CodeMirror(el, opts); // one editor |
| 13 | + |
| 14 | +const state = { |
| 15 | + files: { // one Doc per file |
| 16 | + 'main.das': editor.getDoc(), |
| 17 | + 'utils.das': new CodeMirror.Doc('', 'daslang'), |
| 18 | + }, |
| 19 | + active: 'main.das', |
| 20 | +}; |
| 21 | + |
| 22 | +function switchFile(name) { |
| 23 | + state.active = name; |
| 24 | + editor.swapDoc(state.files[name]); // history preserved |
| 25 | + autosave(); // ← see below |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +## Autosave discipline — the trap |
| 30 | + |
| 31 | +The instinct is to autosave on content change only: |
| 32 | +```js |
| 33 | +doc.on('change', autosave); // covers content edits |
| 34 | +``` |
| 35 | + |
| 36 | +But that misses **every other state mutation** — tab switch, add, delete, rename. If the user picks tab B, types nothing, and reloads, persistence restores tab A. Fix: call `autosave()` from *every* state-modifying entry point, not just `change`: |
| 37 | + |
| 38 | +- `switchFile` → `autosave()` |
| 39 | +- `addFile` → `autosave()` |
| 40 | +- `deleteFile` → `autosave()` |
| 41 | +- `renameFile` → `autosave()` |
| 42 | +- Initial `change` listener stays (per-Doc, attached when Doc is created) |
| 43 | + |
| 44 | +Debounce the writer (~250ms) so rapid edits coalesce. Redundant calls during rapid state changes are free — `clearTimeout(autosaveTimer)` then `setTimeout(..., 250)`. |
| 45 | + |
| 46 | +## Serialization |
| 47 | + |
| 48 | +Each Doc stringifies via `.getValue()`. Whole state: |
| 49 | +```js |
| 50 | +function getStateJson() { |
| 51 | + return { |
| 52 | + files: Object.fromEntries( |
| 53 | + Object.entries(state.files).map(([k, d]) => [k, d.getValue()]) |
| 54 | + ), |
| 55 | + active: state.active, |
| 56 | + }; |
| 57 | +} |
| 58 | +``` |
| 59 | +Persist `JSON.stringify(getStateJson())` into `localStorage`. Restore by parsing then reconstructing fresh Docs. |
| 60 | + |
| 61 | +## URL share format |
| 62 | + |
| 63 | +LZ-string the JSON, encode URL-safely: |
| 64 | +```js |
| 65 | +const url = location.origin + location.pathname |
| 66 | + + '#z=' + LZString.compressToEncodedURIComponent(JSON.stringify(getStateJson())); |
| 67 | +``` |
| 68 | +Decode with `LZString.decompressFromEncodedURIComponent`. ~50-line program → ~250B encoded → fits in any URL bar. |
| 69 | + |
| 70 | +## Where this came from |
| 71 | + |
| 72 | +PR #2648 (daslang.io playground): `web/ui` shipped a multi-file `files: [...]` JSON schema in samples since forever, but the loader only ever read `files[0]`. Adding tabs was a Doc map + `swapDoc` per click. The autosave-on-tab-switch bug landed in round 6 of review — `pgSwitchFile` updated state but didn't call `autosave()`. Three rounds of testing later, Copilot caught it. |
| 73 | + |
| 74 | +## Bonus |
| 75 | + |
| 76 | +`editor.refresh()` after layout changes (e.g. splitter resize) — CM caches gutter widths and won't redraw unless told. |
| 77 | + |
| 78 | +## Questions |
| 79 | +- How do I implement a multi-file editor in CodeMirror 5 with per-tab undo history and localStorage autosave? |
0 commit comments