add getNodeAttrs, fix #622

This commit is contained in:
Philipp Kühn
2020-04-11 13:02:19 +02:00
parent 0228001aa4
commit 0ca798fe23
7 changed files with 37 additions and 1 deletions

View File

@@ -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'

View 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 {}
}

View File

@@ -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,
})
},

View File

@@ -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),
})
},

View File

@@ -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,
})
},

View File

@@ -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({