Merge pull request #7910 from open-sausages/pulls/4/api-tinymce-content-css

API Add getContentCSS() / setContentCSS() to allow per-config customisation of content_css
This commit is contained in:
Daniel Hensby 2018-03-05 16:12:54 +00:00 committed by GitHub
commit e0b94e6971
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 11 deletions

View File

@ -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/))

View File

@ -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" <hamish@silverstripe.com>
*/
@ -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.

View File

@ -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

View File

@ -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()
);
}
}