add replaceRange command
This commit is contained in:
@@ -147,7 +147,8 @@ Have a look at all of the core commands listed below. They should give you a goo
|
|||||||
| .lift() | Removes an existing wrap. |
|
| .lift() | Removes an existing wrap. |
|
||||||
| .liftEmptyBlock() | Lift block if empty. |
|
| .liftEmptyBlock() | Lift block if empty. |
|
||||||
| .newlineInCode() | Add a newline character in code. |
|
| .newlineInCode() | Add a newline character in code. |
|
||||||
| .replace() | Replaces text with a node within a range. |
|
| .replace() | Replaces text with a node. |
|
||||||
|
| .replaceRange() | Replaces text with a node within a range. |
|
||||||
| .resetNodeAttributes() | Resets all node attributes to the default value. |
|
| .resetNodeAttributes() | Resets all node attributes to the default value. |
|
||||||
| .selectParentNode() | Select the parent node. |
|
| .selectParentNode() | Select the parent node. |
|
||||||
| .setMark() | Add a mark with new attributes. |
|
| .setMark() | Add a mark with new attributes. |
|
||||||
|
|||||||
@@ -1,24 +1,12 @@
|
|||||||
import { NodeType } from 'prosemirror-model'
|
import { NodeType } from 'prosemirror-model'
|
||||||
import getNodeType from '../helpers/getNodeType'
|
import { Command, AnyObject } from '../types'
|
||||||
import { Command, Range, AnyObject } from '../types'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replaces text with a node within a range.
|
* Replaces text with a node.
|
||||||
*/
|
*/
|
||||||
export const replace = (range: Range | null = null, typeOrName: string | NodeType, attrs: AnyObject = {}): Command => ({ tr, state, dispatch }) => {
|
export const replace = (typeOrName: string | NodeType, attributes: AnyObject = {}): Command => ({ state, commands }) => {
|
||||||
const type = getNodeType(typeOrName, state.schema)
|
const { from, to } = state.selection
|
||||||
const { $from, $to } = state.selection
|
const range = { from, to }
|
||||||
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 commands.replaceRange(range, typeOrName, attributes)
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dispatch) {
|
|
||||||
tr.replaceWith(from, to, type.create(attrs))
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|||||||
23
packages/core/src/commands/replaceRange.ts
Normal file
23
packages/core/src/commands/replaceRange.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import { NodeType } from 'prosemirror-model'
|
||||||
|
import getNodeType from '../helpers/getNodeType'
|
||||||
|
import { Command, Range, AnyObject } from '../types'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces text with a node within a range.
|
||||||
|
*/
|
||||||
|
export const replaceRange = (range: Range, typeOrName: string | NodeType, attributes: AnyObject = {}): Command => ({ tr, state, dispatch }) => {
|
||||||
|
const type = getNodeType(typeOrName, state.schema)
|
||||||
|
const { from, to } = range
|
||||||
|
const $from = tr.doc.resolve(from)
|
||||||
|
const index = $from.index()
|
||||||
|
|
||||||
|
if (!$from.parent.canReplaceWith(index, index, type)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dispatch) {
|
||||||
|
tr.replaceWith(from, to, type.create(attributes))
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
@@ -18,6 +18,7 @@ import * as liftEmptyBlock from '../commands/liftEmptyBlock'
|
|||||||
import * as liftListItem from '../commands/liftListItem'
|
import * as liftListItem from '../commands/liftListItem'
|
||||||
import * as newlineInCode from '../commands/newlineInCode'
|
import * as newlineInCode from '../commands/newlineInCode'
|
||||||
import * as replace from '../commands/replace'
|
import * as replace from '../commands/replace'
|
||||||
|
import * as replaceRange from '../commands/replaceRange'
|
||||||
import * as resetNodeAttributes from '../commands/resetNodeAttributes'
|
import * as resetNodeAttributes from '../commands/resetNodeAttributes'
|
||||||
import * as scrollIntoView from '../commands/scrollIntoView'
|
import * as scrollIntoView from '../commands/scrollIntoView'
|
||||||
import * as selectAll from '../commands/selectAll'
|
import * as selectAll from '../commands/selectAll'
|
||||||
@@ -65,6 +66,7 @@ export const Commands = Extension.create({
|
|||||||
...liftListItem,
|
...liftListItem,
|
||||||
...newlineInCode,
|
...newlineInCode,
|
||||||
...replace,
|
...replace,
|
||||||
|
...replaceRange,
|
||||||
...resetNodeAttributes,
|
...resetNodeAttributes,
|
||||||
...scrollIntoView,
|
...scrollIntoView,
|
||||||
...selectAll,
|
...selectAll,
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export const Mention = Node.create({
|
|||||||
editor
|
editor
|
||||||
.chain()
|
.chain()
|
||||||
.focus()
|
.focus()
|
||||||
.replace(range, 'mention', attributes)
|
.replaceRange(range, 'mention', attributes)
|
||||||
.insertText(' ')
|
.insertText(' ')
|
||||||
.run()
|
.run()
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user