add getNodeAttrs

This commit is contained in:
Philipp Kühn 2020-04-11 11:45:41 +02:00
parent bb742c1db2
commit 303f6b55fe
2 changed files with 24 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import elementFromString from './utils/elementFromString'
import getAllMethodNames from './utils/getAllMethodNames'
import nodeIsActive from './utils/nodeIsActive'
import markIsActive from './utils/markIsActive'
import getNodeAttrs from './utils/getNodeAttrs'
import getMarkAttrs from './utils/getMarkAttrs'
import removeElement from './utils/removeElement'
import getSchemaTypeByName from './utils/getSchemaTypeByName'
@ -214,6 +215,10 @@ export class Editor extends EventEmitter {
this.emit('update', { transaction })
}
public getNodeAttrs(name: string) {
return getNodeAttrs(this.state, this.schema.nodes[name])
}
public getMarkAttrs(name: string) {
return getMarkAttrs(this.state, this.schema.marks[name])
}

View File

@ -0,0 +1,19 @@
import { EditorState } from 'prosemirror-state'
import { Node, NodeType } from 'prosemirror-model'
export default function getNodeAttrs(state: EditorState, type: NodeType) {
const { from, to } = state.selection
let nodes: Node[] = []
state.doc.nodesBetween(from, to, node => {
nodes = [...nodes, node]
})
const node = nodes.find(nodeItem => nodeItem.type.name === type.name)
if (node) {
return node.attrs
}
return {}
}