From 3a1c813b288090853942296d52f12157ae83c946 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Mon, 5 Mar 2018 16:06:51 +1300 Subject: [PATCH] API Add getContentCSS() / setContentCSS() to allow per-config customisation of content_css Fixes #7873 --- .../05_Typography.md | 7 +++ src/Forms/HTMLEditor/HTMLEditorConfig.php | 10 ++-- src/Forms/HTMLEditor/TinyMCEConfig.php | 56 +++++++++++++++++-- .../Forms/HTMLEditor/TinyMCEConfigTest.php | 24 ++++++++ 4 files changed, 86 insertions(+), 11 deletions(-) diff --git a/docs/en/02_Developer_Guides/15_Customising_the_Admin_Interface/05_Typography.md b/docs/en/02_Developer_Guides/15_Customising_the_Admin_Interface/05_Typography.md index a9460f6bd..eee575a9e 100644 --- a/docs/en/02_Developer_Guides/15_Customising_the_Admin_Interface/05_Typography.md +++ b/docs/en/02_Developer_Guides/15_Customising_the_Admin_Interface/05_Typography.md @@ -17,6 +17,13 @@ SilverStripe\Forms\HTMLEditor\TinyMCEConfig: Will load the `mysite/css/editor.css` file. +Alternatively, you can set this on a specific `TinyMCEConfig` instance via `setContentCSS` method. + +```php +$config = new TinyMCEConfig(); +$config->setContentCSS([ '/mysite/client/css/editor.css' ]); +``` + ## Custom style dropdown The custom style dropdown can be enabled via the `importcss` plugin bundled with admin module. ([Doc](https://www.tinymce.com/docs/plugins/importcss/)) diff --git a/src/Forms/HTMLEditor/HTMLEditorConfig.php b/src/Forms/HTMLEditor/HTMLEditorConfig.php index 3faf8ef40..24b4c8e9a 100644 --- a/src/Forms/HTMLEditor/HTMLEditorConfig.php +++ b/src/Forms/HTMLEditor/HTMLEditorConfig.php @@ -20,7 +20,7 @@ use SilverStripe\Core\Injector\Injectable; * * Typically global config changes should set the active config. * - * The defaut config class can be changed via dependency injection to replace HTMLEditorConfig. + * The default config class can be changed via dependency injection to replace HTMLEditorConfig. * * @author "Hamish Friedlander" */ @@ -59,7 +59,7 @@ abstract class HTMLEditorConfig * @var array */ private static $user_themes = []; - + /** * List of the current themes set for this config * @@ -103,7 +103,7 @@ abstract class HTMLEditorConfig } return $config; } - + /** * Gets the current themes, if it is not set this will fallback to config * @return array @@ -115,7 +115,7 @@ abstract class HTMLEditorConfig } return Config::inst()->get(static::class, 'user_themes'); } - + /** * Sets the current theme * @@ -125,7 +125,7 @@ abstract class HTMLEditorConfig { static::$current_themes = $themes; } - + /** * Set the currently active configuration object. Note that the existing active * config will not be renamed to the new identifier. diff --git a/src/Forms/HTMLEditor/TinyMCEConfig.php b/src/Forms/HTMLEditor/TinyMCEConfig.php index ae68f4725..c1d7d95a7 100644 --- a/src/Forms/HTMLEditor/TinyMCEConfig.php +++ b/src/Forms/HTMLEditor/TinyMCEConfig.php @@ -202,6 +202,13 @@ class TinyMCEConfig extends HTMLEditorConfig */ private static $editor_css = []; + /** + * List of content css files to use for this instance, or null to default to editor_css config. + * + * @var string[]|null + */ + protected $contentCSS = null; + /** * TinyMCE JS settings * @@ -621,20 +628,42 @@ class TinyMCEConfig extends HTMLEditorConfig } /** - * Get location of all editor.css files + * Get location of all editor.css files. + * All resource specifiers are resolved to urls. * * @return array */ protected function getEditorCSS() { - $editor = array(); + $editor = []; + $resourceLoader = ModuleResourceLoader::singleton(); + foreach ($this->getContentCSS() as $contentCSS) { + $editor[] = $resourceLoader->resolveURL($contentCSS); + } + return $editor; + } + + /** + * Get list of resource paths to css files. + * + * Will default to `editor_css` config, as well as any themed `editor.css` files. + * Use setContentCSS() to override. + * + * @return string[] + */ + public function getContentCSS() + { + // Prioritise instance specific content + if (isset($this->contentCSS)) { + return $this->contentCSS; + } // Add standard editor.css + $editor = []; $editorCSSFiles = $this->config()->get('editor_css'); - $resourceLoader = ModuleResourceLoader::singleton(); if ($editorCSSFiles) { foreach ($editorCSSFiles as $editorCSS) { - $editor[] = $resourceLoader->resolveURL($editorCSS); + $editor[] = $editorCSS; } } @@ -642,12 +671,27 @@ class TinyMCEConfig extends HTMLEditorConfig $themes = HTMLEditorConfig::getThemes() ?: SSViewer::get_themes(); $themedEditor = ThemeResourceLoader::inst()->findThemedCSS('editor', $themes); if ($themedEditor) { - $editor[] = $resourceLoader->resolveURL($themedEditor); + $editor[] = $themedEditor; } - return $editor; } + /** + * Set explicit set of CSS resources to use for `content_css` option. + * + * Note: If merging with default paths, you should call getContentCSS() and merge + * prior to assignment. + * + * @param string[] $css Array of resource paths. Supports module prefix, + * e.g. `silverstripe/admin:client/dist/styles/editor.css` + * @return $this + */ + public function setContentCSS($css) + { + $this->contentCSS = $css; + return $this; + } + /** * Generate gzipped TinyMCE configuration including plugins and languages. * This ends up "pre-loading" TinyMCE bundled with the required plugins diff --git a/tests/php/Forms/HTMLEditor/TinyMCEConfigTest.php b/tests/php/Forms/HTMLEditor/TinyMCEConfigTest.php index 26edbb3e2..831d28c7e 100644 --- a/tests/php/Forms/HTMLEditor/TinyMCEConfigTest.php +++ b/tests/php/Forms/HTMLEditor/TinyMCEConfigTest.php @@ -37,4 +37,28 @@ class TinyMCEConfigTest extends SapphireTest } } } + + public function testGetContentCSS() + { + TinyMCEConfig::config()->set('editor_css', [ + 'silverstripe/framework:tests/php/Forms/HTMLEditor.css' + ]); + + // Test default config + $config = new TinyMCEConfig(); + $this->assertContains('silverstripe/framework:tests/php/Forms/HTMLEditor.css', $config->getContentCSS()); + + // Test manual disable + $config->setContentCSS([]); + $this->assertEmpty($config->getContentCSS()); + + // Test replacement config + $config->setContentCSS([ + 'silverstripe/framework:tests/php/Forms/HTMLEditor_another.css' + ]); + $this->assertEquals( + [ 'silverstripe/framework:tests/php/Forms/HTMLEditor_another.css'], + $config->getContentCSS() + ); + } }