add deleteTableWhenAllCellsSelected
This commit is contained in:
23
packages/core/src/helpers/findParentNodeClosestToPos.ts
Normal file
23
packages/core/src/helpers/findParentNodeClosestToPos.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { ResolvedPos, Node as ProsemirrorNode } from 'prosemirror-model'
|
||||
|
||||
export type Predicate = (node: ProsemirrorNode) => boolean
|
||||
|
||||
export default function findParentNodeClosestToPos($pos: ResolvedPos, predicate: Predicate): ({
|
||||
pos: number,
|
||||
start: number,
|
||||
depth: number,
|
||||
node: ProsemirrorNode,
|
||||
} | undefined) {
|
||||
for (let i = $pos.depth; i > 0; i -= 1) {
|
||||
const node = $pos.node(i)
|
||||
|
||||
if (predicate(node)) {
|
||||
return {
|
||||
pos: i > 0 ? $pos.before(i) : 0,
|
||||
start: $pos.start(i),
|
||||
depth: i,
|
||||
node,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
6
packages/core/src/helpers/isCellSelection.ts
Normal file
6
packages/core/src/helpers/isCellSelection.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { CellSelection } from 'prosemirror-tables'
|
||||
import isObject from '../utilities/isObject'
|
||||
|
||||
export default function isCellSelection(value: unknown): value is CellSelection {
|
||||
return isObject(value) && value instanceof CellSelection
|
||||
}
|
||||
6
packages/core/src/helpers/isNodeSelection.ts
Normal file
6
packages/core/src/helpers/isNodeSelection.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { NodeSelection } from 'prosemirror-state'
|
||||
import isObject from '../utilities/isObject'
|
||||
|
||||
export default function isNodeSelection(value: unknown): value is NodeSelection {
|
||||
return isObject(value) && value instanceof NodeSelection
|
||||
}
|
||||
@@ -16,5 +16,9 @@ export { default as mergeAttributes } from './utilities/mergeAttributes'
|
||||
export { default as isActive } from './helpers/isActive'
|
||||
export { default as isMarkActive } from './helpers/isMarkActive'
|
||||
export { default as isNodeActive } from './helpers/isNodeActive'
|
||||
export { default as isNodeSelection } from './helpers/isNodeSelection'
|
||||
export { default as isTextSelection } from './helpers/isTextSelection'
|
||||
export { default as isCellSelection } from './helpers/isCellSelection'
|
||||
export { default as findParentNodeClosestToPos } from './helpers/findParentNodeClosestToPos'
|
||||
|
||||
export interface AllExtensions {}
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
import { Command, Node, mergeAttributes } from '@tiptap/core'
|
||||
import {
|
||||
Command,
|
||||
Node,
|
||||
mergeAttributes,
|
||||
isCellSelection,
|
||||
findParentNodeClosestToPos,
|
||||
} from '@tiptap/core'
|
||||
import {
|
||||
tableEditing,
|
||||
columnResizing,
|
||||
@@ -143,6 +149,39 @@ export const Table = Node.create({
|
||||
},
|
||||
|
||||
addKeyboardShortcuts() {
|
||||
const deleteTableWhenAllCellsSelected = () => {
|
||||
const { selection } = this.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
|
||||
}
|
||||
|
||||
this.editor.commands.deleteTable()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return {
|
||||
Tab: () => {
|
||||
if (this.editor.commands.goToNextCell()) {
|
||||
@@ -160,6 +199,10 @@ export const Table = Node.create({
|
||||
.run()
|
||||
},
|
||||
'Shift-Tab': () => this.editor.commands.goToPreviousCell(),
|
||||
Backspace: deleteTableWhenAllCellsSelected,
|
||||
'Mod-Backspace': deleteTableWhenAllCellsSelected,
|
||||
Delete: deleteTableWhenAllCellsSelected,
|
||||
'Mod-Delete': deleteTableWhenAllCellsSelected,
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user