0

我想在 CKEditor 中制作侧边栏插件。当我选择带有粗体和斜体标签的文本时,提取内容会跳过该标签,并只返回文本。我想要所有选定的文本,包括所有标签。

这是我的代码:

function sidebar(editor){
    var selection = editor.getSelection();
    if (selection.getSelectedText() != "") {

        var ranges = selection.getRanges();
        var pNode = editor.document.createElement('p');
        var extractedContent = ranges[0].extractContents();
        pNode.append(extractedContent);
        var customNode = editor.document.createElement('cdl:sidebar');
        customNode.append(pNode);
        var sidebarHolder = editor.document.createElement("sidebarholder");
        sidebarHolder.append(customNode);
        var nodeHtml = sidebarHolder.getHtml();
        selection.selectRanges(ranges);
        editor.insertHtml(nodeHtml);
    }
    else {
        showErrorMessage("Selection is not proper");
    }
}
4

1 回答 1

1

您可以使用 ckeditor walk 获取多选文本。

// Walker searching for guardElements.
                    var walker = new CKEDITOR.dom.walker( range );

                    var start = bookmarks[ i ].startNode,
                        end = bookmarks[ i++ ].endNode;

                    walker.evaluator = function( node )
                    {
                        return !! ( node.type == CKEDITOR.NODE_ELEMENT
                                && node.getName() in guardElements
                                && !( node.getName() == ( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' )
                                    && node.getParent().type == CKEDITOR.NODE_ELEMENT
                                    && node.getParent().getName() == 'blockquote' )
                                // Element must be fully included in the range as well. (#6485).
                                && node.getPosition( start ) & CKEDITOR.POSITION_FOLLOWING
                                && ( ( node.getPosition( end ) & CKEDITOR.POSITION_PRECEDING + CKEDITOR.POSITION_CONTAINS ) == CKEDITOR.POSITION_PRECEDING ) );
                    };

使用 walker.js http://docs.cksource.com/ckeditor_api/symbols/src/core_dom_walker.js.html

于 2012-12-19T11:11:50.770 回答