mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-12 21:00:02 +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 { 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) {
|
||||||
|
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 {
|
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()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user