Emulating Bold Button Behaviour for H1

Last post 08-23-2007, 1:37 PM by Adam. 3 replies.
Sort Posts: Previous Next
  •  08-23-2007, 10:07 AM 32763

    Emulating Bold Button Behaviour for H1

    Hi Adam,
     
    I'd like to emulate the behaviour of the Bold toolbar button, but for h1 tags instead.  I know the size/format comboboxes are available, but I'd rather not give that much control to clients.
     
    We will always have a sensible h1 style defined in the CSS so it makes sense to use it and I rather like the way the bold button works, being highlighted and switching functionality depending on the location of the cursor etc.
     
    Any help much appreciated,
     
    Richard.
  •  08-23-2007, 11:14 AM 32771 in reply to 32763

    Re: Emulating Bold Button Behaviour for H1

    Richard,
     
    You can create a custom button. When people click it. It calls the following JavaScript function:

    <script runat="server">
     void Page_Load(object sender, System.EventArgs e)
     {
      if(! this.IsPostBack )
      {
       Editor1.Attributes["oncommand"]="return CatchCommand(this);";
      }

      CuteEditor.ToolControl tc = Editor1.ToolControls["insertcustombutonhere"];
      if(tc!=null)
      {    
       System.Web.UI.WebControls.Image Image1 = new System.Web.UI.WebControls.Image ();
       Image1.ToolTip    = "Head 1";
       Image1.ImageUrl    = "tools.gif";
       Image1.CssClass    = "CuteEditorButton";
       SetMouseEvents(Image1);
       Image1.Attributes["onclick"]="var editdoc=CuteEditor_GetEditor(this).GetDocument(); editdoc.execCommand('formatblock', false, "Heading 1");";
       tc.Control.Controls.Add(Image1);
      }
     }
     
     void SetMouseEvents(WebControl control)
     {
      control.Attributes["onmouseover"]="CuteEditor_ButtonCommandOver(this)";
      control.Attributes["onmouseout"]="CuteEditor_ButtonCommandOut(this)";
      control.Attributes["onmousedown"]="CuteEditor_ButtonCommandDown(this)";
      control.Attributes["onmouseup"]="CuteEditor_ButtonCommandUp(this)";
      control.Attributes["ondragstart"]="CuteEditor_CancelEvent()";
     }

    </script>

     

    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

  •  08-23-2007, 12:08 PM 32779 in reply to 32771

    Re: Emulating Bold Button Behaviour for H1

    Hi Adam,
     
    Thanks for the reply, this isn't quite what I'm after.  Ideally I'd like it to wrap only the selected text or nearest word with h1 tags, in the same way the Bold function does.  Also, it would seem using the formatblock command identifier is only one way, i.e. you can only apply the format, not reverse it.
     
    I'd really like to mimic the Bold button behavior 100% if possible, especially the way it can detect whether the highlighted text is bold already and change to a 'Bold Off' button.  Can it be done?
     
    Regards,
     
    Richard.
  •  08-23-2007, 1:37 PM 32784 in reply to 32779

    Re: Emulating Bold Button Behaviour for H1

    Richard,
     
    The <H1> tag is a block level tag. So the correct way to apply Heading 1 tag is using 'formatblock'.
     
    If you still want to do this, use the following code:
     

       var editor1 = document.getElementById('<%=Editor1.ClientID%>');
       var editor1doc = editor1.GetDocument();

       var editdoc=editor1.GetDocument();  
       var editwin = editor1.GetWindow();

       var selectedhtml = getSelectedHTML();

       editor1.PasteHTML("<h1>"+ selectedhtml + "</h1>");

     

        function getSelectedHTML(){
          var rng=null,html="";

          if (document.selection && document.selection.createRange){
            rng=editdoc.selection.createRange();
            html=rng.htmlText||"";
          }else if (window.getSelection){
            rng=editwin.getSelection();

            if (rng.rangeCount > 0 && window.XMLSerializer){
              rng=rng.getRangeAt(0);
              html=new XMLSerializer().serializeToString(rng.cloneContents());
            }
          }
          return html;
        }

    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

View as RSS news feed in XML