From ebfd2c468301d4a5d348eb0a69c15e118288613c Mon Sep 17 00:00:00 2001 From: Nick Vanpraet Date: Thu, 15 Oct 2020 17:38:55 +0200 Subject: [PATCH] add theme config option + add schema --- config/install/yaml_editor.config.yml | 1 + config/schema/yaml_editor.schema.yml | 10 ++++++++++ js/yaml-editor.js | 2 +- src/Form/ConfigForm.php | 9 +++++++++ yaml_editor.install | 11 +++++++++++ yaml_editor.module | 5 ++++- 6 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 config/schema/yaml_editor.schema.yml create mode 100644 yaml_editor.install diff --git a/config/install/yaml_editor.config.yml b/config/install/yaml_editor.config.yml index ee32150..efbc08b 100644 --- a/config/install/yaml_editor.config.yml +++ b/config/install/yaml_editor.config.yml @@ -1 +1,2 @@ editor_source: //cdnjs.cloudflare.com/ajax/libs/ace/1.2.0/ace.min.js +editor_theme: ace/theme/chrome diff --git a/config/schema/yaml_editor.schema.yml b/config/schema/yaml_editor.schema.yml new file mode 100644 index 0000000..c89e2fc --- /dev/null +++ b/config/schema/yaml_editor.schema.yml @@ -0,0 +1,10 @@ +yaml_editor.config: + type: config_object + label: 'Yaml Editor Config' + mapping: + editor_source: + type: string + label: 'Editor source' + editor_theme: + type: string + label: 'Editor theme' diff --git a/js/yaml-editor.js b/js/yaml-editor.js index 206ce0f..bd28ad0 100644 --- a/js/yaml-editor.js +++ b/js/yaml-editor.js @@ -16,7 +16,7 @@ editor.getSession().setValue($textarea.val()); editor.getSession().setMode('ace/mode/yaml'); editor.getSession().setTabSize(2); - editor.setTheme('ace/theme/chrome'); + editor.setTheme(drupalSettings.yamlEditor.theme); editor.setOptions({ minLines: 3, maxLines: 20 diff --git a/src/Form/ConfigForm.php b/src/Form/ConfigForm.php index 2ec3e17..b903b79 100644 --- a/src/Form/ConfigForm.php +++ b/src/Form/ConfigForm.php @@ -46,6 +46,13 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#default_value' => $config->get('editor_source'), ]; + $form['editor_theme'] = [ + '#type' => 'textfield', + '#title' => $this->t('Editor theme'), + '#description' => $this->t('Enter name of Ace theme to use.'), + '#default_value' => $config->get('editor_theme'), + ]; + return parent::buildForm($form, $form_state); } @@ -61,9 +68,11 @@ public function validateForm(array &$form, FormStateInterface $form_state) { */ public function submitForm(array &$form, FormStateInterface $form_state) { $editor_source = $form_state->getValue('editor_source'); + $editor_theme = $form_state->getValue('editor_theme'); $this->config('yaml_editor.config') ->set('editor_source', $editor_source) + ->set('editor_theme', $editor_theme) ->save(); parent::submitForm($form, $form_state); diff --git a/yaml_editor.install b/yaml_editor.install new file mode 100644 index 0000000..3be6d5e --- /dev/null +++ b/yaml_editor.install @@ -0,0 +1,11 @@ +getEditable('yaml_editor.config') + ->set('editor_theme', 'ace/theme/chrome') + ->save(TRUE); +} diff --git a/yaml_editor.module b/yaml_editor.module index 48ec17e..dde4398 100644 --- a/yaml_editor.module +++ b/yaml_editor.module @@ -8,9 +8,12 @@ function yaml_editor_page_attachments(array &$attachments) { $is_admin = \Drupal::service('router.admin_context')->isAdminRoute($route); if ($is_admin) { - $source = \Drupal::config('yaml_editor.config')->get('editor_source'); + $config = \Drupal::config('yaml_editor.config'); + $source = $config->get('editor_source'); + $theme = $config->get('editor_theme'); $attachments['#attached']['drupalSettings']['yamlEditor']['source'] = $source; + $attachments['#attached']['drupalSettings']['yamlEditor']['theme'] = $theme; $attachments['#attached']['library'][] = 'yaml_editor/yaml_editor'; } }