Files
overleaf-cep/services/web/frontend/js/features/source-editor/utils/position.ts
Alf Eaton 06219ee2dc Merge pull request #27123 from overleaf/ae-goto-scroll-select-text
Highlight double-clicked word when syncing position from PDF to code

GitOrigin-RevId: da120af9dec203346cb85c6aa7e403f4e585c748
2025-07-29 08:05:51 +00:00

20 lines
423 B
TypeScript

import { Text } from '@codemirror/state'
export const findValidPosition = (
doc: Text,
lineNumber: number, // 1-indexed
columnNumber = 0 // 0-indexed
): number => {
const lines = doc.lines
if (lineNumber > lines) {
// end of the doc
return doc.length
}
const line = doc.line(lineNumber)
// requested line and column, or the end of the line
return Math.min(line.from + columnNumber, line.to)
}