Commit 953a69bf authored by Ben Mullins's avatar Ben Mullins
Browse files

Issue #3261599 by hooroomoo, Wim Leers, lauriii: Use CKEditor 5's native <ol...

Issue #3261599 by hooroomoo, Wim Leers, lauriii: Use CKEditor 5's native <ol start> support (and also support <ol reversed>)
parent 22dab5e4
Loading
Loading
Loading
Loading
+12 −2
Original line number Diff line number Diff line
@@ -352,11 +352,21 @@ ckeditor5_linkMedia:

ckeditor5_list:
  ckeditor5:
    plugins: [list.List]
    plugins:
     - list.List
     - list.ListProperties
    config:
      list:
        properties:
          reversed: true
          startIndex: true
          # @todo Make this configurable in https://www.drupal.org/project/drupal/issues/3274635
          styles: false
  drupal:
    label: List
    library: core/ckeditor5.list
    admin_library: ckeditor5/admin.list
    class: Drupal\ckeditor5\Plugin\CKEditor5Plugin\ListPlugin
    toolbar_items:
      bulletedList:
        label: Bulleted list
@@ -364,7 +374,7 @@ ckeditor5_list:
        label: Numbered list
    elements:
      - <ul>
      - <ol>
      - <ol reversed start>
      - <li>

ckeditor5_horizontalLine:
+16 −0
Original line number Diff line number Diff line
@@ -85,3 +85,19 @@ ckeditor5.plugin.ckeditor5_sourceEditing:
        label: 'Allowed Tag'
        constraints:
          SourceEditingRedundantTags: []

# Plugin \Drupal\ckeditor5\Plugin\CKEditor5Plugin\ListPlugin
ckeditor5.plugin.ckeditor5_list:
  type: mapping
  label: List
  mapping:
    reversed:
      type: boolean
      label: 'Allow reverse list'
      constraints:
        NotNull: []
    startIndex:
      type: boolean
      label: 'Allow start index'
      constraints:
        NotNull: []
+14 −0
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@
 *   },
 *   cke5_plugin_elements_subset_configuration = {
 *    "ckeditor5_heading",
 *    "ckeditor5_list",
 *   }
 * )
 *
@@ -226,6 +227,19 @@ public function computeCKEditor5PluginSubsetConfiguration(string $cke5_plugin_id
        }
        return $configuration;

      case 'ckeditor5_list':
        $restrictions = $text_format->getHtmlRestrictions();
        if ($restrictions === FALSE) {
          // The default is to allow a reversed list and a start index, which makes sense when there
          // are no restrictions.
          // @see \Drupal\ckeditor5\Plugin\CKEditor5Plugin\ListPlugin::default_configuration()
          return NULL;
        }
        $configuration = [];
        $configuration['reversed'] = !empty($restrictions['allowed']['ol']['reversed']);
        $configuration['startIndex'] = !empty($restrictions['allowed']['ol']['start']);
        return $configuration;

      default:
        throw new \OutOfBoundsException();
    }
+90 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\ckeditor5\Plugin\CKEditor5Plugin;

use Drupal\ckeditor5\Plugin\CKEditor5PluginConfigurableInterface;
use Drupal\ckeditor5\Plugin\CKEditor5PluginConfigurableTrait;
use Drupal\ckeditor5\Plugin\CKEditor5PluginDefault;
use Drupal\ckeditor5\Plugin\CKEditor5PluginElementsSubsetInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\EditorInterface;

/**
 * CKEditor 5 List plugin.
 *
 * @internal
 *   Plugin classes are internal.
 */
class ListPlugin extends CKEditor5PluginDefault implements CKEditor5PluginConfigurableInterface, CKEditor5PluginElementsSubsetInterface {

  use CKEditor5PluginConfigurableTrait;

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return ['reversed' => TRUE, 'startIndex' => TRUE];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['reversed'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Allow the user to reverse an ordered list'),
      '#default_value' => $this->configuration['reversed'],
    ];
    $form['startIndex'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Allow the user to specify the start index of an ordered list'),
      '#default_value' => $this->configuration['startIndex'],
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $form_value = $form_state->getValue('reversed');
    $form_state->setValue('reversed', (bool) $form_value);
    $form_value = $form_state->getValue('startIndex');
    $form_state->setValue('startIndex', (bool) $form_value);
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['reversed'] = $form_state->getValue('reversed');
    $this->configuration['startIndex'] = $form_state->getValue('startIndex');
  }

  /**
   * {@inheritdoc}
   */
  public function getDynamicPluginConfig(array $static_plugin_config, EditorInterface $editor): array {
    $static_plugin_config['list']['properties'] = array_merge(
      $static_plugin_config['list']['properties'],
      $this->getConfiguration()
    );
    return $static_plugin_config;
  }

  /**
   * {@inheritdoc}
   */
  public function getElementsSubset(): array {
    $subset = $this->getPluginDefinition()->getElements();
    $subset = array_diff($subset, ['<ol reversed start>']);
    $reversed_enabled = $this->getConfiguration()['reversed'];
    $start_index_enabled = $this->getConfiguration()['startIndex'];
    $subset[] = "<ol" . ($reversed_enabled ? ' reversed' : '') . ($start_index_enabled ? ' start' : '') . '>';
    return $subset;
  }

}
+3 −1
Original line number Diff line number Diff line
@@ -329,7 +329,9 @@ public function getProvidedElements(array $plugin_ids = [], EditorInterface $edi
        // work: otherwise it would not be able to know which plugins to enable.
        elseif (isset($editor)) {
          $subset = $this->getPlugin($id, $editor)->getElementsSubset();
          $subset_violations = array_diff($subset, $defined_elements);
          $subset_restrictions = HTMLRestrictions::fromString(implode($subset));
          $defined_restrictions = HTMLRestrictions::fromString(implode($defined_elements));
          $subset_violations = $subset_restrictions->diff($defined_restrictions)->toCKEditor5ElementsArray();
          if (!empty($subset_violations)) {
            throw new \LogicException(sprintf('The "%s" CKEditor 5 plugin implements ::getElementsSubset() and did not return a subset, the following tags are absent from the plugin definition: "%s".', $id, implode(' ', $subset_violations)));
          }
Loading