Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- document.designMode = "on";
- // Color the first section
- var selection = window.getSelection();
- selection.removeAllRanges();
- selection.addRange(range1);
- if (!selection.isCollapsed){
- document.execCommand("foreColor", false, color1);
- }
- // Color the middle section
- selection.removeAllRanges();
- selection.addRange(range2);
- if (!selection.isCollapsed){
- document.execCommand("backColor", false, color2);
- document.execCommand("foreColor", false, color3);
- }
- // Color the last section
- selection.removeAllRanges();
- selection.addRange(range3);
- if (!selection.isCollapsed){
- document.execCommand("foreColor", false, color1);
- }
- document.designMode = "off";
- selection.removeAllRanges();
- function styleRange(range, style) // the style is a string of css styles. eg."background-color:darkblue; color:blue;"
- {
- // Get the start and end nodes and split them to give new start and end nodes with only text that falls inside the range.
- var startNode = range.startContainer.splitText(range.startOffset);
- var endNode = range.endContainer.splitText(range.endOffset).previousSibling;
- // Adjust the range to contain the new start and end nodes
- // The offsets are not really important anymore but might as well set them correctly
- range.setStart(startNode,0);
- range.setEnd(endNode,endNode.length);
- // Get an array of all text nodes within the range
- var nodes = getNodesInRange(range);
- // Place span tags with style around each textnode
- for (i = 0; i < nodes.length; i++)
- {
- var span = document.createElement('span');
- span.setAttribute("style", style);
- span.appendChild( document.createTextNode(nodes[i].nodeValue));
- nodes[i].parentNode.replaceChild( span, nodes[i] );
- }
- }
- function getNodesInRange(range)
- {
- var start = range.startContainer;
- var end = range.endContainer;
- var commonAncestor = range.commonAncestorContainer;
- var nodes = [];
- var node;
- // walk parent nodes from start to common ancestor
- for (node = start.parentNode; node; node = node.parentNode)
- {
- if (node.nodeType == 3) //modified to only add text nodes to the array
- nodes.push(node);
- if (node == commonAncestor)
- break;
- }
- nodes.reverse();
- // walk children and siblings from start until end is found
- for (node = start; node; node = getNextNode(node))
- {
- if (node.nodeType == 3) //modified to only add text nodes to the array
- nodes.push(node);
- if (node == end)
- break;
- }
- return nodes;
- }
- function getNextNode(node, end)
- {
- if (node.firstChild)
- return node.firstChild;
- while (node)
- {
- if (node.nextSibling)
- return node.nextSibling;
- node = node.parentNode;
- }
- }
- span.addEventListener("click",callbackwhenclickeventtrigger,false);
- function callbackwhenclickeventtrigger(e){//pass as param the event target, inside this function create the un-bold function
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement