tiptap/packages/core/src/helpers/isAtEndOfNode.ts
bdbch d1e879dfab
Fix List issues & add support for Mod keys (#4210)
* add support for mod-delete and mod-backspace

* fix backspace not working right behind a list

* move list helpers to core, add support for task lists

* add option to check for node in isAtEndOfNode

---------

Co-authored-by: bdbch <dominik@bdbch.com>
2023-08-03 10:19:32 +02:00

30 lines
659 B
TypeScript

import { EditorState } from '@tiptap/pm/state'
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
}
if ($to.parentOffset < $to.parent.nodeSize - 2 || $from.pos !== $to.pos) {
return false
}
return true
}