Verified Commit dd9c7183 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3356929 by Wim Leers, kevinquillen: Provide an upgrade path from...

Issue #3356929 by Wim Leers, kevinquillen: Provide an upgrade path from "codesnippet" contrib CKEditor 4 plugin to "CodeBlock" core CKEditor 5 plugin
parent 575c5331
Loading
Loading
Loading
Loading
+28 −2
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@

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 @@
 * @CKEditor4To5Upgrade(
 *   id = "contrib",
 *   cke4_buttons = {
 *     "Code"
 *     "Code",
 *     "CodeSnippet",
 *   },
 *   cke4_plugin_settings = {
 *     "codesnippet",
 *   },
 *   cke5_plugin_elements_subset_configuration = {
 *   }
@@ -37,6 +41,10 @@ public function mapCKEditor4ToolbarButtonToCKEditor5ToolbarItem(string $cke4_but
      case 'Code':
        return ['code'];

      // @see https://www.drupal.org/project/codesnippet
      case 'CodeSnippet':
        return ['codeBlock'];

      default:
        throw new \OutOfBoundsException();
    }
@@ -46,8 +54,26 @@ public function mapCKEditor4ToolbarButtonToCKEditor5ToolbarItem(string $cke4_but
   * {@inheritdoc}
   */
  public function mapCKEditor4SettingsToCKEditor5Configuration(string $cke4_plugin_id, array $cke4_plugin_settings): ?array {
    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();
    }
  }

  /**
   * {@inheritdoc}
+75 −2
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@

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 @@ protected function setUp(): void {
                ],
              ],
            ],
            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 @@ public function provider() {
        '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' => [],