From f20780446d3803051470edc714408ac07ef864ea Mon Sep 17 00:00:00 2001 From: Harlor <harlor@web.de> Date: Tue, 12 Apr 2022 16:22:53 +0200 Subject: [PATCH 1/2] Issue #2999328 introduce syntax_field --- config/install/ace_editor.settings.yml | 1 + .../Field/FieldFormatter/AceFormatter.php | 53 ++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/config/install/ace_editor.settings.yml b/config/install/ace_editor.settings.yml index b097881..36a78e1 100644 --- a/config/install/ace_editor.settings.yml +++ b/config/install/ace_editor.settings.yml @@ -178,6 +178,7 @@ syntax_list: yaml: "YAML" theme: 'cobalt' syntax: 'html' +syntax_field: '_none' height: '300px' width: '100%' font_size: '12pt' diff --git a/src/Plugin/Field/FieldFormatter/AceFormatter.php b/src/Plugin/Field/FieldFormatter/AceFormatter.php index b6ef2a5..174b7ae 100644 --- a/src/Plugin/Field/FieldFormatter/AceFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AceFormatter.php @@ -2,6 +2,7 @@ namespace Drupal\ace_editor\Plugin\Field\FieldFormatter; +use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; @@ -39,6 +40,13 @@ class AceFormatter extends FormatterBase implements ContainerFactoryPluginInterf */ protected $renderer; + /** + * The entity field manager. + * + * @var \Drupal\Core\Entity\EntityFieldManagerInterface + */ + protected $entityFieldManager; + /** * Constructs an AceFormatter instance. * @@ -60,13 +68,16 @@ class AceFormatter extends FormatterBase implements ContainerFactoryPluginInterf * The rendered service. * @param \Drupal\Core\Config\ConfigFactory $config_factory * The config factory. + * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager + * The entity field manager. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, RendererInterface $renderer, ConfigFactory $config_factory) { + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, RendererInterface $renderer, ConfigFactory $config_factory, EntityFieldManagerInterface $entity_field_manager) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); $this->renderer = $renderer; $this->configFactory = $config_factory; + $this->entityFieldManager = $entity_field_manager; } /** @@ -82,7 +93,8 @@ class AceFormatter extends FormatterBase implements ContainerFactoryPluginInterf $configuration['view_mode'], $configuration['third_party_settings'], $container->get('renderer'), - $container->get('config.factory') + $container->get('config.factory'), + $container->get('entity_field.manager') ); } @@ -111,6 +123,7 @@ class AceFormatter extends FormatterBase implements ContainerFactoryPluginInterf $summary[] = $this->t('Show print margin:') . ' ' . ($settings['print_margins'] ? $this->t('On') : $this->t('Off')); $summary[] = $this->t('Show invisible characters:') . ' ' . ($settings['show_invisibles'] ? $this->t('On') : $this->t('Off')); $summary[] = $this->t('Toggle word wrapping:') . ' ' . ($settings['use_wrap_mode'] ? $this->t('On') : $this->t('Off')); + $summary[] = $this->t('Syntax field:') . ' ' . $this->getSyntaxOptions()[$settings['syntax_field']]; return $summary; } @@ -146,6 +159,12 @@ class AceFormatter extends FormatterBase implements ContainerFactoryPluginInterf ], '#default_value' => $settings['syntax'], ], + 'syntax_field' => [ + '#type' => 'select', + '#title' => $this->t('Syntax field'), + '#default_value' => $settings['syntax_field'], + '#options' => $this->getSyntaxOptions(), + ], 'height' => [ '#type' => 'textfield', '#title' => $this->t('Height'), @@ -203,8 +222,21 @@ class AceFormatter extends FormatterBase implements ContainerFactoryPluginInterf // Renders front-end of our formatter. $elements = []; $settings = $this->getSettings(); + $default_syntax = $settings['syntax']; foreach ($items as $delta => $item) { + if (isset($settings['syntax_field']) + && $settings['syntax_field'] != '_none' + && $item->getEntity()->hasField($settings['syntax_field']) + && !$item->getEntity()->get($settings['syntax_field'])->isEmpty() + ) { + $field_storage_definition = $item->getEntity()->get($settings['syntax_field'])->getFieldDefinition()->getFieldStorageDefinition(); + $key = $field_storage_definition->getMainPropertyName(); + $settings['syntax'] = $item->getEntity()->get($settings['syntax_field'])->{$key}; + } + else { + $settings['syntax'] = $default_syntax; + } $elements[$delta] = [ '#type' => 'textarea', @@ -230,4 +262,21 @@ class AceFormatter extends FormatterBase implements ContainerFactoryPluginInterf return $elements; } + /** + * Helper method to get all possible options for the syntax_field setting. + * + * @return array + * Select options for syntax_field. + */ + protected function getSyntaxOptions(): array { + $syntax_options = ['_none' => $this->t('None')]; + $entity_type_id = $this->fieldDefinition->getTargetEntityTypeId(); + $bundle = $this->fieldDefinition->getTargetBundle(); + foreach ($this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle) as $field) { + $syntax_options[$field->getName()] = $field->getLabel(); + } + + return $syntax_options; + } + } -- GitLab From 28e9bfd89534129810dfa8a573de3629d3e41a5c Mon Sep 17 00:00:00 2001 From: Harlor <harlor@web.de> Date: Thu, 21 Apr 2022 13:55:50 +0200 Subject: [PATCH 2/2] Issue #2999328 introduce modelist option --- ace_editor.module | 1 + config/install/ace_editor.settings.yml | 2 ++ js/formatter.js | 11 +++++++++- .../Field/FieldFormatter/AceFormatter.php | 21 +++++++++++++++---- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/ace_editor.module b/ace_editor.module index 2e42215..9680ace 100644 --- a/ace_editor.module +++ b/ace_editor.module @@ -100,6 +100,7 @@ function ace_editor_library_info_alter(&$libraries, $extension) { $libraries['primary']['js'][$library_path . 'ext-language_tools.js'] = ['weight' => -2]; } $libraries['formatter']['js'][$library_path . 'ace.js'] = ['weight' => -2]; + $libraries['formatter']['js'][$library_path . 'ext-modelist.js'] = ['weight' => -2]; $libraries['filter']['js'][$library_path . 'ace.js'] = ['weight' => -2]; } } diff --git a/config/install/ace_editor.settings.yml b/config/install/ace_editor.settings.yml index 36a78e1..3925ae7 100644 --- a/config/install/ace_editor.settings.yml +++ b/config/install/ace_editor.settings.yml @@ -179,6 +179,8 @@ syntax_list: theme: 'cobalt' syntax: 'html' syntax_field: '_none' +modelist: false +inline: false height: '300px' width: '100%' font_size: '12pt' diff --git a/js/formatter.js b/js/formatter.js index 6a1a60f..ae8ec4a 100644 --- a/js/formatter.js +++ b/js/formatter.js @@ -31,12 +31,21 @@ // Setting theme and mode variable. var theme = ace_settings.theme; var mode = ace_settings.syntax; + var inline = Boolean(ace_settings.inline); + if (ace_settings.syntax_field_value) { + mode = ace_settings.syntax_field_value; + if (ace_settings.modelist) { + let modelist = ace.require("ace/ext/modelist"); + mode = modelist.getModeForPath("." + mode).name; + } + } // Setting editor style and properties. var editor = ace.edit(display_id); editor.setReadOnly(true); editor.setTheme("ace/theme/"+theme); - editor.getSession().setMode("ace/mode/"+mode); + editor.getSession().setMode({path:"ace/mode/" + mode, inline:inline}); + editor.getSession().setValue(content.val()); $("#"+display_id).height(ace_settings.height).width(ace_settings.width); diff --git a/src/Plugin/Field/FieldFormatter/AceFormatter.php b/src/Plugin/Field/FieldFormatter/AceFormatter.php index 174b7ae..0f4b0f0 100644 --- a/src/Plugin/Field/FieldFormatter/AceFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AceFormatter.php @@ -116,6 +116,9 @@ class AceFormatter extends FormatterBase implements ContainerFactoryPluginInterf $summary = []; $summary[] = $this->t('Theme:') . ' ' . $settings['theme']; $summary[] = $this->t('Syntax:') . ' ' . $settings['syntax']; + $summary[] = $this->t('Syntax field:') . ' ' . $this->getSyntaxOptions()[$settings['syntax_field']]; + $summary[] = $this->t('Syntax use modelist:') . ' ' . ($settings['modelist'] ? $this->t('Yes') : $this->t('No')); + $summary[] = $this->t('Inline:') . ' ' . ($settings['inline'] ? $this->t('Yes') : $this->t('No')); $summary[] = $this->t('Height:') . ' ' . $settings['height']; $summary[] = $this->t('Width:') . ' ' . $settings['width']; $summary[] = $this->t('Font size:') . ' ' . $settings['font_size']; @@ -123,7 +126,6 @@ class AceFormatter extends FormatterBase implements ContainerFactoryPluginInterf $summary[] = $this->t('Show print margin:') . ' ' . ($settings['print_margins'] ? $this->t('On') : $this->t('Off')); $summary[] = $this->t('Show invisible characters:') . ' ' . ($settings['show_invisibles'] ? $this->t('On') : $this->t('Off')); $summary[] = $this->t('Toggle word wrapping:') . ' ' . ($settings['use_wrap_mode'] ? $this->t('On') : $this->t('Off')); - $summary[] = $this->t('Syntax field:') . ' ' . $this->getSyntaxOptions()[$settings['syntax_field']]; return $summary; } @@ -165,6 +167,18 @@ class AceFormatter extends FormatterBase implements ContainerFactoryPluginInterf '#default_value' => $settings['syntax_field'], '#options' => $this->getSyntaxOptions(), ], + 'modelist' => [ + '#type' => 'checkbox', + '#title' => $this->t('Modelist'), + '#description' => $this->t('Use the ACE modelist to convert a file-extension to a syntax mode.'), + '#default_value' => $settings['modelist'], + ], + 'inline' => [ + '#type' => 'checkbox', + '#title' => $this->t('Inline'), + '#description' => $this->t('Specify if the code shall be displayed as inline code.'), + '#default_value' => $settings['inline'], + ], 'height' => [ '#type' => 'textfield', '#title' => $this->t('Height'), @@ -222,7 +236,6 @@ class AceFormatter extends FormatterBase implements ContainerFactoryPluginInterf // Renders front-end of our formatter. $elements = []; $settings = $this->getSettings(); - $default_syntax = $settings['syntax']; foreach ($items as $delta => $item) { if (isset($settings['syntax_field']) @@ -232,10 +245,10 @@ class AceFormatter extends FormatterBase implements ContainerFactoryPluginInterf ) { $field_storage_definition = $item->getEntity()->get($settings['syntax_field'])->getFieldDefinition()->getFieldStorageDefinition(); $key = $field_storage_definition->getMainPropertyName(); - $settings['syntax'] = $item->getEntity()->get($settings['syntax_field'])->{$key}; + $settings['syntax_field_value'] = $item->getEntity()->get($settings['syntax_field'])->{$key}; } else { - $settings['syntax'] = $default_syntax; + $settings['syntax_field_value'] = NULL; } $elements[$delta] = [ -- GitLab