Issue #3479411 by phenaproxima, thejimbirch: The addItemToToolbar config action should not, by default, add items that are already in the toolbar

merge-requests/10044/head
Lee Rowlands 2024-11-04 08:11:17 +10:00
parent 85dc67bfa7
commit c1927736a8
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
2 changed files with 36 additions and 21 deletions

View File

@ -47,30 +47,40 @@ final class AddItemToToolbar implements ConfigActionPluginInterface, ContainerFa
throw new ConfigActionException(sprintf('The %s config action only works with editors that use CKEditor 5.', $this->pluginId));
}
$editor_settings = $editor->getSettings();
if (is_string($value)) {
$editor_settings['toolbar']['items'][] = $item_name = $value;
$value = ['item_name' => $value];
}
assert(is_array($value));
$item_name = $value['item_name'];
assert(is_string($item_name));
$replace = $value['replace'] ?? FALSE;
assert(is_bool($replace));
$position = $value['position'] ?? NULL;
$allow_duplicate = $value['allow_duplicate'] ?? FALSE;
assert(is_bool($allow_duplicate));
$editor_settings = $editor->getSettings();
// If the item is already in the toolbar and we're not allowing duplicate
// items, we're done.
if (in_array($item_name, $editor_settings['toolbar']['items'], TRUE) && $allow_duplicate === FALSE && $item_name !== '|') {
return;
}
if (is_int($position)) {
// If we want to replace the item at this position, then `replace`
// should be true. This would be useful if, for example, we wanted to
// replace the Image button with the Media Library.
array_splice($editor_settings['toolbar']['items'], $position, $replace ? 1 : 0, $item_name);
}
else {
assert(is_array($value));
$item_name = $value['item_name'];
assert(is_string($item_name));
$replace = $value['replace'] ?? FALSE;
assert(is_bool($replace));
$position = $value['position'] ?? NULL;
if (is_int($position)) {
// If we want to replace the item at this position, then `replace`
// should be true. This would be useful if, for example, we wanted to
// replace the Image button with the Media Library.
array_splice($editor_settings['toolbar']['items'], $position, $replace ? 1 : 0, $item_name);
}
else {
$editor_settings['toolbar']['items'][] = $item_name;
}
$editor_settings['toolbar']['items'][] = $item_name;
}
// If we're just adding a vertical separator, there's nothing else we need
// to do at this point.
if ($item_name === '|') {

View File

@ -69,6 +69,8 @@ class AddItemToToolbarConfigActionTest extends KernelTestBase {
* [{"item_name": "sourceEditing"}, ["heading", "bold", "italic", "sourceEditing"]]
* [{"item_name": "sourceEditing", "position": 1}, ["heading", "sourceEditing", "bold", "italic"]]
* [{"item_name": "sourceEditing", "position": 1, "replace": true}, ["heading", "sourceEditing", "italic"]]
* [{"item_name": "bold"}, ["heading", "bold", "italic"]]
* [{"item_name": "bold", "allow_duplicate": true}, ["heading", "bold", "italic", "bold"]]
*/
public function testAddItemToToolbar(string|array $action, array $expected_toolbar_items): void {
$recipe = $this->createRecipe([
@ -86,8 +88,11 @@ class AddItemToToolbarConfigActionTest extends KernelTestBase {
/** @var array{toolbar: array{items: string[]}, plugins: array<string, array<mixed>>} $settings */
$settings = Editor::load('filter_test')?->getSettings();
$this->assertSame($expected_toolbar_items, $settings['toolbar']['items']);
// The plugin's default settings should have been added.
$this->assertSame([], $settings['plugins']['ckeditor5_sourceEditing']['allowed_tags']);
if (in_array('sourceEditing', $expected_toolbar_items, TRUE)) {
$this->assertSame([], $settings['plugins']['ckeditor5_sourceEditing']['allowed_tags']);
}
}
public function testAddNonExistentItem(): void {