mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-25 12:39:03 +08:00
add getNodeAttrs
This commit is contained in:
parent
bb742c1db2
commit
303f6b55fe
@ -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])
|
||||
}
|
||||
|
19
packages/core/src/utils/getNodeAttrs.ts
Normal file
19
packages/core/src/utils/getNodeAttrs.ts
Normal 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 {}
|
||||
}
|
Loading…
Reference in New Issue
Block a user