You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
830 B
37 lines
830 B
import { findParentNodeClosestToPos, KeyboardShortcutCommand } from '@tiptap/core'
|
|
|
|
import { isCellSelection } from './isCellSelection'
|
|
|
|
export const deleteTableWhenAllCellsSelected: KeyboardShortcutCommand = ({ editor }) => {
|
|
const { selection } = editor.state
|
|
|
|
if (!isCellSelection(selection)) {
|
|
return false
|
|
}
|
|
|
|
let cellCount = 0
|
|
const table = findParentNodeClosestToPos(selection.ranges[0].$from, node => {
|
|
return node.type.name === 'table'
|
|
})
|
|
|
|
table?.node.descendants(node => {
|
|
if (node.type.name === 'table') {
|
|
return false
|
|
}
|
|
|
|
if (['tableCell', 'tableHeader'].includes(node.type.name)) {
|
|
cellCount += 1
|
|
}
|
|
})
|
|
|
|
const allCellsSelected = cellCount === selection.ranges.length
|
|
|
|
if (!allCellsSelected) {
|
|
return false
|
|
}
|
|
|
|
editor.commands.deleteTable()
|
|
|
|
return true
|
|
}
|