Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TinyMCE 4 with elFinder

Is somebody already tried to integrate elFinder into new (4b1) version of TinyMCE? It looks like previous implementation isn't working. Please post some snippets, thanks a lot.

like image 349
dikirill Avatar asked Apr 15 '13 13:04

dikirill


2 Answers

Ok. I found the solution:

  1. Create folder in plugins named elfinder.
  2. Download latest elFinder and put into this folder plugins/elfinder.
  3. Add plugin 'elfinder' to the list of plugins (tinymce.init).
  4. Rename js/elfinder.min.js to js/plugin.min.js
  5. Create file plugin.min.js in root folder of plugin (elfinder/plugin.min.js)
  6. Insert next text inside and save:

tinymce.PluginManager.add("elfinder", function (editor, url) {

editor.settings.file_browser_callback = function (id, value, type, win) {

  $('<div />').dialogelfinder({
     url: url + '/php/connector.php',
     commandsOptions: {
        getfile: {
           oncomplete: 'destroy'
        }
     },
     getFileCallback: function (url)
     {
        var fieldElm = win.document.getElementById(id);
        fieldElm.value = editor.convertURL(url, null, true);
        if ("fireEvent"in fieldElm) {
           fieldElm.fireEvent("onchange")
        } else {
           var evt = document.createEvent("HTMLEvents");
           evt.initEvent("change", false, true);
           fieldElm.dispatchEvent(evt)
        }
     }
  });   

}; }, ["elfinder/js"]);

like image 113
dikirill Avatar answered Oct 14 '22 22:10

dikirill


I updated the Wiki, should work now when following the steps: https://github.com/Studio-42/elFinder/wiki/Integration-with-TinyMCE-4.x

Primary changes are that TinyMCE doesn't use the InlinePopup plugin any more, the callback is changed and instead of file_browser_callback : 'elFinderBrowser' you have to remove the quotes:

In the TinyMCE init: file_browser_callback : elFinderBrowser

Add the elFinderBrowser callback to your javascript:

function elFinderBrowser (field_name, url, type, win) {
  tinymce.activeEditor.windowManager.open({
    file: '/elfinder/elfinder.html',// use an absolute path!
    title: 'elFinder 2.0',
    width: 900,  
    height: 450,
    resizable: 'yes'
  }, {
    setUrl: function (url) {
      win.document.getElementById(field_name).value = url;
    }
  });
  return false;
}

And finally modify/copy elfinder.html file to use the callback:

<!-- Include jQuery, jQuery UI, elFinder (REQUIRED) -->

<script type="text/javascript">
  var FileBrowserDialogue = {
    init: function() {
      // Here goes your code for setting your custom things onLoad.
    },
    mySubmit: function (URL) {
      // pass selected file path to TinyMCE
      top.tinymce.activeEditor.windowManager.getParams().setUrl(URL);

      // close popup window
      top.tinymce.activeEditor.windowManager.close();
    }
  }

  $().ready(function() {
    var elf = $('#elfinder').elfinder({
      // set your elFinder options here
      url: 'php/connector.php',  // connector URL
      getFileCallback: function(file) { // editor callback
        FileBrowserDialogue.mySubmit(file.url); // pass selected file path to TinyMCE 
      }
    }).elfinder('instance');      
  });
</script>
like image 34
Barryvdh Avatar answered Oct 14 '22 23:10

Barryvdh