将对话框字段值设置为所选文本

Set dialog field value as selected text

本文关键字:文本 设置 对话框 字段      更新时间:2023-09-26

我想编写CKEditor插件。我在面板上添加了按钮,当我按下它时,会显示对话框。在此对话框中,我可以设置文本,并在确定后按下此文本插入编辑器。

但是我想添加功能。当我在编辑器中选择文本并按下此按钮时,我希望在该对话框字段中看到选定的文本。编辑并按Ok后,所选文本必须替换为新文本。

谢谢!

这不是

100%工作,第一部分正在工作,最终替换不是。

CKEDITOR.plugins.add( 'example',
{
    init: function( editor )
    {
        editor.addCommand( 'exampleDialog', new CKEDITOR.dialogCommand( 'exampleDialog' ) );
        editor.ui.addButton( 'example',
        {
            label: 'Insert a Link',
            command: 'exampleDialog'//,
            //icon: this.path + 'images/icon.png'
        } );
        CKEDITOR.dialog.add( 'exampleDialog', function( editor )
        {
            return {
                title : 'example Properties',
                minWidth : 400,
                minHeight : 200,
                contents :
                [
                    {
                        id : 'general',
                        label : 'Settings',
                        elements :
                        [
                            {
                                type : 'text',
                                id : 'mystring',
                                label : 'text',                             
                                commit : function( data )
                                {
                                    data.text = this.getValue();
                                }
                            }
                        ]
                    }
                ],
                onShow : function() {
                    //this._ranges = editor.getSelection().getRanges()
                    var mySelection = editor.getSelection().getSelectedText();
                    this.setValueOf("general","mystring",mySelection);
                },
                onOk : function()
                {
                    var data = {};
                    this.commitContent( data );
                    var txt = data.text;

                    editor.insertText(txt);  //this is not correct, since selection is being cleared...
                }
            };
        });
    }
});

我的解决方案是简单地将其设置为onShow函数:

    onShow: function () {
        this.setValueOf('my-tab-id', 'my-element-id', editor.getSelection().getSelectedText());
        // ...
    },

完整代码框架:

(function () {
    CKEDITOR.dialog.add('mySelectorDialog', function (editor) {
        return {
            contents: [
                {
                    id: 'my-tab-id',
                    label: 'My Tab Label',
                    elements: [
                        {
                            id: 'my-element-id',
                            type: 'text',
                            label: 'My Element Label'
                        }
                        // ...
                    ]
                }
            ],
            onShow: function () {
                this.setValueOf('my-tab-id', 'my-element-id', editor.getSelection().getSelectedText());
                // ...
            },
            onOk: function () {
                // ...
            }
            // ...
        };
    });
})();