Announcement

Do not use the forums to submit bug reports, feature requests or patches, submit a New Ticket instead.

#1 2007-06-12 14:35:32

cjn
New member
Registered: 2007-06-12
Posts: 4

Help me write a plugin

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

#2 2007-06-14 17:22:16

junkwarrior
Xinha Community Member
Registered: 2006-03-12
Posts: 14

Re: Help me write a plugin

Does using HTMLArea.removeFromParent(tt)  instaed of  tt.parentNode.removeChild(tt) work for deletion?

Offline

#3 2007-06-15 08:07:42

cjn
New member
Registered: 2007-06-12
Posts: 4

Re: Help me write a plugin

Nope, that doesn't change anything. I tried editor.removeFromParent(tt) too, with the same result.

Thanks for the idea though.

Offline

#4 2007-06-15 09:26:29

wymsy
Xinha Community Member
From: Massachusetts, USA
Registered: 2005-04-01
Posts: 44
Website

Re: Help me write a plugin

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.

Offline

#5 2007-06-15 09:43:39

cjn
New member
Registered: 2007-06-12
Posts: 4

Re: Help me write a plugin

wymsy wrote:

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

#6 2007-06-15 10:35:37

ray
Xinha Administrator
From: Germany
Registered: 2005-03-23
Posts: 521
Website

Re: Help me write a plugin

Just leave the else part away, as you only get there when HAVE the element and then you set it NULL again

Offline

#7 2007-06-15 10:57:00

cjn
New member
Registered: 2007-06-12
Posts: 4

Re: Help me write a plugin

ray wrote:

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

Board footer

Powered by FluxBB