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

View File

@ -23,6 +23,10 @@ declare(strict_types=1);
namespace O3\TinyMCE\Application\Core\TinyMCE\Options; 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 class BaseUrl extends AbstractOption
{ {
protected string $key = 'base_url'; protected string $key = 'base_url';
@ -32,8 +36,14 @@ class BaseUrl extends AbstractOption
*/ */
public function get(): string public function get(): string
{ {
return $this->loader->getShopConfig()->getActiveView()->getViewConfig()->getBaseDir() . try {
'modules/o3-shop/tinymce-editor/out/tinymce/'; 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; namespace O3\TinyMCE\Application\Core;
use O3\TinyMCE\Application\Core\TinyMCE\Loader; use O3\TinyMCE\Application\Core\TinyMCE\Loader;
use OxidEsales\Eshop\Core\Exception\FileException;
use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Registry;
class ViewConfig extends ViewConfig_parent class ViewConfig extends ViewConfig_parent
{ {
/** /**
* @return string * @return string
* @throws FileException
*/ */
public function loadTinyMce(): string public function getTinyMceInitCode(): string
{ {
$config = Registry::getConfig(); $config = Registry::getConfig();
$language = Registry::getLang(); $language = Registry::getLang();
$loader = oxNew(Loader::class, $config, $language); $loader = oxNew(Loader::class, $config, $language);
return $loader->getEditorCode(); 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}] [{$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() }} {{ parent() }}
{% if method_exists(oViewConf,'loadTinyMce') %} {% if method_exists(oViewConf,'getTinyMceIncludes') %}
{{ oViewConf.loadTinyMce()|raw }} {% for tmceinclude in oViewConf.getTinyMceIncludes() %}
{{ script({ include: tmceinclude|raw }) }}
{% endfor %}
{% endif %} {% endif %}
{% endblock %} {% 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 %}