13

我正在使用 django-ckeditor,但链接和图像存在一些问题。

关于链接:

添加链接接口

在此界面中,您可以看到最终用户无法使用它,因为它太复杂并且可能导致错误和安全问题,因为该按钮Browse Server实际上允许用户浏览上传的内容。我想要的是非常简单的东西:只是一个自动附加 http (如果不是由用户输入)并在新窗口中打开链接的输入文本,也就是 target _blank

我尝试使用以下代码编辑 config.js。这已删除UploadAdvanced选项卡,从Info选项卡中删除了不必要的小部件并默认设置为目标_blank。但是该Target选项卡仍然存在并且用户可以更改它,因为我显然无法删除此选项卡,否则默认目标将被忽略我坚持这一点。那么,我怎样才能将目标设置为_blank并删除Target选项卡呢?有没有办法隐藏这个选项卡,但不删除它?

CKEDITOR.on('dialogDefinition', function(ev) {
    // Take the dialog name and its definition from the event data.
    var dialogName = ev.data.name;
    var dialogDefinition = ev.data.definition;

    // Check if the definition is from the dialog we're
    // interested in (the 'link' dialog).
    if (dialogName == 'link') {
        // Remove the 'Target', 'Upload' and 'Advanced' tabs from the 'Link' dialog.
//        dialogDefinition.removeContents('target');
        dialogDefinition.removeContents('upload');
        dialogDefinition.removeContents('advanced');

        // Get a reference to the 'Link Info' tab.
        var infoTab = dialogDefinition.getContents('info');

        // Remove unnecessary widgets from the 'Link Info' tab.         
        infoTab.remove('linkType');
        infoTab.remove('protocol');
        infoTab.remove('browse');

        // Get a reference to the "Target" tab.
        var targetTab = dialogDefinition.getContents('target');
        // Set the default value for the URL field.
        var targetField = targetTab.get('linkTargetType');
        targetField['default'] = '_blank';
    }

});

关于图片:

有一个非常相似的情况:几个选项卡有太多选项。我需要的是像在 Stackoverflow 中附加图像的选项一样简单。是否有任何免费插件可以让我通过链接添加图像并使用 ckeditor 从计算机(带有预可视化)上传它们?

谢谢!

4

4 回答 4

20

最后,我得到了简单的对话框:包括链接,从链接附加图像或从计算机上传,并以简单的方式包括 Youtube 视频。为此,我编辑了名为的配置文件config.js,对于我的版本 CKeditor 4.1.2,它看起来像这样:

CKEDITOR.editorConfig = function( config ) {
    // Define changes to default configuration here.
    // For the complete reference:
    // http://docs.ckeditor.com/#!/api/CKEDITOR.config

    // Comment the following line in DEBUG mode:
    config.removePlugins = 'devtools';

    // See the most common block elements.
    config.format_tags = 'p;h1;h2;h3;pre';

    // Make dialogs simpler.
    config.removeDialogTabs = 'image:advanced;image:Link;link:advanced;link:upload';
    config.linkShowTargetTab = false;

    // In CKEditor 4.1 or higher you need to disable ACF (Advanced Content Filter)
    // to make Youtube plugin work:
    config.allowedContent = true;
};

CKEDITOR.on('dialogDefinition', function(ev) {
    // Take the dialog name and its definition from the event data.
    var dialogName = ev.data.name;
    var dialogDefinition = ev.data.definition;

    // Check if the definition is from the dialog we're
    // interested in (the 'link' dialog).
    if (dialogName == 'link') {
//        Remove the 'Upload' and 'Advanced' tabs from the 'Link' dialog.
//        dialogDefinition.removeContents('upload');
//        dialogDefinition.removeContents('advanced');

        // Get a reference to the 'Link Info' tab.
        var infoTab = dialogDefinition.getContents('info');
        // Remove unnecessary widgets from the 'Link Info' tab.
        infoTab.remove('linkType');
        infoTab.remove('protocol');
        infoTab.remove('browse');

        // Get a reference to the "Target" tab and set default to '_blank'
        var targetTab = dialogDefinition.getContents('target');
        var targetField = targetTab.get('linkTargetType');
        targetField['default'] = '_blank';

    } else if (dialogName == 'image') {
//        Remove the 'Link' and 'Advanced' tabs from the 'Image' dialog.
//        dialogDefinition.removeContents('Link');
//        dialogDefinition.removeContents('advanced');

        // Get a reference to the 'Image Info' tab.
        var infoTab = dialogDefinition.getContents('info');
        // Remove unnecessary widgets/elements from the 'Image Info' tab.
        infoTab.remove('browse');
        infoTab.remove('txtHSpace');
        infoTab.remove('txtVSpace');
        infoTab.remove('txtBorder');
        // infoTab.remove('cmbAlign');

    }
});

