Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ckeditor插件:如何展开文本?_Ckeditor_Unwrap - Fatal编程技术网

Ckeditor插件:如何展开文本?

Ckeditor插件:如何展开文本?,ckeditor,unwrap,Ckeditor,Unwrap,我已经创建了一个ckeditor插件,它将所选文本包装成一个范围。 我想知道,当我将此插件应用于先前已包装到span中的文本时,如何才能打开选定的 CKEDITOR.plugins.add('important', { // Register the icons. They must match command names. //trick to get a 16*16 icon : http://www.favicomatic.com icons: 'important

我已经创建了一个ckeditor插件,它将所选文本包装成一个范围。 我想知道,当我将此插件应用于先前已包装到span中的文本时,如何才能打开选定的

CKEDITOR.plugins.add('important', {
    // Register the icons. They must match command names.
    //trick to get a 16*16 icon : http://www.favicomatic.com
    icons: 'important',
    init: function (editor) {
        editor.addCommand('important', {
            // Define the function that will be fired when the command is executed.
            exec: function (editor) {
                var selected_text = editor.getSelection().getSelectedText();
                console.log(editor.getSelection()) ;
                var newElement = new CKEDITOR.dom.element("span");
                newElement.setAttributes({class: 'important'});
                newElement.setText(selected_text);
                editor.insertElement(newElement);
                //how to unwrap the selected text ?


        });

        // Create the toolbar button that executes the above command.
        editor.ui.addButton('important', {
            label: 'Set this as important',
            command: 'important',
            toolbar: 'insert'
        });
    }
});

最后,使用editor.getSelection().getStartElement(),我可以检查起始元素是否已经用类包装,并在必要时删除它

CKEDITOR.plugins.add('important', {
    //trick to get a 16*16 icon : http://www.favicomatic.com
    icons: 'important',
    init: function (editor) {
        var className = 'important';
        editor.addCommand('important', {
            // Define the function that will be fired when the command is executed.
            exec: function (editor) {
                var editorSelection = editor.getSelection();
                var selected_text = editorSelection.getSelectedText();
                var startElement = editorSelection.getStartElement();

                //if the element has already been wrapped, let's UNwrap it
                if (className === startElement.$.className) {
                    var html = startElement.$.innerHTML;
                    editor.getSelection().getStartElement().remove();
                    editor.insertHtml(html);
                } else {
                    //if the element has NOT already been wrapped, let's wrap it
                    var newElement = new CKEDITOR.dom.element("span");
                    newElement.setAttributes({class: 'important'});
                    newElement.setText(selected_text);
                    editor.insertElement(newElement);
                }
            }
        });

        // Create the toolbar button that executes the above command.
        editor.ui.addButton('important', {
            label: 'Set this as important',
            command: 'important',
            toolbar: 'insert'
        });
    }
});