Menu

#6 Enable image links, better remove link.

open
nobody
None
5
2002-10-26
2002-10-26
No

Hi there!

I've fixed two issues in the richtext application. One was
that it wasn't possible to link images and the other was
that the remove link function didn't work properly..

Cheers,
Spocke

------------------------------------------------------------------------------
// Removes any "anchor" link elemens in the current
selection
function removeLinkElements(node) {
// * * If link found remove it
if (node.nodeName == "A" && node.firstChild)
node.replaceNode(node.firstChild);

// * * Process children
if (node.hasChildNodes) {
for( var i=0; i<node.childNodes.length; i++ )
removeLinkElements(node.childNodes[i]);
}
}

// link(): called to insert a hyperlink. It will use the
selected text
// if there is some, or the URL entered if not. If clicked
when over a
// link, that link is allowed to be edited.
function link(on)
{
if (!RichEditor.txtView) return; // Disabled in View
Source mode

var strURL = "http://";
var strText;
var el;
var isImage;

// First, pick up the current selection.
doc.focus();
var r = document.selection.createRange();
isImage = (document.selection.type == "Control");

// * * Handle link on for example images
if (!r.parentElement) {
el = r.item(0);
} else
el = r.parentElement();

// * * Handle remove link
if (!on) {
removeLinkElements(el);
return;
}

// Is this aready a link?
if (el && el.nodeName == "A") {
r.moveToElementText(el);
strURL = el.href;
}

// Get the text associated with this link
if (!isImage)
strText = r.text;
else {
// Get parent anchor url
if (el.parentNode &&
el.parentNode.nodeName == "A")
strURL = el.parentNode.href;
}

// Prompt for the URL
strURL = window.prompt("Enter URL", strURL);
if (strURL) {
if (!isImage) {// Replace text with new URL
// Default the TEXT to the url if non selected
if (!strText || !strText.length) {
strText = strURL;
}

try {
r.pasteHTML('<A href=' + strURL + '
target=_new>' + strText + '</a>');
} catch (e) {
// * * Do nothing, case
of unsupported linkage
}
} else { // Handle image link
var element = document.createElement( "A" );

element.href = strURL;
el.border = "0";
element.appendChild(el.cloneNode());

el.replaceNode(element);
}
}

reset();
}

Discussion


Log in to post a comment.