memos/web/src/components/MemoEditor/state/reducer.ts
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

89 lines
1.8 KiB
TypeScript

import type { EditorAction, EditorState } from "./types";
import { createInitialState } from "./types";
export function editorReducer(state: EditorState, action: EditorAction): EditorState {
switch (action.type) {
case "INIT_MEMO":
return {
...state,
content: action.payload.content,
metadata: action.payload.metadata,
timestamps: action.payload.timestamps,
};
case "UPDATE_CONTENT":
return {
...state,
content: action.payload,
};
case "SET_METADATA":
return {
...state,
metadata: {
...state.metadata,
...action.payload,
},
};
case "ADD_LOCAL_FILE":
return {
...state,
localFiles: [...state.localFiles, action.payload],
};
case "REMOVE_LOCAL_FILE":
return {
...state,
localFiles: state.localFiles.filter((f) => f.previewUrl !== action.payload),
};
case "SET_LOCAL_FILES":
return {
...state,
localFiles: action.payload,
};
case "TOGGLE_FOCUS_MODE":
return {
...state,
ui: {
...state.ui,
isFocusMode: !state.ui.isFocusMode,
},
};
case "SET_LOADING":
return {
...state,
ui: {
...state.ui,
isLoading: {
...state.ui.isLoading,
[action.payload.key]: action.payload.value,
},
},
};
case "SET_TIMESTAMPS":
return {
...state,
timestamps: {
...state.timestamps,
...action.payload,
},
};
case "SET_RECORDER_BUSY":
return {
...state,
recorderBusy: action.payload,
};
case "RESET":
return createInitialState();
default:
return state;
}
}