切换到源模式后,CKEditor会丢弃窗口小部件数据

时间:2016-04-06 13:45:44

标签: javascript ckeditor

我正在使用以下代码开发一个简单的小部件。任何人都可以向我解释为什么切换到"源模式"后,属性和文本iv设置转为未定义?

<pre>
    /*
    /dialogs/b2eupobject.js
    */

    CKEDITOR.dialog.add( 'b2eupobject', function( editor ) {
        return {
            title: 'Edit Simple Box',
            minWidth: 200,
            minHeight: 100,
            contents: [
                {
                    id: 'info',
                    elements: [
                        {
                            id: 'objectId',
                            type: 'select',
                            label: 'ObjectName',
                            items: [ ['select', '-1' ],
                               ['first name', '1' ],
                               ['last name', '2' ],

                            ],
                            setup: function( widget ) {
                                this.setValue( widget.data.title );
                                //this.setValue(widget.data.desc);
                            },
                            commit: function( widget ) {

                                widget.setData( 'title', this.getValue() );
                                var input = this.getInputElement().$;
                                widget.setData('desc',input.options[ input.selectedIndex ].text);

                            }
                        },


                    ]
                }
            ],
            onShow: function() {

                            // The code that will be executed when a dialog window is loaded. This function will be defined above the onOk function
                            var selection = editor.getSelection(); //get to the element that is selected by the user (either highlighted or just having the caret inside)
                            var element = selection.getStartElement(); // get the element in which the selection starts
                            if ( element )
                            element = element.getAscendant( 'em', true ); // get the abbr element
                            console.log(this);
                },
            onOk: function() {

                            // The code that will be executed when a dialog window is loaded. This function will be defined above the onOk function
                            var selection = editor.getSelection(); //get to the element that is selected by the user (either highlighted or just having the caret inside)
                            var element = selection.getStartElement(); // get the element in which the selection starts

                        //  if ( element )

                        console.log (element);
                            console.log(this);
                }   

        };
    } );
</pre>
<pre>
    /*
    plugin.js
     */

    CKEDITOR.plugins.add( 'b2eupobject', {


            requires: 'widget', //reference the generic Widget plugin that provides the Widget API
            icons: 'b2eupobject',

            init: function( editor ) {
            CKEDITOR.dialog.add( 'b2eupobject', this.path + 'dialogs/b2eupobject.js' );
            editor.widgets.add( 'b2eupobject', {
                button: 'Insert user object',
                template:'<em class="b2eUp">test </em>',
                toolbar:'widgets',
                parts: {elm: 'em.b2eUp'},  //this is a CSS selector of the element within the template above that you want to target

                allowedContent: 'em ( b2eUp) [title]',
                requiredContent : 'em(b2eUp) [title]',
                dialog: 'b2eupobject',

                init: function (widget) {
                    console.log("widget init : "+widget);
                    // widget.parts.elm.setAttribute('title',this.data.title); 
                    // widget.parts.elm.setHtml(this.data.desc);
                },
                data: function (widget) {
                    this.parts.elm.setAttribute('title',this.data.title); 
                    this.parts.elm.setHtml(this.data.desc);
                },
                upcast: function( element ) {
                    alert(element.name == 'em' && element.hasClass( 'b2eUp' ));
                    return element.name == 'em' && element.hasClass( 'b2eUp' );
                    },

            } );



            var pluginDirectory = this.path;
            editor.addContentsCss( pluginDirectory + 'style/style.css' );

            if ( editor.contextMenu ) {// check if contextmenu plugin exsists 
                editor.addMenuGroup( 'b2eupgrouGroup' ); // register a new menu group called abbrGroup.
                editor.addMenuItem( 'b2eupItem', { //register a new menu item that will belong to the newly created group.
                label: 'Edit user object',
                icon: this.path + 'icons/b2eupobject.png',
                command: 'b2eupobject',
                group: 'b2eupgrouGroup'
                });
            editor.contextMenu.addListener( function( element ) { // add an event listener function that will be called whenever the context menu is fired.
            if ( element.getAscendant( 'em', true ) ) {
                // check if element is of type abbr
                return { simpleboxitem: CKEDITOR.TRISTATE_OFF };
                }
                });
                };

            },



    enter code here

    } );
</pre>

我正在使用CKEditor 4.5版,我安装了小部件和对话框插件。 *******另外******

当我在编辑器中从源模式返回时,我可以看到调用了upcast函数。 写入日志,我可以看到它返回true并且元素对象:

FF控制台:

attributes: Object { class="b2eUp",  title="1",  data-cke-widget-data="%7B%7D",  more...}
children:[Object { value="first name",  _={...},  parent={...},  more...}]

0 个答案:

没有答案