add scripts via templates for independence from template renderer

This commit is contained in:
Daniel Seifert 2024-12-04 09:27:21 +01:00
parent 22ac2bf970
commit 93120e5383
5 changed files with 116 additions and 58 deletions

View File

@ -33,21 +33,26 @@ use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServ
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRenderer;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRendererBridgeInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class Loader
{
protected Config $config;
protected Language $language;
protected Configuration $configuration;
public function __construct(Config $config, Language $language)
{
$this->config = $config;
$this->language = $language;
$this->configuration = oxNew( Configuration::class, $this );
$this->configuration->build();
}
/**
* @return string
* @throws FileException
*/
public function getEditorCode(): string
{
@ -61,14 +66,12 @@ class Loader
return $message;
}
$configuration = oxNew(Configuration::class, $this);
$configuration->build();
$this->registerScripts($configuration);
$this->registerIncludes();
try {
$engine = $this->getTemplateRenderer()->getTemplateEngine();
return $engine->render( '@' . Constants::OXID_MODULE_ID . '/admin/editorswitch' );
} catch ( NotFoundExceptionInterface|ContainerExceptionInterface) {
return '';
}
}
/**
@ -76,12 +79,16 @@ class Loader
*/
protected function isEnabledForCurrentController(): bool
{
try {
/** @var ModuleSettingService $service */
$service = ContainerFactory::getInstance()->getContainer()->get( ModuleSettingServiceInterface::class );
/** @var string[] $aEnabledClasses */
$aEnabledClasses = $service->getCollection( "aTinyMCE_classes", Constants::OXID_MODULE_ID );
return in_array( $this->getShopConfig()->getActiveView()->getClassKey(), $aEnabledClasses );
} catch (ContainerExceptionInterface|NotFoundExceptionInterface) {
return false;
}
}
/**
@ -114,55 +121,55 @@ class Loader
}
/**
* @param Configuration $configuration
*
* @return void
* @return array
*/
protected function registerScripts(Configuration $configuration): void
public function getScripts(): array
{
if (!$this->isEnabledForCurrentController()) {
return [];
}
$sCopyLongDescFromTinyMCE = file_get_contents(__DIR__.'/../../../assets/out/scripts/copyLongDesc.js');
$sUrlConverter = file_get_contents(__DIR__.'/../../../assets/out/scripts/urlConverter.js');
$sInit = str_replace(
"'CONFIG':'VALUES'",
$configuration->getConfig(),
$this->configuration->getConfig(),
(string) file_get_contents(__DIR__.'/../../../assets/out/scripts/init.js')
);
$engine = $this->getTemplateRenderer()->getTemplateEngine();
$globals = $engine->getGlobals();
$sSuffix = ($globals['__oxid_include_dynamic']) ? '_dynamic' : '';
$aScript = (array) Registry::getConfig()->getGlobalParameter('scripts' . $sSuffix);
$aScript[] = $sCopyLongDescFromTinyMCE;
$aScript[] = $sUrlConverter;
$aScript[] = $sInit;
Registry::getConfig()->setGlobalParameter('scripts' . $sSuffix, $aScript);
return [
$sCopyLongDescFromTinyMCE,
$sUrlConverter,
$sInit
];
}
/**
* @return void
* @throws FileException
* @return array
*/
protected function registerIncludes(): void
public function getIncludes(): array
{
$engine = $this->getTemplateRenderer()->getTemplateEngine();
$globals = $engine->getGlobals();
$sSuffix = ($globals['__oxid_include_dynamic']) ? '_dynamic' : '';
/** @var array<int, string[]> $aInclude */
$aInclude = (array) Registry::getConfig()->getGlobalParameter('includes' . $sSuffix);
$aInclude[3][] = Registry::getConfig()->getActiveView()->getViewConfig()->getModuleUrl(
Constants::OXID_MODULE_ID,
'assets/out/tinymce/tinymce.min.js'
);
Registry::getConfig()->setGlobalParameter('includes' . $sSuffix, $aInclude);
if (!$this->isEnabledForCurrentController()) {
return [];
}
try {
return [
Registry::getConfig()->getActiveView()->getViewConfig()->getModuleUrl(
Constants::OXID_MODULE_ID,
'out/tinymce/tinymce.min.js'
)
];
} catch (FileException) {
return [];
}
}
/**
* @return TemplateRenderer
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
protected function getTemplateRenderer(): TemplateRenderer
{
return ContainerFactory::getInstance()->getContainer()

View File

@ -23,6 +23,10 @@ declare(strict_types=1);
namespace O3\TinyMCE\Application\Core\TinyMCE\Options;
use O3\TinyMCE\Application\Model\Constants;
use OxidEsales\Eshop\Core\Exception\FileException;
use OxidEsales\Eshop\Core\Registry;
class BaseUrl extends AbstractOption
{
protected string $key = 'base_url';
@ -32,8 +36,14 @@ class BaseUrl extends AbstractOption
*/
public function get(): string
{
return $this->loader->getShopConfig()->getActiveView()->getViewConfig()->getBaseDir() .
'modules/o3-shop/tinymce-editor/out/tinymce/';
try {
return Registry::getConfig()->getActiveView()->getViewConfig()->getModuleUrl(
Constants::OXID_MODULE_ID,
'out/tinymce/'
);
} catch (FileException) {
return '';
}
}
/**

View File

@ -24,22 +24,37 @@ declare(strict_types=1);
namespace O3\TinyMCE\Application\Core;
use O3\TinyMCE\Application\Core\TinyMCE\Loader;
use OxidEsales\Eshop\Core\Exception\FileException;
use OxidEsales\Eshop\Core\Registry;
class ViewConfig extends ViewConfig_parent
{
/**
* @return string
* @throws FileException
*/
public function loadTinyMce(): string
public function getTinyMceInitCode(): string
{
$config = Registry::getConfig();
$language = Registry::getLang();
$loader = oxNew(Loader::class, $config, $language);
return $loader->getEditorCode();
}
public function getTinyMceScripts(): array
{
$config = Registry::getConfig();
$language = Registry::getLang();
$loader = oxNew(Loader::class, $config, $language);
return $loader->getScripts();
}
public function getTinyMceIncludes(): array
{
$config = Registry::getConfig();
$language = Registry::getLang();
$loader = oxNew(Loader::class, $config, $language);
return $loader->getIncludes();
}
}

