Skip to content

Commit

Permalink
API Add getContentCSS() / setContentCSS() to allow per-config customi…
Browse files Browse the repository at this point in the history
…sation of content_css

Fixes #7873
  • Loading branch information
Damian Mooyman committed Mar 5, 2018
1 parent 15410cb commit 3a1c813
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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/))
Expand Down
10 changes: 5 additions & 5 deletions src/Forms/HTMLEditor/HTMLEditorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* 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>
*/
Expand Down Expand Up @@ -59,7 +59,7 @@ abstract class HTMLEditorConfig
* @var array
*/
private static $user_themes = [];

/**
* List of the current themes set for this config
*
Expand Down Expand Up @@ -103,7 +103,7 @@ public static function set_config($identifier, HTMLEditorConfig $config = null)
}
return $config;
}

/**
* Gets the current themes, if it is not set this will fallback to config
* @return array
Expand All @@ -115,7 +115,7 @@ public static function getThemes()
}
return Config::inst()->get(static::class, 'user_themes');
}

/**
* Sets the current theme
*
Expand All @@ -125,7 +125,7 @@ public static function setThemes($themes)
{
static::$current_themes = $themes;
}

/**
* Set the currently active configuration object. Note that the existing active
* config will not be renamed to the new identifier.
Expand Down
56 changes: 50 additions & 6 deletions src/Forms/HTMLEditor/TinyMCEConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -621,33 +628,70 @@ protected function getConfig()
}

/**
* 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;
}
}

// Themed editor.css
$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
Expand Down
24 changes: 24 additions & 0 deletions tests/php/Forms/HTMLEditor/TinyMCEConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,28 @@ public function testLanguagesValid()
}
}
}

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

0 comments on commit 3a1c813

Please sign in to comment.