You are not logged in.
Pages: 1
I'm trying to write a plugin that converts selected text to monospace and back, via the <tt> tag. This is partly because I want this functionality, and partly because I'm trying to learn how Xinha works.
Anyway, my plugin actually does add <tt>'s to selected text (at least in firefox). The problem is that it doesn't remove them, instead adding nested <tt>'s. Of course, the toolbar button doesn't update in the same way that bold and underline do either. I've been trying to copy from Abbreviation and InsertMarquee, but those use popups, so they're hard to mould to my purposes. Can anyone tell me what I'm doing wrong, or what I'm not doing? Here's what I've got:
MonoSpace.prototype.buttonPress = function(editor, node) {
var html = editor.getSelectedHTML();
var sel = editor._getSelection();
var range = editor._createRange(sel);
var tt = editor._activeElement(sel);
if (tt && tt.tagName.toLowerCase() != 'tt') {
tt = editor._getFirstAncestor(sel, 'tt');
}
else {
tt = null;
}
if (tt) {
var child = tt.innerHTML;
tt.parentNode.removeChild(tt);
editor.insertHTML(child);
}
else {
//This works too
//editor.insertHTML('<tt>' + html + '</tt>');
var doc = editor._doc;
tt = doc.createElement('tt');
tt.innerHTML = html;
if (Xinha.is_ie) {
range.pasteHTML(tt.outerHTML);
} else {
editor.insertNodeAtSelection(tt);
}
}
}
If this ends up working, I'll do up a plugin-writing HOWTO based on my experience.
Offline
Does using HTMLArea.removeFromParent(tt) instaed of tt.parentNode.removeChild(tt) work for deletion?
Offline
Nope, that doesn't change anything. I tried editor.removeFromParent(tt) too, with the same result.
Thanks for the idea though.
Offline
The problem is most likely that tt is always null, i.e. the program never finds the existing <tt> and therefore always executes the "else" branch.
You're right, of course. Which means that this part:
var tt = editor._activeElement(sel);
if (tt && tt.tagName.toLowerCase() != 'tt') {
tt = editor._getFirstAncestor(sel, 'tt');
}
is not doing what I want it to do. The question is: "why?". Any ideas?
Offline
Just leave the else part away, as you only get there when HAVE the element and then you set it NULL again
Yup, that did it. Thank you!
Yeesh, I haven't been such a clueless noob in a long time! Sorry to bother all of you about things I should've been able to figure out on my own. Now I'm off to figure out how to toggle the toolbar button off and on, and write up a how-to.
Offline
Pages: 1