From 282fc84d98d1a1f87920695128b8ac735f23955b Mon Sep 17 00:00:00 2001 From: leemour Date: Fri, 20 Nov 2026 11:34:18 +0100 Subject: [PATCH 1/2] fix(editor): paint preview selection natively on Linux WebKitGTK repaints CodeMirror's absolutely positioned selection layer on every pointer move. On Linux (measured on Wayland, AMD, WebKitGTK 2.52 with the DMABUF renderer off) this made drag selection in paragraphs and scrolling visibly lag, while table cells, which use native selection, stayed instant. Editor work per selection step was about 3.5 ms, so the time went into painting, not JavaScript. Expose the desktop platform on the root element and, on Linux only, hide the selection layer and paint the native ::selection with the same theme color. Other platforms keep the drawn layer. Co-Authored-By: Claude Opus 5.5 --- packages/app/src/App.tsx | 10 ++++++++++ packages/app/src/styles.css | 12 ++++++++++++ packages/app/src/styles.test.ts | 19 ++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/packages/app/src/App.tsx b/packages/app/src/App.tsx index 2c8e1a35..25b2db8e 100644 --- a/packages/app/src/App.tsx +++ b/packages/app/src/App.tsx @@ -614,6 +614,16 @@ function WorkspaceApp() { delete root.dataset.webkitScrollWorkaround; }, [webKitScrollWorkaround]); + useEffect(() => { + const root = globalThis.document?.documentElement; + if (!root) return; + + root.dataset.desktopPlatform = desktopPlatform; + return () => { + delete root.dataset.desktopPlatform; + }; + }, [desktopPlatform]); + useEffect(() => { const rememberFocusedEditor = (event: FocusEvent) => { const target = event.target instanceof Element ? event.target : null; diff --git a/packages/app/src/styles.css b/packages/app/src/styles.css index b23f9045..ebfb9268 100644 --- a/packages/app/src/styles.css +++ b/packages/app/src/styles.css @@ -4641,6 +4641,18 @@ background: color-mix(in srgb, var(--editor-caret-color, var(--accent)) 20%, transparent) !important; } + /* WebKitGTK repaints CodeMirror's absolutely positioned selection layer on + every pointer move, which makes drag selection and scrolling lag. The + native selection paints in place with the same theme color. */ + html[data-desktop-platform="linux"] .markdown-paper .cm-selectionLayer { + display: none !important; + } + + html[data-desktop-platform="linux"] .markdown-paper .cm-line::selection, + html[data-desktop-platform="linux"] .markdown-paper .cm-line ::selection { + background: color-mix(in srgb, var(--editor-caret-color, var(--accent)) 20%, transparent) !important; + } + .editor-content-slot[data-document-search-open="true"] .markdown-paper, .editor-content-slot[data-document-search-open="true"] .markdown-paper *, .editor-content-slot[data-document-search-open="true"] .markdown-source-paper, diff --git a/packages/app/src/styles.test.ts b/packages/app/src/styles.test.ts index b9f0f568..4afc0ec0 100644 --- a/packages/app/src/styles.test.ts +++ b/packages/app/src/styles.test.ts @@ -125,6 +125,23 @@ describe("editor stylesheet", () => { ); }); + it("paints the preview selection natively on Linux", () => { + const styles = readFileSync(`${process.cwd()}/src/styles.css`, "utf8"); + const layerStart = styles.indexOf( + 'html[data-desktop-platform="linux"] .markdown-paper .cm-selectionLayer {', + ); + const layerRule = styles.slice(layerStart, styles.indexOf("\n }", layerStart)); + const nativeStart = styles.indexOf( + 'html[data-desktop-platform="linux"] .markdown-paper .cm-line ::selection {', + ); + const nativeRule = styles.slice(nativeStart, styles.indexOf("\n }", nativeStart)); + + expect(layerRule).toContain("display: none !important"); + expect(nativeRule).toContain( + "background: color-mix(in srgb, var(--editor-caret-color, var(--accent)) 20%, transparent) !important;", + ); + }); + it("keeps code block selections between the background and foreground", () => { const styles = readFileSync(`${process.cwd()}/src/styles.css`, "utf8"); const codeLineStart = styles.indexOf( @@ -143,7 +160,7 @@ describe("editor stylesheet", () => { const lineNumberEnd = styles.indexOf("\n }", lineNumberStart); const lineNumberRule = styles.slice(lineNumberStart, lineNumberEnd); - expect(styles).not.toContain(".markdown-paper .cm-selectionLayer {"); + expect(styles).not.toContain("\n .markdown-paper .cm-selectionLayer {"); expect(styles).not.toContain(".markdown-paper .cm-cursorLayer {"); expect(codeLineRule).toContain("position: relative"); expect(codeLineRule).toContain("background: transparent !important"); From 1299beed0260cfef49348264d44945bc78605dce Mon Sep 17 00:00:00 2001 From: leemour Date: Tue, 24 Nov 2026 15:16:20 +0100 Subject: [PATCH 2/2] fix(editor): keep the drawn selection in the inactive pane on Linux Review follow-up for native selection painting on Linux. The browser holds one selection range for the whole page. With two documents side by side, selecting in the right pane moved the browser selection there, and the Linux rule still hid the left pane's drawn layer, so its CodeMirror selection became invisible. The same happens to the extra ranges of a multi-range selection. Expose data-markra-selection-ranges ("single" or "multiple") on the editor and scope the native painting to .cm-focused editors with a single range. Unfocused panes and multi-range selections keep CodeMirror's drawn layer. The stylesheet test now checks the selector against a two-pane DOM. Co-Authored-By: Claude Opus 5.5 --- packages/app/src/styles.css | 10 +++--- packages/app/src/styles.test.ts | 36 +++++++++++++++---- packages/editor/src/codemirror/index.ts | 4 +++ .../src/codemirror/live-markdown.test.ts | 16 +++++++++ 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/packages/app/src/styles.css b/packages/app/src/styles.css index ebfb9268..234efefc 100644 --- a/packages/app/src/styles.css +++ b/packages/app/src/styles.css @@ -4643,13 +4643,15 @@ /* WebKitGTK repaints CodeMirror's absolutely positioned selection layer on every pointer move, which makes drag selection and scrolling lag. The - native selection paints in place with the same theme color. */ - html[data-desktop-platform="linux"] .markdown-paper .cm-selectionLayer { + native selection paints in place with the same theme color. The browser + holds one selection range for one editor, so an unfocused pane or a + multi-range selection keeps the drawn layer. */ + html[data-desktop-platform="linux"] .markdown-paper .cm-editor.cm-focused[data-markra-selection-ranges="single"] .cm-selectionLayer { display: none !important; } - html[data-desktop-platform="linux"] .markdown-paper .cm-line::selection, - html[data-desktop-platform="linux"] .markdown-paper .cm-line ::selection { + html[data-desktop-platform="linux"] .markdown-paper .cm-editor.cm-focused[data-markra-selection-ranges="single"] .cm-line::selection, + html[data-desktop-platform="linux"] .markdown-paper .cm-editor.cm-focused[data-markra-selection-ranges="single"] .cm-line ::selection { background: color-mix(in srgb, var(--editor-caret-color, var(--accent)) 20%, transparent) !important; } diff --git a/packages/app/src/styles.test.ts b/packages/app/src/styles.test.ts index 4afc0ec0..d525eb3f 100644 --- a/packages/app/src/styles.test.ts +++ b/packages/app/src/styles.test.ts @@ -125,21 +125,43 @@ describe("editor stylesheet", () => { ); }); - it("paints the preview selection natively on Linux", () => { + it("paints the preview selection natively on Linux only in the focused single-range editor", () => { const styles = readFileSync(`${process.cwd()}/src/styles.css`, "utf8"); - const layerStart = styles.indexOf( - 'html[data-desktop-platform="linux"] .markdown-paper .cm-selectionLayer {', - ); + const nativeScope = + 'html[data-desktop-platform="linux"] .markdown-paper .cm-editor.cm-focused[data-markra-selection-ranges="single"]'; + const layerSelector = `${nativeScope} .cm-selectionLayer`; + const layerStart = styles.indexOf(`${layerSelector} {`); const layerRule = styles.slice(layerStart, styles.indexOf("\n }", layerStart)); - const nativeStart = styles.indexOf( - 'html[data-desktop-platform="linux"] .markdown-paper .cm-line ::selection {', - ); + const nativeStart = styles.indexOf(`${nativeScope} .cm-line ::selection {`); const nativeRule = styles.slice(nativeStart, styles.indexOf("\n }", nativeStart)); expect(layerRule).toContain("display: none !important"); expect(nativeRule).toContain( "background: color-mix(in srgb, var(--editor-caret-color, var(--accent)) 20%, transparent) !important;", ); + + document.documentElement.dataset.desktopPlatform = "linux"; + document.body.innerHTML = ["focused single", "inactive single", "focused multiple"] + .map((pane) => { + const [focus, ranges] = pane.split(" "); + return `
+
+
+
+
`; + }) + .join(""); + + try { + expect( + [...document.querySelectorAll(layerSelector)].map( + (layer) => layer.closest(".markdown-paper")?.dataset.pane, + ), + ).toEqual(["focused single"]); + } finally { + delete document.documentElement.dataset.desktopPlatform; + document.body.innerHTML = ""; + } }); it("keeps code block selections between the background and foreground", () => { diff --git a/packages/editor/src/codemirror/index.ts b/packages/editor/src/codemirror/index.ts index b3c8375b..ea3a8a23 100644 --- a/packages/editor/src/codemirror/index.ts +++ b/packages/editor/src/codemirror/index.ts @@ -1,6 +1,7 @@ import { markdown } from "@codemirror/lang-markdown"; import { codeFolding } from "@codemirror/language"; import type { Extension } from "@codemirror/state"; +import { EditorView } from "@codemirror/view"; import { GFM } from "@lezer/markdown"; import { markraSetextHeading } from "@markra/markdown"; import { @@ -275,6 +276,9 @@ export function liveMarkdown(config: LiveMarkdownConfig = {}): Extension { codeFolding(), livePreview(previewConfig), markraTheme, + EditorView.editorAttributes.compute(["selection"], (state) => ({ + "data-markra-selection-ranges": state.selection.ranges.length > 1 ? "multiple" : "single", + })), markraPlugins(plugins), slashMenu ? markraSlashMenu() : [], ]; diff --git a/packages/editor/src/codemirror/live-markdown.test.ts b/packages/editor/src/codemirror/live-markdown.test.ts index 6bc40eb8..58d25aee 100644 --- a/packages/editor/src/codemirror/live-markdown.test.ts +++ b/packages/editor/src/codemirror/live-markdown.test.ts @@ -907,6 +907,22 @@ describe("liveMarkdown", () => { ); }); + it("tells app styles whether the selection has more than one range", () => { + const doc = "First line\n\nSecond line"; + const view = createView({ doc, anchor: 0 }); + + expect(view.dom.getAttribute("data-markra-selection-ranges")).toBe("single"); + + view.dispatch({ + selection: EditorSelection.create([ + EditorSelection.range(0, 5), + EditorSelection.range(doc.indexOf("Second"), doc.length), + ]), + }); + + expect(view.dom.getAttribute("data-markra-selection-ranges")).toBe("multiple"); + }); + it("limits syntax-tree traversal to CodeMirror's visible ranges", () => { syntaxTreeIterations.splice(0); const doc = Array.from(