PDA

View Full Version : Quicktags editor + createDelegate issue



moraes
6 Nov 2006, 3:47 AM
Hi.

I've made a port from Alex King's JS Quicktags to use with YUI.ext Toolbar. The result is here:

http://tipos.org/demos/quicktags/

It works very well on FF/Windows, but there is one problem on IE: when you select a text to apply a tag, it ignores document.selection.createRange(). Maybe it is related to how createDelegate() works. This is how buttons are added (this.b is an array of buttons definitions):


tb.addButton({className: this.b[i].id, text: this.b[i].text, click: this.insertTag.createDelegate(this, [i])});

And this is the IE part in the insertTag() function:

insertTag : function(i) {
//IE support
if (document.selection) {
this.f.focus();
sel = document.selection.createRange();
if (sel.text.length > 0) {
sel.text = this.b[i].tagStart + sel.text + this.b[i].tagEnd;
}
else {
if (!this.checkOpenTags(i) || this.b[i].tagEnd == '') {
sel.text = this.b[i].tagStart;
this.addTag(i);
}
else {
sel.text = this.b[i].tagEnd;
this.removeTag(i);
}
}
this.f.focus();
}
//MOZILLA/NETSCAPE support
...

Despite a text is selected in the textarea, sel.text.length is always 0. Any clues?

jack.slocum
6 Nov 2006, 4:38 AM
Try removing the focus() call from the beginning. I think focus() will blow away the selection. I could be wrong though.

moraes
6 Nov 2006, 4:46 AM
Unfortunately it didn't work. Removing focus(), the tag is inserted in the toolbar button instead of the textarea.

jack.slocum
6 Nov 2006, 5:00 AM
I just set up a page on it's own, the selection is lost when you click and focus doesn't appear to bring it back.

jack.slocum
6 Nov 2006, 5:12 AM
This is a different library but it should help you with it. IE wipes the selection when you click and you will have to store it when it's created to later do what you want.

http://www.telerik.com/community/forums/thread/b311D-edhdk.aspx

moraes
6 Nov 2006, 5:22 AM
Ok, I'll take a look.

In the demo, it works fine on IE:
http://alexking.org/projects/js-quicktags/demo/index.html

But not using a YUI.ext toolbar + the click event (although for FF it is still fine). Somewhere in the middle it loses the selection on IE. Weird. I'll keep trying.

jack.slocum
6 Nov 2006, 5:48 AM
I just swapped my local test to use a button and it works. I guess buttons don't steal selection.

jack.slocum
6 Nov 2006, 5:55 AM
I got ya covered! Here's a monster patch, include it anywhere after yui-ext.js. The change was the addition of the IE only attribute unselectable="on".



YAHOO.ext.ToolbarButton.prototype.init = function(appendTo){
var element = document.createElement('span');
element.className = 'ytb-button';
element.unselectable = 'on';
if(this.id){
element.id = this.id;
}
this.disabled = (this.disabled === true);
var inner = document.createElement('span');
inner.className = 'ytb-button-inner ' + this.className;
inner.unselectable = 'on';
if(this.tooltip){
element.setAttribute('title', this.tooltip);
}
if(this.style){
YAHOO.ext.DomHelper.applyStyles(inner, this.style);
}
element.appendChild(inner);
appendTo.appendChild(element);
this.el = getEl(element, true);
inner.innerHTML = (this.text ? this.text : '');
this.el.mon('click', this.onClick, this, true);
this.el.mon('mouseover', this.onMouseOver, this, true);
this.el.mon('mouseout', this.onMouseOut, this, true);
};

moraes
6 Nov 2006, 6:06 AM
Yes, it worked! :D

Thank you very much, Jack. I'd never figure out this patch.

jack.slocum
6 Nov 2006, 6:10 AM
It took me a little longer than usual... sorry about that. It's 9am and I've been up all night. :)

moraes
6 Nov 2006, 6:15 AM
I should have pointed the demo before. It seems it gave you a good clue.

jack.slocum
6 Nov 2006, 6:30 AM
As soon as I saw the hideous buttons I knew there had to be a reason he was using them. :D

moraes
6 Nov 2006, 12:24 PM
How about a section on this forum where people could share what they have done with YUI.ext? I would post this quicktags + YUI.ext toolbar combination there. It's the WordPress basic editor, plus a spice. :D

jack.slocum
6 Nov 2006, 4:39 PM
Good idea. I will add it.