web-dev-qa-db-fra.com

quicktag fonctionnel

Je veux ajouter des balises rapides qui font plus que simplement ajouter du balisage. Par exemple, je voudrais ajouter un quicktag pour exécuter une fonction appliquer une regex js sur le contenu de la page. Comment puis-je ajouter un quicktag qui exécute une fonction?

Pour donner un exemple spécifique, je voudrais ajouter un bouton qui supprime toutes les balises de balisage dans l'ensemble du texte du message. Ceci est analogue aux quicktags prédéfinis pour trouver des tags sans correspondance, ou "relire", etc.

1
dkretz

Exemple simple qui va ajouter un bouton Quicktag qui appelle une fonction Javascript quand on clique dessus ...

<?PHP
function _add_my_quicktags(){ ?>
    <script type="text/javascript">
    QTags.addButton( 'alert', 'Alert Button', simple_alert );

        function simple_alert() { 
            alert('hello, example callback function');
        }
    </script>
<?php 
}
add_action('admin_print_footer_scripts',  '_add_my_quicktags');
?>

UPDATE .... Ajout d'une fonction de rappel utilisant les variables passées ...

    // Add a button called 'abbr' with a callback function
    QTags.addButton( 'abbr', 'Abbr', abbr_Prompt );
    // and this is the callback function
    function abbr_Prompt(e, c, ed) {
        var prmt, t = this;
        if ( ed.canvas.selectionStart !== ed.canvas.selectionEnd ) {
            // if we have a selection in the editor define out tagStart and tagEnd to wrap around the text
            // Prompt the user for the abbreviation and return gracefully on a null input
            prmt = Prompt('Enter Abbreviation');
            if ( prmt === null ) return;
            t.tagStart = '<abbr title="' + prmt + '">';
            t.tagEnd = '</abbr>';
        } else if ( ed.openTags ) {
            // if we have an open tag, see if it's ours
            var ret = false, i = 0, t = this;
            while ( i < ed.openTags.length ) {
                ret = ed.openTags[i] == t.id ? i : false;
                i ++;
            }
            if ( ret === false ) {
                // if the open tags don't include 'abbr' Prompt for input
                prmt = Prompt('Enter Abbreviation');
                if ( prmt === null ) return;
                t.tagStart = '<abbr title="' + prmt + '">';
                t.tagEnd = false;
                if ( ! ed.openTags ) {
                    ed.openTags = [];
                }
                ed.openTags.Push(t.id);
                e.value = '/' + e.value;
            } else {
                // otherwise close the 'abbr' tag
                ed.openTags.splice(ret, 1);
                t.tagStart = '</abbr>';
                e.value = t.display;
            }
        } else {
            // last resort, no selection and no open tags
            // so Prompt for input and just open the tag
            prmt = Prompt('Enter Abbreviation');
            if ( prmt === null ) return;
            t.tagStart = '<abbr title="' + prmt + '">';
            t.tagEnd = false;
            if ( ! ed.openTags ) {
                ed.openTags = [];
            }
            ed.openTags.Push(t.id);
            e.value = '/' + e.value;
        }
        // now we've defined all the tagStart, tagEnd and openTags we process it all to the active window
        QTags.TagButton.prototype.callback.call(t, e, c, ed);
    };
2
JasonDavis

vous devriez écrire un plugin tinymce, puis le charger (en utilisant le plugin tinymce advanced)

0
boyska