Replace Monaco with one CodeMirror editor and nest list items with Tab
Phase 4b. DocumentEditor.svelte replaces LiveWriter.svelte and the Monaco EditorHost.svelte. Each open document has one CodeMirror state. Write and Source are two compartment configurations of it, so they share the undo history. editorStates.ts keeps the state while the tab stays open. The Monaco model, the diff bridge, its timers, and sessions.ts are gone. - Source: syntax colors from the theme tokens, lazy language packages, line numbers, folding, and the current line in IDE, wrapped lines in Writing, several carets, and bracket matching and closing. - Lists (card 088e778b): Tab nests a list item and Shift+Tab moves it up, in Write and Source. Both renumber the list. The source keeps "1.". Write and Read show numbers, letters, then Roman numerals. - Indent (card 696c30ce): a fixed 4 spaces in Write and Source. - The code menu uses CodeMirror commands. The command palette item and the Monaco styles, search, theme, and workers are removed. - @codemirror/legacy-modes is pinned to 6.5.4. Version 6.5.5 uses parser types that do not match @codemirror/language 6.12. - 26 smoke tests use CodeMirror instead of Monaco. New tests: editor/listNesting and the markdownLists unit tests. - Docs: development, UI design, plugin host, user guide, and UI-040. pnpm check, pnpm test (341 of 341), and pnpm build pass. The full smoke suite passes, except one template test that passed on retry.
This commit is contained in:
+22
-18
@@ -1,9 +1,12 @@
|
||||
import { tr } from '../i18n/index.ts';
|
||||
import type { editor } from "monaco-editor";
|
||||
import { EditorSelection } from "@codemirror/state";
|
||||
import type { EditorView } from "@codemirror/view";
|
||||
import { redo, redoDepth, undo, undoDepth } from "@codemirror/commands";
|
||||
import { selectSelectionMatches } from "@codemirror/search";
|
||||
import { actionMenu, type ActionMenuItem } from "../ui/actionMenu";
|
||||
|
||||
/** Keep Monaco editing commands in the host's shared action menu. */
|
||||
export function codeMenu(view: editor.IStandaloneCodeEditor, report: (message: string) => void) {
|
||||
/** Keep source editing commands in the host's shared action menu. */
|
||||
export function codeMenu(view: EditorView, report: (message: string) => void) {
|
||||
let root: HTMLElement | undefined;
|
||||
function close(focus = true) {
|
||||
root?.remove(); root = undefined;
|
||||
@@ -17,16 +20,14 @@ export function codeMenu(view: editor.IStandaloneCodeEditor, report: (message: s
|
||||
window.addEventListener("resize", resize);
|
||||
function open(x: number, y: number) {
|
||||
close(false);
|
||||
const model = view.getModel(), selection = view.getSelection();
|
||||
if (!model || !selection) return;
|
||||
const version = model.getVersionId();
|
||||
const valid = () => view.getModel() === model && !model.isDisposed() && model.getVersionId() === version && !!view.getSelection()?.equalsRange(selection);
|
||||
const selected = model.getValueInRange(selection);
|
||||
const state = view.state, main = state.selection.main;
|
||||
const valid = () => view.state.doc === state.doc && view.state.selection.eq(state.selection);
|
||||
const selected = state.sliceDoc(main.from, main.to);
|
||||
const word = main.empty ? state.wordAt(main.head) : main;
|
||||
const replace = (text: string) => {
|
||||
if (!valid()) return;
|
||||
view.pushUndoStop();
|
||||
view.executeEdits("code-menu", [{ range: selection, text }]);
|
||||
view.pushUndoStop(); view.focus();
|
||||
view.dispatch(view.state.replaceSelection(text), { userEvent: "input", scrollIntoView: true });
|
||||
view.focus();
|
||||
};
|
||||
const clipboard = async (kind: "cut" | "copy" | "paste") => {
|
||||
close();
|
||||
@@ -35,19 +36,22 @@ export function codeMenu(view: editor.IStandaloneCodeEditor, report: (message: s
|
||||
else { await navigator.clipboard.writeText(selected); if (kind === "cut") replace(""); }
|
||||
} catch { report("Clipboard unavailable. Use the keyboard shortcut to copy, cut, or paste."); }
|
||||
};
|
||||
const run = (id: string) => { close(); void view.getAction(id)?.run(); };
|
||||
const changeAll = () => {
|
||||
close();
|
||||
if (!valid() || !word) return;
|
||||
if (main.empty) view.dispatch({ selection: EditorSelection.single(word.from, word.to) });
|
||||
selectSelectionMatches(view);
|
||||
};
|
||||
const items: ActionMenuItem[] = [
|
||||
{ label: tr("Undo"), icon: "undo", shortcut: "Ctrl+Z", disabled: !model.canUndo(), run: () => { close(); view.trigger("code-menu", "undo", null); } },
|
||||
{ label: tr("Redo"), icon: "redo", shortcut: "Ctrl+Shift+Z", disabled: !model.canRedo(), run: () => { close(); view.trigger("code-menu", "redo", null); } },
|
||||
{ label: tr("Undo"), icon: "undo", shortcut: "Ctrl+Z", disabled: !undoDepth(state), run: () => { close(); undo(view); } },
|
||||
{ label: tr("Redo"), icon: "redo", shortcut: "Ctrl+Shift+Z", disabled: !redoDepth(state), run: () => { close(); redo(view); } },
|
||||
{ separator: true },
|
||||
{ label: tr("Change all occurrences"), icon: "select", shortcut: "Ctrl+F2", disabled: !view.getAction("editor.action.changeAll")?.isSupported(), run: () => run("editor.action.changeAll") },
|
||||
{ label: tr("Change all occurrences"), icon: "select", shortcut: "Ctrl+Shift+L", disabled: !word || word.empty, run: changeAll },
|
||||
{ separator: true },
|
||||
{ label: tr("Cut"), icon: "cut", shortcut: "Ctrl+X", disabled: !selected, run: () => void clipboard("cut") },
|
||||
{ label: tr("Copy"), icon: "copy", shortcut: "Ctrl+C", disabled: !selected, run: () => void clipboard("copy") },
|
||||
{ label: tr("Paste"), icon: "paste", shortcut: "Ctrl+V", run: () => void clipboard("paste") },
|
||||
{ label: tr("Select all"), icon: "select", shortcut: "Ctrl+A", run: () => { close(); view.setSelection(model.getFullModelRange()); } },
|
||||
{ separator: true },
|
||||
{ label: tr("Command palette"), icon: "terminal", shortcut: "F1", run: () => run("editor.action.quickCommand") },
|
||||
{ label: tr("Select all"), icon: "select", shortcut: "Ctrl+A", run: () => { close(); view.dispatch({ selection: { anchor: 0, head: view.state.doc.length } }); } },
|
||||
];
|
||||
root = actionMenu(x, y, items, close, tr("Code text actions"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user