tiptap/packages/core/src/helpers/nodeIsActive.ts

19 lines
594 B
TypeScript
Raw Normal View History

2020-03-30 06:15:20 +08:00
import { findParentNode, findSelectedNodeOfType } from 'prosemirror-utils'
import { EditorState } from 'prosemirror-state'
import { Node, NodeType } from 'prosemirror-model'
2020-11-30 16:21:31 +08:00
export default function nodeIsActive(state: EditorState, type: NodeType, attributes = {}) {
2020-03-30 06:15:20 +08:00
const predicate = (node: Node) => node.type === type
const node = findSelectedNodeOfType(type)(state.selection)
|| findParentNode(predicate)(state.selection)
2020-11-30 16:21:31 +08:00
if (!Object.keys(attributes).length || !node) {
2020-03-30 06:15:20 +08:00
return !!node
}
2020-11-30 16:21:31 +08:00
return node.node.hasMarkup(type, {
...node.node.attrs,
...attributes,
})
2020-03-30 06:15:20 +08:00
}