tiptap/packages/core/src/utils/getNodeAttributes.ts

22 lines
480 B
TypeScript
Raw Normal View History

2020-04-11 17:45:41 +08:00
import { EditorState } from 'prosemirror-state'
import { Node, NodeType } from 'prosemirror-model'
2020-11-18 04:10:08 +08:00
export default function getNodeAttributes(state: EditorState, type: NodeType) {
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 {}
}