From 13d26990111b23a3831d3ac818179cd9326729ed Mon Sep 17 00:00:00 2001 From: Stephen Abello Date: Fri, 4 Dec 2020 10:53:22 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B02629=20Allow=20user=20to=20choose=20defa?= =?UTF-8?q?ult=20expanded/collapsed=20toolbar=20for=20richtext=20editors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/cmdbabstract.class.inc.php | 6 +--- application/ui.htmleditorwidget.class.inc.php | 6 +--- application/utils.inc.php | 15 ++++++++ .../pages/en.dictionary.itop.preferences.php | 4 +++ pages/preferences.php | 36 ++++++++++++++++++- .../FieldRenderer/BsSimpleFieldRenderer.php | 7 ++-- .../ConsoleSimpleFieldRenderer.php | 7 ++-- .../Component/Input/RichText/RichText.php | 23 ++++++++++++ .../components/input/richtext/layout.js.twig | 2 +- 9 files changed, 90 insertions(+), 16 deletions(-) diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index 776c95088..80a05cb4e 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -2012,11 +2012,7 @@ EOF // To change the default settings of the editor, // a) edit the file /js/ckeditor/config.js // b) or override some of the configuration settings, using the second parameter of ckeditor() - $aConfig = array(); - $sLanguage = strtolower(trim(UserRights::GetUserLanguage())); - $aConfig['language'] = $sLanguage; - $aConfig['contentsLanguage'] = $sLanguage; - $aConfig['extraPlugins'] = 'disabler,codesnippet'; + $aConfig = utils::GetCkeditorPref(); $aConfig['placeholder'] = Dict::S('UI:CaseLogTypeYourTextHere'); $sConfigJS = json_encode($aConfig); diff --git a/application/ui.htmleditorwidget.class.inc.php b/application/ui.htmleditorwidget.class.inc.php index 47fe4d0eb..cecfe8aa8 100644 --- a/application/ui.htmleditorwidget.class.inc.php +++ b/application/ui.htmleditorwidget.class.inc.php @@ -70,11 +70,7 @@ class UIHTMLEditorWidget // To change the default settings of the editor, // a) edit the file /js/ckeditor/config.js // b) or override some of the configuration settings, using the second parameter of ckeditor() - $aConfig = array(); - $sLanguage = strtolower(trim(UserRights::GetUserLanguage())); - $aConfig['language'] = $sLanguage; - $aConfig['contentsLanguage'] = $sLanguage; - $aConfig['extraPlugins'] = 'disabler,codesnippet'; + $aConfig = utils::GetCkeditorPref(); $sWidthSpec = addslashes(trim($this->m_oAttDef->GetWidth())); if ($sWidthSpec != '') { diff --git a/application/utils.inc.php b/application/utils.inc.php index 78e848f09..30e76577f 100644 --- a/application/utils.inc.php +++ b/application/utils.inc.php @@ -2471,4 +2471,19 @@ class utils { return static::$iNextId++; } + + public static function GetCkeditorPref() + { + $sLanguage = strtolower(trim(UserRights::GetUserLanguage())); + + $aDefaultConf = array('language'=> $sLanguage, + 'contentsLanguage' => $sLanguage, + 'extraPlugins' => 'disabler,codesnippet', + ); + + $aRichTextConfig = json_decode(appUserPreferences::GetPref('richtext_config', '{}'), true); + + + return array_merge($aDefaultConf, $aRichTextConfig); + } } diff --git a/dictionaries/ui/pages/en.dictionary.itop.preferences.php b/dictionaries/ui/pages/en.dictionary.itop.preferences.php index 50a38080b..9f3928703 100644 --- a/dictionaries/ui/pages/en.dictionary.itop.preferences.php +++ b/dictionaries/ui/pages/en.dictionary.itop.preferences.php @@ -22,4 +22,8 @@ Dict::Add('EN US', 'English', 'English', array( 'UI:Preferences:Title' => 'Preferences', 'UI:Preferences:ChooseAPlaceholder' => 'User placeholder image', 'UI:Preferences:ChooseAPlaceholder+' => 'Choose a placeholder image that will be displayed if the contact linked to your user doesn\'t have one', + 'UI:RichText:Preferences' => 'Rich text editor', + 'UI:RichText:ToolbarState' => 'Toolbar default state', + 'UI:RichText:ToolbarState:Expanded' => 'Expanded', + 'UI:RichText:ToolbarState:Collapsed' => 'Collapsed', )); \ No newline at end of file diff --git a/pages/preferences.php b/pages/preferences.php index 97a52320a..c24e527ed 100644 --- a/pages/preferences.php +++ b/pages/preferences.php @@ -381,6 +381,32 @@ EOF $oNewsroomBlock->AddSubBlock($oNewsroomEndHtmlBlock); $oContentLayout->AddMainBlock($oNewsroomBlock); } + ////////////////////////////////////////////////////////////////////////// + // + // Rich text editor preferences + // + ////////////////////////////////////////////////////////////////////////// + $oRichTextBlock = new Panel(Dict::S('UI:RichText:Preferences'), array(), 'grey', 'ibo-richtext'); + + $oRichTextForm = new Form(); + $oRichTextForm->AddSubBlock(InputFactory::MakeForHidden('operation', 'apply_richtext_config')); + + $sRichTextToolbarDefaultState = isset(utils::GetCkeditorPref()['toolbarStartupExpanded']) ? (bool)utils::GetCkeditorPref()['toolbarStartupExpanded'] : false; + $oRichTextToolbarDefaultStateInput = InputFactory::MakeForSelectWithLabel('toolbarexpanded', Dict::S('UI:RichText:ToolbarState')); + $oRichTextToolbarDefaultStateInput->GetInput()->AddOption(InputFactory::MakeForSelectOption('true', Dict::S('UI:RichText:ToolbarState:Expanded'), $sRichTextToolbarDefaultState)); + $oRichTextToolbarDefaultStateInput->GetInput()->AddOption(InputFactory::MakeForSelectOption('false', Dict::S('UI:RichText:ToolbarState:Collapsed'), !$sRichTextToolbarDefaultState)); + $oRichTextForm->AddSubBlock($oRichTextToolbarDefaultStateInput); + + // - Cancel button + $oRichTextCancelButton = ButtonFactory::MakeForSecondaryAction(Dict::S('UI:Button:Cancel')); + $oRichTextCancelButton->SetOnClickJsCode("window.location.href = '$sURL'"); + $oRichTextForm->AddSubBlock($oRichTextCancelButton); + // - Submit button + $oRichTextSubmitButton = ButtonFactory::MakeForPrimaryAction(Dict::S('UI:Button:Apply'), null, null, true); + $oRichTextForm->AddSubBlock($oRichTextSubmitButton); + + $oRichTextBlock->AddSubBlock($oRichTextForm); + $oContentLayout->AddMainBlock($oRichTextBlock); ////////////////////////////////////////////////////////////////////////// // @@ -532,7 +558,15 @@ try } DisplayPreferences($oPage); break; - + case 'apply_richtext_config': + $aRichTextConfig = json_decode(appUserPreferences::GetPref('richtext_config', '{}'), true); + + $bToolbarExpanded = utils::ReadParam('toolbarexpanded', 'false') === 'true'; + $aRichTextConfig['toolbarStartupExpanded'] = $bToolbarExpanded; + + appUserPreferences::SetPref('richtext_config', json_encode($aRichTextConfig)); + DisplayPreferences($oPage); + break; case 'apply_language': $sLangCode = utils::ReadParam('language', 'EN US'); $oUser = UserRights::GetUserObject(); diff --git a/sources/Renderer/Bootstrap/FieldRenderer/BsSimpleFieldRenderer.php b/sources/Renderer/Bootstrap/FieldRenderer/BsSimpleFieldRenderer.php index 8edde1a19..6115e53b8 100644 --- a/sources/Renderer/Bootstrap/FieldRenderer/BsSimpleFieldRenderer.php +++ b/sources/Renderer/Bootstrap/FieldRenderer/BsSimpleFieldRenderer.php @@ -153,11 +153,14 @@ EOF // Some additional stuff if we are displaying it with a rich editor if ($bRichEditor) { - $sEditorLanguage = strtolower(trim(UserRights::GetUserLanguage())); + $aConfig = utils::GetCkeditorPref(); + $aConfig['extraPlugins'] = 'codesnippet'; + $sJsConfig = json_encode($aConfig); + $oOutput->AddJs( <<oField->GetGlobalId()}').addClass('htmlEditor'); - $('#{$this->oField->GetGlobalId()}').ckeditor(function(){}, {language: '$sEditorLanguage', contentsLanguage: '$sEditorLanguage', extraPlugins: 'codesnippet'}).editor.on("change", function(){ + $('#{$this->oField->GetGlobalId()}').ckeditor(function(){}, $sJsConfig).editor.on("change", function(){ $('#{$this->oField->GetGlobalId()}').trigger("change"); }); EOF diff --git a/sources/Renderer/Console/FieldRenderer/ConsoleSimpleFieldRenderer.php b/sources/Renderer/Console/FieldRenderer/ConsoleSimpleFieldRenderer.php index a9e5dd4a3..79ac1e0d3 100644 --- a/sources/Renderer/Console/FieldRenderer/ConsoleSimpleFieldRenderer.php +++ b/sources/Renderer/Console/FieldRenderer/ConsoleSimpleFieldRenderer.php @@ -103,11 +103,14 @@ class ConsoleSimpleFieldRenderer extends FieldRenderer // Some additional stuff if we are displaying it with a rich editor if ($bRichEditor) { - $sEditorLanguage = strtolower(trim(UserRights::GetUserLanguage())); + $aConfig = utils::GetCkeditorPref(); + $aConfig['extraPlugins'] = 'codesnippet'; + $sJsConfig = json_encode($aConfig); + $oOutput->AddJs( <<oField->GetGlobalId()}').addClass('htmlEditor'); - $('#{$this->oField->GetGlobalId()}').ckeditor(function(){}, {language: '$sEditorLanguage', contentsLanguage: '$sEditorLanguage', extraPlugins: 'codesnippet'}); + $('#{$this->oField->GetGlobalId()}').ckeditor(function(){}, $sJsConfig); EOF ); if (($this->oField->GetObject() !== null) && ($this->oField->GetTransactionId() !== null)) diff --git a/sources/application/UI/Base/Component/Input/RichText/RichText.php b/sources/application/UI/Base/Component/Input/RichText/RichText.php index 0c04dfd7c..290a58afd 100644 --- a/sources/application/UI/Base/Component/Input/RichText/RichText.php +++ b/sources/application/UI/Base/Component/Input/RichText/RichText.php @@ -5,6 +5,7 @@ */ namespace Combodo\iTop\Application\UI\Base\Component\Input\RichText; use Combodo\iTop\Application\UI\Base\UIBlock; +use utils; /** * Class RichText @@ -17,4 +18,26 @@ class RichText extends UIBlock public const BLOCK_CODE = 'ibo-richtext'; public const DEFAULT_HTML_TEMPLATE_REL_PATH = 'base/components/input/richtext/layout'; public const DEFAULT_JS_TEMPLATE_REL_PATH = 'base/components/input/richtext/layout'; + /** @var string */ + protected $aConfig; + + /** + * RichText constructor. + * + * @param string|null $sId + */ + public function __construct(?string $sId = null) + { + parent::__construct($sId); + $this->aConfig = json_encode(utils::GetCkeditorPref()); + } + + + /** + * @return string + */ + public function GetConfig() + { + return $this->aConfig; + } } \ No newline at end of file diff --git a/templates/base/components/input/richtext/layout.js.twig b/templates/base/components/input/richtext/layout.js.twig index 2fe43cdf6..c2da6b6a1 100644 --- a/templates/base/components/input/richtext/layout.js.twig +++ b/templates/base/components/input/richtext/layout.js.twig @@ -1 +1 @@ -$('#{{ oUIBlock.GetId() }}').ckeditor(function() { /* callback code */ }, {'language': 'en', 'contentsLanguage' : 'en', 'extraPlugins' : 'disabler,codesnippet'}); \ No newline at end of file +$('#{{ oUIBlock.GetId() }}').ckeditor(function() { /* callback code */ }, {{ oUIBlock.GetConfig()| raw }}); \ No newline at end of file