File
Blob: src/client/components/editor/extensions/code-block/selection.ts
| 1 | export interface CodeBlockLineRange { |
| 2 | from: number; |
| 3 | to: number; |
| 4 | } |
| 5 | |
| 6 | export function resolveCodeBlockLineRange(text: string, offset: number): CodeBlockLineRange { |
| 7 | const clampedOffset = Math.max(0, Math.min(offset, text.length)); |
| 8 | const breakBefore = clampedOffset > 0 ? text.lastIndexOf("\n", clampedOffset - 1) : -1; |
| 9 | const lineStart = breakBefore + 1; |
| 10 | const nextBreak = text.indexOf("\n", clampedOffset); |
| 11 | const lineEnd = nextBreak === -1 ? text.length : nextBreak; |
| 12 | |
| 13 | if (lineStart === lineEnd && lineEnd < text.length) { |
| 14 | return { from: lineStart, to: lineEnd + 1 }; |
| 15 | } |
| 16 | |
| 17 | return { from: lineStart, to: lineEnd }; |
| 18 | } |