memos/web/tests/editor-keys.test.ts
boojack 177d65a90e feat(web): add max-columns memo feed layout
Replace the single-column-only feed with a max-columns model: one
Columns setting (1 / 2 / 3 / ∞) where 1 is the reading list and
anything wider packs into a Google-Keep-style column grid. There is
deliberately no separate list/grid mode — the setting is a ceiling,
and widths that only fit one column fall back to the flow list.

- ColumnGrid: absolute-positioned packing that only translates cards,
  so appends and reorders never remount them; sticky column assignment
  keeps existing cards in place when new memos arrive; tiles are capped
  at 360px with a fade; columns clamp to 420px and center.
- Column one is the action column: the composer and active filters
  stack as its first tile, and a just-created memo is pinned directly
  beneath them.
- Multi-column always renders compact cards (policy centralized in
  PagedMemoList and threaded through renderer(memo, { compact })).
- Setting persists in ViewContext localStorage; the settings menu
  derives its options from the context's canonical value list.
2026-07-06 23:55:36 +08:00

118 lines
4.2 KiB
TypeScript

import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import React from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, it } from "vitest";
import { MemoMarkdownRenderer } from "@/components/MemoContent/MemoMarkdownRenderer";
import { buildEditorExtensions } from "@/components/MemoEditor/Editor/extensions";
function makeView(doc: string, onSubmit: () => void = () => {}) {
return new EditorView({
state: EditorState.create({
doc,
extensions: buildEditorExtensions({ placeholder: "", onChange: () => {}, onUpdate: () => {}, onSubmit, getTags: () => [] }),
}),
parent: document.body,
});
}
function press(view: EditorView, key: string, opts: KeyboardEventInit = {}) {
view.contentDOM.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true, cancelable: true, ...opts }));
}
/** Place the cursor on the given 1-based line, then press Tab/Shift-Tab. */
function tabOnLine(view: EditorView, lineNumber: number, shiftKey = false) {
const line = view.state.doc.line(lineNumber);
view.dispatch({ selection: { anchor: line.to } });
press(view, "Tab", { shiftKey });
}
describe("editor key bindings", () => {
it("Cmd+Enter submits without inserting a blank line", () => {
let submitted = 0;
const view = makeView("hello", () => {
submitted += 1;
});
view.dispatch({ selection: { anchor: 5 } });
press(view, "Enter", { metaKey: true });
// The save shortcut must outrank defaultKeymap's Mod-Enter (insertBlankLine).
expect(submitted).toBe(1);
expect(view.state.doc.toString()).toBe("hello");
view.destroy();
});
it("Ctrl+Enter also submits without editing the document", () => {
let submitted = 0;
const view = makeView("hello", () => {
submitted += 1;
});
press(view, "Enter", { ctrlKey: true });
expect(submitted).toBe(1);
expect(view.state.doc.toString()).toBe("hello");
view.destroy();
});
it("plain Enter still inserts a newline", () => {
const view = makeView("hello");
view.dispatch({ selection: { anchor: 5 } });
press(view, "Enter");
expect(view.state.doc.toString()).toBe("hello\n");
view.destroy();
});
it("Tab indents a non-list line by two spaces", () => {
const view = makeView("hello");
view.dispatch({ selection: { anchor: 0 } });
press(view, "Tab");
expect(view.state.doc.toString()).toBe(" hello");
view.destroy();
});
it("Escape blurs the editor (keyboard escape hatch)", () => {
const view = makeView("x");
view.focus();
expect(view.hasFocus).toBe(true);
press(view, "Escape");
expect(view.hasFocus).toBe(false);
view.destroy();
});
it("Tab nests an ordered item: marker-aligned indent + renumbered to 1", () => {
const view = makeView("1. a\n2. b");
tabOnLine(view, 2);
// 3-space indent (past "1. ") AND renumbered 2 -> 1 so CommonMark nests it.
expect(view.state.doc.toString()).toBe("1. a\n 1. b");
view.destroy();
});
it("Tab nests a bullet item to two spaces", () => {
const view = makeView("- a\n- b");
tabOnLine(view, 2);
expect(view.state.doc.toString()).toBe("- a\n - b");
view.destroy();
});
it("Shift-Tab outdents a nested list item back to its parent level", () => {
const view = makeView("1. a\n 2. b");
tabOnLine(view, 2, true);
expect(view.state.doc.toString()).toBe("1. a\n2. b");
view.destroy();
});
it("Tab-nested ordered lists render nested in the display (no flat siblings)", () => {
const view = makeView("1. a\n2. b\n3. c");
tabOnLine(view, 2); // nest b under a
tabOnLine(view, 3); // nest c under a ...
tabOnLine(view, 3); // ... then under b
const md = view.state.doc.toString();
view.destroy();
// Each nested level renumbers to 1, which is what CommonMark requires to
// nest an ordered sublist (a 2./3. start can't interrupt the parent line).
expect(md).toBe("1. a\n 1. b\n 1. c");
// remark-gfm (the display) now reads this as three nested ordered lists.
const html = renderToStaticMarkup(
React.createElement(MemoMarkdownRenderer, { content: md, resolvedMentionUsernames: new Set<string>() }),
);
expect((html.match(/<ol/g) ?? []).length).toBe(3);
});
});