Permalink
Browse files

ENHANCEMENT Added support for loading external plugins (with relative…

… paths) in HtmlEditorConfig. This means relative paths can be separate from the plugin name, and fixes a bug where paths containing dashes were ignored by TinyMCE.init().

ENHANCEMENT Changed sapphire/thirdparty/tinymce-advcode to use the original plugin name, and specify its relative path through HtmlEditorConfig instead. (from r94060)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@95582 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information...
1 parent 62de1eb commit 1588da54d85cf2cda154f3e205982c129e0b85dd @halkyon halkyon committed Dec 16, 2009
View
@@ -87,7 +87,12 @@ static function get_available_configs_map() {
* 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 @@ function setOptions($a) {
}
/**
- * 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 @@ function enablePlugins() {
*/
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,14 +275,31 @@ function removeButtons() {
*/
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);
}
return "
if((typeof tinyMCE != 'undefined')) {
+ $externalPluginsJS
tinyMCE.init(" . Convert::raw2json($config) . ");
}
";
@@ -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",
@@ -0,0 +1,78 @@
+<?php
+/**
+ * @package sapphire
+ * @subpackage tests
+ */
+class HtmlEditorConfigTest extends SapphireTest {
+
+ function testEnablePluginsByString() {
+ $c = new HtmlEditorConfig();
+ $c->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());
+ }
+}
@@ -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);
})();

0 comments on commit 1588da5

Please sign in to comment.