Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. document.designMode = "on";
  2.  
  3. // Color the first section
  4. var selection = window.getSelection();
  5. selection.removeAllRanges();
  6. selection.addRange(range1);
  7.  
  8. if (!selection.isCollapsed){
  9. document.execCommand("foreColor", false, color1);
  10. }
  11.  
  12. // Color the middle section
  13. selection.removeAllRanges();
  14. selection.addRange(range2);
  15.  
  16. if (!selection.isCollapsed){
  17. document.execCommand("backColor", false, color2);
  18. document.execCommand("foreColor", false, color3);
  19. }
  20.  
  21. // Color the last section
  22. selection.removeAllRanges();
  23. selection.addRange(range3);
  24.  
  25. if (!selection.isCollapsed){
  26. document.execCommand("foreColor", false, color1);
  27. }
  28.  
  29. document.designMode = "off";
  30. selection.removeAllRanges();
  31.  
  32. function styleRange(range, style) // the style is a string of css styles. eg."background-color:darkblue; color:blue;"
  33. {
  34. // Get the start and end nodes and split them to give new start and end nodes with only text that falls inside the range.
  35. var startNode = range.startContainer.splitText(range.startOffset);
  36. var endNode = range.endContainer.splitText(range.endOffset).previousSibling;
  37.  
  38. // Adjust the range to contain the new start and end nodes
  39. // The offsets are not really important anymore but might as well set them correctly
  40. range.setStart(startNode,0);
  41. range.setEnd(endNode,endNode.length);
  42.  
  43. // Get an array of all text nodes within the range
  44. var nodes = getNodesInRange(range);
  45.  
  46. // Place span tags with style around each textnode
  47. for (i = 0; i < nodes.length; i++)
  48. {
  49. var span = document.createElement('span');
  50. span.setAttribute("style", style);
  51. span.appendChild( document.createTextNode(nodes[i].nodeValue));
  52. nodes[i].parentNode.replaceChild( span, nodes[i] );
  53. }
  54. }
  55.  
  56. function getNodesInRange(range)
  57. {
  58. var start = range.startContainer;
  59. var end = range.endContainer;
  60. var commonAncestor = range.commonAncestorContainer;
  61. var nodes = [];
  62. var node;
  63.  
  64. // walk parent nodes from start to common ancestor
  65. for (node = start.parentNode; node; node = node.parentNode)
  66. {
  67. if (node.nodeType == 3) //modified to only add text nodes to the array
  68. nodes.push(node);
  69. if (node == commonAncestor)
  70. break;
  71. }
  72. nodes.reverse();
  73.  
  74. // walk children and siblings from start until end is found
  75. for (node = start; node; node = getNextNode(node))
  76. {
  77. if (node.nodeType == 3) //modified to only add text nodes to the array
  78. nodes.push(node);
  79. if (node == end)
  80. break;
  81. }
  82.  
  83. return nodes;
  84. }
  85.  
  86.  
  87. function getNextNode(node, end)
  88. {
  89. if (node.firstChild)
  90. return node.firstChild;
  91. while (node)
  92. {
  93. if (node.nextSibling)
  94. return node.nextSibling;
  95. node = node.parentNode;
  96. }
  97. }
  98.  
  99. span.addEventListener("click",callbackwhenclickeventtrigger,false);
  100. function callbackwhenclickeventtrigger(e){//pass as param the event target, inside this function create the un-bold function
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement