Merge pull request #7287 from open-sausages/pulls/4.0/fix-multi-configs

BUG Fix issue with multiple editors breaking plugins
This commit is contained in:
Chris Joe 2017-08-21 10:58:27 +12:00 committed by GitHub
commit c8d8adfefe
4 changed files with 47 additions and 12 deletions

View File

@ -78,8 +78,7 @@ class TinyMCECombinedGenerator implements TinyMCEScriptGenerator, Flushable
{
$tinymceDir = $config->getTinyMCEPath();
// Core JS file
$files = [ $tinymceDir . '/tinymce' ];
$files = [ ];
// Add core languages
$language = $config->getOption('language');
@ -137,18 +136,43 @@ class TinyMCECombinedGenerator implements TinyMCEScriptGenerator, Flushable
return null;
}, $files));
// Set base URL for where tinymce is loaded from
$buffer = "var tinyMCEPreInit={base:'" . Convert::raw2js($tinymceDir) . "',suffix:'.min'};\n";
$libContent = $this->getFileContents(Director::baseFolder() . '/' . $tinymceDir . '/tinymce.min.js');
// Register vars for config
$baseDirJS = Convert::raw2js(Director::absoluteBaseURL());
$buffer = [];
$buffer[] = <<<SCRIPT
(function() {
var baseTag = window.document.getElementsByTagName('base');
var baseURL = baseTag.length ? baseTag[0].baseURI : '$baseDirJS';
SCRIPT;
$buffer[] = <<<SCRIPT
(function() {
// Avoid double-registration
if (window.tinymce) {
return;
}
var tinyMCEPreInit = {
base: baseURL,
suffix: '.min',
};
$libContent
})();
SCRIPT;
// Load all tinymce script files into buffer
foreach ($files as $file) {
$buffer .= $this->getFileContents(Director::baseFolder() . '/' . $file) . "\n";
$buffer[] = $this->getFileContents(Director::baseFolder() . '/' . $file);
}
$filesList = Convert::raw2js(implode(',', $files));
// Mark all themes, plugins and languages as done
$buffer .= 'tinymce.each("' . Convert::raw2js(implode(',', $files)) . '".split(","),function(f){tinymce.ScriptLoader.markDone(f);});';
$buffer[] = "window.tinymce.each('$filesList'.split(','),".
"function(f){tinymce.ScriptLoader.markDone(baseURL+f);});";
return $buffer . "\n";
$buffer[] = '})();';
return implode("\n", $buffer) . "\n";
}

View File

@ -9,6 +9,8 @@ use SilverStripe\Assets\Folder;
use SilverStripe\Assets\Image;
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Core\Manifest\ModuleManifest;
use SilverStripe\Dev\CSSContentParser;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
@ -66,7 +68,13 @@ class HTMLEditorFieldTest extends FunctionalTest
public function testCasting()
{
// Shim TinyMCE so silverstripe/admin doesn't have to be installed
TinyMCEConfig::config()->set('base_dir', 'test');
$framework = ModuleLoader::getModule('silverstripe/framework');
TinyMCEConfig::config()->set(
'base_dir',
$framework->getRelativeResourcePath(
'tests/php/Forms/HTMLEditor/TinyMCECombinedGeneratorTest/tinymce'
)
);
HtmlEditorField::config()->set('use_gzip', false);
// Test special characters

View File

@ -4,6 +4,7 @@ namespace SilverStripe\Forms\Tests\HTMLEditor;
use SilverStripe\Control\Director;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
use SilverStripe\Forms\HTMLEditor\TinyMCECombinedGenerator;
@ -59,7 +60,10 @@ class TinyMCECombinedGeneratorTest extends SapphireTest
"Filename for config: " . json_encode($c->getAttributes()) . " should match expected value"
);
$content = $generator->generateContent($c);
$this->assertStringStartsWith("var tinyMCEPreInit={base:'tinymce',suffix:'.min'};\n", $content);
$this->assertContains(
"var baseURL = baseTag.length ? baseTag[0].baseURI : 'http://www.mysite.com/basedir/';\n",
$content
);
// Main script file
$this->assertContains("/* tinymce.js */\n", $content);
// Locale file
@ -81,10 +85,9 @@ class TinyMCECombinedGeneratorTest extends SapphireTest
$this->assertContains("/* testtheme/langs/en.js */\n", $content);
// Register done scripts
$this->assertStringEndsWith(
$this->assertContains(
<<<EOS
tinymce.each("tinymce/tinymce.js,tinymce/langs/en.js,mycode/plugin1.js,tinymce/plugins/plugin4/plugin.min.js,tinymce/plugins/plugin4/langs/en.js,tinymce/plugins/plugin5/plugin.js,mycode/plugin6.js,tinymce/themes/testtheme/theme.js,tinymce/themes/testtheme/langs/en.js".split(","),function(f){tinymce.ScriptLoader.markDone(f);});
tinymce.each('tinymce/langs/en.js,mycode/plugin1.js,tinymce/plugins/plugin4/plugin.min.js,tinymce/plugins/plugin4/langs/en.js,tinymce/plugins/plugin5/plugin.js,mycode/plugin6.js,tinymce/themes/testtheme/theme.js,tinymce/themes/testtheme/langs/en.js'.split(','),function(f){tinymce.ScriptLoader.markDone(baseURL+f);});
EOS
,
$content