christiaan / tinymce-codemirror

Development moved to gitlab.com/tinymce-plugins/tinymce-codemirror
https://gitlab.com/tinymce-plugins/tinymce-codemirror
Apache License 2.0
71 stars 29 forks source link

Plugin outside of 'js/tinymce/plugins' #33

Closed peterw8102 closed 5 years ago

peterw8102 commented 7 years ago

I'm trying to keep non-Tinymce plugins outside of the Tinymce tree to make upgrades easier.

This plugin works fine when within js/tinymce/plugins/codemirror directory. However the following fails:

<script src="/tinymce/js/tinymce/tinymce.min.js"></script>
<script src="/myplugins/codemirror/plugin.min.js></script>

The problem is the plugin tries to load 'source.html' from the path provided by TinyMCE as a parameter to tinymce.PluginManager.add()

tinymce.PluginManager.add('codemirror', function(editor, url) {
   ...
   var config = {
      title: 'HTML source code',
      url: url+'/source.html', 
      ...

'url' here will be /tinymce/js/plugins - not where the plugin actually resides.

I've fixed this by working out the path of the currently executing script then using that as the base path for source.html.

/** Work out path of current script **/
var currentPath = (function () {
    var src;
  if (document.currentScript) {
    src = document.currentScript.src;
  }
    else {
        var scripts = document.getElementsByTagName('script');
      src = scripts[scripts.length-1].src;
    }
    return src.replace(/[^\/]+$/,'');
})();
tinymce.PluginManager.requireLangPack('codemirror');

tinymce.PluginManager.add('codemirror', function(editor, url) {

    function showSourceEditor() {
                /** ..... **/
        var config = {
            title: 'HTML source code',
            url: currentPath+'/source.html',

I really don't like this as a solution and will look for something better. It should be possible though to have the plugin outside of the TinyMCE source tree.

peterw8102 commented 7 years ago

Stripping out the crud the current path can be reduced to:

var currentPath = (document.currentScript ?
                document.currentScript.src :
                Array.prototype.slice.call(document.getElementsByTagName('script')).pop().src
             ) .replace(/[^\/]+$/,'');

Still don't particularly like this. Other suggestions?

peterw8102 commented 7 years ago

I want to push a branch to this repo with suggested changed but don't have permission :(

thomich commented 5 years ago

Please check: https://github.com/christiaan/tinymce-codemirror/issues/46

thomich commented 5 years ago

@peterw8102 please check this issue on gitlab.. still open ;).. sorry, just moved the repo... https://gitlab.com/tinymce-plugins/tinymce-codemirror/issues/33

but as you wrote earlier (https://github.com/christiaan/tinymce-codemirror/issues/33#issuecomment-318521866), you can make a MR and we/i will check

peterw8102 commented 5 years ago

Thanks @thomich - I realised after I'd left the other comment so deleted it. I'll start using the new repo and when I get a moment I'll look into this again. It's been quite a while now so I need to refresh my memory.