You are here

class AceEditor in Ace Code Editor 8

Defines AceEditor as an Editor plugin.

Plugin annotation


@Editor(
  id = "ace_editor",
  label = "Ace Editor",
  supports_content_filtering = TRUE,
  supports_inline_editing = FALSE,
  is_xss_safe = FALSE,
  supported_element_types = {
    "textarea"
  }
)

Hierarchy

Expanded class hierarchy of AceEditor

File

src/Plugin/Editor/AceEditor.php, line 23

Namespace

Drupal\ace_editor\Plugin\Editor
View source
class AceEditor extends EditorBase {

  /**
   * {@inheritdoc}
   */
  public function getDefaultSettings() {
    $config = \Drupal::config('ace_editor.settings')
      ->get();
    return $config;
  }

  /**
   * Returns a settings form to configure this text editor.
   *
   * @param array $settings
   *   An array containing form configuration.
   *
   * @return array
   *   A primary render array for the settings form.
   */
  public function getForm(array $settings) {
    $config = \Drupal::config('ace_editor.settings');
    return [
      'theme' => [
        '#type' => 'select',
        '#title' => t('Theme'),
        '#options' => $config
          ->get('theme_list'),
        '#attributes' => [
          'style' => 'width: 150px;',
        ],
        '#default_value' => $settings['theme'],
      ],
      'syntax' => [
        '#type' => 'select',
        '#title' => t('Syntax'),
        '#description' => t('The syntax that will be highlighted.'),
        '#options' => $config
          ->get('syntax_list'),
        '#attributes' => [
          'style' => 'width: 150px;',
        ],
        '#default_value' => $settings['syntax'],
      ],
      'height' => [
        '#type' => 'textfield',
        '#title' => t('Height'),
        '#description' => t('The height of the editor in either pixels or percents.'),
        '#attributes' => [
          'style' => 'width: 100px;',
        ],
        '#default_value' => $settings['height'],
      ],
      'width' => [
        '#type' => 'textfield',
        '#title' => t('Width'),
        '#description' => t('The width of the editor in either pixels or percents.'),
        '#attributes' => [
          'style' => 'width: 100px;',
        ],
        '#default_value' => $settings['width'],
      ],
      'font_size' => [
        '#type' => 'textfield',
        '#title' => t('Font size'),
        '#description' => t('The font size used in the editor.'),
        '#attributes' => [
          'style' => 'width: 100px;',
        ],
        '#default_value' => $settings['font_size'],
      ],
      'line_numbers' => [
        '#type' => 'checkbox',
        '#title' => t('Show line numbers'),
        '#default_value' => $settings['line_numbers'],
      ],
      'print_margins' => [
        '#type' => 'checkbox',
        '#title' => t('Show print margin (80 chars)'),
        '#default_value' => $settings['print_margins'],
      ],
      'show_invisibles' => [
        '#type' => 'checkbox',
        '#title' => t('Show invisible characters (whitespaces, EOL...)'),
        '#default_value' => $settings['show_invisibles'],
      ],
      'use_wrap_mode' => [
        '#type' => 'checkbox',
        '#title' => t('Toggle word wrapping'),
        '#default_value' => $settings['use_wrap_mode'],
      ],
      'auto_complete' => [
        '#type' => 'checkbox',
        '#title' => t('Enable Autocomplete (Ctrl+Space'),
        '#default_value' => isset($settings['auto_complete']) ? $settings['auto_complete'] : TRUE,
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $editor = $form_state
      ->get('editor');
    $settings = $editor
      ->getSettings();
    $form = [];
    $form['fieldset'] = [
      '#type' => 'fieldset',
      '#title' => t('Ace Editor Settings'),
      '#collapsable' => TRUE,
    ];
    if (array_key_exists('fieldset', $settings)) {
      $form['fieldset'] = array_merge($form['fieldset'], $this
        ->getForm($settings['fieldset']));
    }
    else {
      $form['fieldset'] = array_merge($form['fieldset'], $this
        ->getForm($settings));
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsFormValidate(array $form, FormStateInterface $formState) {
  }

  /**
   * {@inheritdoc}
   */
  public function getLibraries(Editor $editor) {

    // Get default ace_editor configuration.
    $config = \Drupal::config('ace_editor.settings');

    // Get theme and mode.
    $theme = trim($editor
      ->getSettings()['fieldset']['theme']);
    $mode = trim($editor
      ->getSettings()['fieldset']['syntax']);

    // Check if theme and mode library exist.
    $theme_exist = \Drupal::service('library.discovery')
      ->getLibraryByName('ace_editor', 'theme.' . $theme);
    $mode_exist = \Drupal::service('library.discovery')
      ->getLibraryByName('ace_editor', 'mode.' . $mode);

    // ace_editor/primary the basic library for ace_editor.
    $libs = [
      'ace_editor/primary',
    ];
    if ($theme_exist) {
      $libs[] = 'ace_editor/theme.' . $theme;
    }
    else {
      $libs[] = 'ace_editor/theme.' . $config
        ->get('theme');
    }
    if ($mode_exist) {
      $libs[] = 'ace_editor/mode.' . $mode;
    }
    else {
      $libs[] = 'ace_editor/mode.' . $config
        ->get('syntax');
    }
    return $libs;
  }

  /**
   * {@inheritdoc}
   */
  public function getJsSettings(Editor $editor) {

    // Pass settings to javascript.
    return $editor
      ->getSettings()['fieldset'];
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AceEditor::buildConfigurationForm public function Form constructor. Overrides EditorBase::buildConfigurationForm
AceEditor::getDefaultSettings public function Returns the default settings for this configurable text editor. Overrides EditorBase::getDefaultSettings
AceEditor::getForm public function Returns a settings form to configure this text editor.
AceEditor::getJsSettings public function
AceEditor::getLibraries public function Returns libraries to be attached. Overrides EditorPluginInterface::getLibraries
AceEditor::settingsFormValidate public function
AceEditor::submitConfigurationForm public function Form submission handler. Overrides EditorBase::submitConfigurationForm
AceEditor::validateConfigurationForm public function Form validation handler. Overrides EditorBase::validateConfigurationForm
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EditorPluginInterface::getJSSettings public function Returns JavaScript settings to be attached. 4
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.