Skip to content

Commit

Permalink
Fixed tests, reorganized CKEditor custom styles logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
damianz5 committed Sep 24, 2021
1 parent 7838f48 commit d9c332e
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class RichText extends AbstractFieldTypeParser
private const ATTRIBUTE_TYPE_STRING = 'string';
private const ATTRIBUTE_TYPE_NUMBER = 'number';

private const TOOLBARS_NODE_KEY = 'toolbars';
public const TOOLBARS_SA_SETTINGS_ID = 'fieldtypes.ezrichtext.' . self::TOOLBARS_NODE_KEY;
private const TOOLBAR_NODE_KEY = 'toolbar';
public const TOOLBARS_SA_SETTINGS_ID = 'fieldtypes.ezrichtext.' . self::TOOLBAR_NODE_KEY;

// constants common for OE custom classes and data attributes configuration
private const ELEMENT_NODE_KEY = 'element';
Expand Down Expand Up @@ -193,7 +193,7 @@ public function addFieldTypeSemanticConfig(NodeBuilder $nodeBuilder)

// RichText Toolbars configuration (defines list of Toolbars and Buttons enabled for current SiteAccess scope)
$nodeBuilder
->arrayNode(self::TOOLBARS_NODE_KEY)
->arrayNode(self::TOOLBAR_NODE_KEY)
->useAttributeAsKey('group_name')
->info('List of grouped Toolbars and Buttons enabled for current SiteAccess scope.')
->prototype('array')
Expand Down Expand Up @@ -273,7 +273,7 @@ public function mapConfig(array &$scopeSettings, $currentScope, ContextualizerIn
$onlineEditorSettingsMap = [
self::CLASSES_NODE_KEY => self::CLASSES_SA_SETTINGS_ID,
self::ATTRIBUTES_NODE_KEY => self::ATTRIBUTES_SA_SETTINGS_ID,
self::TOOLBARS_NODE_KEY => self::TOOLBARS_SA_SETTINGS_ID,
self::TOOLBAR_NODE_KEY => self::TOOLBARS_SA_SETTINGS_ID,
];
foreach ($onlineEditorSettingsMap as $key => $settingsId) {
if (isset($scopeSettings['fieldtypes']['ezrichtext'][$key])) {
Expand Down
20 changes: 11 additions & 9 deletions src/bundle/Resources/config/prepend/ezpublish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ system:
admin_group:
fieldtypes:
ezrichtext:
toolbars:
toolbar:
group1:
priority: 60
buttons:
Expand Down Expand Up @@ -39,22 +39,24 @@ system:
priority: 30
buttons:
bold:
priority: 110
priority: 120
italic:
priority: 100
priority: 110
underline:
priority: 90
priority: 100
subscript:
priority: 80
priority: 90
superscript:
priority: 70
priority: 80
strikethrough:
priority: 60
priority: 70
blockQuote:
priority: 50
priority: 60
ibexaLink:
priority: 40
priority: 50
ibexaAnchor:
priority: 40
ibexaCustomStyleInline:
priority: 30
ibexaFormatted:
priority: 20
Expand Down
6 changes: 2 additions & 4 deletions src/bundle/Resources/public/js/CKEditor/core/base-ckeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const VIEWPORT_TOP_OFFSET = 102;
init(container) {
const wrapper = this.getHTMLDocumentFragment(container.closest('.ibexa-data-source').querySelector('textarea').value);
const section = wrapper.childNodes[0];
const toolbars = window.eZ.richText.CKEditor.toolbars;
const toolbar = window.eZ.richText.CKEditor.toolbar;
const blockCustomStyles = Object.entries(eZ.richText.customStyles)
.filter(([customStyleName, customStyleConfig]) => !customStyleConfig.inline)
.map(([customStyleName, customStyleConfig]) => {
Expand Down Expand Up @@ -169,9 +169,7 @@ const VIEWPORT_TOP_OFFSET = 102;
IbexaRemoveElement,
],
toolbar: {
items: [
...toolbars,
],
items: toolbar,
viewportTopOffset: VIEWPORT_TOP_OFFSET,
},
embedImage: {
Expand Down
119 changes: 59 additions & 60 deletions src/lib/Configuration/Provider/CKEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
final class CKEditor implements Provider
{
private const SEPARATOR = '|';
private const CUSTOM_TAGS_GROUP_KEY = 'custom_tags_group';
private const CUSTOM_STYLE_INLINE = 'ibexaCustomStyleInline';

/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
private $configResolver;
Expand All @@ -40,113 +40,112 @@ public function getName(): string
* Returns CKEditor configuration.
*
* @phpstan-return array<array-key, array{
* toolbars: array<string>,
* customTags: array<string>,
* toolbar: array<string>,
* }>
*/
public function getConfiguration(): array
{
return [
'toolbars' => $this->getToolbars(),
'customTags' => $this->getCustomTags(),
'toolbar' => $this->getToolbar(),
];
}

/**
* Returns toolbars configuration.
* Returns toolbar buttons.
*
* @phpstan-return array<string>
*/
private function getToolbars(): array
private function getToolbar(): array
{
$toolbarsByGroupsConfiguration = $this->getSiteAccessConfigArray(RichText::TOOLBARS_SA_SETTINGS_ID);

$toolbarsByGroupsConfiguration = array_filter(
$toolbarsByGroupsConfiguration,
static function (string $key): bool {
return $key !== self::CUSTOM_TAGS_GROUP_KEY;
},
ARRAY_FILTER_USE_KEY
$filteredButtons = $this->filterButtonsByGroups(
$this->getSiteAccessConfigArray(RichText::TOOLBARS_SA_SETTINGS_ID)
);

return $this->filterToolbars($toolbarsByGroupsConfiguration);
}

/**
* Returns customTags configuration.
*
* @phpstan-return array<string>
*/
private function getCustomTags(): array
{
$toolbarsByGroupsConfiguration = $this->getSiteAccessConfigArray(RichText::TOOLBARS_SA_SETTINGS_ID);

$toolbarsByGroupsConfiguration = array_filter(
$toolbarsByGroupsConfiguration,
static function (string $key): bool {
return $key === self::CUSTOM_TAGS_GROUP_KEY;
},
ARRAY_FILTER_USE_KEY
);
if (\in_array(self::CUSTOM_STYLE_INLINE, $filteredButtons) && !$this->hasInlineCustomStyles()) {
return $this->removeInlineCustomStyleButton($filteredButtons);
}

return $this->filterToolbars($toolbarsByGroupsConfiguration);
return $filteredButtons;
}

/**
* Returns filtered Toolbars configuration.
* Returns filtered Toolbar buttons configuration.
*
* @phpstan-return array<string>
*/
private function filterToolbars(
array $toolbarsByGroupsConfiguration = []
private function filterButtonsByGroups(
array $groupsConfiguration = []
): array {
$toolbars = [];
$buttons = [];

$toolbarsByGroupsConfiguration = array_filter(
$toolbarsByGroupsConfiguration,
static function (array $value): bool {
return $value['visible'];
$groupsConfiguration = \array_filter(
$groupsConfiguration,
static function (array $group): bool {
return $group['visible'];
}
);

uasort($toolbarsByGroupsConfiguration, static function (array $a, array $b): int {
\uasort($groupsConfiguration, static function (array $a, array $b): int {
return $b['priority'] <=> $a['priority'];
});

foreach ($toolbarsByGroupsConfiguration as $configuration) {
$toolbarButtons = $this->getToolbarButtons($configuration['buttons'] ?? []);
foreach ($groupsConfiguration as $configuration) {
$filteredButtons = $this->filterButtons($configuration['buttons'] ?? []);

if (count($toolbarButtons)) {
$toolbars = array_merge(
$toolbars,
$toolbarButtons,
[self::SEPARATOR],
);
if (\count($filteredButtons) === 0) {
continue;
}

$buttons = \array_merge($buttons, $filteredButtons, [self::SEPARATOR]);
}

// Removes last separator from the toolbars list.
array_pop($toolbars);
// Removes last separator from the buttons list.
\array_pop($buttons);

return array_values($toolbars);
return \array_values($buttons);
}

/**
* Returns List of visible, sorted buttons.
*
* @phpstan-return array<string>
*/
private function getToolbarButtons(array $buttons): array
private function filterButtons(array $buttons): array
{
$buttons = array_filter($buttons, static function (array $value): bool {
return $value['visible'];
$buttons = \array_filter($buttons, static function (array $button): bool {
return $button['visible'];
});

uasort($buttons, static function (array $a, array $b): int {
\uasort($buttons, static function (array $a, array $b): int {
return $b['priority'] <=> $a['priority'];
});

return array_keys($buttons);
return \array_keys($buttons);
}

private function hasInlineCustomStyles(): bool
{
$customStyles = $this->getSiteAccessConfigArray('fieldtypes.ezrichtext.custom_styles');

return 0 !== \count(\array_filter(
$customStyles,
static function (array $customStyle): bool {
return $customStyle['inline'];
}
));
}

/**
* @phpstan-return array<string>
*/
private function removeInlineCustomStyleButton(array $filteredButtons): array
{
return \array_filter(
$filteredButtons,
static function (string $buttonValue): bool {
return $buttonValue !== self::CUSTOM_STYLE_INLINE;
},
);
}

/**
Expand Down
Loading

0 comments on commit d9c332e

Please sign in to comment.