import { clamp } from "@noya-app/noya-utils"; import { VOffsetRange, VRange } from "@noya-app/text-editing"; export type SelectionPoint = VRange["anchor"]; export type SelectionRange = VRange; export const TEXT_SELECTION_PATH = [0]; export function selectionToOffsetRange( range: SelectionRange | null, textLength: number ): VOffsetRange | null { if (!range) return null; return { anchor: clamp(range.anchor.column, 0, textLength), focus: clamp(range.focus.column, 0, textLength), xPosition: range.xPosition, }; } export function offsetRangeToSelection(range: VOffsetRange): SelectionRange { return { anchor: { path: TEXT_SELECTION_PATH, column: range.anchor }, focus: { path: TEXT_SELECTION_PATH, column: range.focus }, xPosition: range.xPosition, }; }