fix(editor): remove mode indicator and source background

This commit is contained in:
boojack
2026-11-16 09:01:17 +08:00
parent ae744d4586
commit 938ceae842
8 changed files with 85 additions and 14 deletions
+3 -2
View File
@@ -27,7 +27,7 @@ MemoEditor memo level: name | draft seed, mode, data, draft stor
- **`view/`** is the read-only surface over `MemoMarkdown`: tags and mentions navigate, in-memo anchors scroll, a paragraph that is one bare link becomes a preview card through the editor's block view (`linkCards.ts`), the page's search matches are painted, and heading ids match the outline. Tag and mention clicks arrive through the editor's `onClick` callbacks. Read-only tags with a click handler are focusable buttons; Enter activates on press and Space on release through the same click callback. Editable tags remain text. Nothing in memos matches the editor's class names or mutates its DOM. Previews that have Markdown but no memo query (the link-memo search, the share image) use `MemoContentView` directly.
- **`edit/`** is the editing surface: `EditSession` renders the chrome (timestamp, attachments, suggestions, toolbar, focus mode, audio recording) over the draft store `MemoEditor` provides, persists the draft while open, and saves through the memo mutations. `Editor.tsx` is the composer's editor: `MemoMarkdown` editable with the composer's own extensions (`#tag` and `:emoji` autocomplete, file paste and drop), behind the `EditorController` contract in `controller.ts`.
Memo content uses 16px text on 24px lines with 12px between blocks; the editor keeps list rows compact. Code blocks use the full `well` fill so they remain distinct from the surrounding surface in Dark. Task items use `control-edge` for their checkbox border and darken Dark's pale accent behind the editor's white checkmark. Callouts use the app's accent, success, warning and danger colors. Custom tag tints bound lightness and sRGB channels so black and white remain visible across themes without coloring the text.
Memo content uses 16px text on 24px lines with 12px between blocks; the editor keeps list rows compact. Rich-text code blocks use the full `well` fill so they remain distinct from the surrounding surface in Dark; Markdown source mode stays transparent. Task items use `control-edge` for their checkbox border and darken Dark's pale accent behind the editor's white checkmark. Callouts use the app's accent, success, warning and danger colors. Custom tag tints bound lightness and sRGB channels so black and white remain visible across themes without coloring the text.
Code blocks use lazy-loaded `@pierre/highlights` tokens as editor decorations, without an HTML conversion. GitHub Light and GitHub Dark Dimmed colors follow the document's color scheme; ordinary text and structural punctuation keep the editor's text color. Unsupported languages, unavailable WebAssembly, or browsers without `light-dark()` colors leave the code as plain text. Highlighting never changes the saved Markdown.
@@ -49,7 +49,7 @@ A reducer (`edit/state/reducer.ts`) drives an external store; consumers subscrib
### Formatting and source mode
Formatting is the editor's own: its toolbar appears over selected text, the slash menu and Markdown shortcuts change block types, and its keys cover the rest. The editor's `mode` option shows the whole memo as Markdown text, with its syntax highlighted, and edits it as text; `useSourceMode` (`@/hooks/useSourceMode`) keeps the choice in the account's `EDITOR` user setting (`source_mode`), so it follows the user to every device; a switch applies at once and reverts if saving fails, and Settings → Editor changes the same value. To people it is "Markdown mode", the + menu's item with the code glyph (Lucide `CodeXml`), checked while on; in code it keeps the editor's name, source mode. A small, noninteractive `Markdown` label sits beside the commit button while source mode is on, where it never covers the text. Switching changes no text and emits no change. In source mode the toolbar, slash menu, block handle and link card do not show; the `#tag` and `:emoji` popups open as in rich mode, the formatting keys edit the Markdown, and the checklist chip toggles the task marker on the caret's line through the same `toggleTaskList` command. The controller reads tags and checklists from the text parsed as a memo. The source text is in the body font (the editor's `sourceFont` token is left at its default) so a switch keeps the text's rhythm; nothing in memos targets the editor's source classes.
Formatting is the editor's own: its toolbar appears over selected text, the slash menu and Markdown shortcuts change block types, and its keys cover the rest. The editor's `mode` option shows the whole memo as Markdown text, with its syntax highlighted, and edits it as text; `useSourceMode` (`@/hooks/useSourceMode`) keeps the choice in the account's `EDITOR` user setting (`source_mode`), so it follows the user to every device; a switch applies at once and reverts if saving fails, and Settings → Editor changes the same value. To people it is "Markdown mode", the + menu's item with the code glyph (Lucide `CodeXml`), checked while on; in code it keeps the editor's name, source mode. Switching changes no text and emits no change. In source mode the toolbar, slash menu, block handle and link card do not show; the `#tag` and `:emoji` popups open as in rich mode, the formatting keys edit the Markdown, and the checklist chip toggles the task marker on the caret's line through the same `toggleTaskList` command. The controller reads tags and checklists from the text parsed as a memo. The source text is in the body font (the editor's `sourceFont` token is left at its default) so a switch keeps the text's rhythm; nothing in memos targets the editor's source classes.
### Saving
@@ -124,6 +124,7 @@ MemoEditor/
## Testing
- `tests/memo-record.test.tsx`: reads by name, freshness, optimistic patches, the comment mutations.
- `tests/browser/editor-style.spec.ts`: source-mode transparency and rich-text code backgrounds in Light, Dark and Paper; run `pnpm exec playwright test --config tests/browser/editor-style.config.ts` from `web/`.
- `tests/memo-editor-view.test.tsx`: view mode by name, task ticking through the mutation.
- `tests/memo-content-view.test.tsx`, `tests/editor.test.tsx`: the surfaces over the binding.
- `tests/use-memo-init.test.tsx`, `tests/draft-key.test.ts`, `tests/use-auto-save.test.tsx`: drafts and keys.
@@ -132,12 +132,6 @@ export const EditorToolbar: FC<EditorToolbarProps> = ({
</div>
<div className="flex shrink-0 flex-row items-center justify-end gap-1">
{/* Markdown mode is said beside the commit button, where it never covers the text. */}
{sourceMode.isSourceMode && (
<span className="px-1.5 mu-label-xs font-mono text-mu-text-secondary select-none" aria-label={t("editor.markdown-mode")}>
Markdown
</span>
)}
{onCancel && (
<Button variant="ghost" onClick={onCancel} disabled={committing}>
{t("common.cancel")}
@@ -163,8 +163,8 @@ const InsertMenu = (props: InsertMenuProps) => {
))}
{/* How the composer presents itself. Focus mode is absent when a host owns the
editor's presentation; Markdown mode (the editor's source mode) only changes how
the text is edited, so every composer has it. Checked while on, with a corner
label in the composer. The code glyph says this mode shows the markup. */}
the text is edited, so every composer has it. Checked while on.
The code glyph says this mode shows the markup. */}
<Menu.Separator />
{viewToggles && (
<Menu.Item onClick={viewToggles.onToggleFocusMode} icon={<Maximize2Icon />}>
+1 -1
View File
@@ -22,7 +22,7 @@ export const headingClasses: Readonly<Partial<Record<HeadingLevel, string>>> = {
export const markdownStyles = {
// Relative size so code inside headings scales with the heading.
inlineCode: "font-mono text-[0.875em] bg-mu-well px-1 py-0.5 rounded-mu-md",
codeBlock: "bg-mu-well!",
codeBlock: "rich:bg-mu-well!",
// Keep the editor's white checkmark readable when Dark uses a pale accent.
taskList:
"[--mbe-border:var(--mu-control-edge)] [--mbe-accent:light-dark(var(--mu-accent),color-mix(in_oklab,var(--mu-accent)_55%,var(--mu-surface)))]",
+8
View File
@@ -0,0 +1,8 @@
import { defineConfig } from "@playwright/test";
import config from "./playwright.config";
export default defineConfig({
...config,
testMatch: "editor-style.spec.ts",
outputDir: "../../node_modules/.cache/editor-style-browser-results",
});
+40
View File
@@ -0,0 +1,40 @@
<!doctype html>
<html lang="en" data-theme="light">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<main class="mx-auto flex max-w-2xl flex-col gap-4 p-4">
<section class="rounded-mu-lg border border-mu-line bg-mu-surface p-4">
<div id="empty-source"></div>
</section>
<section class="rounded-mu-lg border border-mu-line bg-mu-surface p-4">
<div id="filled-source"></div>
</section>
<section class="rounded-mu-lg border border-mu-line bg-mu-surface p-4">
<div id="rich-code"></div>
</section>
</main>
<script type="module">
import { Editor } from "@usememos/editor";
import "@usememos/editor/style.css";
import "/src/index.css";
import { MEMO_EDITOR_THEME } from "/src/lib/memo-markdown.ts";
for (const [id, mode, content] of [
["empty-source", "source", ""],
["filled-source", "source", "# A thought\n\nSome **Markdown**."],
["rich-code", "rich", "```text\nA code block\n```"],
]) {
new Editor({
element: document.getElementById(id),
mode,
content,
placeholder: "Any thoughts...",
theme: MEMO_EDITOR_THEME,
});
}
</script>
</body>
</html>
+28
View File
@@ -0,0 +1,28 @@
import { expect, test } from "@playwright/test";
for (const theme of ["light", "dark", "paper"]) {
test(`${theme}: source mode stays transparent and rich code keeps its background`, async ({ page }, testInfo) => {
await page.goto("/tests/browser/editor-style.html");
await page.evaluate((theme) => {
document.documentElement.dataset.theme = theme;
}, theme);
for (const id of ["empty-source", "filled-source"]) {
await expect(page.locator(`#${id} > .mbe-source`)).toHaveCSS("background-color", "rgba(0, 0, 0, 0)");
}
const code = page.locator("#rich-code > .mbe-block");
await expect(code).toBeVisible();
const expected = await code.evaluate(() => {
const sample = document.createElement("div");
sample.style.backgroundColor = "var(--mu-well)";
document.body.append(sample);
const color = getComputedStyle(sample).backgroundColor;
sample.remove();
return color;
});
expect(expected).not.toBe("rgba(0, 0, 0, 0)");
await expect(code).toHaveCSS("background-color", expected);
await page.locator("main").screenshot({ path: testInfo.outputPath(`${theme}.png`) });
});
}
+3 -3
View File
@@ -96,7 +96,7 @@ const toggleSourceMode = () => {
};
describe("MemoEditor source mode", () => {
it("shows the Markdown label only while source mode is on", () => {
it("switches modes without adding a toolbar label", () => {
const { container } = mount();
expect(sourceText(container)).toBeNull();
expect(screen.queryByText("Markdown", { exact: true })).not.toBeInTheDocument();
@@ -105,7 +105,7 @@ describe("MemoEditor source mode", () => {
expect(sourceText(container)).not.toBeNull();
expect(updateEditorSetting).toHaveBeenLastCalledWith(expect.objectContaining({ sourceMode: true }), expect.anything());
expect(screen.getByText("Markdown", { exact: true })).toHaveAttribute("aria-label", "editor.markdown-mode");
expect(screen.queryByText("Markdown", { exact: true })).not.toBeInTheDocument();
expect(container.querySelector(".memo-editor-content")).toHaveAttribute("data-source-mode");
toggleSourceMode();
@@ -130,7 +130,7 @@ describe("MemoEditor source mode", () => {
const { container } = mount();
expect(sourceText(container)).not.toBeNull();
expect(screen.getByText("Markdown", { exact: true })).toBeInTheDocument();
expect(screen.queryByText("Markdown", { exact: true })).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "common.add" }));
expect(screen.getByRole("menuitemcheckbox", { name: "editor.markdown-mode" })).toHaveAttribute("aria-checked", "true");
});