improve isActive for marks

This commit is contained in:
Philipp Kühn
2020-11-30 15:40:16 +01:00
parent 4fbdb0ff0c
commit c0911c315c
5 changed files with 59 additions and 70 deletions

View File

@@ -1,18 +1,29 @@
import { findParentNode, findSelectedNodeOfType } from 'prosemirror-utils'
import { EditorState } from 'prosemirror-state'
import { Node, NodeType } from 'prosemirror-model'
import objectIncludes from '../utilities/objectIncludes'
import getNodeType from '../helpers/getNodeType'
export default function nodeIsActive(state: EditorState, type: NodeType, attributes = {}) {
const predicate = (node: Node) => node.type === type
const node = findSelectedNodeOfType(type)(state.selection)
|| findParentNode(predicate)(state.selection)
export default function nodeIsActive(state: EditorState, typeOrName: NodeType | string | null, attributes = {}) {
const { from, to } = state.selection
const type = typeOrName
? getNodeType(typeOrName, state.schema)
: null
if (!Object.keys(attributes).length || !node) {
return !!node
}
let nodes: Node[] = []
return node.node.hasMarkup(type, {
...node.node.attrs,
...attributes,
state.doc.nodesBetween(from, to, node => {
nodes = [...nodes, node]
})
const nodeWithAttributes = nodes
.filter(node => {
if (!type) {
return true
}
return type.name === node.type.name
})
.find(node => objectIncludes(node.attrs, attributes))
return !!nodeWithAttributes
}