@primer/react
Version:
An implementation of GitHub's Primer Design System using React
19 lines (15 loc) • 1.12 kB
JavaScript
import { isMacOS } from '@primer/behaviors/utils';
const getSelectedLineRange = textarea => {
// Subtract one from the caret position so the newline found is not the one _at_ the caret position
// then add one because we don't want to include the found newline. Also changes -1 (not found) result to 0
const start = textarea.value.lastIndexOf('\n', textarea.selectionStart - 1) + 1;
// activeLineEnd will be the index of the next newline inclusive, which works because slice is last-index exclusive
let end = textarea.value.indexOf('\n', textarea.selectionEnd);
if (end === -1) end = textarea.value.length;
return [start, end];
};
const markdownComment = text => `<!-- ${text.replaceAll('--', '\\-\\-')} -->`;
const markdownLink = (text, url) => `[${text.replaceAll('[', '\\[').replaceAll(']', '\\]')}](${url.replaceAll('(', '\\(').replaceAll(')', '\\)')})`;
const markdownImage = (altText, url) => `!${markdownLink(altText, url)}`;
const isModifierKey = event => isMacOS() ? event.metaKey : event.ctrlKey;
export { getSelectedLineRange, isModifierKey, markdownComment, markdownImage, markdownLink };