Kawiesh

JS Selection

Nov 4th, 2021 (edited)
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*childNodes
  2. collection of an element's child nodes, as a NodeList object (array-like)
  3. Whitespaces, comments and other text are also considered as nodes.
  4. To skip text [...Nodelist].map(i=> i.nodeType != 1);
  5.  
  6. children
  7. collection of an element's child elements, as an HTMLCollection object.
  8. only elements are included. text is excluded*/
  9.  
  10.  
  11.  
  12.  
  13. /*Select contenrpt between 2 elements________________________________*/
  14. function selectBetween(start,endNode,startOffset=0,endOffset=0){
  15. let selection= window.getSelection();
  16. selection.setBaseAndExtent(start,startOffset,endNode,endOffset);
  17. }
  18.  
  19. /*
  20. ●startNode: node at the start of the selection.
  21. ●startOffset: The number of childNodes from the start of the startNode,
  22. that should be excluded from the selection.
  23. E.g value is 0=> whole node is included.
  24. value is 1=> whole node minus the first childNode is included
  25. value is (element.childNodes.length)=> whole node is excluded
  26. ●endNode: The node at the end of the selection.
  27. ●endOffset: The number of childNodes from the start of the endNode,
  28. that should be included in the selection.
  29. E.g value is 0=> whole node is excluded.
  30. value is 1=> first childNode is included
  31. value is (element.childNodes.length)=> whole node is included*/
  32.  
  33.  
  34.  
  35. /*Select whole element________________________________*/
  36. function selectText(element) {
  37. let range = document.createRange();
  38. //range.selectNode(element); **or**
  39. range.selectNodeContents(element);
  40. let selection= window.getSelection();
  41. selection.removeAllRanges();
  42. selection.addRange(range);
  43. }
  44.  
  45. //●selectNode(): sets the Range to contain the Node and its contents
  46. //●selectNodeContent: sets the Range to contain (only) the contents of a Node.
  47.  
  48.  
  49.  
  50. /*Select children of parent element________________________________*/
  51. function selectText(element){
  52. window.getSelection().selectAllChildren(element);
  53. }
  54. //●All children of parentNode (but not the parentNode itself) will be selected
  55. //●Element parameter in this function refers to the parentNode
  56.  
  57.  
  58.  
  59.  
  60. /*Select whole element using CSS4________________________________*/
  61. .selectall{
  62. -webkit-touch-callout: all;
  63. -webkit-user-select: all;
  64. -khtml-user-select: all;
  65. -moz-user-select: all;
  66. -ms-user-select: all;
  67. user-select: all;
  68. }
  69. //Just add class "selectall" to elements, that should be selected completely on focus
  70.  
  71.  
  72.  
  73. /*------------STORING SELECTION----------*/
  74.  
  75. let selected= "empty";
  76. document.onselectionchange= function() {
  77. let a= window.getSelection();
  78. selected= (a.toString().length > 0) ? a.getRangeAt(0) : "empty";
  79. };
  80.  
  81. /*When text is selected it's stored in variable "selected", which can be used later
  82. to reselect e.g. to prevent lost of selection due to un-focus/blur. e.g. clicking on element*/
  83.  
  84. if(selected=="empty"){
  85. //Nothing selected
  86. }
  87. else{
  88. let selection= window.getSelection();
  89. selection.removeAllRanges();
  90. selection.addRange(selected);
  91. }
  92.  
  93.  
  94.  
  95. //-----------FIND ELEMENTS WITHIN SELECTION------
  96.  
  97.  
  98. let selection= window.getSelection();
  99. let selected= selection.getRangeAt(0);
  100. let element= selected.commonAncestorContainer.querySelector(element);
  101.  
  102. let elements= document.querySelectorAll(elements);
  103. let elinselection= [...elements].map(i=>{
  104. if(selection.containsNode(i, true)) return i;
  105. });
  106.  
  107. /* containsNode() method indicates whether a specfied node is part of the selection.
  108. sel.containsNode(node, partialContainment)
  109. ●node: element(s) you're looking for
  110. ●partialContainment:
  111. true: element is considered to be part of selection, even if partially selected
  112. false: element is considered to be part of selection, only if it's fully selected */
  113.  
  114. //-----
  115.  
Add Comment
Please, Sign In to add comment