Skip to content

Commit 95cadf1

Browse files
committed
Update method for loading first party plugins
1 parent a6f892c commit 95cadf1

3 files changed

Lines changed: 81 additions & 20 deletions

File tree

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,20 +312,24 @@ The command will generate a new content migration, which will need to be run on
312312
## Adding CKEditor Plugins
313313

314314
### First Party plugins
315-
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`.
315+
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.
316316

317317
```php
318-
use craft\ckeditor\events\ModifyConfigEvent;
319-
use craft\ckeditor\Field;
320-
use yii\base\Event;
318+
use craft\ckeditor\helpers\CkeditorConfig;
321319

322-
Event::on(
323-
Field::class,
324-
Field::EVENT_MODIFY_CONFIG,
325-
handler: function(ModifyConfigEvent $event) {
326-
$event->extraPlugins = ['ImageResize'];
320+
class Site extends BaseModule
321+
{
322+
public function init(): void
323+
{
324+
parent::init();
325+
326+
// Register a package with toolbar items and multiple plugins
327+
CkeditorConfig::registerFirstPartyPackage(['SpecialCharacters', 'SpecialCharactersEssentials'], ['specialCharacters']);
328+
329+
// Register a package with a single plugin.
330+
CkeditorConfig::registerFirstPartyPackage(['ImageResize']);
327331
}
328-
);
332+
}
329333
```
330334

331335
### Custom plugins

src/Field.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,8 @@ protected function inputHtml(mixed $value, ?ElementInterface $element, $inline):
998998
$removePlugins->push('ImageTransforms');
999999
}
10001000

1001+
// TODO remove plugins not in toolbar
1002+
10011003
$plugins = CkeditorConfig::getPluginsByPackage();
10021004
$plugins['ckeditor5'] = array_merge($plugins['ckeditor5'], $event->extraPlugins);
10031005

@@ -1009,12 +1011,7 @@ protected function inputHtml(mixed $value, ?ElementInterface $element, $inline):
10091011

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

1012-
$imports = $plugins
1013-
->reduce(function(Collection $carry, Collection $plugins, string $import) {
1014-
$carry->push('import { ' . $plugins->join(', ') . ' } from "' . $import . '";');
1015-
return $carry;
1016-
}, Collection::empty())
1017-
->join("\n");
1014+
$imports = CkeditorConfig::getImportStatements();
10181015

10191016
// Add the translation import
10201017
$uiLanguage = BaseCkeditorPackageAsset::uiLanguage();

src/helpers/CkeditorConfig.php

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
final class CkeditorConfig
88
{
9+
/**
10+
* @see self::registerPackage()
11+
* @see self::registerFirstPartyPackage()
12+
* @var array|array[] plugins registered keyed by the package namespace
13+
*/
914
private static array $pluginsByPackage = [
1015
'ckeditor5' => [
1116
'Bookmark',
@@ -108,17 +113,54 @@ final class CkeditorConfig
108113
];
109114

110115

116+
/**
117+
* Register a custom CKEditor plugin
118+
*
119+
* @param string $name the namespace of the plugin
120+
* @param array $config plugins and toolbar items created by the plugin
121+
* @return void
122+
*/
111123
public static function registerPackage(string $name, array $config): void
112124
{
113-
self::$pluginsByPackage[$name] = $config['plugins'] ?? [];
125+
$plugins = $config['plugins'] ?? [];
126+
127+
if (!isset(self::$pluginsByPackage[$name])) {
128+
self::$pluginsByPackage[$name] = $plugins;
129+
} else {
130+
self::$pluginsByPackage[$name] = array_merge(self::$pluginsByPackage[$name], $plugins);
131+
}
132+
114133
self::$toolbarItems[] = $config['toolbarItems'] ?? [];
115134
}
116135

136+
/**
137+
* Register a first party plugin
138+
*
139+
* @param array $pluginNames plugins to register
140+
* @param array $toolbarItems toolbar items to add
141+
* @return void
142+
*/
143+
public static function registerFirstPartyPackage(array $pluginNames, array $toolbarItems = []): void
144+
{
145+
self::registerPackage('ckeditor5', ['plugins' => $pluginNames, 'toolbarItems' => $toolbarItems]);
146+
}
147+
148+
/**
149+
* Get all the package namespaces registered
150+
*
151+
* @return array
152+
*/
117153
public static function getPluginPackages(): array
118154
{
119155
return array_keys(self::$pluginsByPackage);
120156
}
121157

158+
/**
159+
* Get the plugins associated with a specific namespace
160+
*
161+
* @param string|null $name namespace of the package
162+
* @return array|array[]|string[] plugins registered from the package
163+
*/
122164
public static function getPluginsByPackage(string $name = null): array
123165
{
124166
if (!$name) {
@@ -132,22 +174,34 @@ public static function getPluginsByPackage(string $name = null): array
132174
return self::$pluginsByPackage[$name];
133175
}
134176

177+
/**
178+
* Return all plugins, regardless of namespace
179+
*
180+
* @return array
181+
*/
135182
public static function getAllPlugins(): array
136183
{
137184
return collect(self::getPluginsByPackage())
138185
->flatten()
139186
->toArray();
140187
}
141188

142-
public static function getImportStatements(): string
189+
/**
190+
* Get the JavaScript import statements for all plugins
191+
*
192+
* @param string|null $name namespace of the package
193+
* @return string
194+
*/
195+
public static function getImportStatements(string $name = null): string
143196
{
144-
return collect(self::getPluginsByPackage())
197+
return collect(self::getPluginsByPackage($name))
145198
->reduce(function(Collection $carry, array $plugins, string $import) {
146199
$carry->push('import { ' . implode(', ', $plugins) . ' } from "' . $import . '";');
147200

148201
return $carry;
149202
}, Collection::empty())->join("\n");
150203
}
204+
151205
private static function normalizeToolbarItem($item): array
152206
{
153207
if (is_string($item)) {
@@ -163,7 +217,13 @@ private static function normalizeToolbarItem($item): array
163217
return [$item];
164218
}
165219

166-
public static function normalizeToolbarItems($items): array
220+
/**
221+
* Normalizes toolbar items
222+
*
223+
* @param array $items toolbar items
224+
* @return array normalized toolbar items
225+
*/
226+
public static function normalizeToolbarItems(array $items): array
167227
{
168228
return collect($items)
169229
->map(fn($item) => self::normalizeToolbarItem($item))

0 commit comments

Comments
 (0)