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

25 lines
601 B
TypeScript
Raw Normal View History

2020-04-11 17:45:41 +08:00
import { Node, NodeType } from 'prosemirror-model'
import { EditorState } from 'prosemirror-state'
import { getNodeType } from './getNodeType'
2020-04-11 17:45:41 +08:00
export 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
const nodes: Node[] = []
2020-04-11 17:45:41 +08:00
state.doc.nodesBetween(from, to, node => {
nodes.push(node)
2020-04-11 17:45:41 +08:00
})
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
2021-09-23 04:45:27 +08:00
if (!node) {
return {}
2020-04-11 17:45:41 +08:00
}
2021-09-23 04:45:27 +08:00
return { ...node.attrs }
2020-04-11 17:45:41 +08:00
}