Menu

Optimize the implement for modify link

lhelper
2003-07-27
2003-07-27
  • lhelper

    lhelper - 2003-07-27

    <DIV align=left><FONT size=2>I encountered a problem when I tried to modify the attribute (href, target, etc.)&nbsp;of a exist link:&nbsp;the editor&nbsp;inserts a new &lt;A&gt;&lt;/A&gt; element between my original link with the new attributes, but not changes the attribute of the exist &lt;A&gt;&lt;/A&gt; element.<BR>
    <BR>
    Im using the 0.18b release edition, but i get the same problem in the later release.<BR>
    </FONT><FONT size=2><BR>
    So I hacked the code to fit my need.&nbsp;here is what i did:</FONT></DIV>
    <DIV align=left>
    <TABLE class="" style="WIDTH: 100%; HEIGHT: 100%" border=1 maxcols="1">
    <TBODY>
    <TR>
    <TD>

    <P>
    function c_link( e, v ) {<BR>
    &nbsp;&nbsp;&nbsp; /*====================================<BR>
    &nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;get the&nbsp;correct parentElemnt of the link<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*====================================*/<BR>
    &nbsp;&nbsp;&nbsp; var start = d.selection.createRange().parentElement();<BR>
    &nbsp;&nbsp;&nbsp; while((start!=null) &amp;&amp; (start.tagName != "A")) {<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; start = start.parentElement;<BR>
    &nbsp;&nbsp;&nbsp; }<BR>
    &nbsp;&nbsp;&nbsp; /*====================================<BR>
    &nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;expand the selection to the whole content of the link<BR>
    &nbsp;&nbsp;&nbsp; &nbsp;*====================================*/<BR>
    &nbsp;&nbsp;&nbsp; if( start ) {<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var r = d.body.createTextRange();<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.findText( start.innerText );<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.select();<BR>
    &nbsp;&nbsp;&nbsp; }</P>

    <P>
    &nbsp;&nbsp;&nbsp; if( v ) {<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lnk.href = v.href;<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lnk.target = v.target;<BR>
    &nbsp;&nbsp;&nbsp; }<BR>
    &nbsp;&nbsp;&nbsp; /*====================================<BR>
    &nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;get the&nbsp;attribute of the link<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*====================================*/<BR>
    &nbsp;&nbsp;&nbsp;&nbsp; else if( start ) {<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lnk.href = start.href;<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lnk.target = start.target;<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lnk.innerHTML = start.innerText;<BR>
    &nbsp;&nbsp;&nbsp; } else {<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lnk.href = '';<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lnk.target = '_blank';<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lnk.innerHTML = range().htmlText;<BR>
    &nbsp;&nbsp;&nbsp; }<BR>
    &nbsp;&nbsp;&nbsp; <BR>
    &nbsp;&nbsp;&nbsp; lnk.anchors = d.anchors;<BR>
    &nbsp;&nbsp;&nbsp; var a = showModalDialog( path + 'aiLink.html', lnk, 'scroll:0;help:0;status:0;dialogWidth:400px;dialogHeight:275px;' );<BR>
    &nbsp;&nbsp;&nbsp; if( a ) {<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var r = range();<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if( r.control ) {<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.item(0).applyElement( d.createElement( '&lt;a href="' + a.href + '" target="' + a.target + '"&gt;' ) );<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.pasteHTML( '&lt;a href="' + a.href + '" target="' + a.target + '" &gt;' + a.innerHTML + '&lt;/a&gt;' );<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>
    &nbsp;&nbsp;&nbsp; }<BR>
    }</P>
    </TD>
    </TR></TBODY></TABLE></DIV>

     
    • lhelper

      lhelper - 2003-07-27

      Sorry! I copied and pasted the source code from my editor directly! What I want to say is:

      I encountered a problem when I tried to modify the attribute (href, target, etc.) of a exist link: the editor inserts a new <A></A> element between my original link with the new attributes, but not changes the attribute of the exist <A></A> element.

      Im using the 0.18b release edition, but i get the same problem in the later release.

      So I hacked the code to fit my need. here is what i did:
      function c_link( e, v ) {
          /*====================================
           * get the correct parentElemnt of the link
           *====================================*/
          var start = d.selection.createRange().parentElement();
          while((start!=null) && (start.tagName != "A")) {
              start = start.parentElement;
          }
          /*====================================
           * expand the selection to the whole content of the link
           *====================================*/
          if( start ) {
              var r = d.body.createTextRange();
              r.findText( start.innerText );
              r.select();
          }

          if( v ) {
              lnk.href = v.href;
              lnk.target = v.target;
          }
          /*====================================
           * get the attribute of the link
           *====================================*/
           else if( start ) {
              lnk.href = start.href;
              lnk.target = start.target;
              lnk.innerHTML = start.innerText;
          } else {
              lnk.href = '';
              lnk.target = '_blank';
              lnk.innerHTML = range().htmlText;
          }
         
          lnk.anchors = d.anchors;
          var a = showModalDialog( path + 'aiLink.html', lnk, 'scroll:0;help:0;status:0;dialogWidth:400px;dialogHeight:275px;' );
          if( a ) {
              var r = range();
              if( r.control ) {
                  r.item(0).applyElement( d.createElement( '<a href="' + a.href + '" target="' + a.target + '">' ) );
              } else {
                  r.pasteHTML( '<a href="' + a.href + '" target="' + a.target + '" >' + a.innerHTML + '</a>' );
              }
          }
      }

       

Log in to post a comment.

MongoDB Logo MongoDB