最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - tinymce custom-button replace content - Stack Overflow

programmeradmin2浏览0评论

I added a custom 'quote' button.

ed.addButton('blockquote', {
    title : 'blockquote',
    cmd : 'mceblockquote',
    image : url + '/img/blockquote.gif',
    onclick : function() {
        var blockquoteActive = tinyMCE.activeEditor.controlManager.get('blockquote').isActive();
        if (blockquoteActive) {
             //replace <blockquote> tags ?!
             //set Button inactive       
        }
        else {
            ed.selection.setContent('<blockquote>' + ed.selection.getContent() + '</blockquote><br />');
        }
        }

});

ed.onNodeChange.add(function(ed, cm, n) {
    cm.setActive('blockquote', n.nodeName == 'IMG');
})

When I click the button, everything works fine. The selection is quoted.

  • How do I replace the blockquote-tags when klicking the button again?
  • How do I set the button inactive?

Regards, saromba

I added a custom 'quote' button.

ed.addButton('blockquote', {
    title : 'blockquote',
    cmd : 'mceblockquote',
    image : url + '/img/blockquote.gif',
    onclick : function() {
        var blockquoteActive = tinyMCE.activeEditor.controlManager.get('blockquote').isActive();
        if (blockquoteActive) {
             //replace <blockquote> tags ?!
             //set Button inactive       
        }
        else {
            ed.selection.setContent('<blockquote>' + ed.selection.getContent() + '</blockquote><br />');
        }
        }

});

ed.onNodeChange.add(function(ed, cm, n) {
    cm.setActive('blockquote', n.nodeName == 'IMG');
})

When I click the button, everything works fine. The selection is quoted.

  • How do I replace the blockquote-tags when klicking the button again?
  • How do I set the button inactive?

Regards, saromba

Share Improve this question edited Dec 8, 2011 at 14:33 Thariama 50.9k13 gold badges145 silver badges175 bronze badges asked Dec 8, 2011 at 12:58 sarombasaromba 4982 gold badges8 silver badges24 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

it worked thanks... I've made some changes (maybe / probably improvements).

When nothing is selected, do nothing

When text is already quoted

When user marked the text with a double-click, the blockquote element will now be removed

onclick : function() {
                var blockquoteActive = tinyMCE.activeEditor.controlManager.get('blockquote').isActive();
                var selection = ed.selection.getContent();
                if (blockquoteActive) {

                   if (selection) {
                       var parElem = ed.dom.getParent(ed.selection.getNode(), 'blockquote');
                       var inner = parElem.innerHTML;
                       ed.dom.remove(parElem);
                       ed.selection.setContent(inner);
                   }
                   else return

                }
                else {
                    if (selection) {
                        ed.selection.setContent('<blockquote>' + ed.selection.getContent() + '</blockquote><br />');
                    }
                }
            }

Try this. You may modify it a bit.

ed.addButton('blockquote', {
    title : 'blockquote',
    cmd : 'mceblockquote',
    image : url + '/img/blockquote.gif',
    onclick : function() {
        var blockquoteActive = tinyMCE.activeEditor.controlManager.get('blockquote').isActive();
        if (blockquoteActive) {
             //replace <blockquote> tags ?!

             content = ed.selection.getContent();
             content.replace(/<\/?blockquote>/ig,'');
             ed.selection.setContent(content);

             //set Button inactive

             // works only if blockquote is registered at the controlManager
             ed.controlManager.setActive('blockquote', false);

        }
        else {
            ed.selection.setContent('<blockquote>' + ed.selection.getContent() + '</blockquote><br />');
        }
    }

});
发布评论

评论列表(0)

  1. 暂无评论