This is a migrated thread and some comments may be shown as answers.

[Solved] Insert Symol before & after selected text in Editor

0 Answers 156 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Atul Deshmukh
Top achievements
Rank 1
Atul Deshmukh asked on 06 Aug 2009, 06:53 AM
Hello,

I am using a Editor control in that i want to do one functionality i.e when i select text from the editor & pressing on symbol button
 then i want to insert that symbol before & after  the selected text.

Here are Code that i USe

   <radE:RadEditor ID="txtExpressionText" Editable="true" EnableServerSideRendering="false"
                        EnableHtmlIndentation="false" ToolsFile="~/SurveyAnalysis/ToolsFile.xml" runat="server"
                        OnClientLoad="OnClientLoad" Width="100%"  Height="100px" onchange="UpdateDirtyFlag();">
                    </radE:RadEditor>

Button that inset  the symbol before & after the selected text

<asp:Button ID="btnPlus" OnClientClick="return GetContent('+ ');" UseSubmitBehavior="false"
                        runat="server" Text="+" />
Javascript
 function GetContent(Symbol)  
    {
 
        //Get a reference to the RadEditor  
        var editor = GetRadEditor("<%=txtExpressionText.ClientID%>");  
        var content = editor.GetHtml();  
        var text = editor.GetText();  
        var xxx=addSymbol(editor,Symbol);
        //document.getElementById('<%= hiddenName.ClientID %>').value=xxx;
        // update the dirty flag as the formula expression is modified
        UpdateDirtyFlag();
           return false;
    } 

function addSymbol(editor,Symbol) {
     var elementArray=Symbol.split(",");
     var ArrayLength=elementArray.length;
     if(ArrayLength==4)
     {
        if(elementArray[2]=='undefined')
        {
            alert("Please Select Element ");
            return;
        }
     }
     
  var formData = editor.GetSelection();
  formData=formData.GetText();
   if (formData == undefined) {
    /* This function is in TabbedApplicationPage.master and handles the prefixes */
      /* that can get applied to the IDs set in the source of the ASP page.        */
      var txtArea =editor.GetText();  
      var txtLength=txtArea.length;
      ctl00_contentPlaceHolder_txtExpressionText.SetFocus();
      var cursorpos = getCursorPos(editor);
     
      if(cursorpos==0)
      {
        cursorpos = GetPreviousCursorPosition();
            if(isNaN(cursorpos))
            {
                cursorpos=txtLength;
                alert("Please Select Cursor position");
                return;
            }
        if(cursorpos > txtLength || cursorpos==0 )
        {
            cursorpos=txtLength;
        }
      }
      SetPreviousCursorPosition(cursorpos + Symbol.length);
      var firstPart = txtArea.substring(0, cursorpos );
      var secondPart = txtArea.substring(cursorpos , txtLength);
      editor.SetHtml(firstPart + Symbol + secondPart);
      var text=editor.Html;
      editor.innerHTML = text;
      editor.SetFocus();
      return text;
      }
   else {
            var txtArea =editor.GetText();
            txtArea=txtArea.replace(formData,Symbol);
            editor.SetHtml(txtArea);
            editor.SetFocus();
            return txtArea;
      }
       editor.SetFocus();
   return false;
   }

function getCursorPos(textElement) {
 //save off the current value to restore it later,
 var sOldText =textElement.GetHtml(); //textElement.value;

//create a range object and save off it's text
 var objRange = document.selection.createRange();
 
 var sOldRange =objRange.text;

//set this string to a small string that will not normally be encountered
 var sWeirdString = '#%~';

//insert the weirdstring where the cursor is at
 objRange.text = sOldRange + sWeirdString;
 objRange.moveStart('character',(sOldRange.length - sWeirdString.length));//(0 - sOldRange.length - sWeirdString.length));

//save off the new string with the weirdstring in it
 var sNewText = textElement.GetHtml(); //textElement.value;

//set the actual text value back to how it was
 objRange.text = sOldRange;

//look through the new string we saved off and find the location of
//the weirdstring that was inserted and return that value
 for (i=0; i <= sNewText.length; i++) {
   var sTemp = sNewText.substring(i, i + sWeirdString.length);
   if (sTemp == sWeirdString) {
     var cursorPos = (i - sOldRange.length);
     return  cursorPos;
  }
   
 }
}


Here i provide all the code which i used in my application
I want to insert symbol before & after the selected text in editor. please provide the solution

Thanks,
Atul




   





No answers yet. Maybe you can help?

Tags
Editor
Asked by
Atul Deshmukh
Top achievements
Rank 1
Share this question
or