View source
<?php
namespace Drupal\ckeditor_codemirror\Plugin\CKEditorPlugin;
use Drupal\ckeditor\CKEditorPluginBase;
use Drupal\ckeditor\CKEditorPluginConfigurableInterface;
use Drupal\ckeditor\CKEditorPluginContextualInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;
class CodeMirror extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface, CKEditorPluginContextualInterface {
public function getFile() {
return _ckeditor_codemirror_get_library_path() . '/codemirror/plugin.js';
}
public function getDependencies(Editor $editor) {
return [];
}
public function getLibraries(Editor $editor) {
return [];
}
public function isInternal() {
return FALSE;
}
public function getButtons() {
return [];
}
public function isEnabled(Editor $editor) {
$settings = $editor
->getSettings();
if (isset($settings['plugins']['codemirror'])) {
return $editor
->getSettings()['plugins']['codemirror']['enable'];
}
return FALSE;
}
public function getConfig(Editor $editor) {
$settings = $editor
->getSettings()['plugins']['codemirror'];
$config = [
'codemirror' => [
'enable' => isset($settings['enable']) ? $settings['enable'] : FALSE,
'mode' => isset($settings['mode']) ? $settings['mode'] : 'htmlmixed',
'theme' => isset($settings['theme']) ? $settings['theme'] : 'default',
],
'startupMode' => isset($settings['startupMode']) ? $settings['startupMode'] : 'wysiwyg',
];
foreach ($this
->options() as $option => $description) {
$config['codemirror'][$option] = isset($settings['options'][$option]) ? $settings['options'][$option] : TRUE;
}
return $config;
}
private function options() {
return [
'lineNumbers' => $this
->t('Show line numbers.'),
'lineWrapping' => $this
->t('Enable line wrapping.'),
'matchBrackets' => $this
->t('Highlight matching brackets.'),
'autoCloseTags' => $this
->t('Close tags automatically.'),
'autoCloseBrackets' => $this
->t('Close brackets automatically.'),
'enableSearchTools' => $this
->t('Enable search tools.'),
'enableCodeFolding' => $this
->t('Enable code folding.'),
'enableCodeFormatting' => $this
->t('Enable code formatting.'),
'autoFormatOnStart' => $this
->t('Format code on start.'),
'autoFormatOnModeChange' => $this
->t('Format code each time source is opened.'),
'autoFormatOnUncomment' => $this
->t('Format code when a line is uncommented.'),
];
}
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
$editor_settings = $editor
->getSettings();
if (isset($editor_settings['plugins']['codemirror'])) {
$settings = $editor_settings['plugins']['codemirror'];
}
$form['#attached']['library'][] = 'ckeditor_codemirror/ckeditor_codemirror.admin';
$form['enable'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable CodeMirror source view syntax highlighting.'),
'#default_value' => isset($settings['enable']) ? $settings['enable'] : FALSE,
];
$form['startupMode'] = [
'#type' => 'select',
'#title' => $this
->t('Editor startup Mode'),
'#options' => [
'wysiwyg' => $this
->t('WYSIWYG (default)'),
'source' => $this
->t('Source'),
],
'#default_value' => isset($settings['startupMode']) ? $settings['startupMode'] : 'wysiwyg',
];
$form['mode'] = [
'#type' => 'select',
'#title' => $this
->t('Mode'),
'#options' => [
'htmlmixed' => $this
->t('HTML (including css, xml and javascript)'),
'text/html' => $this
->t('HTML only'),
'application/x-httpd-php' => $this
->t('PHP (including HTML)'),
'text/javascript' => $this
->t('Javascript only'),
],
'#default_value' => isset($settings['mode']) ? $settings['mode'] : 'htmlmixed',
];
$theme_options = [
'default' => 'default',
];
$themes_directory = _ckeditor_codemirror_get_library_path() . '/codemirror/theme';
if (is_dir($themes_directory)) {
$theme_css_files = file_scan_directory($themes_directory, '/\\.css/i');
foreach ($theme_css_files as $file) {
$theme_options[$file->name] = $file->name;
}
}
$form['theme'] = [
'#type' => 'select',
'#title' => $this
->t('Theme'),
'#options' => $theme_options,
'#default_value' => isset($settings['theme']) ? $settings['theme'] : 'default',
];
$form['options'] = [
'#type' => 'details',
'#title' => t('Additional settings'),
'#description' => t('Source highlighting and code formatting options:'),
'#open' => FALSE,
];
foreach ($this
->options() as $setting => $description) {
$form['options'][$setting] = [
'#type' => 'checkbox',
'#title' => $description,
'#default_value' => isset($settings['options'][$setting]) ? $settings['options'][$setting] : TRUE,
];
}
return $form;
}
}