-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: initialize autofocus selection in
createView
(#2212)
* initialize autofocus selection in `createView` * fix missing variable and null error * remove unused imports
- Loading branch information
Showing
3 changed files
with
31 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |