Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(open-knowledge): keep source-editor compartments on the cached view (#2026)

sarah0fa8a60ff495
diff --git a/.changeset/source-editor-theme-staleness.md b/.changeset/source-editor-theme-staleness.md
new file mode 100644
index 000000000..d7bc49edd
--- /dev/null
+++ b/.changeset/source-editor-theme-staleness.md
@@ -0,0 +1,7 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Fix stale source-mode editor styling on backgrounded documents. Switching between light and dark mode (or toggling word wrap) previously only updated the focused document — other open documents kept the prior theme's syntax highlighting (washed-out colors) or the prior word-wrap setting until a hard refresh.
+
+The theme, word-wrap, and placeholder CodeMirror compartments are now stored with the cached editor view instead of per React component. Because editor views are cached and reparented across tab switches while their component remounts, a per-component compartment was absent from the reused view's config, so the reconfigure on theme/setting change was a silent no-op. Reopening a backgrounded document now re-applies the current settings.
diff --git a/packages/app/src/editor/SourceEditor.tsx b/packages/app/src/editor/SourceEditor.tsx
index 0801301bf..6c9e41aff 100644
--- a/packages/app/src/editor/SourceEditor.tsx
+++ b/packages/app/src/editor/SourceEditor.tsx
@@ -104,9 +104,6 @@ export function SourceEditor({
}: SourceEditorProps) {
const containerRef = useRef<HTMLDivElement>(null);
const viewRef = useRef<EditorView | null>(null);
- const themeCompartmentRef = useRef(new Compartment());
- const placeholderCompartmentRef = useRef(new Compartment());
- const wordWrapCompartmentRef = useRef(new Compartment());
const [mountError, setMountError] = useState<Error | null>(null);
if (mountError) throw mountError;
const { resolvedTheme } = useTheme();
@@ -148,6 +145,9 @@ export function SourceEditor({
ydoc: provider.document,
ytext,
});
+ const themeCompartment = new Compartment();
+ const wordWrapCompartment = new Compartment();
+ const placeholderCompartment = new Compartment();
const state = EditorState.create({
doc: ytext.toString(),
extensions: [
@@ -155,10 +155,10 @@ export function SourceEditor({
keymap.of([indentWithTab]),
yCollab(ytext, provider.awareness),
...createNestedCMExtensions({
- themeCompartment: themeCompartmentRef.current,
+ themeCompartment,
resolvedTheme,
ydoc: provider.document,
- wordWrapCompartment: wordWrapCompartmentRef.current,
+ wordWrapCompartment,
wordWrap,
}),
createSourcePolishExtension(),
@@ -191,7 +191,7 @@ export function SourceEditor({
return true;
},
}),
- placeholderCompartmentRef.current.of(cmPlaceholder(placeholder ?? '')),
+ placeholderCompartment.of(cmPlaceholder(placeholder ?? '')),
EditorView.theme({
'&': {
height: '100%',
@@ -212,6 +212,9 @@ export function SourceEditor({
ydoc: provider.document,
ytext,
provider,
+ themeCompartment,
+ wordWrapCompartment,
+ placeholderCompartment,
};
},
});
@@ -264,25 +267,28 @@ export function SourceEditor({
}, [docName]);
useEffect(() => {
- if (!viewRef.current) return;
- viewRef.current.dispatch({
- effects: themeCompartmentRef.current.reconfigure(
+ const entry = cmEntryRef.current;
+ if (!entry) return;
+ entry.view.dispatch({
+ effects: entry.themeCompartment.reconfigure(
resolvedTheme === 'dark' ? darkTheme : lightTheme,
),
});
}, [resolvedTheme]);
useEffect(() => {
- if (!viewRef.current) return;
- viewRef.current.dispatch({
- effects: wordWrapCompartmentRef.current.reconfigure(wordWrap ? EditorView.lineWrapping : []),
+ const entry = cmEntryRef.current;
+ if (!entry) return;
+ entry.view.dispatch({
+ effects: entry.wordWrapCompartment.reconfigure(wordWrap ? EditorView.lineWrapping : []),
});
}, [wordWrap]);
useEffect(() => {
- if (!viewRef.current) return;
- viewRef.current.dispatch({
- effects: placeholderCompartmentRef.current.reconfigure(cmPlaceholder(placeholder ?? '')),
+ const entry = cmEntryRef.current;
+ if (!entry) return;
+ entry.view.dispatch({
+ effects: entry.placeholderCompartment.reconfigure(cmPlaceholder(placeholder ?? '')),
});
}, [placeholder]);
diff --git a/packages/app/src/editor/editor-cache.test.ts b/packages/app/src/editor/editor-cache.test.ts
index a9a47f01c..d7e5de953 100644
--- a/packages/app/src/editor/editor-cache.test.ts
+++ b/packages/app/src/editor/editor-cache.test.ts
@@ -1,4 +1,5 @@
import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test';
+import { Compartment } from '@codemirror/state';
import type { EditorView } from '@codemirror/view';
import type { HocuspocusProvider } from '@hocuspocus/provider';
import type { Editor } from '@tiptap/core';
@@ -213,11 +214,17 @@ interface CmHarness {
spies: FakeCmViewSpies;
providerSpies: FakeProviderSpies;
factoryCallCount: number;
+ themeCompartment: Compartment;
+ wordWrapCompartment: Compartment;
+ placeholderCompartment: Compartment;
factory: (container: FakeNode) => {
view: EditorView;
ydoc: Y.Doc;
ytext: Y.Text;
provider: HocuspocusProvider;
+ themeCompartment: Compartment;
+ wordWrapCompartment: Compartment;
+ placeholderCompartment: Compartment;
};
}
@@ -228,6 +235,9 @@ function makeCmHarness(docName: string): CmHarness {
const { view, spies } = makeFakeCmView(viewDom);
const { provider, spies: providerSpies } = makeFakeProvider(ydoc);
const container = makeNode();
+ const themeCompartment = new Compartment();
+ const wordWrapCompartment = new Compartment();
+ const placeholderCompartment = new Compartment();
let factoryCallCount = 0;
const harness: CmHarness = {
docName,
@@ -240,11 +250,22 @@ function makeCmHarness(docName: string): CmHarness {
spies,
providerSpies,
factoryCallCount: 0,
+ themeCompartment,
+ wordWrapCompartment,
+ placeholderCompartment,
factory: (ctr) => {
factoryCallCount++;
harness.factoryCallCount = factoryCallCount;
ctr.appendChild(viewDom);
- return { view, ydoc, ytext, provider };
+ return {
+ view,
+ ydoc,
+ ytext,
+ provider,
+ themeCompartment,
+ wordWrapCompartment,
+ placeholderCompartment,
+ };
},
};
return harness;
@@ -858,6 +879,49 @@ describe('CM6 cache — lifecycle', () => {
expect(entry.activeMountKey).toBe(h.docName);
});
+ test('mount: stores the factory compartments on the entry and preserves them across cache-hit', () => {
+ const h = makeCmHarness('cm-doc-a');
+ const first = mountCmEditor({
+ docName: h.docName,
+ container: h.container as unknown as HTMLElement,
+ factory: h.factory as unknown as (el: HTMLElement) => ReturnType<typeof h.factory>,
+ });
+ expect(first.themeCompartment).toBe(h.themeCompartment);
+ expect(first.wordWrapCompartment).toBe(h.wordWrapCompartment);
+ expect(first.placeholderCompartment).toBe(h.placeholderCompartment);
+
+ const second = mountCmEditor({
+ docName: h.docName,
+ container: makeNode() as unknown as HTMLElement,
+ factory: h.factory as unknown as (el: HTMLElement) => ReturnType<typeof h.factory>,
+ });
+ expect(second.themeCompartment).toBe(h.themeCompartment);
+ expect(second.wordWrapCompartment).toBe(h.wordWrapCompartment);
+ expect(second.placeholderCompartment).toBe(h.placeholderCompartment);
+ expect(h.factoryCallCount).toBe(1);
+ });
+
+ test('park then remount: compartments survive the park→remount round-trip', () => {
+ const h = makeCmHarness('cm-doc-a');
+ const first = mountCmEditor({
+ docName: h.docName,
+ container: h.container as unknown as HTMLElement,
+ factory: h.factory as unknown as (el: HTMLElement) => ReturnType<typeof h.factory>,
+ });
+ parkCmEditor(first);
+
+ const remounted = mountCmEditor({
+ docName: h.docName,
+ container: makeNode() as unknown as HTMLElement,
+ factory: h.factory as unknown as (el: HTMLElement) => ReturnType<typeof h.factory>,
+ });
+ expect(remounted).toBe(first);
+ expect(remounted.themeCompartment).toBe(h.themeCompartment);
+ expect(remounted.wordWrapCompartment).toBe(h.wordWrapCompartment);
+ expect(remounted.placeholderCompartment).toBe(h.placeholderCompartment);
+ expect(h.factoryCallCount).toBe(1);
+ });
+
test('mount: cache-hit reparents view.dom without construction', () => {
const h = makeCmHarness('cm-doc-a');
const first = mountCmEditor({
@@ -974,6 +1038,10 @@ describe('CM6 cache — lifecycle', () => {
ydoc: h.ydoc,
ytext: h.ytext,
provider: h.provider,
+ themeCompartment: h.themeCompartment,
+ wordWrapCompartment: h.wordWrapCompartment,
+ placeholderCompartment: h.placeholderCompartment,
+ parkingNode: null,
scrollTop: 0,
hadFocus: false,
activeMountKey: h.docName,
diff --git a/packages/app/src/editor/editor-cache.ts b/packages/app/src/editor/editor-cache.ts
index 4aa7e4d57..8a5528941 100644
--- a/packages/app/src/editor/editor-cache.ts
+++ b/packages/app/src/editor/editor-cache.ts
@@ -1,3 +1,4 @@
+import type { Compartment } from '@codemirror/state';
import type { EditorView } from '@codemirror/view';
import type { HocuspocusProvider } from '@hocuspocus/provider';
import type { RenamedDocMapping } from '@inkeep/open-knowledge-core';
@@ -62,6 +63,14 @@ export interface CmCacheEntry {
ydoc: Y.Doc;
ytext: Y.Text;
provider: HocuspocusProvider;
+ themeCompartment: Compartment;
+ /** Word-wrap compartment embedded in `view`. Same per-entry rationale as
+ * `themeCompartment` — a per-component compartment goes stale on reattach
+ * after the global word-wrap setting changes while the doc is backgrounded. */
+ wordWrapCompartment: Compartment;
+ /** Placeholder compartment embedded in `view`. Same per-entry rationale as
+ * `themeCompartment` (lowest impact — placeholder only shows on empty docs). */
+ placeholderCompartment: Compartment;
scrollTop: number;
hadFocus: boolean;
activeMountKey: string | null;
@@ -89,6 +98,9 @@ interface CmFactoryResult {
ydoc: Y.Doc;
ytext: Y.Text;
provider: HocuspocusProvider;
+ themeCompartment: Compartment;
+ wordWrapCompartment: Compartment;
+ placeholderCompartment: Compartment;
}
type CmFactory = (container: HTMLElement) => CmFactoryResult;
@@ -457,6 +469,9 @@ export function mountCmEditor(params: MountCmParams): CmCacheEntry {
ydoc: fresh.ydoc,
ytext: fresh.ytext,
provider: fresh.provider,
+ themeCompartment: fresh.themeCompartment,
+ wordWrapCompartment: fresh.wordWrapCompartment,
+ placeholderCompartment: fresh.placeholderCompartment,
scrollTop: 0,
hadFocus: false,
activeMountKey: docName,
@@ -516,6 +531,9 @@ export function mountCmEditor(params: MountCmParams): CmCacheEntry {
ydoc: fresh.ydoc,
ytext: fresh.ytext,
provider: fresh.provider,
+ themeCompartment: fresh.themeCompartment,
+ wordWrapCompartment: fresh.wordWrapCompartment,
+ placeholderCompartment: fresh.placeholderCompartment,
scrollTop: 0,
hadFocus: false,
activeMountKey: docName,
diff --git a/packages/app/src/editor/provider-pool.test.ts b/packages/app/src/editor/provider-pool.test.ts
index 738cd7671..c1556e440 100644
--- a/packages/app/src/editor/provider-pool.test.ts
+++ b/packages/app/src/editor/provider-pool.test.ts
@@ -1,6 +1,7 @@
import { afterEach, beforeEach, describe, expect, mock, spyOn, test } from 'bun:test';
import { randomUUID } from 'node:crypto';
import { setTimeout as wait } from 'node:timers/promises';
+import { Compartment } from '@codemirror/state';
import { PROTOCOL_VERSION } from '@inkeep/open-knowledge-core';
import { parseHocuspocusAuthToken } from '@inkeep/open-knowledge-server';
import * as Y from 'yjs';
@@ -2196,7 +2197,15 @@ describe('ProviderPool → V2 editor cache eviction coupling (Critical #2)', ()
cacheModule.mountCmEditor({
docName: 'doc-eviction-regression',
container: makeFakeNode() as unknown as HTMLElement,
- factory: () => ({ view: fakeView, ydoc: fakeYDoc, ytext: fakeYText, provider: makeProv() }),
+ factory: () => ({
+ view: fakeView,
+ ydoc: fakeYDoc,
+ ytext: fakeYText,
+ provider: makeProv(),
+ themeCompartment: new Compartment(),
+ wordWrapCompartment: new Compartment(),
+ placeholderCompartment: new Compartment(),
+ }),
});
expect(cacheModule.peekTiptap('doc-eviction-regression')).toBeDefined();
expect(cacheModule.__peekCm('doc-eviction-regression')).toBeDefined();
@@ -2247,7 +2256,15 @@ describe('ProviderPool → V2 editor cache eviction coupling (Critical #2)', ()
cacheModule.mountCmEditor({
docName: 'doc-recycle-regression',
container: makeFakeNode() as unknown as HTMLElement,
- factory: () => ({ view: fakeView, ydoc: fakeYDoc, ytext: fakeYText, provider: makeProv() }),
+ factory: () => ({
+ view: fakeView,
+ ydoc: fakeYDoc,
+ ytext: fakeYText,
+ provider: makeProv(),
+ themeCompartment: new Compartment(),
+ wordWrapCompartment: new Compartment(),
+ placeholderCompartment: new Compartment(),
+ }),
});
pool = new ProviderPool(3, DUMMY_WS);