Skip to content

Commit cf762da

Browse files
Fix header reveal flash on note switch
1 parent 6f709e7 commit cf762da

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

app/src/features/editor/extensions/markdown-decorations/markdown-decorations.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ function createDecoratedView(doc: string, selection: EditorSelection) {
4040
});
4141
}
4242

43+
async function flush() {
44+
await new Promise((resolve) => setTimeout(resolve, 0));
45+
await new Promise(requestAnimationFrame);
46+
}
47+
4348
afterEach(() => {
4449
document.body.replaceChildren();
4550
});
@@ -189,4 +194,30 @@ describe("markdownDecorations pointer selection normalization", () => {
189194
expect(view.state.selection.main.head).toBe(2);
190195
view.destroy();
191196
});
197+
198+
it("keeps heading syntax hidden when a new note loads into an inactive editor", async () => {
199+
const view = createDecoratedView(
200+
"# Old heading\nBody",
201+
EditorSelection.create([EditorSelection.cursor(2)]),
202+
);
203+
204+
view.contentDOM.focus();
205+
await flush();
206+
207+
view.dom.classList.add("comet-editor-inactive");
208+
view.dispatch({
209+
changes: {
210+
from: 0,
211+
to: view.state.doc.length,
212+
insert: "# New heading\nBody",
213+
},
214+
});
215+
216+
await flush();
217+
218+
const firstLine = view.dom.querySelector(".cm-line");
219+
expect(firstLine?.textContent).toBe("New heading");
220+
221+
view.destroy();
222+
});
192223
});

app/src/features/editor/extensions/markdown-decorations/plugin.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,16 @@ function expandedVisibleRanges(
129129
return ranges;
130130
}
131131

132+
function hasActiveEditorFocus(view: EditorView) {
133+
return view.hasFocus && !view.dom.classList.contains("comet-editor-inactive");
134+
}
135+
132136
function buildDecorations(
133137
view: EditorView,
134138
searchMatches: SearchMatch[],
135139
): { atomicRanges: DecorationSet; decorations: DecorationSet } {
136140
const { state } = view;
137-
const hasFocus = view.hasFocus;
141+
const hasFocus = hasActiveEditorFocus(view);
138142
const debugEnabled = isEditorDebugEnabled();
139143
const ranges = expandedVisibleRanges(view);
140144
const ctx: BuilderContext = {

0 commit comments

Comments
 (0)