add extendMarkRange command
This commit is contained in:
23
packages/core/src/commands/extendMarkRange.ts
Normal file
23
packages/core/src/commands/extendMarkRange.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import { TextSelection } from 'prosemirror-state'
|
||||||
|
import { MarkType } from 'prosemirror-model'
|
||||||
|
import { Command } from '../Editor'
|
||||||
|
import getMarkType from '../utils/getMarkType'
|
||||||
|
import getMarkRange from '../utils/getMarkRange'
|
||||||
|
|
||||||
|
export default (typeOrName: string | MarkType): Command => ({ tr, state, dispatch }) => {
|
||||||
|
const type = getMarkType(typeOrName, state.schema)
|
||||||
|
const { doc, selection } = tr
|
||||||
|
const { $from, empty } = selection
|
||||||
|
|
||||||
|
if (empty && dispatch) {
|
||||||
|
const range = getMarkRange($from, type)
|
||||||
|
|
||||||
|
if (range) {
|
||||||
|
const newSelection = TextSelection.create(doc, range.from, range.to)
|
||||||
|
|
||||||
|
tr.setSelection(newSelection)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ import blur from '../commands/blur'
|
|||||||
import clearContent from '../commands/clearContent'
|
import clearContent from '../commands/clearContent'
|
||||||
import clearNodes from '../commands/clearNodes'
|
import clearNodes from '../commands/clearNodes'
|
||||||
import deleteSelection from '../commands/deleteSelection'
|
import deleteSelection from '../commands/deleteSelection'
|
||||||
|
import extendMarkRange from '../commands/extendMarkRange'
|
||||||
import focus from '../commands/focus'
|
import focus from '../commands/focus'
|
||||||
import insertHTML from '../commands/insertHTML'
|
import insertHTML from '../commands/insertHTML'
|
||||||
import insertText from '../commands/insertText'
|
import insertText from '../commands/insertText'
|
||||||
@@ -34,6 +35,7 @@ export const Commands = createExtension({
|
|||||||
clearContent,
|
clearContent,
|
||||||
clearNodes,
|
clearNodes,
|
||||||
deleteSelection,
|
deleteSelection,
|
||||||
|
extendMarkRange,
|
||||||
focus,
|
focus,
|
||||||
insertHTML,
|
insertHTML,
|
||||||
insertText,
|
insertText,
|
||||||
|
|||||||
Reference in New Issue
Block a user