簡體   English   中英

在CKEditor中,如何在刪除目標選項卡時將鏈接在新窗口中設置為默認打開?

[英]In CKEditor, how to set link open in a new window as default while removing the target tab?

我希望鏈接默認在新窗口中打開。 我試過:

CKEDITOR.on('dialogDefinition', function ( ev ){
   if(ev.data.name == 'link'){
      ev.data.definition.getContents('target').get('linkTargetType')['default']='_blank';
   }
});

它不起作用。 但我發現如果我刪除以下行。 有用。

config.removeDialogTabs = 'image:advanced;image:Link;link:advanced;link:target';

但問題是現在存在允許用戶更改鏈接目標的目標選項卡。

我想讓編輯器盡可能簡單並且不想讓用戶更改鏈接目標。 但是,我想將默認目標設置為 target:_blank。 有什么建議? 謝謝!

似乎如果您刪除“目標”選項卡,則無法將默認值更改為“新窗口”。

但是,您可以刪除目標列表中除“新窗口”之外的所有選項,並將其設置為默認值。

試試下面的代碼:

CKEDITOR.on('dialogDefinition', function(e) {
    if (e.data.name === 'link') {
        var target = e.data.definition.getContents('target');
        var options = target.get('linkTargetType').items;
        for (var i = options.length-1; i >= 0; i--) {
            var label = options[i][0];
            if (!label.match(/new window/i)) {
                options.splice(i, 1);
            }
        }
        var targetField = target.get( 'linkTargetType' );
        targetField['default'] = '_blank';
    }
});

在這種情況下,目標選項卡仍然存在,但只有一個值(“新窗口”)可供選擇,因此用戶無法更改此值。

希望這可以幫助。

在 onOk 函數中提供必要數據的另一種方法,請參閱此特定提交

您在 plugins/link/dialogs/link.js 的 onOk 函數中向數據對象添加目標屬性

onOk: function() {
            var data = {};

            // Collect data from fields.
            this.commitContent( data );


            // Overwrite, always set target="_blank"
            data.target = {
                dependent: "",
                fullscreen: "",
                height: "",
                left: "",
                location: "",
                menubar: "",
                name: "_blank",
                resizable: "",
                scrollbars: "",
                status: "",
                toolbar: "",
                top: "",
                type: "_blank",
                width: ""
            };
     //more code below
     }

這是我更新鏈接的方式,以便它們在新選項卡中打開,而不會影響頁面上的任何其他鏈接,但只會影響來自 ckEditor 的鏈接。 這是在 Angular 中,但您可以根據您的平台使用相同的想法。

我創建了一個管道來轉換鏈接以包含一個目標:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'setlinktarget'
})
export class SetLinkTarget implements PipeTransform {

  transform(value: any): any {
    return value.split('<a ').join('<a target="_new"');
  }

}

然后我將管道應用於我的內容:

<div [innerHTML]="myRecord.commentsWithHTML | setlinktarget">

這似乎比您必須修改 ckeditor 文件的所有其他選項容易得多。 希望這可以幫助某人。

CKEDITOR.on('dialogDefinition', function (ev) { 
   var dialogName = ev.data.name;
   var dialog = ev.data.definition.dialog;
   var dialogDefinition = ev.data.definition;

   if(dialogName == 'link') {
      dialogDefinition.onLoad = function () {
         if(dialogName == 'link'){
             dialogDefinition.getContents('target').get('linkTargetType')['default']='_blank';
         }
         dialog.hidePage( 'target' );                   
    };
}
});

//And configure the below
 config.removeDialogTabs = 'image:advanced;link:advanced;link:upload;';

轉到ckeditor/plugins/link/dialogs/link.js並粘貼以下代碼:

/* Here we are latching on an event ... in this case, the dialog open event */

CKEDITOR.on('dialogDefinition', function(ev) {
    try {    
        /* this just gets the name of the dialog */    
        var dialogName = ev.data.name;

        /* this just gets the contents of the opened dialog */
        var dialogDefinition = ev.data.definition;

        /* Make sure that the dialog opened is the link plugin ... otherwise do nothing */
        if(dialogName == 'link') {`enter code here`    
            /* Getting the contents of the Target tab */
            var informationTab = dialogDefinition.getContents('target');

            /* Getting the contents of the dropdown field "Target" so we can set it */
            var targetField = informationTab.get('linkTargetType');

            /* Now that we have the field, we just set the default to _blank
               A good modification would be to check the value of the
               URL field and if the field does not start with "mailto:" or a
               relative path, then set the value to "_blank" */
            targetField['default'] = '_blank';
        }
    } catch(exception) {
         alert('Error ' + ev.message);
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM