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..234efefc 100644 --- a/packages/app/src/styles.css +++ b/packages/app/src/styles.css @@ -4641,6 +4641,20 @@ 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. 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-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; + } + .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..d525eb3f 100644 --- a/packages/app/src/styles.test.ts +++ b/packages/app/src/styles.test.ts @@ -125,6 +125,45 @@ describe("editor stylesheet", () => { ); }); + 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 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(`${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", () => { const styles = readFileSync(`${process.cwd()}/src/styles.css`, "utf8"); const codeLineStart = styles.indexOf( @@ -143,7 +182,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"); 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(