Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(web): insert formulas at the caret instead of the note end

tianma-if10c70f49bc80
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsx
index d333ac26..ef75278f 100644
--- a/apps/web/src/components/EditorPane.tsx
+++ b/apps/web/src/components/EditorPane.tsx
@@ -603,7 +603,8 @@ const RichEditorPane = ({
const mobileSaveTimerRef = useRef<number | null>(null);
const fileInputRef = useRef<HTMLInputElement | null>(null);
const openExternalLinkDialogRef = useRef<() => void>(() => undefined);
- const openMathFormulaRef = useRef<(kind?: "inline" | "block") => void>(() => undefined);
+ const openMathFormulaRef = useRef<(kind?: "inline" | "block", range?: { from: number; to: number }) => void>(() => undefined);
+ const mathFormulaDraftRef = useRef<MathFormulaDraft | null>(null);
const mathClickRef = useRef<(node: { attrs: Record<string, unknown> }, pos: number, kind: "inline" | "block") => void>(() => undefined);
const mathematicsExtensionsRef = useRef(createEdgeEverMathematics({
onInlineClick: (node, pos) => mathClickRef.current(node, pos, "inline"),
@@ -684,7 +685,7 @@ const RichEditorPane = ({
openAttachmentPicker: () => fileInputRef.current?.click(),
openExternalLinkPicker: () => openExternalLinkDialogRef.current(),
openNoteLinkPicker: () => setNoteLinkPickerOpen(true),
- openMathFormula: (kind) => openMathFormulaRef.current(kind),
+ openMathFormula: (kind, range) => openMathFormulaRef.current(kind, range),
};
}
const slashCommandExtensionRef = useRef<ReturnType<typeof createSlashCommandExtension> | null>(null);
@@ -1379,25 +1380,31 @@ const RichEditorPane = ({
openExternalLinkDialogRef.current = openExternalLinkDialog;
- const openMathFormula = useCallback((kind: "inline" | "block" = "inline") => {
+ const openMathFormula = useCallback((kind: "inline" | "block" = "inline", range?: { from: number; to: number }) => {
if (effectiveReadOnly || !isEditorReady(editor) || useMarkdownSourceEditor || useMobilePlainTextEditor) {
return;
}
- const existing = resolveMathFormulaTarget(editor);
- if (existing) {
- setMathFormulaDraft(existing);
- setMathFormulaOpen(true);
- return;
+ if (!range) {
+ const existing = resolveMathFormulaTarget(editor);
+ if (existing) {
+ mathFormulaDraftRef.current = existing;
+ setMathFormulaDraft(existing);
+ setMathFormulaOpen(true);
+ return;
+ }
}
- const { from, to } = editor.state.selection;
- setMathFormulaDraft({
+ const from = range?.from ?? editor.state.selection.from;
+ const to = range?.to ?? editor.state.selection.to;
+ const draft: MathFormulaDraft = {
kind,
- latex: selectedTextAsLatex(editor),
+ latex: range ? "" : selectedTextAsLatex(editor),
from,
to,
- });
+ };
+ mathFormulaDraftRef.current = draft;
+ setMathFormulaDraft(draft);
setMathFormulaOpen(true);
}, [editor, effectiveReadOnly, useMarkdownSourceEditor, useMobilePlainTextEditor]);
@@ -1406,12 +1413,13 @@ const RichEditorPane = ({
if (effectiveReadOnly || !isEditorReady(editor) || useMarkdownSourceEditor || useMobilePlainTextEditor) {
return;
}
- const existing = resolveMathFormulaTarget(editor, pos);
- setMathFormulaDraft(existing ?? {
+ const existing = resolveMathFormulaTarget(editor, pos) ?? {
kind,
latex: String(_node.attrs.latex ?? ""),
pos,
- });
+ };
+ mathFormulaDraftRef.current = existing;
+ setMathFormulaDraft(existing);
setMathFormulaOpen(true);
};
@@ -3467,15 +3475,24 @@ const RichEditorPane = ({
onOpenChange={setMathFormulaOpen}
onApply={(draft) => {
if (!isEditorReady(editor) || effectiveReadOnly) return;
- editor.commands.focus();
- applyMathFormula(editor, draft);
+ const stored = mathFormulaDraftRef.current;
+ applyMathFormula(editor, {
+ ...draft,
+ from: stored?.from ?? draft.from,
+ to: stored?.to ?? draft.to,
+ pos: stored?.pos ?? draft.pos,
+ });
+ editor.chain().focus(stored?.from ?? draft.from ?? null, { scrollIntoView: true }).run();
}}
onRemove={
mathFormulaDraft && typeof mathFormulaDraft.pos === "number"
? () => {
if (!isEditorReady(editor) || effectiveReadOnly) return;
- editor.commands.focus();
- deleteMathFormula(editor, { kind: mathFormulaDraft.kind, pos: mathFormulaDraft.pos as number });
+ const stored = mathFormulaDraftRef.current;
+ const pos = stored?.pos ?? mathFormulaDraft.pos;
+ if (typeof pos !== "number") return;
+ deleteMathFormula(editor, { kind: stored?.kind ?? mathFormulaDraft.kind, pos });
+ editor.chain().focus(pos, { scrollIntoView: true }).run();
}
: undefined
}
diff --git a/apps/web/src/components/editor/SlashCommandMenu.tsx b/apps/web/src/components/editor/SlashCommandMenu.tsx
index bc5d1d20..ae711e71 100644
--- a/apps/web/src/components/editor/SlashCommandMenu.tsx
+++ b/apps/web/src/components/editor/SlashCommandMenu.tsx
@@ -79,7 +79,7 @@ export type SlashCommandActions = {
openAttachmentPicker: () => void;
openExternalLinkPicker: () => void;
openNoteLinkPicker: () => void;
- openMathFormula: (kind: "inline" | "block") => void;
+ openMathFormula: (kind: "inline" | "block", range?: { from: number; to: number }) => void;
};
export type SlashCommandItem = {
@@ -256,11 +256,11 @@ const runSlashCommand = ({
case "table": chain.insertTable({ rows: 3, cols: 3, withHeaderRow: true }).run(); break;
case "inline-math":
chain.run();
- window.requestAnimationFrame(() => actions.openMathFormula("inline"));
+ actions.openMathFormula("inline", { from: range.from, to: range.from });
break;
case "block-math":
chain.run();
- window.requestAnimationFrame(() => actions.openMathFormula("block"));
+ actions.openMathFormula("block", { from: range.from, to: range.from });
break;
case "current-date": chain.insertContent(formatCurrentDate(new Date())).run(); break;
case "current-time": chain.insertContent(formatCurrentTime(new Date())).run(); break;
diff --git a/apps/web/src/components/editor/math-formula.test.mjs b/apps/web/src/components/editor/math-formula.test.mjs
index ee20fcb8..10e9951a 100644
--- a/apps/web/src/components/editor/math-formula.test.mjs
+++ b/apps/web/src/components/editor/math-formula.test.mjs
@@ -79,6 +79,25 @@ describe("math formula commands", () => {
editor.destroy();
});
+ test("inserts at the stored caret even if the live selection moved to the end", () => {
+ const editor = createEditor({
+ type: "doc",
+ content: [
+ { type: "paragraph", content: [{ type: "text", text: "ab" }] },
+ { type: "paragraph", content: [{ type: "text", text: "cd" }] },
+ ],
+ });
+ editor.commands.setTextSelection(2);
+ editor.commands.setTextSelection(editor.state.doc.content.size);
+ expect(applyMathFormula(editor, { kind: "inline", latex: "x", from: 2, to: 2 })).toBe(true);
+ expect(editor.getJSON().content[0].content).toEqual([
+ { type: "text", text: "a" },
+ { type: "inlineMath", attrs: { latex: "x" } },
+ { type: "text", text: "b" },
+ ]);
+ editor.destroy();
+ });
+
test("rejects empty latex", () => {
const editor = createEditor({
type: "doc",
diff --git a/apps/web/src/components/editor/math-formula.ts b/apps/web/src/components/editor/math-formula.ts
index e9075200..18e93d3e 100644
--- a/apps/web/src/components/editor/math-formula.ts
+++ b/apps/web/src/components/editor/math-formula.ts
@@ -47,14 +47,12 @@ export const applyMathFormula = (editor: Editor, draft: MathFormulaDraft) => {
}
const from = draft.from ?? editor.state.selection.from;
- const to = draft.to ?? editor.state.selection.to;
- const chain = editor.chain();
- if (to > from) {
- chain.deleteRange({ from, to });
- }
- return draft.kind === "inline"
- ? chain.insertInlineMath({ latex, pos: from }).run()
- : chain.insertBlockMath({ latex, pos: from }).run();
+ const to = draft.to ?? from;
+ const content = draft.kind === "inline"
+ ? { type: INLINE_MATH_NODE_TYPE, attrs: { latex } }
+ : { type: BLOCK_MATH_NODE_TYPE, attrs: { latex } };
+
+ return editor.chain().insertContentAt(from === to ? from : { from, to }, content).run();
};
export const deleteMathFormula = (editor: Editor, target: Pick<MathFormulaTarget, "kind" | "pos">) => {