selectText.tsx 415 B

12345678910111213
  1. export function selectText(node: HTMLElement): void {
  2. if (node instanceof HTMLInputElement && node.type === 'text') {
  3. node.select();
  4. } else if (node instanceof Node && window.getSelection) {
  5. const range = document.createRange();
  6. range.selectNode(node);
  7. const selection = window.getSelection();
  8. if (selection) {
  9. selection.removeAllRanges();
  10. selection.addRange(range);
  11. }
  12. }
  13. }