add setBlockType command
This commit is contained in:
@@ -11,6 +11,7 @@ export { replaceWithNode } from './replaceWithNode'
|
|||||||
export { scrollIntoView } from './scrollIntoView'
|
export { scrollIntoView } from './scrollIntoView'
|
||||||
export { selectAll } from './selectAll'
|
export { selectAll } from './selectAll'
|
||||||
export { selectParentNode } from './selectParentNode'
|
export { selectParentNode } from './selectParentNode'
|
||||||
|
export { setBlockType } from './setBlockType'
|
||||||
export { setContent } from './setContent'
|
export { setContent } from './setContent'
|
||||||
export { sinkListItem } from './sinkListItem'
|
export { sinkListItem } from './sinkListItem'
|
||||||
export { splitListItem } from './splitListItem'
|
export { splitListItem } from './splitListItem'
|
||||||
|
|||||||
21
packages/core/src/commands/setBlockType.ts
Normal file
21
packages/core/src/commands/setBlockType.ts
Normal file
@@ -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)
|
||||||
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import { NodeType } from 'prosemirror-model'
|
import { NodeType } from 'prosemirror-model'
|
||||||
import { setBlockType } from 'prosemirror-commands'
|
|
||||||
import { Command } from '../Editor'
|
import { Command } from '../Editor'
|
||||||
import nodeIsActive from '../utils/nodeIsActive'
|
import nodeIsActive from '../utils/nodeIsActive'
|
||||||
import getNodeType from '../utils/getNodeType'
|
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 type = getNodeType(typeOrName, state.schema)
|
||||||
const toggleType = getNodeType(toggleTypeOrName, state.schema)
|
const toggleType = getNodeType(toggleTypeOrName, state.schema)
|
||||||
const isActive = nodeIsActive(state, type, attrs)
|
const isActive = nodeIsActive(state, type, attrs)
|
||||||
|
|
||||||
if (isActive) {
|
if (isActive) {
|
||||||
return setBlockType(toggleType)(state, dispatch)
|
return commands.setBlockType(toggleType)
|
||||||
}
|
}
|
||||||
|
|
||||||
return setBlockType(type, attrs)(state, dispatch)
|
return commands.setBlockType(type, attrs)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user