From 93120e538344699611eb5747885c3a89f91f5efe Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Wed, 4 Dec 2024 09:27:21 +0100 Subject: [PATCH] add scripts via templates for independence from template renderer --- Application/Core/TinyMCE/Loader.php | 103 ++++++++++-------- Application/Core/TinyMCE/Options/BaseUrl.php | 14 ++- Application/Core/ViewConfig.php | 23 +++- .../bottomnaviitem_admin_bottomnaviitem.tpl | 16 ++- .../admin_twig/bottomnaviitem.html.twig | 18 ++- 5 files changed, 116 insertions(+), 58 deletions(-) diff --git a/Application/Core/TinyMCE/Loader.php b/Application/Core/TinyMCE/Loader.php index de1817d..c6d9273 100644 --- a/Application/Core/TinyMCE/Loader.php +++ b/Application/Core/TinyMCE/Loader.php @@ -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(); - - $engine = $this->getTemplateRenderer()->getTemplateEngine(); - return $engine->render('@' . Constants::OXID_MODULE_ID.'/admin/editorswitch'); + 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 { - /** @var ModuleSettingService $service */ - $service = ContainerFactory::getInstance()->getContainer()->get(ModuleSettingServiceInterface::class); - /** @var string[] $aEnabledClasses */ - $aEnabledClasses = $service->getCollection("aTinyMCE_classes", Constants::OXID_MODULE_ID); + 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); + return in_array( $this->getShopConfig()->getActiveView()->getClassKey(), $aEnabledClasses ); + } catch (ContainerExceptionInterface|NotFoundExceptionInterface) { + return false; + } } /** @@ -114,59 +121,59 @@ 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' : ''; + if (!$this->isEnabledForCurrentController()) { + return []; + } - /** @var array $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); + 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() - ->get(TemplateRendererBridgeInterface::class) - ->getTemplateRenderer(); + ->get(TemplateRendererBridgeInterface::class) + ->getTemplateRenderer(); } } diff --git a/Application/Core/TinyMCE/Options/BaseUrl.php b/Application/Core/TinyMCE/Options/BaseUrl.php index c74bfbf..4a9d56f 100644 --- a/Application/Core/TinyMCE/Options/BaseUrl.php +++ b/Application/Core/TinyMCE/Options/BaseUrl.php @@ -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 ''; + } } /** diff --git a/Application/Core/ViewConfig.php b/Application/Core/ViewConfig.php index f129a15..386ea9a 100755 --- a/Application/Core/ViewConfig.php +++ b/Application/Core/ViewConfig.php @@ -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(); + } } diff --git a/views/smarty/blocks/admin/bottomnaviitem_admin_bottomnaviitem.tpl b/views/smarty/blocks/admin/bottomnaviitem_admin_bottomnaviitem.tpl index b991781..c2d8905 100755 --- a/views/smarty/blocks/admin/bottomnaviitem_admin_bottomnaviitem.tpl +++ b/views/smarty/blocks/admin/bottomnaviitem_admin_bottomnaviitem.tpl @@ -1,3 +1,17 @@ [{$smarty.block.parent}] -[{if method_exists($oViewConf,'loadTinyMce') }][{ $oViewConf->loadTinyMce() }][{/if}] \ No newline at end of file +[{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}] \ No newline at end of file diff --git a/views/twig/extensions/themes/admin_twig/bottomnaviitem.html.twig b/views/twig/extensions/themes/admin_twig/bottomnaviitem.html.twig index 7d65788..b5077c6 100644 --- a/views/twig/extensions/themes/admin_twig/bottomnaviitem.html.twig +++ b/views/twig/extensions/themes/admin_twig/bottomnaviitem.html.twig @@ -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 %} -{% endblock %} \ No newline at end of file + {% 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 %}