Re: Problem with Indenting and lists:Text pasted from Word document

  •  07-21-2010, 3:29 PM

    Re: Problem with Indenting and lists:Text pasted from Word document

    Made it better :)  You can now highlight and each line ending with a BR will get bulleted.
     
    function CuteEditor_OnCommand(editor,command,ui,value)   
     {   
        if (command=="InsertUnorderedList"){
            
            var selectedText = getSelectedHTML(editor);
            
            if (selectedText!=""){
                  
                //Lets create a regEx to find all BR tags
                var myRegEx = new RegExp("<[Bb][Rr][^>]*>");
                
                //Split all rows into an array
                var splitString=selectedText.split(myRegEx);
                var newString="";
                var x;
                
                //Lets create the Bullet list
                newString="<ul>";
                for (x in splitString){newString+="<li>"+splitString[x]+"</li>";}
                newString+="</ul>";
                
                //Lets paste er back
                editor.PasteHTML(newString);
                     
            } else{
            
                //create some temp text
                var tempTEXT="‰";
                
                //lets insert some temp text where our cursor is
                editor.PasteHTML(tempTEXT);
                
                //put our new html into a var and define some other vars
                var editorText=editor.getHTML();
                var editorTextToBR="";
                var startOfText="";
                var endOfText="";
                
                //get the index of the pasted text and use that to where we are entering the bullet
                var cursorPos=editorText.indexOf(tempTEXT, 0);
                
                //Lets find the index of the next breaking character BR/P/DIV in that order
                var nextBRPos=getNextBRPos(editorText,cursorPos);

                //lets define the start of the text, start of the text to the temp char
                startOfText=editorText.substring(0,cursorPos);
                
                //lets define whats going onto the bullet            
                editorTextToBR=editorText.substring(cursorPos,nextBRPos).replace(tempTEXT,"");
                
                //lets define the rest of the document text starting from after the bullet text                      
                endOfText=editorText.substring(nextBRPos,editorText.length);
                
                //lets now put all 3 components together and set our text
                editor.setHTML(startOfText+"<ul><li>"+editorTextToBR+"</li></ul>"+endOfText);
     
          }        
          return true;
       } else if (command=="InsertOrderedList"){
            
            var selectedText = getSelectedHTML(editor);
            
            if (selectedText!=""){
                    
                //Lets create a regEx to find all BR tags
                var myRegEx = new RegExp("<[Bb][Rr][^>]*>");
                
                //Split all rows into an array
                var splitString=selectedText.split(myRegEx);
                var newString="";
                var x;
                
                //Lets create the Bullet list
                newString="<ol>";
                for (x in splitString){newString+="<li>"+splitString[x]+"</li>";}
                newString+="</ol>";
                
                //Lets paste er back
                editor.PasteHTML(newString);
                        
            } else{
            
                //create some temp text
                var tempTEXT="‰";
                
                //lets insert some temp text where our cursor is
                editor.PasteHTML(tempTEXT);
                
                //put our new html into a var and define some other vars
                var editorText=editor.getHTML();
                var editorTextToBR="";
                var startOfText="";
                var endOfText="";
                
                //get the index of the pasted text and use that to where we are entering the bullet
                var cursorPos=editorText.indexOf(tempTEXT, 0);
                
                //Lets find the index of the next breaking character BR/P/DIV in that order
                var nextBRPos=getNextBRPos(editorText,cursorPos);

                //lets define the start of the text, start of the text to the temp char
                startOfText=editorText.substring(0,cursorPos);
                
                //lets define whats going onto the bullet            
                editorTextToBR=editorText.substring(cursorPos,nextBRPos).replace(tempTEXT,"");
                
                //lets define the rest of the document text starting from after the bullet text                      
                endOfText=editorText.substring(nextBRPos,editorText.length);
                
                //lets now put all 3 components together and set our text
                editor.setHTML(startOfText+"<ol><li>"+editorTextToBR+"</li></ol>"+endOfText);           

          }        
          return true;
       }
    }

    function getNextBRPos(myText,startingAtPos){
        
        //lets find the index of the next paragraph
        //break whatever it is
        //and return it's position
        var posBR = myText.indexOf("<br />", startingAtPos);    //get the index of the next BR
        var posP = myText.indexOf("</p>", startingAtPos);       //get the index of the next P
        var posDIV = myText.indexOf("</div>", startingAtPos);   //get the index of the next DIV
        
        if (posBR>0){return posBR;}
        else if(posP>0){return posP;}
        else if(posDIV>0){return posDIV;}
        else{return myText.length;}
    }
View Complete Thread