mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-11 11:45:15 +08:00
add getNodeType
This commit is contained in:
parent
023c16a4f5
commit
3d02da20ff
@ -2,10 +2,11 @@ import { NodeType } from 'prosemirror-model'
|
||||
import { setBlockType } from 'prosemirror-commands'
|
||||
import { Editor } from '../Editor'
|
||||
import nodeIsActive from '../utils/nodeIsActive'
|
||||
import getNodeType from '../utils/getNodeType'
|
||||
|
||||
type ToggleBlockType = (
|
||||
type: NodeType,
|
||||
toggleType: NodeType,
|
||||
type: string | NodeType,
|
||||
toggleType: string | NodeType,
|
||||
attrs?: {}
|
||||
) => any
|
||||
|
||||
@ -15,8 +16,10 @@ declare module '../Editor' {
|
||||
}
|
||||
}
|
||||
|
||||
export default (next: Function, editor: Editor): ToggleBlockType => (type, toggleType, attrs) => {
|
||||
const { view, state } = editor
|
||||
export default (next: Function, editor: Editor): ToggleBlockType => (typeOrName, toggleTypeOrName, attrs) => {
|
||||
const { view, state, schema } = editor
|
||||
const type = getNodeType(typeOrName, schema)
|
||||
const toggleType = getNodeType(toggleTypeOrName, schema)
|
||||
const isActive = nodeIsActive(state, type, attrs)
|
||||
|
||||
if (isActive) {
|
||||
|
9
packages/core/src/utils/getNodeType.ts
Normal file
9
packages/core/src/utils/getNodeType.ts
Normal 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
|
||||
}
|
@ -48,7 +48,7 @@ export default class Heading extends Node {
|
||||
commands(): CommandSpec {
|
||||
return {
|
||||
heading: next => attrs => {
|
||||
this.editor.toggleBlockType(this.type, this.editor.schema.nodes.paragraph, attrs)
|
||||
this.editor.toggleBlockType(this.name, 'paragraph', attrs)
|
||||
next()
|
||||
},
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user