From dd9c718324b0d3f0c34e84de34004b2e87966e16 Mon Sep 17 00:00:00 2001 From: Lauri Eskola Date: Tue, 20 Jun 2023 14:56:38 +0300 Subject: [PATCH] Issue #3356929 by Wim Leers, kevinquillen: Provide an upgrade path from "codesnippet" contrib CKEditor 4 plugin to "CodeBlock" core CKEditor 5 plugin --- .../Plugin/CKEditor4To5Upgrade/Contrib.php | 30 +++++++- .../src/Kernel/SmartDefaultSettingsTest.php | 77 ++++++++++++++++++- 2 files changed, 103 insertions(+), 4 deletions(-) diff --git a/core/modules/ckeditor5/src/Plugin/CKEditor4To5Upgrade/Contrib.php b/core/modules/ckeditor5/src/Plugin/CKEditor4To5Upgrade/Contrib.php index ca24398b7b8..ef62d2ed366 100644 --- a/core/modules/ckeditor5/src/Plugin/CKEditor4To5Upgrade/Contrib.php +++ b/core/modules/ckeditor5/src/Plugin/CKEditor4To5Upgrade/Contrib.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Drupal\ckeditor5\Plugin\CKEditor4To5Upgrade; +// cspell:ignore codesnippet + use Drupal\ckeditor5\HTMLRestrictions; use Drupal\ckeditor5\Plugin\CKEditor4To5UpgradePluginInterface; use Drupal\Core\Plugin\PluginBase; @@ -15,9 +17,11 @@ use Drupal\filter\FilterFormatInterface; * @CKEditor4To5Upgrade( * id = "contrib", * cke4_buttons = { - * "Code" + * "Code", + * "CodeSnippet", * }, * cke4_plugin_settings = { + * "codesnippet", * }, * cke5_plugin_elements_subset_configuration = { * } @@ -37,6 +41,10 @@ class Contrib extends PluginBase implements CKEditor4To5UpgradePluginInterface { case 'Code': return ['code']; + // @see https://www.drupal.org/project/codesnippet + case 'CodeSnippet': + return ['codeBlock']; + default: throw new \OutOfBoundsException(); } @@ -46,7 +54,25 @@ class Contrib extends PluginBase implements CKEditor4To5UpgradePluginInterface { * {@inheritdoc} */ public function mapCKEditor4SettingsToCKEditor5Configuration(string $cke4_plugin_id, array $cke4_plugin_settings): ?array { - throw new \OutOfBoundsException(); + switch ($cke4_plugin_id) { + case 'codesnippet': + $languages = []; + $enabled_cke4_languages = array_filter($cke4_plugin_settings['highlight_languages']); + foreach ($enabled_cke4_languages as $language) { + $languages[] = [ + 'language' => $language, + 'label' => $language, + ]; + } + return [ + 'ckeditor5_codeBlock' => [ + 'languages' => $languages, + ], + ]; + + default: + throw new \OutOfBoundsException(); + } } /** diff --git a/core/modules/ckeditor5/tests/src/Kernel/SmartDefaultSettingsTest.php b/core/modules/ckeditor5/tests/src/Kernel/SmartDefaultSettingsTest.php index bf294d83752..6d2cb07bc5e 100644 --- a/core/modules/ckeditor5/tests/src/Kernel/SmartDefaultSettingsTest.php +++ b/core/modules/ckeditor5/tests/src/Kernel/SmartDefaultSettingsTest.php @@ -4,6 +4,8 @@ declare(strict_types = 1); namespace Drupal\Tests\ckeditor5\Kernel; +// cspell:ignore arta codesnippet + use Drupal\ckeditor5\HTMLRestrictions; use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Utility\NestedArray; @@ -423,9 +425,50 @@ class SmartDefaultSettingsTest extends KernelTestBase { ], ], ], + 1 => [ + [ + 'name' => 'Contributed modules providing buttons with settings', + 'items' => [ + // @see https://www.drupal.org/project/codesnippet + 'CodeSnippet', + ], + ], + ], + ], + ], + 'plugins' => [ + 'codesnippet' => [ + 'highlight_style' => 'arta', + 'highlight_languages' => [ + 'cs' => 'cs', + 'cpp' => 'cpp', + 'coffeescript' => 'coffeescript', + 'css' => 'css', + 'diff' => 'diff', + 'html' => 'html', + 'http' => 'http', + 'ini' => 'ini', + 'java' => 'java', + 'javascript' => 'javascript', + 'json' => 'json', + 'makefile' => 'makefile', + 'markdown' => 'markdown', + 'nginx' => 'nginx', + 'objectivec' => 'objectivec', + 'perl' => 'perl', + 'php' => 'php', + 'python' => 'python', + 'ruby' => 'ruby', + 'sql' => 'sql', + 'vbscript' => 'vbscript', + 'xhtml' => 'xhtml', + 'xml' => 'xml', + // These 2 languages have been disabled. + 'apache' => 0, + 'bash' => 0, + ], ], ], - 'plugins' => [], ], ])->setSyncing(TRUE)->save(); } @@ -1452,9 +1495,39 @@ class SmartDefaultSettingsTest extends KernelTestBase { 'toolbar' => [ 'items' => [ 'code', + '|', + 'codeBlock', + ], + ], + 'plugins' => [ + 'ckeditor5_codeBlock' => [ + 'languages' => [ + ['label' => 'cs', 'language' => 'cs'], + ['label' => 'cpp', 'language' => 'cpp'], + ['label' => 'coffeescript', 'language' => 'coffeescript'], + ['label' => 'css', 'language' => 'css'], + ['label' => 'diff', 'language' => 'diff'], + ['label' => 'html', 'language' => 'html'], + ['label' => 'http', 'language' => 'http'], + ['label' => 'ini', 'language' => 'ini'], + ['label' => 'java', 'language' => 'java'], + ['label' => 'javascript', 'language' => 'javascript'], + ['label' => 'json', 'language' => 'json'], + ['label' => 'makefile', 'language' => 'makefile'], + ['label' => 'markdown', 'language' => 'markdown'], + ['label' => 'nginx', 'language' => 'nginx'], + ['label' => 'objectivec', 'language' => 'objectivec'], + ['label' => 'perl', 'language' => 'perl'], + ['label' => 'php', 'language' => 'php'], + ['label' => 'python', 'language' => 'python'], + ['label' => 'ruby', 'language' => 'ruby'], + ['label' => 'sql', 'language' => 'sql'], + ['label' => 'vbscript', 'language' => 'vbscript'], + ['label' => 'xhtml', 'language' => 'xhtml'], + ['label' => 'xml', 'language' => 'xml'], + ], ], ], - 'plugins' => [], ], 'expected_superset' => '', 'expected_fundamental_compatibility_violations' => [],