(editor: Editable, parentRange: Range)
| 27 | * @returns {Range} A valid portion of the parentRange which is one a single line |
| 28 | */ |
| 29 | export const findCurrentLineRange = (editor: Editable, parentRange: Range): Range => { |
| 30 | const parentRangeBoundary = Editor.range(editor, Range.end(parentRange)) |
| 31 | const positions = Array.from(Editor.positions(editor, { at: parentRange })) |
| 32 | |
| 33 | let left = 0 |
| 34 | let right = positions.length |
| 35 | let middle = Math.floor(right / 2) |
| 36 | |
| 37 | if (areRangesSameLine(editor, Editor.range(editor, positions[left]), parentRangeBoundary)) { |
| 38 | return Editor.range(editor, positions[left], parentRangeBoundary) |
| 39 | } |
| 40 | |
| 41 | if (positions.length < 2) { |
| 42 | return Editor.range(editor, positions[positions.length - 1], parentRangeBoundary) |
| 43 | } |
| 44 | |
| 45 | while (middle !== positions.length && middle !== left) { |
| 46 | if (areRangesSameLine(editor, Editor.range(editor, positions[middle]), parentRangeBoundary)) { |
| 47 | right = middle |
| 48 | } else { |
| 49 | left = middle |
| 50 | } |
| 51 | |
| 52 | middle = Math.floor((left + right) / 2) |
| 53 | } |
| 54 | |
| 55 | return Editor.range(editor, positions[right], parentRangeBoundary) |
| 56 | } |
no test coverage detected