View source
<?php
namespace Drupal\codesnippet\Plugin\CKEditorPlugin;
use Drupal\ckeditor\CKEditorPluginBase;
use Drupal\ckeditor\CKEditorPluginConfigurableInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;
class CodeSnippet extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface {
public function getFile() {
return 'libraries/codesnippet/plugin.js';
}
public function getConfig(Editor $editor) {
$settings = $editor
->getSettings();
if (!empty($settings['plugins']['codesnippet']['highlight_style'])) {
$style = $settings['plugins']['codesnippet']['highlight_style'];
}
else {
$style = $this
->getDefaultStyle();
}
if (!empty($settings['plugins']['codesnippet']['highlight_languages'])) {
$languages = array_filter($settings['plugins']['codesnippet']['highlight_languages']);
}
else {
$languages = $this
->getLanguages();
}
return [
'codeSnippet_theme' => $style,
'codeSnippet_languages' => $languages,
];
}
public function getButtons() {
return [
'CodeSnippet' => [
'label' => $this
->t('CodeSnippet'),
'image' => 'libraries/codesnippet/icons/codesnippet.png',
],
];
}
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
$settings = $editor
->getSettings();
$styles = $this
->getStyles();
$languages = $this
->getLanguages();
natcasesort($languages);
$form['#attached']['library'][] = 'codesnippet/codesnippet.admin';
$form['highlight_style'] = [
'#type' => 'select',
'#title' => 'highlight.js Style',
'#description' => $this
->t('Select a style to apply to all highlighted code snippets. You can preview the styles at <a href=":url">:url</a>.', [
':url' => 'https://highlightjs.org/static/demo',
]),
'#options' => $styles,
'#default_value' => !empty($settings['plugins']['codesnippet']['highlight_style']) ? $settings['plugins']['codesnippet']['highlight_style'] : $this
->getDefaultStyle(),
];
$form['highlight_languages'] = [
'#type' => 'checkboxes',
'#title' => 'Supported Languages',
'#options' => $languages,
'#description' => $this
->t('Enter languages you want to have as options in the editor dialog. To add a language not in this list, please see the README.txt of this module.'),
'#default_value' => isset($settings['plugins']['codesnippet']['highlight_languages']) ? $settings['plugins']['codesnippet']['highlight_languages'] : $this
->getDefaultLanguages(),
];
return $form;
}
private function getStyles() {
$styles = preg_grep('/\\.css/', scandir(DRUPAL_ROOT . '/libraries/codesnippet/lib/highlight/styles'));
$style_options = [];
foreach ($styles as $stylesheet) {
$name = str_replace('.css', '', $stylesheet);
$style_options[$name] = $name;
}
return $style_options;
}
private function getDefaultStyle() {
$styles = $this
->getStyles();
return reset($styles);
}
private function getDefaultLanguages() {
$languages = array_keys($this
->getLanguages());
return array_combine($languages, $languages);
}
private function getLanguages() {
return [
'apache' => 'Apache',
'bash' => 'Bash',
'coffeescript' => 'CoffeeScript',
'cpp' => 'C++',
'cs' => 'C#',
'css' => 'CSS',
'diff' => 'Diff',
'html' => 'HTML',
'http' => 'HTTP',
'ini' => 'INI',
'java' => 'Java',
'javascript' => 'JavaScript',
'json' => 'JSON',
'makefile' => 'Makefile',
'markdown' => 'Markdown',
'nginx' => 'Nginx',
'objectivec' => 'Objective-C',
'perl' => 'Perl',
'php' => 'PHP',
'python' => 'Python',
'ruby' => 'Ruby',
'sql' => 'SQL',
'vbscript' => 'VBScript',
'xhtml' => 'XHTML',
'xml' => 'XML',
];
}
}