fix: initialize autofocus selection in createView (#2212)
* initialize autofocus selection in `createView` * fix missing variable and null error * remove unused imports
This commit is contained in:
22
packages/core/src/helpers/resolveFocusPosition.ts
Normal file
22
packages/core/src/helpers/resolveFocusPosition.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Node as ProseMirrorNode } from 'prosemirror-model'
|
||||
import { Selection, TextSelection } from 'prosemirror-state'
|
||||
import { FocusPosition } from '../types'
|
||||
import minMax from '../utilities/minMax'
|
||||
|
||||
export default function resolveFocusPosition(
|
||||
doc: ProseMirrorNode,
|
||||
position: FocusPosition = null
|
||||
): Selection | null {
|
||||
|
||||
if (!position) return null
|
||||
if (position === 'start' || position === true) return Selection.atStart(doc)
|
||||
if (position === 'end') return Selection.atEnd(doc)
|
||||
if (position === 'all') return TextSelection.create(doc, 0, doc.content.size)
|
||||
|
||||
// Check if `position` is in bounds of the doc if `position` is a number.
|
||||
const minPos = Selection.atStart(doc).from
|
||||
const maxPos = Selection.atEnd(doc).to
|
||||
const resolvedFrom = minMax(position, minPos, maxPos)
|
||||
const resolvedEnd = minMax(position, minPos, maxPos)
|
||||
return TextSelection.create(doc, resolvedFrom, resolvedEnd)
|
||||
}
|
||||
Reference in New Issue
Block a user