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

@ -110,6 +110,7 @@ The `<editor-menu-bar />` component is renderless and will receive some properti
| `commands` | `Array` | A list of all commands. | | `commands` | `Array` | A list of all commands. |
| `isActive` | `Object` | An object of functions to check if your selected text is a node or mark. `isActive.{node|mark}(attrs)` | | `isActive` | `Object` | An object of functions to check if your selected text is a node or mark. `isActive.{node|mark}(attrs)` |
| `getMarkAttrs` | `Function` | A function to get all mark attributes of your selection. | | `getMarkAttrs` | `Function` | A function to get all mark attributes of your selection. |
| `getNodeAttrs` | `Function` | A function to get all node attributes of your selection. |
| `focused` | `Boolean` | Whether the editor is focused. | | `focused` | `Boolean` | Whether the editor is focused. |
| `focus` | `Function` | A function to focus the editor. | | `focus` | `Function` | A function to focus the editor. |
@ -139,6 +140,7 @@ The `<editor-menu-bubble />` component is renderless and will receive some prope
| `commands` | `Array` | A list of all commands. | | `commands` | `Array` | A list of all commands. |
| `isActive` | `Object` | An object of functions to check if your selected text is a node or mark. `isActive.{node|mark}(attrs)` | | `isActive` | `Object` | An object of functions to check if your selected text is a node or mark. `isActive.{node|mark}(attrs)` |
| `getMarkAttrs` | `Function` | A function to get all mark attributes of your selection. | | `getMarkAttrs` | `Function` | A function to get all mark attributes of your selection. |
| `getNodeAttrs` | `Function` | A function to get all node attributes of your selection. |
| `focused` | `Boolean` | Whether the editor is focused. | | `focused` | `Boolean` | Whether the editor is focused. |
| `focus` | `Function` | A function to focus the editor. | | `focus` | `Function` | A function to focus the editor. |
| `menu` | `Object` | An object for positioning your menu. | | `menu` | `Object` | An object for positioning your menu. |
@ -172,6 +174,7 @@ The `<editor-floating-menu />` component is renderless and will receive some pro
| `commands` | `Array` | A list of all commands. | | `commands` | `Array` | A list of all commands. |
| `isActive` | `Object` | An object of functions to check if your selected text is a node or mark. `isActive.{node|mark}(attrs)` | | `isActive` | `Object` | An object of functions to check if your selected text is a node or mark. `isActive.{node|mark}(attrs)` |
| `getMarkAttrs` | `Function` | A function to get all mark attributes of your selection. | | `getMarkAttrs` | `Function` | A function to get all mark attributes of your selection. |
| `getNodeAttrs` | `Function` | A function to get all node attributes of your selection. |
| `focused` | `Boolean` | Whether the editor is focused. | | `focused` | `Boolean` | Whether the editor is focused. |
| `focus` | `Function` | A function to focus the editor. | | `focus` | `Function` | A function to focus the editor. |
| `menu` | `Object` | An object for positioning your menu. | | `menu` | `Object` | An object for positioning your menu. |

View File

@ -1,4 +1,5 @@
export { default as getMarkAttrs } from './utils/getMarkAttrs' export { default as getMarkAttrs } from './utils/getMarkAttrs'
export { default as getNodeAttrs } from './utils/getNodeAttrs'
export { default as getMarkRange } from './utils/getMarkRange' export { default as getMarkRange } from './utils/getMarkRange'
export { default as markIsActive } from './utils/markIsActive' export { default as markIsActive } from './utils/markIsActive'
export { default as nodeEqualsType } from './utils/nodeEqualsType' 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, commands: this.editor.commands,
isActive: this.editor.isActive, isActive: this.editor.isActive,
getMarkAttrs: this.editor.getMarkAttrs.bind(this.editor), getMarkAttrs: this.editor.getMarkAttrs.bind(this.editor),
getNodeAttrs: this.editor.getNodeAttrs.bind(this.editor),
menu: this.menu, menu: this.menu,
}) })
}, },

View File

@ -52,6 +52,7 @@ export default {
commands: this.editor.commands, commands: this.editor.commands,
isActive: this.editor.isActive, isActive: this.editor.isActive,
getMarkAttrs: this.editor.getMarkAttrs.bind(this.editor), 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, commands: this.editor.commands,
isActive: this.editor.isActive, isActive: this.editor.isActive,
getMarkAttrs: this.editor.getMarkAttrs.bind(this.editor), getMarkAttrs: this.editor.getMarkAttrs.bind(this.editor),
getNodeAttrs: this.editor.getNodeAttrs.bind(this.editor),
menu: this.menu, menu: this.menu,
}) })
}, },

View File

@ -11,7 +11,12 @@ import { gapCursor } from 'prosemirror-gapcursor'
import { keymap } from 'prosemirror-keymap' import { keymap } from 'prosemirror-keymap'
import { baseKeymap } from 'prosemirror-commands' import { baseKeymap } from 'prosemirror-commands'
import { inputRules, undoInputRule } from 'prosemirror-inputrules' import { inputRules, undoInputRule } from 'prosemirror-inputrules'
import { markIsActive, nodeIsActive, getMarkAttrs } from 'tiptap-utils' import {
markIsActive,
nodeIsActive,
getMarkAttrs,
getNodeAttrs,
} from 'tiptap-utils'
import { import {
injectCSS, injectCSS,
camelCase, camelCase,
@ -475,6 +480,12 @@ export default class Editor extends Emitter {
return this.activeMarkAttrs[type] return this.activeMarkAttrs[type]
} }
getNodeAttrs(type = null) {
return {
...getNodeAttrs(this.state, this.schema.nodes[type]),
}
}
get isActive() { get isActive() {
return Object return Object
.entries({ .entries({