2015-10-05 78 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); 
     } 
    } 
});