2015-10-05 94 views
2

我在我的web应用中使用TinyMCE,并允许人们在其中使用link。 这是我的配置:只允许TinyMCE的外部链接

 var editor = tinymce.init({ 
     plugins: "link", 
     selector: this.$el.find("#shortdesc")["selector"], 
     toolbar: "bold italic | undo redo | link unlink", 
     link_list: [ 
     ], 
     menubar: false, 
     relative_urls: false, 
     link_assume_external_targets: true, 
     setup: function (editor) { 
      editor.on("change", function (e) {}) 
     } 
    }); 

我试图解决的问题是,我希望让人们只插入外部链接。在当前形势下,当用户点击link按钮并确认,它显示了这个弹出

enter image description here

我的目标是避免显示这个弹出,并且只使用http://前缀的链接。

我使用的是最后一个版本tinyMCE

据我了解relative_urls选项不适合我的必需品。

任何想法?

回答

0

您应该复制链接插件,将其重命名为“mylink”,将所有引用调整为链接到mylink并修改代码,以便弹出窗口不会显示,并检查链接url为“https”/ “HTTP”,

+0

哦,我明白了。我认为有一种方法“hacky” – steo

0

其实我解决了重写tinymce.editor.convertURLfunction

   setup: function (editor) { 
       var fn = editor.convertURL; 
       editor.convertURL = convertURL_; 
       function convertURL_(url, name, elm){ 
        fn.apply(this, arguments); 
        console.log(arguments); 
        var regex = new RegExp("(http:|https:)?\/\/"); 
        if (!regex.test(url)) { 
         return url = "http://" + url 
        } 
        return url; 
       } 
      } 
+0

虽然覆盖'convertURL'是一个好的开始,对话要求我预先“http”仍然弹出(按任何按钮将愉快地结束在一个URL以“http”开头。 – Hille

1

基于一个答案found here,我写了你的问题如下紧凑的解决方案:

editor.on('init',function(e) { 

    // reference to original windowManager function 
    var fn = editor.windowManager.open; 

    // override with your own version of the function 
    editor.windowManager.open = function(t,r){ 

     // make sure you only target the 'insert link' dialog 
     if(t.title == 'Insert link'){ 

      // reference to the subumit function of the dialog 
      var oldsubmit = t.onSubmit; 

      // override the submit function 
      t.onSubmit = function(e){ 

       // append the "http://" prefix here, note that the URL is contained within the property 'href' of data. 
       // also see link/plugin.js 
       if(!e.data.href.match(/(ftp|https?):\/\//i)){ 
        e.data.href = "http://" + e.data.href; 
       } 

       // submit original function 
       return oldsubmit(e); 
      } 

      // after overwriting the submit function within the windowManager, make sure to call the original function 
      fn.apply(this, [t,r]); 
     } 

     // use return instead of apply to prevent bugs in other dialogs 
     else{ 
      return fn(t,r); 
     } 
    } 
});