add getNodeType

This commit is contained in:
Philipp Kühn 2020-04-22 00:09:31 +02:00
parent 023c16a4f5
commit 3d02da20ff
3 changed files with 17 additions and 5 deletions

View File

@ -2,10 +2,11 @@ import { NodeType } from 'prosemirror-model'
import { setBlockType } from 'prosemirror-commands' import { setBlockType } from 'prosemirror-commands'
import { Editor } from '../Editor' import { Editor } from '../Editor'
import nodeIsActive from '../utils/nodeIsActive' import nodeIsActive from '../utils/nodeIsActive'
import getNodeType from '../utils/getNodeType'
type ToggleBlockType = ( type ToggleBlockType = (
type: NodeType, type: string | NodeType,
toggleType: NodeType, toggleType: string | NodeType,
attrs?: {} attrs?: {}
) => any ) => any
@ -15,8 +16,10 @@ declare module '../Editor' {
} }
} }
export default (next: Function, editor: Editor): ToggleBlockType => (type, toggleType, attrs) => { export default (next: Function, editor: Editor): ToggleBlockType => (typeOrName, toggleTypeOrName, attrs) => {
const { view, state } = editor const { view, state, schema } = editor
const type = getNodeType(typeOrName, schema)
const toggleType = getNodeType(toggleTypeOrName, schema)
const isActive = nodeIsActive(state, type, attrs) const isActive = nodeIsActive(state, type, attrs)
if (isActive) { if (isActive) {

View File

@ -0,0 +1,9 @@
import { NodeType, Schema } from 'prosemirror-model'
export default function getNodeType(nameOrType: string | NodeType, schema: Schema): NodeType {
if (typeof nameOrType === 'string') {
return schema.nodes[nameOrType]
}
return nameOrType
}

View File

@ -48,7 +48,7 @@ export default class Heading extends Node {
commands(): CommandSpec { commands(): CommandSpec {
return { return {
heading: next => attrs => { heading: next => attrs => {
this.editor.toggleBlockType(this.type, this.editor.schema.nodes.paragraph, attrs) this.editor.toggleBlockType(this.name, 'paragraph', attrs)
next() next()
}, },
} }