mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-07 09:25:29 +08:00

* Require .js endings * add extension alias for cypress to resolve ts files with js endings
32 lines
944 B
TypeScript
32 lines
944 B
TypeScript
import { TextSelection } from '@tiptap/pm/state'
|
|
|
|
import { Range, RawCommands } from '../types.js'
|
|
import { minMax } from '../utilities/minMax.js'
|
|
|
|
declare module '@tiptap/core' {
|
|
interface Commands<ReturnType> {
|
|
setTextSelection: {
|
|
/**
|
|
* Creates a TextSelection.
|
|
*/
|
|
setTextSelection: (position: number | Range) => ReturnType
|
|
}
|
|
}
|
|
}
|
|
|
|
export const setTextSelection: RawCommands['setTextSelection'] = position => ({ tr, dispatch }) => {
|
|
if (dispatch) {
|
|
const { doc } = tr
|
|
const { from, to } = typeof position === 'number' ? { from: position, to: position } : position
|
|
const minPos = TextSelection.atStart(doc).from
|
|
const maxPos = TextSelection.atEnd(doc).to
|
|
const resolvedFrom = minMax(from, minPos, maxPos)
|
|
const resolvedEnd = minMax(to, minPos, maxPos)
|
|
const selection = TextSelection.create(doc, resolvedFrom, resolvedEnd)
|
|
|
|
tr.setSelection(selection)
|
|
}
|
|
|
|
return true
|
|
}
|