add getNodeAttrs, fix #622
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
export { default as getMarkAttrs } from './utils/getMarkAttrs'
|
||||
export { default as getNodeAttrs } from './utils/getNodeAttrs'
|
||||
export { default as getMarkRange } from './utils/getMarkRange'
|
||||
export { default as markIsActive } from './utils/markIsActive'
|
||||
export { default as nodeEqualsType } from './utils/nodeEqualsType'
|
||||
|
||||
18
packages/tiptap-utils/src/utils/getNodeAttrs.js
Normal file
18
packages/tiptap-utils/src/utils/getNodeAttrs.js
Normal file
@@ -0,0 +1,18 @@
|
||||
export default function getNodeAttrs(state, type) {
|
||||
const { from, to } = state.selection
|
||||
let nodes = []
|
||||
|
||||
state.doc.nodesBetween(from, to, node => {
|
||||
nodes = [...nodes, node]
|
||||
})
|
||||
|
||||
const node = nodes
|
||||
.reverse()
|
||||
.find(nodeItem => nodeItem.type.name === type.name)
|
||||
|
||||
if (node) {
|
||||
return node.attrs
|
||||
}
|
||||
|
||||
return {}
|
||||
}
|
||||
@@ -55,6 +55,7 @@ export default {
|
||||
commands: this.editor.commands,
|
||||
isActive: this.editor.isActive,
|
||||
getMarkAttrs: this.editor.getMarkAttrs.bind(this.editor),
|
||||
getNodeAttrs: this.editor.getNodeAttrs.bind(this.editor),
|
||||
menu: this.menu,
|
||||
})
|
||||
},
|
||||
|
||||
@@ -52,6 +52,7 @@ export default {
|
||||
commands: this.editor.commands,
|
||||
isActive: this.editor.isActive,
|
||||
getMarkAttrs: this.editor.getMarkAttrs.bind(this.editor),
|
||||
getNodeAttrs: this.editor.getNodeAttrs.bind(this.editor),
|
||||
})
|
||||
},
|
||||
|
||||
|
||||
@@ -60,6 +60,7 @@ export default {
|
||||
commands: this.editor.commands,
|
||||
isActive: this.editor.isActive,
|
||||
getMarkAttrs: this.editor.getMarkAttrs.bind(this.editor),
|
||||
getNodeAttrs: this.editor.getNodeAttrs.bind(this.editor),
|
||||
menu: this.menu,
|
||||
})
|
||||
},
|
||||
|
||||
@@ -11,7 +11,12 @@ import { gapCursor } from 'prosemirror-gapcursor'
|
||||
import { keymap } from 'prosemirror-keymap'
|
||||
import { baseKeymap } from 'prosemirror-commands'
|
||||
import { inputRules, undoInputRule } from 'prosemirror-inputrules'
|
||||
import { markIsActive, nodeIsActive, getMarkAttrs } from 'tiptap-utils'
|
||||
import {
|
||||
markIsActive,
|
||||
nodeIsActive,
|
||||
getMarkAttrs,
|
||||
getNodeAttrs,
|
||||
} from 'tiptap-utils'
|
||||
import {
|
||||
injectCSS,
|
||||
camelCase,
|
||||
@@ -475,6 +480,12 @@ export default class Editor extends Emitter {
|
||||
return this.activeMarkAttrs[type]
|
||||
}
|
||||
|
||||
getNodeAttrs(type = null) {
|
||||
return {
|
||||
...getNodeAttrs(this.state, this.schema.nodes[type]),
|
||||
}
|
||||
}
|
||||
|
||||
get isActive() {
|
||||
return Object
|
||||
.entries({
|
||||
|
||||
Reference in New Issue
Block a user