Files
tiptap/packages/core/src/commands/setNodeSelection.ts
2021-04-27 11:52:50 +02:00

27 lines
645 B
TypeScript

import { NodeSelection } from 'prosemirror-state'
import minMax from '../utilities/minMax'
import { Command, RawCommands } from '../types'
declare module '@tiptap/core' {
interface Commands {
setNodeSelection: {
/**
* Creates a NodeSelection.
*/
setNodeSelection: (position: number) => Command,
}
}
}
export const setNodeSelection: RawCommands['setNodeSelection'] = position => ({ tr, dispatch }) => {
if (dispatch) {
const { doc } = tr
const from = minMax(position, 0, doc.content.size)
const selection = NodeSelection.create(doc, from)
tr.setSelection(selection)
}
return true
}