diff --git a/packages/client/src/tiptap/core/extensions/table-cell.tsx b/packages/client/src/tiptap/core/extensions/table-cell.tsx index 21c08d0f..51966045 100644 --- a/packages/client/src/tiptap/core/extensions/table-cell.tsx +++ b/packages/client/src/tiptap/core/extensions/table-cell.tsx @@ -1,20 +1,7 @@ -import { mergeAttributes } from '@tiptap/core'; import { TableCell as BuiltInTableCell } from '@tiptap/extension-table-cell'; -import ReactDOM from 'react-dom'; -import { Button } from '@douyinfe/semi-ui'; -import { IconDelete, IconPlus } from '@douyinfe/semi-icons'; -import { Tooltip } from 'components/tooltip'; -import { Plugin, PluginKey } from 'prosemirror-state'; +import { Plugin } from 'prosemirror-state'; import { Decoration, DecorationSet } from 'prosemirror-view'; -import { - getCellsInRow, - getCellsInColumn, - isRowSelected, - isTableSelected, - selectRow, - selectTable, -} from 'tiptap/prose-utils'; -import { FloatMenuView } from 'tiptap/views/float-menu'; +import { getCellsInColumn, selectTable, isRowSelected, isTableSelected, selectRow } from 'tiptap/prose-utils'; export const TableCell = BuiltInTableCell.extend({ addAttributes() { @@ -40,173 +27,68 @@ export const TableCell = BuiltInTableCell.extend({ }; }, - renderHTML({ HTMLAttributes }) { - let totalWidth = 0; - let fixedWidth = true; + addProseMirrorPlugins() { + return [ + new Plugin({ + props: { + decorations: (state) => { + const { doc, selection } = state; + const decorations: Decoration[] = []; + const cells = getCellsInColumn(0)(selection); - if (HTMLAttributes.colwidth) { - HTMLAttributes.colwidth.forEach((col) => { - if (!col) { - fixedWidth = false; - } else { - totalWidth += col; - } - }); - } else { - fixedWidth = false; - } + if (cells) { + cells.forEach(({ pos }, index) => { + if (index === 0) { + decorations.push( + Decoration.widget(pos + 1, () => { + let className = 'grip-table'; + const selected = isTableSelected(selection); + if (selected) { + className += ' selected'; + } + const grip = document.createElement('a'); + grip.className = className; + grip.addEventListener('mousedown', (event) => { + event.preventDefault(); + event.stopImmediatePropagation(); + this.editor.view.dispatch(selectTable(this.editor.state.tr)); + // this.options.onSelectTable(state); + }); + return grip; + }) + ); + } + decorations.push( + Decoration.widget(pos + 1, () => { + const rowSelected = isRowSelected(index)(selection); - if (fixedWidth && totalWidth > 0) { - HTMLAttributes.style = `width: ${totalWidth}px;`; - } else if (totalWidth && totalWidth > 0) { - HTMLAttributes.style = `min-width: ${totalWidth}px`; - } else { - HTMLAttributes.style = null; - } + let className = 'grip-row'; + if (rowSelected) { + className += ' selected'; + } + if (index === 0) { + className += ' first'; + } + if (index === cells.length - 1) { + className += ' last'; + } + const grip = document.createElement('a'); + grip.className = className; + grip.addEventListener('mousedown', (event) => { + event.preventDefault(); + event.stopImmediatePropagation(); + this.editor.view.dispatch(selectRow(index)(this.editor.state.tr)); + }); + return grip; + }) + ); + }); + } - return ['td', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]; + return DecorationSet.create(doc, decorations); + }, + }, + }), + ]; }, - - // addProseMirrorPlugins() { - // const extensionThis = this; - // let selectedRowIndex = -1; - - // return [ - // new Plugin({ - // key: new PluginKey(`${this.name}FloatMenu`), - // view: () => - // new FloatMenuView({ - // editor: this.editor, - // tippyOptions: { - // zIndex: 100, - // offset: [-28, 0], - // }, - // shouldShow: ({ editor }, floatMenuView) => { - // if (!editor.isEditable) { - // return false; - // } - // if (isTableSelected(editor.state.selection)) { - // return false; - // } - // const cells = getCellsInColumn(0)(editor.state.selection); - // if (selectedRowIndex > -1) { - // // 获取当前行的第一个单元格的位置 - // const rowCells = getCellsInRow(selectedRowIndex)(editor.state.selection); - // if (rowCells && rowCells[0]) { - // const node = editor.view.nodeDOM(rowCells[0].pos) as HTMLElement; - // if (node) { - // const el = node.querySelector('a.grip-row') as HTMLElement; - // if (el) { - // floatMenuView.parentNode = el; - // } - // } - // } - // } - // return !!cells?.some((cell, index) => isRowSelected(index)(editor.state.selection)); - // }, - // init: (dom, editor) => { - // dom.classList.add('bubble-memu-table-cell'); - // dom.classList.add('row'); - // ReactDOM.render( - // <> - // - //