diff --git a/packages/core/src/Editor.ts b/packages/core/src/Editor.ts index 63a3d9b0a..392b443c7 100644 --- a/packages/core/src/Editor.ts +++ b/packages/core/src/Editor.ts @@ -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]) } diff --git a/packages/core/src/utils/getNodeAttrs.ts b/packages/core/src/utils/getNodeAttrs.ts new file mode 100644 index 000000000..ac4eaf6a9 --- /dev/null +++ b/packages/core/src/utils/getNodeAttrs.ts @@ -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 {} +}