import type { ContentEditorSelectedMath } from '@admin/utils/editor/content-editor-selected-math' import type { Editor } from '@tiptap/react' import { NodeSelection } from '@tiptap/pm/state' export function getSelectedContentEditorMath( editor: Editor | null ): ContentEditorSelectedMath | null { if (!editor) return null const { selection } = editor.state if (!(selection instanceof NodeSelection)) return null const node = selection.node const pos = selection.from if (node.type.name === 'inlineMath') { const raw = typeof node.attrs.raw === 'string' ? node.attrs.raw : '' const delimiter = node.attrs.delimiter === '\\(' ? '\\(' : '$' return { delimiter, kind: 'inline', node, pos, raw, value: raw } } if (node.type.name === 'rawContentBlock' && node.attrs.kind === 'math') { const raw = typeof node.attrs.raw === 'string' ? node.attrs.raw : '' return { kind: 'display', node, pos, raw, value: raw } } return null }