Skip to content

Commit

Permalink
Update method for loading first party plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
brianjhanson committed Feb 18, 2025
1 parent a6f892c commit 347af19
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 28 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,20 +312,24 @@ The command will generate a new content migration, which will need to be run on
## Adding CKEditor Plugins

### First Party plugins
If you'd like to include any of the [first party packages](https://github.com/ckeditor/ckeditor5/tree/master/packages) from CKEditor, you can add them to the `extraPlugins` property on the `ModifyConfigEvent`.
If you'd like to include any of the [first party packages](https://github.com/ckeditor/ckeditor5/tree/master/packages) from CKEditor, you can call `CkeditorConfig::registerFirstPartyPackage()` in the `init` function of a custom module.

```php
use craft\ckeditor\events\ModifyConfigEvent;
use craft\ckeditor\Field;
use yii\base\Event;
use craft\ckeditor\helpers\CkeditorConfig;

Event::on(
Field::class,
Field::EVENT_MODIFY_CONFIG,
handler: function(ModifyConfigEvent $event) {
$event->extraPlugins = ['ImageResize'];
class Site extends BaseModule
{
public function init(): void
{
parent::init();

// Register a package with toolbar items and multiple plugins
CkeditorConfig::registerFirstPartyPackage(['SpecialCharacters', 'SpecialCharactersEssentials'], ['specialCharacters']);

// Register a package with a single plugin.
CkeditorConfig::registerFirstPartyPackage(['ImageResize']);
}
);
}
```

### Custom plugins
Expand Down
11 changes: 3 additions & 8 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,6 @@ protected function inputHtml(mixed $value, ?ElementInterface $element, $inline):
'baseConfig' => $baseConfig,
'ckeConfig' => $ckeConfig,
'toolbar' => $toolbar,
'extraPlugins' => [],
]);
$this->trigger(self::EVENT_MODIFY_CONFIG, $event);

Expand Down Expand Up @@ -998,8 +997,9 @@ protected function inputHtml(mixed $value, ?ElementInterface $element, $inline):
$removePlugins->push('ImageTransforms');
}

// TODO remove plugins not in toolbar

$plugins = CkeditorConfig::getPluginsByPackage();
$plugins['ckeditor5'] = array_merge($plugins['ckeditor5'], $event->extraPlugins);

$plugins = collect($plugins)
->mapWithKeys(fn(array $plugins, string $import) => [
Expand All @@ -1009,12 +1009,7 @@ protected function inputHtml(mixed $value, ?ElementInterface $element, $inline):

$configPlugins = '[' . $plugins->flatten()->join(',') . ']';

$imports = $plugins
->reduce(function(Collection $carry, Collection $plugins, string $import) {
$carry->push('import { ' . $plugins->join(', ') . ' } from "' . $import . '";');
return $carry;
}, Collection::empty())
->join("\n");
$imports = CkeditorConfig::getImportStatements();

// Add the translation import
$uiLanguage = BaseCkeditorPackageAsset::uiLanguage();
Expand Down
6 changes: 0 additions & 6 deletions src/events/ModifyConfigEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,4 @@ class ModifyConfigEvent extends Event
* @since 4.1.0
*/
public array $toolbar;

/**
* @var string[] $extraPlugins Additional first-party CKEditor plugins to load.
* @since 5.0
*/
public array $extraPlugins;
}
68 changes: 64 additions & 4 deletions src/helpers/CkeditorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

final class CkeditorConfig
{
/**
* @see self::registerPackage()
* @see self::registerFirstPartyPackage()
* @var array|array[] plugins registered keyed by the package namespace
*/
private static array $pluginsByPackage = [
'ckeditor5' => [
'Bookmark',
Expand Down Expand Up @@ -108,17 +113,54 @@ final class CkeditorConfig
];


/**
* Register a custom CKEditor plugin
*
* @param string $name the namespace of the plugin
* @param array $config plugins and toolbar items created by the plugin
* @return void
*/
public static function registerPackage(string $name, array $config): void
{
self::$pluginsByPackage[$name] = $config['plugins'] ?? [];
$plugins = $config['plugins'] ?? [];

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

self::$toolbarItems[] = $config['toolbarItems'] ?? [];
}

/**
* Register a first party plugin
*
* @param array $pluginNames plugins to register
* @param array $toolbarItems toolbar items to add
* @return void
*/
public static function registerFirstPartyPackage(array $pluginNames, array $toolbarItems = []): void
{
self::registerPackage('ckeditor5', ['plugins' => $pluginNames, 'toolbarItems' => $toolbarItems]);
}

/**
* Get all the package namespaces registered
*
* @return array
*/
public static function getPluginPackages(): array
{
return array_keys(self::$pluginsByPackage);
}

/**
* Get the plugins associated with a specific namespace
*
* @param string|null $name namespace of the package
* @return array|array[]|string[] plugins registered from the package
*/
public static function getPluginsByPackage(string $name = null): array
{
if (!$name) {
Expand All @@ -132,22 +174,34 @@ public static function getPluginsByPackage(string $name = null): array
return self::$pluginsByPackage[$name];
}

/**
* Return all plugins, regardless of namespace
*
* @return array
*/
public static function getAllPlugins(): array
{
return collect(self::getPluginsByPackage())
->flatten()
->toArray();
}

public static function getImportStatements(): string
/**
* Get the JavaScript import statements for all plugins
*
* @param string|null $name namespace of the package
* @return string
*/
public static function getImportStatements(string $name = null): string
{
return collect(self::getPluginsByPackage())
return collect(self::getPluginsByPackage($name))
->reduce(function(Collection $carry, array $plugins, string $import) {
$carry->push('import { ' . implode(', ', $plugins) . ' } from "' . $import . '";');

return $carry;
}, Collection::empty())->join("\n");
}

private static function normalizeToolbarItem($item): array
{
if (is_string($item)) {
Expand All @@ -163,7 +217,13 @@ private static function normalizeToolbarItem($item): array
return [$item];
}

public static function normalizeToolbarItems($items): array
/**
* Normalizes toolbar items
*
* @param array $items toolbar items
* @return array normalized toolbar items
*/
public static function normalizeToolbarItems(array $items): array
{
return collect($items)
->map(fn($item) => self::normalizeToolbarItem($item))
Expand Down

0 comments on commit 347af19

Please sign in to comment.