add some more commands

This commit is contained in:
Philipp Kühn
2020-09-21 23:17:30 +02:00
parent 778e064979
commit 4da71ecfbb
27 changed files with 207 additions and 210 deletions

View File

@@ -1,4 +1,4 @@
import { Editor } from '../Editor'
import { Command } from '../Editor'
import { NodeType } from 'prosemirror-model'
import getNodeType from '../utils/getNodeType'
@@ -11,7 +11,7 @@ type ReplaceWithNodeCommand = (
typeOrName: NodeType,
attrs: {},
range?: Range,
) => Editor
) => Command
declare module '../Editor' {
interface Editor {
@@ -19,17 +19,18 @@ declare module '../Editor' {
}
}
export default (next: Function, editor: Editor) => (typeOrName: NodeType, attrs: {}, range?: Range) => {
const { view, state, schema } = editor
const { $from, $to } = state.selection
const type = getNodeType(typeOrName, schema)
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)) {
view.dispatch(state.tr.replaceWith(from, to, type.create(attrs)))
if (!$from.parent.canReplaceWith(index, index, type)) {
return false
}
view.dispatch(tr.replaceWith(from, to, type.create(attrs)))
next()
return true
}