fix: prevent RangeError for posToDOMRect, fix #2112

This commit is contained in:
Philipp Kühn 2021-10-31 12:03:24 +01:00
parent 8b091e8413
commit 010418d0fd

View File

@ -1,8 +1,13 @@
import { EditorView } from 'prosemirror-view' import { EditorView } from 'prosemirror-view'
import minMax from '../utilities/minMax'
export default function posToDOMRect(view: EditorView, from: number, to: number): DOMRect { export default function posToDOMRect(view: EditorView, from: number, to: number): DOMRect {
const start = view.coordsAtPos(from) const minPos = 0
const end = view.coordsAtPos(to, -1) const maxPos = view.state.doc.content.size
const resolvedFrom = minMax(from, minPos, maxPos)
const resolvedEnd = minMax(to, minPos, maxPos)
const start = view.coordsAtPos(resolvedFrom)
const end = view.coordsAtPos(resolvedEnd, -1)
const top = Math.min(start.top, end.top) const top = Math.min(start.top, end.top)
const bottom = Math.max(start.bottom, end.bottom) const bottom = Math.max(start.bottom, end.bottom)
const left = Math.min(start.left, end.left) const left = Math.min(start.left, end.left)