Skip to content

Commit

Permalink
Avoid loading plugins not included in the toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
brianjhanson committed Feb 18, 2025
1 parent 0ef67ab commit 0ca15f6
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -997,13 +997,26 @@ protected function inputHtml(mixed $value, ?ElementInterface $element, $inline):
$removePlugins->push('ImageTransforms');
}

// TODO remove plugins not in toolbar
// Avoid loading plugins not included in the toolbar
$unusedPlugins = collect(CkeditorConfig::$pluginButtonMap)
->filter(function(array $item) use ($event) {
$buttons = $item['buttons'] ?? [];

return collect($event->toolbar)
->doesntContain(function(string $toolbarItem) use ($buttons) {
return in_array($toolbarItem, $buttons);
});
})
->map(fn(array $item) => $item['plugins'] ?? [])
->flatten();

$removePlugins->push(...$unusedPlugins->all());

$plugins = CkeditorConfig::getPluginsByPackage();

$plugins = collect($plugins)
->mapWithKeys(fn(array $plugins, string $import) => [
$import => collect($plugins)
->mapWithKeys(fn(array $plugins, string $namespace) => [
$namespace => collect($plugins)
->reject(fn($plugin) => in_array($plugin, $removePlugins->toArray())),
]);

Expand Down Expand Up @@ -1232,6 +1245,7 @@ protected function prepValueForInput($value, ?ElementInterface $element, bool $s

/**
* Returns if user belongs to a group whose members are allowed to edit source even if they're not admins
*
* @param User $user
* @return bool
*/
Expand Down
82 changes: 81 additions & 1 deletion src/helpers/CkeditorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,81 @@ final class CkeditorConfig
],
];

/**
* Maps toolbar items to plugins so can only load applicable plugins when we render a field.
*
* @var array
*/
public static array $pluginButtonMap = [
['plugins' => ['Alignment'], 'buttons' => ['alignment']],
['plugins' => ['Anchor'], 'buttons' => ['anchor']],
[
'plugins' => [
'AutoImage',
'CraftImageInsertUI',
'Image',
'ImageCaption',
'ImageStyle',
'ImageToolbar',
'ImageTransform',
'ImageEditor',
'LinkImage',
],
'buttons' => ['insertImage'],
],
[
'plugins' => ['AutoLink', 'CraftLinkUI', 'LinkEditing', 'LinkImage'],
'buttons' => ['link'],
],
['plugins' => ['BlockQuote'], 'buttons' => ['blockQuote']],
['plugins' => ['Bold'], 'buttons' => ['bold']],
['plugins' => ['Code'], 'buttons' => ['code']],
['plugins' => ['CodeBlock'], 'buttons' => ['codeBlock']],
[
'plugins' => ['List', 'ListProperties'],
'buttons' => ['bulletedList', 'numberedList'],
],
[
'plugins' => ['Font'],
'buttons' => ['fontSize', 'fontFamily', 'fontColor', 'fontBackgroundColor'],
],
['plugins' => ['FindAndReplace'], 'buttons' => ['findAndReplace']],
['plugins' => ['Heading'], 'buttons' => ['heading']],
['plugins' => ['HorizontalLine'], 'buttons' => ['horizontalLine']],
['plugins' => ['HtmlEmbed'], 'buttons' => ['htmlEmbed']],
[
'plugins' => ['Indent', 'IndentBlock'],
'buttons' => ['outdent', 'indent'],
],
['plugins' => ['Italic'], 'buttons' => ['italic']],
[
'plugins' => ['MediaEmbed', 'MediaEmbedToolbar'],
'buttons' => ['mediaEmbed'],
],
['plugins' => ['PageBreak'], 'buttons' => ['pageBreak']],
['plugins' => ['RemoveFormat'], 'buttons' => ['removeFormat']],
['plugins' => ['SourceEditing'], 'buttons' => ['sourceEditing']],
['plugins' => ['Strikethrough'], 'buttons' => ['strikethrough']],
['plugins' => ['Style'], 'buttons' => ['style']],
['plugins' => ['Subscript'], 'buttons' => ['subscript']],
['plugins' => ['Superscript'], 'buttons' => ['superscript']],
[
'plugins' => [
'Table',
'TableCaption',
'TableCellProperties',
'TableProperties',
'TableToolbar',
'TableUI',
],
'buttons' => ['insertTable'],
],
['plugins' => ['TextPartLanguage'], 'buttons' => ['textPartLanguage']],
['plugins' => ['TodoList'], 'buttons' => ['todoList']],
['plugins' => ['Underline'], 'buttons' => ['underline']],
['plugins' => ['CraftEntries'], 'buttons' => ['createEntry']],
];

public static array $toolbarItems = [
['button' => 'heading', 'configOption' => 'heading'],
['button' => 'style', 'configOption' => 'style'],
Expand Down Expand Up @@ -123,14 +198,19 @@ final class CkeditorConfig
public static function registerPackage(string $name, array $config): void
{
$plugins = $config['plugins'] ?? [];
$toolbarItems = $config['toolbarItems'] ?? [];

if (!isset(self::$pluginsByPackage[$name])) {
self::$pluginsByPackage[$name] = $plugins;
} else {
self::$pluginsByPackage[$name] = array_merge(self::$pluginsByPackage[$name], $plugins);
}

self::$toolbarItems[] = $config['toolbarItems'] ?? [];
self::$toolbarItems[] = $toolbarItems;
self::$pluginButtonMap[] = [
'plugins' => $plugins,
'buttons' => $toolbarItems,
];
}

/**
Expand Down

0 comments on commit 0ca15f6

Please sign in to comment.