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

30 lines
792 B
TypeScript
Raw Normal View History

2020-03-30 06:15:20 +08:00
import { EditorState } from 'prosemirror-state'
import { Node, NodeType } from 'prosemirror-model'
2020-11-30 22:40:16 +08:00
import objectIncludes from '../utilities/objectIncludes'
import getNodeType from '../helpers/getNodeType'
2020-03-30 06:15:20 +08:00
2020-11-30 22:40:16 +08:00
export default function nodeIsActive(state: EditorState, typeOrName: NodeType | string | null, attributes = {}) {
const { from, to } = state.selection
const type = typeOrName
? getNodeType(typeOrName, state.schema)
: null
2020-03-30 06:15:20 +08:00
2020-11-30 22:40:16 +08:00
let nodes: Node[] = []
2020-03-30 06:15:20 +08:00
2020-11-30 22:40:16 +08:00
state.doc.nodesBetween(from, to, node => {
nodes = [...nodes, node]
2020-11-30 16:21:31 +08:00
})
2020-11-30 22:40:16 +08:00
const nodeWithAttributes = nodes
.filter(node => {
if (!type) {
return true
}
return type.name === node.type.name
})
.find(node => objectIncludes(node.attrs, attributes))
return !!nodeWithAttributes
2020-03-30 06:15:20 +08:00
}