Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions packages/app/src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 40 additions & 1 deletion packages/app/src/styles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<div class="markdown-paper" data-pane="${pane}">
<div class="cm-editor${focus === "focused" ? " cm-focused" : ""}" data-markra-selection-ranges="${ranges}">
<div class="cm-selectionLayer"></div>
</div>
</div>`;
})
.join("");

try {
expect(
[...document.querySelectorAll(layerSelector)].map(
(layer) => layer.closest<HTMLElement>(".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(
Expand All @@ -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");
Expand Down
4 changes: 4 additions & 0 deletions packages/editor/src/codemirror/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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() : [],
];
Expand Down
16 changes: 16 additions & 0 deletions packages/editor/src/codemirror/live-markdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading