diff --git a/forms/HtmlEditorConfig.php b/forms/HtmlEditorConfig.php index 3499dde85..aab05dc87 100644 --- a/forms/HtmlEditorConfig.php +++ b/forms/HtmlEditorConfig.php @@ -87,7 +87,12 @@ class HtmlEditorConfig { * Holder list of enabled plugins */ protected $plugins = array( - 'contextmenu', 'table', 'emotions', 'paste', '../../tinymce-advcode', 'spellchecker' + 'contextmenu' => null, + 'table' => null, + 'emotions' => null, + 'paste' => null, + 'advcode' => '../../../sapphire/thirdparty/tinymce-advcode/editor_plugin_src.js', + 'spellchecker' => null ); /** @@ -130,16 +135,26 @@ class HtmlEditorConfig { } /** - * Enable one or several plugins. Will maintain unique list if already enabled plugin is re-passed + * Enable one or several plugins. Will maintain unique list if already + * enabled plugin is re-passed. If passed in as a map of plugin-name to path, + * the plugin will be loaded by tinymce.PluginManager.load() instead of through tinyMCE.init(). + * Keep in mind that these externals plugins require a dash-prefix in their name. + * + * @see http://wiki.moxiecode.com/index.php/TinyMCE:API/tinymce.PluginManager/load + * * @param[0..] a string, or several strings, or a single array of strings - The plugins to enable * @return null */ function enablePlugins() { $plugins = func_get_args(); - if (is_array($plugins[0])) $plugins = $plugins[0]; - - foreach ($plugins as $plugin) { - if (!in_array($plugin, $this->plugins)) $this->plugins[] = $plugin; + if (is_array(current($plugins))) $plugins = current($plugins); + foreach ($plugins as $plugin => $path) { + // if plugins are passed without a path + if(is_numeric($plugin)) { + $plugin = $path; + $path = null; + } + if (!array_key_exists($plugin, $this->plugins)) $this->plugins[$plugin] = $path; } } @@ -150,16 +165,22 @@ class HtmlEditorConfig { */ function disablePlugins() { $plugins = func_get_args(); - if (is_array($plugins[0])) $plugins = $plugins[0]; + if (is_array(current($plugins))) $plugins = current($plugins); foreach ($plugins as $plugin) { - if (($idx = array_search($plugin, $this->plugins)) !== false) { - array_splice($this->plugins, $idx, 1); - continue; + if(array_key_exists($plugin, $this->plugins)) { + unset($this->plugins[$plugin]); } } } + /** + * @return Array + */ + function getPlugins() { + return $this->plugins; + } + /** * Totally re-set the buttons on a given line * @param[0] integer from 1..3 - The line number to redefine @@ -254,7 +275,23 @@ class HtmlEditorConfig { */ function generateJS() { $config = $this->settings; - $config['plugins'] = implode(',', $this->plugins); + + // plugins + $internalPlugins = array(); + $externalPluginsJS = ''; + foreach($this->plugins as $plugin => $path) { + if(!$path) { + $internalPlugins[] = $plugin; + } else { + $internalPlugins[] = '-' . $plugin; + $externalPluginsJS .= sprintf( + 'tinymce.PluginManager.load("%s", "%s");' . "\n", + $plugin, + $path + ); + } + } + $config['plugins'] = implode(',', $internalPlugins); foreach ($this->buttons as $i=>$buttons) { $config['theme_advanced_buttons'.$i] = implode(',', $buttons); @@ -262,6 +299,7 @@ class HtmlEditorConfig { return " if((typeof tinyMCE != 'undefined')) { + $externalPluginsJS tinyMCE.init(" . Convert::raw2json($config) . "); } "; diff --git a/javascript/HtmlEditorField.js b/javascript/HtmlEditorField.js index 6e0406264..e34a1a67c 100644 --- a/javascript/HtmlEditorField.js +++ b/javascript/HtmlEditorField.js @@ -2,6 +2,8 @@ * Simple TinyMCE initialisation */ if((typeof tinyMCE != 'undefined')) { + tinymce.PluginManager.load('advcode', '/some/dir/someplugin/editor_plugin.js'); + tinyMCE.init({ mode : "specific_textareas", editor_selector : "htmleditor", @@ -13,7 +15,7 @@ if((typeof tinyMCE != 'undefined')) { theme_advanced_toolbar_location : "top", theme_advanced_toolbar_align : "left", theme_advanced_toolbar_parent : "right", - plugins : "contextmenu,table,emotions,paste,../../tinymce-advcode,spellchecker", + plugins : "contextmenu,table,emotions,paste,-advcode,spellchecker", blockquote_clear_tag : "p", table_inline_editing : true, theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,justifyleft,justifycenter,justifyright,justifyfull,formatselect,separator,bullist,numlist,outdent,indent,blockquote,hr,charmap", diff --git a/tests/HtmlEditorConfigTest.php b/tests/HtmlEditorConfigTest.php new file mode 100644 index 000000000..bb0e855ee --- /dev/null +++ b/tests/HtmlEditorConfigTest.php @@ -0,0 +1,78 @@ +enablePlugins('plugin1'); + $this->assertContains('plugin1', array_keys($c->getPlugins())); + } + + function testEnablePluginsByArray() { + $c = new HtmlEditorConfig(); + $c->enablePlugins(array('plugin1', 'plugin2')); + $this->assertContains('plugin1', array_keys($c->getPlugins())); + $this->assertContains('plugin2', array_keys($c->getPlugins())); + } + + function testEnablePluginsByMultipleStringParameters() { + $c = new HtmlEditorConfig(); + $c->enablePlugins('plugin1', 'plugin2'); + $this->assertContains('plugin1', array_keys($c->getPlugins())); + $this->assertContains('plugin2', array_keys($c->getPlugins())); + } + + function testEnablePluginsByArrayWithPaths() { + $c = new HtmlEditorConfig(); + $c->enablePlugins(array('plugin1' => '/mypath/plugin1', 'plugin2' => '/mypath/plugin2')); + $plugins = $c->getPlugins(); + $this->assertContains('plugin1', array_keys($plugins)); + $this->assertEquals('/mypath/plugin1', $plugins['plugin1']); + $this->assertContains('plugin2', array_keys($plugins)); + $this->assertEquals('/mypath/plugin2', $plugins['plugin2']); + } + + function testDisablePluginsByString() { + $c = new HtmlEditorConfig(); + $c->enablePlugins('plugin1'); + $c->disablePlugins('plugin1'); + $this->assertNotContains('plugin1', array_keys($c->getPlugins())); + } + + function testDisablePluginsByArray() { + $c = new HtmlEditorConfig(); + $c->enablePlugins(array('plugin1', 'plugin2')); + $c->disablePlugins(array('plugin1', 'plugin2')); + $this->assertNotContains('plugin1', array_keys($c->getPlugins())); + $this->assertNotContains('plugin2', array_keys($c->getPlugins())); + } + + function testDisablePluginsByMultipleStringParameters() { + $c = new HtmlEditorConfig(); + $c->enablePlugins('plugin1', 'plugin2'); + $c->disablePlugins('plugin1', 'plugin2'); + $this->assertNotContains('plugin1', array_keys($c->getPlugins())); + $this->assertNotContains('plugin2', array_keys($c->getPlugins())); + } + + function testDisablePluginsByArrayWithPaths() { + $c = new HtmlEditorConfig(); + $c->enablePlugins(array('plugin1' => '/mypath/plugin1', 'plugin2' => '/mypath/plugin2')); + $c->disablePlugins(array('plugin1', 'plugin2')); + $plugins = $c->getPlugins(); + $this->assertNotContains('plugin1', array_keys($plugins)); + $this->assertNotContains('plugin2', array_keys($plugins)); + } + + function testGenerateJSWritesPlugins() { + $c = new HtmlEditorConfig(); + $c->enablePlugins(array('plugin1')); + $c->enablePlugins(array('plugin2' => '/mypath/plugin2')); + + $this->assertContains('plugin1', $c->generateJS()); + $this->assertContains('tinymce.PluginManager.load("plugin2", "/mypath/plugin2");', $c->generateJS()); + } +} \ No newline at end of file diff --git a/thirdparty/tinymce-advcode/editor_plugin_src.js b/thirdparty/tinymce-advcode/editor_plugin_src.js index e9edd392b..8ec982695 100644 --- a/thirdparty/tinymce-advcode/editor_plugin_src.js +++ b/thirdparty/tinymce-advcode/editor_plugin_src.js @@ -1,5 +1,5 @@ (function() { - tinymce.PluginManager.requireLangPack('../../tinymce-advcode'); + tinymce.PluginManager.requireLangPack('advcode'); tinymce.create('tinymce.plugins.AdvancedCodePlugin', { init : function(ed, url) { @@ -37,5 +37,5 @@ }); // Register plugin - tinymce.PluginManager.add('../../tinymce-advcode', tinymce.plugins.AdvancedCodePlugin); + tinymce.PluginManager.add('advcode', tinymce.plugins.AdvancedCodePlugin); })();