memos/web/src/components/MemoContent/markdown/List.tsx
johnnyjoygh 5a73d7d3e5 refactor: rebuild the editor on CodeMirror as decorated source
Rebuild the memo editor as a single CodeMirror 6 "decorated source"
editor. The document is the raw markdown, stored verbatim and styled in
place (markers stay visible), so the editor never serializes a tree back
to markdown — removing the round-trip fidelity bug class (inline images,
setext headings, ordered-list indentation, HTML entities) that the old
editor needed per-case patches for.

- MemoEditor/Editor: CodeMirror 6 + lang-markdown (GFM). Tokens, heading
  lines, #tag/@mention, and the autocomplete popover are styled in plain
  CSS (Editor/editor.css) with theme tokens, not a CSS-in-JS theme.
- Tab/Shift-Tab nest/outdent list items (marker-aware, ordered items
  renumbered so nesting is CommonMark-valid); Escape blurs; #tag
  autocomplete sourced from useTagCounts.
- Focus-mode toolbar reimplemented as markdown-text ops; active state read
  from the Lezer tree via the backend-agnostic formatting/commands catalog.
- Remove the old serialize-back-to-markdown editor (its Editor dir,
  PlainEditor, the editor-mode system) and its now-unused dependencies
  (marked, textarea-caret, and the rich-text editor packages).
- Consolidate toolbar components under Toolbar/. Read-only MemoContent
  rendering is unchanged.
2026-06-28 22:59:14 +08:00

116 lines
3.7 KiB
TypeScript

import { Children, cloneElement, isValidElement, type ReactElement, type ReactNode } from "react";
import { markdownStyles } from "@/lib/markdownStyles";
import { cn } from "@/lib/utils";
import { TASK_LIST_CLASS, TASK_LIST_ITEM_CLASS } from "../constants";
import { NestedMarkdownRenderContext } from "../MarkdownRenderContext";
import type { ReactMarkdownProps } from "./types";
interface TaskListChildProps {
children?: ReactNode;
node?: {
tagName?: string;
properties?: {
type?: unknown;
};
};
type?: string;
}
const isCheckboxInput = (child: ReactNode): child is ReactElement<TaskListChildProps> => {
return (
isValidElement<TaskListChildProps>(child) && (child.props.type === "checkbox" || child.props.node?.properties?.type === "checkbox")
);
};
const isParagraphElement = (child: ReactNode): child is ReactElement<TaskListChildProps> => {
return isValidElement<TaskListChildProps>(child) && (child.type === "p" || child.props.node?.tagName === "p");
};
const splitTaskListItemChildren = (children: ReactNode) => {
let checkbox: ReactNode;
const content: ReactNode[] = [];
Children.toArray(children).forEach((child) => {
if (!checkbox && isCheckboxInput(child)) {
checkbox = child;
return;
}
if (!checkbox && isParagraphElement(child)) {
const paragraphChildren: ReactNode[] = [];
Children.toArray(child.props.children).forEach((paragraphChild) => {
if (!checkbox && isCheckboxInput(paragraphChild)) {
checkbox = paragraphChild;
return;
}
paragraphChildren.push(paragraphChild);
});
if (checkbox) {
if (paragraphChildren.length > 0) {
content.push(cloneElement(child, undefined, ...paragraphChildren));
}
return;
}
}
content.push(child);
});
return { checkbox, content };
};
interface ListProps extends React.HTMLAttributes<HTMLUListElement | HTMLOListElement>, ReactMarkdownProps {
ordered?: boolean;
children: React.ReactNode;
}
/**
* List component for both regular and task lists (GFM)
* Detects task lists via the "contains-task-list" class added by remark-gfm
*/
export const List = ({ ordered, children, className, node: _node, ...domProps }: ListProps) => {
const Component = ordered ? "ol" : "ul";
const isTaskList = className?.includes(TASK_LIST_CLASS);
// Task list indentation is handled by task item grid columns; regular lists
// use the shared token (padding + list style).
const listClass = isTaskList ? "my-0 mb-2 list-outside list-none" : ordered ? markdownStyles.orderedList : markdownStyles.bulletList;
return (
<Component className={cn(listClass, className)} {...domProps}>
{children}
</Component>
);
};
interface ListItemProps extends React.LiHTMLAttributes<HTMLLIElement>, ReactMarkdownProps {
children: React.ReactNode;
}
/**
* List item component for both regular and task list items
* Detects task items via the "task-list-item" class added by remark-gfm
*/
export const ListItem = ({ children, className, node: _node, ...domProps }: ListItemProps) => {
const isTaskListItem = className?.includes(TASK_LIST_ITEM_CLASS);
if (isTaskListItem) {
const { checkbox, content } = splitTaskListItemChildren(children);
return (
<li className={cn(markdownStyles.taskListItem, className)} {...domProps}>
<NestedMarkdownRenderContext>
{checkbox}
<div className={markdownStyles.taskItemContent}>{content}</div>
</NestedMarkdownRenderContext>
</li>
);
}
return (
<li className={cn(markdownStyles.listItem, className)} {...domProps}>
<NestedMarkdownRenderContext>{children}</NestedMarkdownRenderContext>
</li>
);
};