View File

@ -1,3 +1,17 @@
[{$smarty.block.parent}]
[{if method_exists($oViewConf,'loadTinyMce') }][{ $oViewConf->loadTinyMce() }][{/if}]
[{if method_exists($oViewConf,'getTinyMceIncludes') }]
[{foreach from=$oViewConf->getTinyMceIncludes() item="tmceinclude"}]
[{oxscript include=$tmceinclude}]
[{/foreach}]
[{/if}]
[{if method_exists($oViewConf,'getTinyMceScripts') }]
[{foreach from=$oViewConf->getTinyMceScripts() item="tmcescript"}]
[{oxscript add=$tmcescript}]
[{/foreach}]
[{/if}]
[{if method_exists($oViewConf,'getTinyMceInitCode') }]
[{ $oViewConf->getTinyMceInitCode() }]
[{/if}]

View File

@ -4,8 +4,20 @@
{{ parent() }}
{% if method_exists(oViewConf,'loadTinyMce') %}
{{ oViewConf.loadTinyMce()|raw }}
{% if method_exists(oViewConf,'getTinyMceIncludes') %}
{% for tmceinclude in oViewConf.getTinyMceIncludes() %}
{{ script({ include: tmceinclude|raw }) }}
{% endfor %}
{% endif %}
{% if method_exists(oViewConf,'getTinyMceScripts') %}
{% for tmcescript in oViewConf.getTinyMceScripts() %}
{{ script({ add: tmcescript|raw }) }}
{% endfor %}
{% endif %}
{% if method_exists(oViewConf,'getTinyMceInitCode') %}
{{ oViewConf.getTinyMceInitCode()|raw }}
{% endif %}
{% endblock %}