diff --git a/packages/core/src/commands/index.ts b/packages/core/src/commands/index.ts index 19a6bd1c..7ea3e789 100644 --- a/packages/core/src/commands/index.ts +++ b/packages/core/src/commands/index.ts @@ -11,6 +11,7 @@ export { replaceWithNode } from './replaceWithNode' export { scrollIntoView } from './scrollIntoView' export { selectAll } from './selectAll' export { selectParentNode } from './selectParentNode' +export { setBlockType } from './setBlockType' export { setContent } from './setContent' export { sinkListItem } from './sinkListItem' export { splitListItem } from './splitListItem' diff --git a/packages/core/src/commands/setBlockType.ts b/packages/core/src/commands/setBlockType.ts new file mode 100644 index 00000000..af1fc866 --- /dev/null +++ b/packages/core/src/commands/setBlockType.ts @@ -0,0 +1,21 @@ +import { NodeType } from 'prosemirror-model' +import { setBlockType as originalSetBlockType } from 'prosemirror-commands' +import { Command } from '../Editor' +import getNodeType from '../utils/getNodeType' + +type SetBlockTypeCommand = ( + typeOrName: string | NodeType, + attrs?: {}, +) => Command + +declare module '../Editor' { + interface Commands { + setBlockType: SetBlockTypeCommand, + } +} + +export const setBlockType: SetBlockTypeCommand = (typeOrName, attrs = {}) => ({ state, dispatch }) => { + const type = getNodeType(typeOrName, state.schema) + + return originalSetBlockType(type, attrs)(state, dispatch) +} diff --git a/packages/core/src/commands/toggleBlockType.ts b/packages/core/src/commands/toggleBlockType.ts index e6da0b06..abf47426 100644 --- a/packages/core/src/commands/toggleBlockType.ts +++ b/packages/core/src/commands/toggleBlockType.ts @@ -1,5 +1,4 @@ import { NodeType } from 'prosemirror-model' -import { setBlockType } from 'prosemirror-commands' import { Command } from '../Editor' import nodeIsActive from '../utils/nodeIsActive' import getNodeType from '../utils/getNodeType' @@ -16,14 +15,14 @@ declare module '../Editor' { } } -export const toggleBlockType: ToggleBlockTypeCommand = (typeOrName, toggleTypeOrName, attrs = {}) => ({ state, dispatch }) => { +export const toggleBlockType: ToggleBlockTypeCommand = (typeOrName, toggleTypeOrName, attrs = {}) => ({ state, commands }) => { const type = getNodeType(typeOrName, state.schema) const toggleType = getNodeType(toggleTypeOrName, state.schema) const isActive = nodeIsActive(state, type, attrs) if (isActive) { - return setBlockType(toggleType)(state, dispatch) + return commands.setBlockType(toggleType) } - return setBlockType(type, attrs)(state, dispatch) + return commands.setBlockType(type, attrs) }