Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hyperlink improvements #21

Open
GoogleCodeExporter opened this issue Jun 22, 2015 · 1 comment
Open

hyperlink improvements #21

GoogleCodeExporter opened this issue Jun 22, 2015 · 1 comment

Comments

@GoogleCodeExporter
Copy link

Great script.  

One thing I thought could use some improvements is hyperlinks.  Currently,
clicking the hyperlink toolbar without a range does nothing.  To completely
remove a hyperlink, the range must be perfectly selected on the including a
hyperlink can ... otherwise there's potential for some leftovers.  And
editing hyperlinks is ... rather difficult. 


I've improved this as follows:

Revised the hyperlink code under the widgToolbarAction function.  
Now it loops through parent nodes until the A is found.  The range is then
expanded to include the entire anchor, the HREF is grabbed and
pre-populates the prompt.  If user clears the URL, this will remove
hyperlink; otherwise the link is updated.


case "link":

if (theIframe.contentWindow.document.selection) 
{  /*IE ranges */
theRange = theIframe.contentWindow.document.selection.createRange()
theSelection = theRange.text;
theParentNode = theRange.parentElement();
}
else 
{ /* mozilla ranges */
theSelection = theIframe.contentWindow.getSelection();
theRange = theSelection.getRangeAt(0)
theParentNode = theRange.commonAncestorContainer;
}

// previous function set the toolbar icon if hyperlink or not
if (!this.parentNode.className.classExists("on")){

if (theSelection == "")
{
alert("Please select the text you wish to hyperlink.");
break;
}

var theURL = prompt("Enter the URL for this link:", "http://");
theIframe.contentWindow.document.execCommand("CreateLink", false, theURL);
theWidgEditor.theToolbar.setState("Link", "on");
}

else {

var p = theParentNode;
while(p.nodeName.toLowerCase()!="a" ){ p = p.parentNode }

if( theWidgEditor.IE ) 
{
theRange.moveToElementText( p );
theRange.select();
} 
else 
{
theRange.selectNode( p );
}

var theURL = prompt("Update the URL for this link:", p.href); 
if( theURL == null ) break; // cancel cancels

// always remove
theIframe.contentWindow.document.execCommand("Unlink", false, null);
theWidgEditor.theToolbar.setState("Link", "off");

if( theURL != "" ) 
{ // if url was specified, set it up
theIframe.contentWindow.document.execCommand("CreateLink", false, theURL);
theWidgEditor.theToolbar.setState("Link", "on");
}
break;

}
break;


Original issue reported on code.google.com by tyler.wa...@gmail.com on 20 Feb 2009 at 11:52

@GoogleCodeExporter
Copy link
Author

Oh, i just noticed ... forgot to have an unlink call in the top-section. Ensure
Unlink is always called on the selection: 

"
var theURL = prompt("Enter the URL for this link:", "http://);
theIframe.contentWindow.document.execCommand("Unlink", false, null);
theIframe.contentWindow.document.execCommand("CreateLink", false, theURL);
theWidgEditor.theToolbar.setState("Link", "on");
"


Original comment by tyler.wa...@gmail.com on 21 Feb 2009 at 2:25

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant