2020-04-11 17:45:41 +08:00
|
|
|
import { EditorState } from 'prosemirror-state'
|
|
|
|
import { Node, NodeType } from 'prosemirror-model'
|
2020-11-30 16:21:31 +08:00
|
|
|
import getNodeType from './getNodeType'
|
2020-04-11 17:45:41 +08:00
|
|
|
|
2021-04-21 15:43:31 +08:00
|
|
|
export default function getNodeAttributes(state: EditorState, typeOrName: string | NodeType): Record<string, any> {
|
2020-11-30 16:21:31 +08:00
|
|
|
const type = getNodeType(typeOrName, state.schema)
|
2020-04-11 17:45:41 +08:00
|
|
|
const { from, to } = state.selection
|
|
|
|
let nodes: Node[] = []
|
|
|
|
|
|
|
|
state.doc.nodesBetween(from, to, node => {
|
|
|
|
nodes = [...nodes, node]
|
|
|
|
})
|
|
|
|
|
2020-04-11 19:05:28 +08:00
|
|
|
const node = nodes
|
|
|
|
.reverse()
|
|
|
|
.find(nodeItem => nodeItem.type.name === type.name)
|
2020-04-11 17:45:41 +08:00
|
|
|
|
|
|
|
if (node) {
|
2020-04-11 20:17:31 +08:00
|
|
|
return { ...node.attrs }
|
2020-04-11 17:45:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return {}
|
|
|
|
}
|