2023-02-03 00:37:33 +08:00
|
|
|
import { Node, NodeType } from '@tiptap/pm/model'
|
|
|
|
import { EditorState } from '@tiptap/pm/state'
|
2022-06-08 20:10:25 +08:00
|
|
|
|
2021-12-06 19:00:09 +08:00
|
|
|
import { getNodeType } from './getNodeType'
|
2020-04-11 17:45:41 +08:00
|
|
|
|
2023-02-03 00:37:33 +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
|
2021-09-23 01:43:55 +08:00
|
|
|
const nodes: Node[] = []
|
2020-04-11 17:45:41 +08:00
|
|
|
|
|
|
|
state.doc.nodesBetween(from, to, node => {
|
2021-09-23 01:43:55 +08:00
|
|
|
nodes.push(node)
|
2020-04-11 17:45:41 +08:00
|
|
|
})
|
|
|
|
|
2023-02-03 00:37:33 +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
|
|
|
}
|