Fix notes not loading on session reload

- Initialize CodeMirror with existing Y.Text content (doc: yjs.notes.toString())
  so text is visible immediately, not just after the next remote change
- Use dedicated Y.UndoManager for notes instead of sharing with waypoints
  undo manager (different tracked origins caused conflicts)
- Use yUndoManagerKeymap for proper Yjs-aware undo/redo

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ullrich Schäfer 2026-04-11 03:00:51 +02:00
parent 9e7aa4647d
commit 205e2bbbe0
No known key found for this signature in database
GPG key ID: A32FF691A0F752D9

View file

@ -2,9 +2,9 @@ import { useEffect, useRef } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { EditorView, keymap, placeholder } from "@codemirror/view"; import { EditorView, keymap, placeholder } from "@codemirror/view";
import { EditorState } from "@codemirror/state"; import { EditorState } from "@codemirror/state";
import { defaultKeymap, history, historyKeymap } from "@codemirror/commands"; import { defaultKeymap } from "@codemirror/commands";
import { syntaxHighlighting, defaultHighlightStyle } from "@codemirror/language"; import { yCollab, yUndoManagerKeymap } from "y-codemirror.next";
import { yCollab } from "y-codemirror.next"; import * as Y from "yjs";
import type { YjsState } from "~/lib/use-yjs"; import type { YjsState } from "~/lib/use-yjs";
interface NotesPanelProps { interface NotesPanelProps {
@ -16,9 +16,6 @@ const theme = EditorView.theme({
height: "100%", height: "100%",
fontSize: "13px", fontSize: "13px",
}, },
".cm-editor": {
height: "100%",
},
".cm-scroller": { ".cm-scroller": {
overflow: "auto", overflow: "auto",
fontFamily: "inherit", fontFamily: "inherit",
@ -33,7 +30,6 @@ const theme = EditorView.theme({
"&.cm-focused": { "&.cm-focused": {
outline: "none", outline: "none",
}, },
// Remote cursor styling
".cm-ySelectionInfo": { ".cm-ySelectionInfo": {
fontSize: "10px", fontSize: "10px",
fontFamily: "system-ui, sans-serif", fontFamily: "system-ui, sans-serif",
@ -66,16 +62,18 @@ export function NotesPanel({ yjs }: NotesPanelProps) {
}); });
} }
// Dedicated undo manager for notes (separate from waypoints undo)
const notesUndoManager = new Y.UndoManager(yjs.notes);
const state = EditorState.create({ const state = EditorState.create({
doc: yjs.notes.toString(),
extensions: [ extensions: [
keymap.of([...defaultKeymap, ...historyKeymap]), keymap.of([...yUndoManagerKeymap, ...defaultKeymap]),
history(),
syntaxHighlighting(defaultHighlightStyle),
EditorView.lineWrapping, EditorView.lineWrapping,
placeholder(t("notes.placeholder")), placeholder(t("notes.placeholder")),
theme, theme,
yCollab(yjs.notes, yjs.awareness, { yCollab(yjs.notes, yjs.awareness, {
undoManager: yjs.undoManager, undoManager: notesUndoManager,
}), }),
], ],
}); });
@ -89,6 +87,7 @@ export function NotesPanel({ yjs }: NotesPanelProps) {
return () => { return () => {
view.destroy(); view.destroy();
notesUndoManager.destroy();
viewRef.current = null; viewRef.current = null;
}; };
}, [yjs]); }, [yjs]);