为此,我阅读了很多文档,但启发我的最佳页面如下:

我希望这可以帮助其他有同样问题的人。干杯!

于 2013-06-17T11:30:47.137 回答
6

这是我为 CKEditor v3.6.1 进行的许多调整以使其可用(尤其是图像对话框和链接对话框)。它们似乎也适用于 CKEditor 4.x,只需获取您需要的内容config.js

CKEDITOR.editorConfig = function( config ) {
    // Define changes to default configuration here. For example:
    config.language = 'de';
    config.extraPlugins = 'colordialog';
    // config.extraPlugins = 'matheeditor';
    // config.uiColor = '#AADC6E';
    // config.image_previewText = CKEDITOR.tools.repeat('Custom lorem ipsum text here', 8 );
    // config.contentsLanguage = 'de';

    config.linkShowAdvancedTab = false;
    config.linkShowTargetTab = false; 

    config.height = 350;
    config.width = 680;

    // change color palette
    config.colorButton_colors = 'F00,11C11D,00F,B700B7,FF8C00,008080,808080,D3D3D3';
    config.colorButton_enableMore = false;

    // smaller editor-width for mobile devices
    if (/iPhone|iPod/i.test(navigator.userAgent)) {
        config.width = 300;
    }

    // for resizing the editor window
    config.resize_minHeight = 350;
    config.resize_maxHeight = 880;
    config.resize_maxWidth = 910;

    // remove all formatting from pasted text
    config.forcePasteAsPlainText = true;

    // remove font size, family, bg color from pasted text
    config.pasteFromWordRemoveFontStyles = true;

    // allow browser's spell checker
    config.disableNativeSpellChecker = false;

    // disable ckeditor context menu to allow native context menu (works on holding CTRL)
    // open: http://stackoverflow.com/questions/2246631/how-to-disable-ckeditor-context-menu/12477378

    // shortcuts for firefox and chrome (editor breaks if assigned in IE9)
    // if(navigator.userAgent.toLowerCase().indexOf('firefox') > -1 || navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
    if ( !(/MSIE (\d+\.\d+);/.test(navigator.userAgent)) ) {
        config.keystrokes = [
            // [ CKEDITOR.SHIFT + 45, 'pastefromword' ], //INS
            [ CKEDITOR.CTRL + 76, 'link' ], //L
            [ CKEDITOR.CTRL + CKEDITOR.ALT + 66, 'image' ], //B
            [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 77, 'specialchar' ], //M
            [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 188, 'subscript' ], //COMMA
            [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 109, 'subscript' ], //-
            [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 191, 'subscript' ], //#
            [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 190, 'superscript' ], //PERIOD
            [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 107, 'superscript' ], //+
            [ CKEDITOR.CTRL + 66, 'bold' ], //B
            [ CKEDITOR.CTRL + 73, 'italic' ], //I
            [ CKEDITOR.CTRL + 85, 'underline' ], //U
            [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 70, 'bold' ], //F
            [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 75, 'italic' ], //K
            [ CKEDITOR.CTRL + CKEDITOR.SHIFT + 85, 'underline' ], //U
        ];
    }
};

