Verified Commit e1178387 authored by Dave Long's avatar Dave Long
Browse files

Issue #3274635 by wim leers, acbramley, taran2l, bnjmnm, chrisla, jweowu,...

Issue #3274635 by wim leers, acbramley, taran2l, bnjmnm, chrisla, jweowu, ericgsmith, mstrelan, osman, joaopauloc.dev, gwvoigt, s_leu, isampo, loopy1492, metasim: [upstream] Use CKEditor 5's native <ol type> and <ul type> UX
parent 970ce375
Loading
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -394,11 +394,8 @@ ckeditor5_list:
    plugins:
      - list.List
      - list.ListProperties
    config:
      list:
        properties:
          # @todo Make this configurable in https://www.drupal.org/project/drupal/issues/3274635
          styles: false
    # @see \Drupal\ckeditor5\Plugin\CKEditor5Plugin\ListPlugin::getDynamicPluginConfig()
    config: {}
  drupal:
    label: List
    library: core/ckeditor5.list
@@ -414,6 +411,8 @@ ckeditor5_list:
      - <ol>
      - <ol reversed start>
      - <li>
      - <ol type>
      - <ul type>

ckeditor5_horizontalLine:
  ckeditor5:
+20 −0
Original line number Diff line number Diff line
@@ -6,6 +6,8 @@
 */

// cspell:ignore multiblock
use Drupal\Core\Config\Entity\ConfigEntityUpdater;
use Drupal\editor\Entity\Editor;

/**
 * Implements hook_removed_post_updates().
@@ -20,3 +22,21 @@ function ckeditor5_removed_post_updates(): array {
    'ckeditor5_post_update_list_start_reversed' => '11.0.0',
  ];
}

/**
 * Updates Text Editors using CKEditor 5 to native List "type" functionality.
 */
function ckeditor5_post_update_list_type(array &$sandbox = []): void {
  $config_entity_updater = \Drupal::classResolver(ConfigEntityUpdater::class);
  $config_entity_updater->update($sandbox, 'editor', function (Editor $editor): bool {
    // Only try to update editors using CKEditor 5.
    if ($editor->getEditor() !== 'ckeditor5') {
      return FALSE;
    }
    $settings = $editor->getSettings();

    // @see Ckeditor5Hooks::editorPresave()
    return array_key_exists('ckeditor5_list', $settings['plugins'])
      && array_key_exists('ckeditor5_sourceEditing', $settings['plugins']);
  });
}
+8 −0
Original line number Diff line number Diff line
@@ -142,14 +142,22 @@ ckeditor5.plugin.ckeditor5_list:
      mapping:
        reversed:
          type: boolean
          # @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol#reversed
          label: 'Allow reverse list'
          constraints:
            NotNull: []
        startIndex:
          type: boolean
          # @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol#start
          label: 'Allow start index'
          constraints:
            NotNull: []
        styles:
          type: boolean
          # @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ol#type
          label: 'Allow list style type'
          constraints:
            NotNull: []
    multiBlock:
      type: boolean
      label: 'Allow blocks to be created in list items'
+24 −1
Original line number Diff line number Diff line
@@ -324,7 +324,6 @@
      const addedCss = [
        `${prefix} .ck.ck-content * {display:revert;background:revert;color:initial;padding:revert;}`,
        `${prefix} .ck.ck-content li {display:list-item}`,
        `${prefix} .ck.ck-content ol li {list-style-type: decimal}`,
      ];

      const prefixedCss = [...addedCss].join('\n');
@@ -625,6 +624,30 @@
    },
  };

  Drupal.behaviors.editorStyleFix = {
    attach(context) {
      // CKEditor's DLL injects a style tag that overrides native list
      // type styling. The following find the style(s) causing the problem
      // and removes them.
      // @todo remove this entire behavior when this issue is fixed
      //  https://github.com/ckeditor/ckeditor5/issues/14613
      [...document.styleSheets]
        .filter((sheet) => sheet.ownerNode.hasAttribute('data-cke'))
        .forEach((sheet) => {
          [...sheet.cssRules].forEach((rule, ruleIndex) => {
            if (
              rule?.selectorText &&
              (rule.selectorText.includes(' ol') ||
                rule.selectorText.includes(' ul')) &&
              !rule.selectorText.includes('type')
            ) {
              sheet.cssRules[ruleIndex].style['list-style-type'] = null;
            }
          });
        });
    },
  };

  // Redirect on hash change when the original hash has an associated CKEditor 5.
  function redirectTextareaFragmentToCKEditor5Instance() {
    const hash = window.location.hash.substring(1);
+39 −0
Original line number Diff line number Diff line
@@ -6,12 +6,14 @@
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Core\Render\Element;
use Drupal\ckeditor5\HTMLRestrictions;
use Drupal\ckeditor5\Plugin\Editor\CKEditor5;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\editor\EditorInterface;

/**
 * Hook implementations for ckeditor5.
@@ -377,4 +379,41 @@ public function configSchemaInfoAlter(&$definitions): void {
    $definitions['ckeditor5_valid_pair__format_and_editor']['mapping']['image_upload'] = $definitions['editor.editor.*']['mapping']['image_upload'];
  }

  /**
   * Implements hook_ENTITY_TYPE_presave() for editor entities.
   */
  #[Hook('editor_presave')]
  public function editorPresave(EditorInterface $editor): void {
    if ($editor->getEditor() === 'ckeditor5') {
      $settings = $editor->getSettings();
      // @see ckeditor5_post_update_list_type()
      if (array_key_exists('ckeditor5_list', $settings['plugins']) && array_key_exists('ckeditor5_sourceEditing', $settings['plugins'])) {
        $source_edited = HTMLRestrictions::fromString(implode(' ', $settings['plugins']['ckeditor5_sourceEditing']['allowed_tags']));
        $format_restrictions = HTMLRestrictions::fromTextFormat($editor->getFilterFormat());

        // If neither <ol type> or <ul type> are allowed through Source Editing
        // (the only way it could possibly be supported until now), and it is
        // not an unrestricted text format (such as "Full HTML"), then set the
        // new "styles" setting for the List plugin to false.
        $ol_type = HTMLRestrictions::fromString('<ol type>');
        $ul_type = HTMLRestrictions::fromString('<ul type>');
        if (!array_key_exists('styles', $settings['plugins']['ckeditor5_list']['properties'])) {
          $settings['plugins']['ckeditor5_list']['properties']['styles'] =
            $ol_type->diff($source_edited)->allowsNothing() ||
            $ul_type->diff($source_edited)->allowsNothing() ||
            $format_restrictions->isUnrestricted();
        }

        // Update the Source Editing configuration too.
        $settings['plugins']['ckeditor5_sourceEditing']['allowed_tags'] = $source_edited
          ->diff($ol_type)
          ->diff($ul_type)
          ->toCKEditor5ElementsArray();
      }

      $editor->setSettings($settings);

    }
  }

}
Loading