Commit e0131790 authored by Tino Goratsch's avatar Tino Goratsch

rework how the TinyMCE gets initialized

parent 28875c2c
/*
* Copyright (c) by the ACP3 Developers.
* See the LICENSE file at the top-level module directory for licensing details.
*/
/* global tinymce */
/* global tinyMCEFileBrowserCallback */
const initializeTinyMCEInstances = () => {
jQuery('.wysiwyg-tinymce').each((index, element) => {
const config = jQuery(element).data('wysiwygConfig');
let fileManagerConfig = {};
if (typeof tinyMCEFileBrowserCallback !== 'undefined') {
fileManagerConfig = {
file_browser_callback: tinyMCEFileBrowserCallback
};
}
const finalConfig = Object.assign({}, config, fileManagerConfig);
const existingInstance = tinymce.get(element.id);
if (existingInstance) {
existingInstance.remove();
}
tinymce.init(finalConfig);
});
};
jQuery(document).ready(($) => {
$(document).on('acp3.ajaxFrom.submit.before', () => {
if (typeof tinymce !== 'undefined') {
tinymce.triggerSave();
}
});
$(document).on('acp3.ajaxFrom.complete', () => {
initializeTinyMCEInstances();
});
initializeTinyMCEInstances();
});
"use strict";/*
* Copyright (c) by the ACP3 Developers.
* See the LICENSE file at the top-level module directory for licensing details.
*/ /* global tinymce */ /* global tinyMCEFileBrowserCallback */var initializeTinyMCEInstances=function(){jQuery(".wysiwyg-tinymce").each(function(a,b){var c=jQuery(b).data("wysiwygConfig"),d={};"undefined"!=typeof tinyMCEFileBrowserCallback&&(d={file_browser_callback:tinyMCEFileBrowserCallback});var e=Object.assign({},c,d),f=tinymce.get(b.id);f&&f.remove(),tinymce.init(e)})};jQuery(document).ready(function(a){a(document).on("acp3.ajaxFrom.submit.before",function(){"undefined"!=typeof tinymce&&tinymce.triggerSave()}),a(document).on("acp3.ajaxFrom.complete",function(){initializeTinyMCEInstances()}),initializeTinyMCEInstances()});
\ No newline at end of file
{if !$config.is_initialized}
{if !$tinymce.initialized}
<script src="{$ROOT_DIR}vendor/tinymce/tinymce/tinymce.min.js"></script>
{/if}
<script>
tinymce.init({
selector: '{$config.selector}',
theme: '{$config.theme}',
height: '{$config.height}',
content_css: '{$config.content_css}',
plugins: {$config.plugins},
toolbar: '{$config.toolbar}',
image_advtab: {$config.image_advtab}
{if isset($config.filemanager_path)}
,
file_browser_callback: function (field, url, type, win) {
{if $tinymce.filemanager_path !== null}
<script>
const tinyMCEFileBrowserCallback = (field, url, type, win) => {
const elem = document.documentElement,
body = document.getElementsByTagName('body')[0],
x = win.innerWidth || elem.clientWidth || body.clientWidth,
y = win.innerHeight|| elem.clientHeight|| body.clientHeight;
let fileBrowserUrl = '{$config.filemanager_path}?field_name=' + field;
let fileBrowserUrl = '{$tinymce.filemanager_path}?field_name=' + field;
if (type === 'image') {
fileBrowserUrl += '&filter=image';
}
......@@ -34,14 +24,7 @@
input: field
});
}
{/if}
});
jQuery(document).ready(function ($) {
$(document).on('acp3.ajaxFrom.submit.before', function () {
if (typeof tinymce !== "undefined") {
tinymce.triggerSave();
}
});
});
</script>
</script>
{/if}
{/if}
{include_js module="Wysiwygtinymce" file="partials/tinymce"}
......@@ -6,6 +6,7 @@ services:
- '@core.assets.minifier.css'
- '@core.i18n.translator'
- '@core.environment.application_path'
- '@core.view'
- '@?filemanager.helpers'
tags:
- { name: core.wysiwyg.editor }
......@@ -35,24 +35,22 @@ class TinyMCE extends Core\WYSIWYG\Editor\Textarea
* @var \ACP3\Core\ACL
*/
private $acl;
/**
* @var \ACP3\Core\View
*/
private $view;
/**
* @var bool
*/
private $initialized = false;
/**
* @param \ACP3\Core\ACL $acl
* @param \ACP3\Core\Assets\Minifier\MinifierInterface $minifier
* @param \ACP3\Core\I18n\Translator $translator
* @param \ACP3\Core\Environment\ApplicationPath $appPath
* @param \ACP3\Modules\ACP3\Filemanager\Helpers|null $filemanagerHelpers
*/
public function __construct(
Core\ACL $acl,
Core\Assets\Minifier\MinifierInterface $minifier,
Core\I18n\Translator $translator,
Core\Environment\ApplicationPath $appPath,
Core\View $view,
?Helpers $filemanagerHelpers = null
) {
$this->minifier = $minifier;
......@@ -60,6 +58,7 @@ class TinyMCE extends Core\WYSIWYG\Editor\Textarea
$this->appPath = $appPath;
$this->filemanagerHelpers = $filemanagerHelpers;
$this->acl = $acl;
$this->view = $view;
}
/**
......@@ -77,8 +76,8 @@ class TinyMCE extends Core\WYSIWYG\Editor\Textarea
{
parent::setParameters($params);
$this->config['toolbar'] = (isset($params['toolbar'])) ? $params['toolbar'] : '';
$this->config['height'] = ((isset($params['height'])) ? $params['height'] : 250) . 'px';
$this->config['toolbar'] = $params['toolbar'] ?? '';
$this->config['height'] = ($params['height'] ?? 250) . 'px';
}
/**
......@@ -91,8 +90,9 @@ class TinyMCE extends Core\WYSIWYG\Editor\Textarea
'id' => $this->id,
'name' => $this->name,
'value' => $this->value,
'js' => $this->configure(),
'js' => $this->init(),
'advanced' => $this->advanced,
'data_config' => $this->configure(),
];
if ($wysiwyg['advanced'] === true) {
......@@ -102,50 +102,62 @@ class TinyMCE extends Core\WYSIWYG\Editor\Textarea
return ['wysiwyg' => $wysiwyg];
}
/**
* @return array
*/
private function configure()
private function init(): string
{
$this->config['tinymce'] = [
'is_initialized' => $this->initialized,
'selector' => 'textarea#' . $this->id,
'theme' => 'modern',
'height' => $this->config['height'],
'content_css' => $this->minifier->getURI(),
];
if ($this->initialized === false) {
$this->initialized = true;
if ($this->initialized) {
return '';
}
$this->configurePlugins();
$this->configureToolbar();
$this->configureAdvancedImages();
$this->addFileManager();
$this->view->assign('tinymce', [
'initialized' => $this->initialized,
'filemanager_path' => $this->getFileManagerPath(),
]);
return [
'template' => 'Wysiwygtinymce/tinymce.tpl',
'config' => $this->config['tinymce'],
];
$this->initialized = true;
return $this->view->fetchTemplate('Wysiwygtinymce/tinymce.tpl');
}
private function addFileManager()
private function getFileManagerPath(): ?string
{
if ($this->filemanagerHelpers === null) {
return;
return null;
}
if (!$this->acl->hasPermission('admin/filemanager/index/richfilemanager')) {
return;
return null;
}
if ($this->isSimpleEditor()) {
return;
return null;
}
$this->config['tinymce']['filemanager_path'] = $this->filemanagerHelpers->getFilemanagerPath();
return $this->filemanagerHelpers->getFilemanagerPath();
}
private function configurePlugins(): void
/**
* @return string
*/
private function configure(): string
{
$config = [
'selector' => 'textarea#' . $this->id,
'theme' => 'modern',
'height' => $this->config['height'],
'content_css' => $this->minifier->getURI(),
];
$this->configurePlugins();
$this->configureToolbar();
$this->configureAdvancedImages();
return \json_encode(\array_merge(
$config,
$this->configurePlugins(),
$this->configureToolbar(),
$this->configureAdvancedImages()
));
}
private function configurePlugins(): array
{
if ($this->isSimpleEditor()) {
$plugins = [
......@@ -162,10 +174,10 @@ class TinyMCE extends Core\WYSIWYG\Editor\Textarea
];
}
$this->config['tinymce']['plugins'] = \json_encode($plugins);
return ['plugins' => $plugins];
}
private function configureToolbar(): void
private function configureToolbar(): array
{
if ($this->isSimpleEditor()) {
$toolbar = 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image';
......@@ -173,12 +185,14 @@ class TinyMCE extends Core\WYSIWYG\Editor\Textarea
$toolbar = 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | print preview media | forecolor backcolor emoticons';
}
$this->config['tinymce']['toolbar'] = $toolbar;
return ['toolbar' => $toolbar];
}
private function configureAdvancedImages(): void
private function configureAdvancedImages(): array
{
$this->config['tinymce']['image_advtab'] = $this->isSimpleEditor() ? 'false' : 'true';
return [
'image_advtab' => !$this->isSimpleEditor(),
];
}
private function isSimpleEditor(): bool
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment