2023-03-28 20:21:16 +08:00
|
|
|
import { EditorState } from '@tiptap/pm/state'
|
|
|
|
|
2023-08-03 16:19:32 +08:00
|
|
|
import { findParentNode } from './findParentNode.js'
|
|
|
|
|
|
|
|
export const isAtEndOfNode = (state: EditorState, nodeType?: string) => {
|
|
|
|
const { $from, $to, $anchor } = state.selection
|
|
|
|
|
|
|
|
if (nodeType) {
|
|
|
|
const parentNode = findParentNode(node => node.type.name === nodeType)(state.selection)
|
|
|
|
|
|
|
|
if (!parentNode) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
const $parentPos = state.doc.resolve(parentNode.pos + 1)
|
|
|
|
|
|
|
|
if ($anchor.pos + 1 === $parentPos.end()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2023-03-28 20:21:16 +08:00
|
|
|
|
|
|
|
if ($to.parentOffset < $to.parent.nodeSize - 2 || $from.pos !== $to.pos) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|