MCPcopy Index your code
hub / github.com/primer/react / getSelectedLineRange

Function getSelectedLineRange

packages/react/src/drafts/MarkdownEditor/utils.ts:3–13  ·  view source on GitHub ↗
(textarea: HTMLTextAreaElement)

Source from the content-addressed store, hash-verified

1import {isMacOS} from '@primer/behaviors/utils'
2
3export const getSelectedLineRange = (textarea: HTMLTextAreaElement): [number, number] => {
4 // Subtract one from the caret position so the newline found is not the one _at_ the caret position
5 // then add one because we don't want to include the found newline. Also changes -1 (not found) result to 0
6 const start = textarea.value.lastIndexOf('\n', textarea.selectionStart - 1) + 1
7
8 // activeLineEnd will be the index of the next newline inclusive, which works because slice is last-index exclusive
9 let end = textarea.value.indexOf('\n', textarea.selectionEnd)
10 if (end === -1) end = textarea.value.length
11
12 return [start, end]
13}
14
15export const markdownComment = (text: string) => `<!-- ${text.replaceAll('--', '\\-\\-')} -->`
16

Callers 2

useIndentingFunction · 0.90
useListEditingFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected