import type { ContentEditorSelectedMath } from '@admin/utils/editor/content-editor-selected-math' import type { Editor } from '@tiptap/react' import { getSelectedContentEditorMath } from '@admin/utils/editor/get-selected-content-editor-math' import { parseContentEditorMathInput } from '@admin/utils/editor/parse-content-editor-math-input' import { NodeSelection } from '@tiptap/pm/state' export function updateSelectedContentEditorMath( editor: Editor | null, input: string, selectedMathOverride?: ContentEditorSelectedMath | null ): boolean { const selectedMath = selectedMathOverride ?? getSelectedContentEditorMath(editor) if (!editor?.isEditable || !selectedMath) return false const math = parseContentEditorMathInput(input, selectedMath.kind) if (!math) return false const attrs = selectedMath.kind === 'display' ? { ...selectedMath.node.attrs, kind: 'math', raw: math.markdown } : { ...selectedMath.node.attrs, delimiter: math.delimiter ?? selectedMath.delimiter ?? '$', raw: math.raw } const tr = editor.state.tr.setNodeMarkup(selectedMath.pos, undefined, attrs) tr.setSelection(NodeSelection.create(tr.doc, selectedMath.pos)) editor.view.dispatch(tr.scrollIntoView()) editor.commands.focus() return true }