4

查看文档,您可以将启动数据传递给小部件:

editor.execCommand( 'simplebox', {
    startupData: {
        align: 'left'
    }
} );

然而,这些数据是毫无意义的,因为似乎没有办法影响模板输出 - 它已经在小部件的初始化之前生成,而且数据在那时甚至不可用:

editor.widgets.add('uselesswidget', {

    init: function() {
        // `this.data` is empty..
        // `this.dataReady` is false..

        // Modifying `this.template` here does nothing..
        // this.template = new CKEDITOR.template('<div>new content</div>');

        // Just after init setData is called..
        this.on('data', function(e) {
            // `e.data` has the data but it's too late to affect the tpl..
        });
    },

    template: '<div>there seems to be no good way of creating the widget based on the data..</div>'

});

此外添加 CKEditor 标记会引发“未捕获的 TypeError:无法读取未定义的属性 'align'”异常,因此数据似乎也没有传递给原始模板:

template: '<div>Align: {align}</div>'

CKEDITOR.template.output如果无法动态传递数据,那么拥有一个可以接受上下文的函数有什么意义?

到目前为止,我发现的唯一可怕的 hacky 解决方案是拦截 a 中的命令beforeCommandExec并阻止它,然后修改template并再次手动执行命令。

根据传递的数据生成动态模板的任何想法?谢谢。

4

1 回答 1

0

这就是我的做法。

小部件定义:

template:
   '<div class="myEditable"></div>',

init: function () {
        // Wait until widget fires data event
        this.on('data', function(e) {

            if (this.data.content) {
                // Clear previous values and set initial content
                this.element.setHtml('')
                var newElement = CKEDITOR.dom.element.createFromHtml( this.data.content );
                this.element.append(newElement,true);
                this.element.setAttribute('id', this.data.id);
            }

            // Create nestedEditable
            this.initEditable('myEditable', {
                selector: '.myEditable',
                allowedContent: this.data.allowedContent
            })

        });
    }

动态小部件创建:

        editor.execCommand('myEditable', {startupData: {
            id: "1",
            content: "some <em>text</em>",
            allowedContent: {
              'em ul li': true,
            }
        }});
于 2018-07-02T14:34:26.350 回答