import type { Editor } from '@tiptap/core' import { getMountedEditorView } from '@admin/utils/editor/editor-view' import { getSelectionTableCell } from '@admin/utils/editor/get-selection-table-cell' import { findTable } from '@tiptap/pm/tables' export function getSelectionTableElement(editor: Editor) { const view = getMountedEditorView(editor) if (!view) return null const table = findTable(editor.state.selection.$from) if (!table) return getSelectionTableCell(editor)?.closest('table') ?? null const tableDom = view.nodeDOM(table.pos) if (tableDom instanceof HTMLTableElement) return tableDom if (!(tableDom instanceof HTMLElement)) return null const tableElement = tableDom.querySelector('table') return tableElement instanceof HTMLTableElement ? tableElement : tableDom }