简体   繁体   English

在CKEditor中,如何在删除目标选项卡时将链接在新窗口中设置为默认打开?

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

I want links to open on a new window as default.我希望链接默认在新窗口中打开。 I tried:我试过:

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

It doesn't work.它不起作用。 But I figured out that if I remove the following line.但我发现如果我删除以下行。 It works.有用。

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

But then the problem is now there is the target tab that allow user to change the link target.但问题是现在存在允许用户更改链接目标的目标选项卡。

What I want to keep the editor as simple as possible and don't want to allow users to change the link target.我想让编辑器尽可能简单并且不想让用户更改链接目标。 Yet, I want to set default target as target:_blank.但是,我想将默认目标设置为 target:_blank。 Any suggestions?有什么建议? Thanks!谢谢!

It seems that if you remove the Target tab, you cannot change the default value to "new window".似乎如果您删除“目标”选项卡,则无法将默认值更改为“新窗口”。

However, you can remove all the options in the Target list except "new window", and set it as the default value.但是,您可以删除目标列表中除“新窗口”之外的所有选项,并将其设置为默认值。

Try the following code:试试下面的代码:

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';
    }
});

In this case, the Target tab still exists, but there's only one value ("new window") to select, so the users can't change this.在这种情况下,目标选项卡仍然存在,但只有一个值(“新窗口”)可供选择,因此用户无法更改此值。

Hope this helps.希望这可以帮助。

Another way to do it to provide the necessary data in onOk function, see this particular commit在 onOk 函数中提供必要数据的另一种方法,请参阅此特定提交

You add target attribute to data object in the onOk function in plugins/link/dialogs/link.js您在 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
     }

Here is how I updated my links so they open in new tab, without affecting any other links on the page but only those from ckEditor.这是我更新链接的方式,以便它们在新选项卡中打开,而不会影响页面上的任何其他链接,但只会影响来自 ckEditor 的链接。 This is in Angular, but you can use the same idea depending on your platform.这是在 Angular 中,但您可以根据您的平台使用相同的想法。

I created a Pipe to transform the link to include a target:我创建了一个管道来转换链接以包含一个目标:

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"');
  }

}

I then applied the pipe to my content:然后我将管道应用于我的内容:

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

This seems much easier than all the other options out there where you have to modify the ckeditor files.这似乎比您必须修改 ckeditor 文件的所有其他选项容易得多。 Hope this helps someone.希望这可以帮助某人。

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;';

Go to ckeditor/plugins/link/dialogs/link.js and paste the below code:转到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.

相关问题 如何在新标签页或窗口中打开 iframe 链接 - How to open iframe link in new tab or window Amcharts-如何在新标签页/窗口中打开链接 - Amcharts - How to open link in a new tab/window 如何在启用了选项卡的新窗口中打开链接 - How to open a link in new window with tab enabled JavaScript:如何在没有 window.open() 的情况下在新选项卡中打开链接? - JavaScript: How to open a link in a new tab without window.open()? 如果有 target=_blank,如何在新选项卡中打开链接? - How can I open a link in a new tab if it has target=_blank? 如何在没有目标属性的选项卡式或新窗口中打开链接? - How to make a link open in tabbed or new window without target attribute? 如何在单击链接时打开新选项卡或窗口? - How can I open a new tab or window when a link is clicked? 如何使用jquery在新选项卡或窗口中捕获打开的链接? - how to capture open link in new tab or window using jquery? 捕获“新标签中的打开链接”和“在新窗口中打开链接”事件 - capture “open link in new tab” and “Open link in new window” event 使用 target="_blank"/window.open() 打开新窗口/选项卡时如何防止 sessionStorage 被继承? - How to prevent sessionStorage being inherited when using target="_blank"/window.open() to open a new window/tab?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM