remove replaceWithNode

This commit is contained in:
Philipp Kühn 2020-09-25 14:26:16 +02:00
parent 571db1042b
commit f71e4df72e
3 changed files with 1 additions and 38 deletions

View File

@ -21,10 +21,10 @@ editor.chain().focus().bold().run()
| ------------------- | ------------------------------------------ | | ------------------- | ------------------------------------------ |
| .removeMark() | Remove a mark in the current selection. | | .removeMark() | Remove a mark in the current selection. |
| .removeMarks() | Remove all marks in the current selection. | | .removeMarks() | Remove all marks in the current selection. |
| .replaceWithNode() | Replace a given range with a node. |
| .selectParentNode() | Select the parent node. | | .selectParentNode() | Select the parent node. |
| .toggleMark() | Toggle a mark on and off. | | .toggleMark() | Toggle a mark on and off. |
| .toggleBlockType() | Toggle a node with another node. | | .toggleBlockType() | Toggle a node with another node. |
| .setBlockType() | Replace a given range with a node. |
| .updateMark() | Update a mark with new attributes. | | .updateMark() | Update a mark with new attributes. |
### Selection ### Selection

View File

@ -7,7 +7,6 @@ export { insertText } from './insertText'
export { liftListItem } from './liftListItem' export { liftListItem } from './liftListItem'
export { removeMark } from './removeMark' export { removeMark } from './removeMark'
export { removeMarks } from './removeMarks' export { removeMarks } from './removeMarks'
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'

View File

@ -1,36 +0,0 @@
import { NodeType } from 'prosemirror-model'
import { Command } from '../Editor'
import getNodeType from '../utils/getNodeType'
interface Range {
from: number,
to: number,
}
type ReplaceWithNodeCommand = (
typeOrName: NodeType,
attrs: {},
range?: Range,
) => Command
declare module '../Editor' {
interface Commands {
replaceWithNode: ReplaceWithNodeCommand,
}
}
export const replaceWithNode: ReplaceWithNodeCommand = (typeOrName, attrs = {}, range) => ({ view, tr, state }) => {
const { $from, $to } = tr.selection
const type = getNodeType(typeOrName, state.schema)
const index = $from.index()
const from = range ? range.from : $from.pos
const to = range ? range.to : $to.pos
if (!$from.parent.canReplaceWith(index, index, type)) {
return false
}
view.dispatch(tr.replaceWith(from, to, type.create(attrs)))
return true
}