CKEDITOR.on( 'dialogDefinition', function( ev ) {
    // take the dialog name and its definition from the event data
    var dialogName = ev.data.name;
    var dialogDefinition = ev.data.definition;
    //var dialog = CKEDITOR.dialog.getCurrent(); 
    //alert( dialog.getName() );

    // check if the definition is from the dialog we are interested in (the 'link' dialog).
    if(dialogName == 'link') {

        dialogDefinition.onShow = function () {
            var dialog = CKEDITOR.dialog.getCurrent(); 
            //dialog.hidePage( 'target' ); // via config 
            //dialog.hidePage( 'advanced' ); // via config 
            elem = dialog.getContentElement('info','anchorOptions');    
            elem.getElement().hide();
            elem = dialog.getContentElement('info','emailOptions'); 
            elem.getElement().hide();
            var elem = dialog.getContentElement('info','linkType'); 
            elem.getElement().hide();
            elem = dialog.getContentElement('info','protocol'); 
            elem.disable();
        };

    }
    else if(dialogName == 'image') {

        // get a reference to the 'Link Info' tab.
        var infoTab = dialogDefinition.getContents('info');
        // remove unnecessary fields
        infoTab.remove('ratioLock');
        infoTab.remove('txtHeight');         
        infoTab.remove('txtWidth');         
        infoTab.remove('txtBorder');
        infoTab.remove('txtHSpace');
        infoTab.remove('txtVSpace');
        infoTab.remove('cmbAlign');

        //hide image preview (v2)
        //field = infoTab.get( 'htmlPreview' );
        //field.style = 'display:none';

        // memo: dialogDefinition.onShow = ... throws JS error (C.preview not defined) 
        dialogDefinition.onLoad = function () {
            var dialog = CKEDITOR.dialog.getCurrent(); 

            // hide image preview
            var elem = dialog.getContentElement('info','htmlPreview');  
            elem.getElement().hide();

            // hide tabs and show only upload
            dialog.hidePage('Link'); 
            dialog.hidePage('advanced'); 
            this.selectPage('Upload');

            // hide url on start up, prevent user input external image URLs 
            // goes in onShow of image.js: dialog.hidePage('info'); 

            // hide ok button so that upload button can only be used
            // goes in onShow of image.js: document.getElementById(this.getButton('ok').domId).style.display='none';

            // on tab switching or automatic after upload
            this.on('selectPage', function (e) {
                // show okay button of ckeditor dialog
                document.getElementById(this.getButton('ok').domId).style.display='inline';
                // after upload the selectPage is fired, show Bild-Info then
                dialog.showPage( 'info' ); 
            });
        };

    }
    else if(dialogName == 'table') {
        dialogDefinition.removeContents('advanced');
    }

});
于 2013-12-04T22:06:45.367 回答
6

如果你使用 django-ckeditor,你可以简单地在 settings.py 文件中进行如下配置。您可以根据需要对其进行配置。无需与 JS 混为一谈。

CKEDITOR_CONFIGS = {
    'default': {
        'toolbar': 'Custom',
        'toolbar_Custom': [
            ['Bold', 'Italic', 'Underline', 'Strike'],
            [
                'NumberedList',
                'BulletedList',
                'Outdent',
                'Indent',
                '-',
                'JustifyLeft',
                'JustifyCenter',
                'JustifyRight',
                'JustifyBlock'
            ],
            ['Link', 'Unlink'],
            ['RemoveFormat', 'Source'],
        ],
        'height': 300,
        'width': 695,
        'linkShowAdvancedTab': False,
        'linkShowTargetTab': True,
    },
}
于 2016-05-03T18:15:28.763 回答
3

关于链接

随意删除“目标”选项卡:

dialogDefinition.removeContents( 'target' );

使用以下功能dataProcessor

CKEDITOR.replace( 'editor1', {
    on: {
        instanceReady: function() {
            this.dataProcessor.htmlFilter.addRules( {
                elements: {
                    a: function( element ) {
                        element.attributes.target = '_blank';
                    }
                }
            });
        }
    }
} );

这将添加target="_blank"到编辑器输出中的所有<a>元素。请参阅文档以了解更多信息。

关于图像

除了 CKFinder(商业)、KCFinder、PDW 文件浏览器和 Jasfinder 之外,别无其他。至少我不能再回忆了。

于 2013-06-11T08:05:09.783 回答