Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #2766 Added a "code" form type (javiereguiluz)
This PR was merged into the 2.0.x-dev branch. Discussion ---------- Added a "code" form type This adds a custom form type for new/edit pages based on the popular CodeMirror code editor (I tested Microsoft's Monaco editor, Ace editor, etc. ... none worked as great as CodeMirror). You only need to add `type: 'code'` in your form property and ... boom 💥 you have a source code editor with syntax highlighting, etc. This is how it looks with more than one editor in the same page: ![image](https://user-images.githubusercontent.com/73419/59554577-6ea20b00-8fa5-11e9-9780-b566b35131b0.png) @yceruto I guess what I did in `EasyAdminCodeTypeConfigurator` is "wrong" 🙈 and the type/value validation should be moved to the form type. Can you give me a clue about that? Thanks! Commits ------- caa3a36 Added a "code editor" form type
- Loading branch information
Showing
21 changed files
with
293 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
@import '~codemirror/lib/codemirror.css'; | ||
|
||
.CodeMirror { | ||
font: 13px/1.5 SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Ubuntu Mono", "Courier New", monospace; | ||
height: auto; | ||
min-height: 45px; | ||
} | ||
|
||
.CodeMirror-wrap { | ||
box-shadow: 0 0 0 1px rgba(43, 45, 80, .16), 0 0 0 1px rgba(6, 122, 184, 0), 0 0 0 2px rgba(6, 122, 184, 0), 0 1px 1px rgba(0, 0, 0, .08);; | ||
border-radius: var(--border-radius); | ||
} | ||
|
||
.CodeMirror-gutters { | ||
background: var(--gray-100); | ||
} | ||
|
||
.CodeMirror-linenumber { | ||
color: var(--text-muted); | ||
} | ||
|
||
.CodeMirror-lines { | ||
padding-bottom: 5px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require('../css/form-type-code-editor.css'); | ||
|
||
import CodeMirror from 'codemirror'; | ||
|
||
import 'codemirror/mode/css/css'; | ||
import 'codemirror/mode/dockerfile/dockerfile'; | ||
import 'codemirror/mode/javascript/javascript'; | ||
import 'codemirror/mode/markdown/markdown'; | ||
import 'codemirror/mode/nginx/nginx'; | ||
import 'codemirror/mode/php/php'; | ||
import 'codemirror/mode/shell/shell'; | ||
import 'codemirror/mode/sql/sql'; | ||
import 'codemirror/mode/twig/twig'; | ||
import 'codemirror/mode/xml/xml'; | ||
import 'codemirror/mode/yaml-frontmatter/yaml-frontmatter'; | ||
import 'codemirror/mode/yaml/yaml'; | ||
|
||
document.querySelectorAll('[data-easyadmin-code-editor]').forEach(function(codeBlock) { | ||
CodeMirror.fromTextArea(codeBlock, { | ||
autocapitalize: false, | ||
autocorrect: false, | ||
indentWithTabs: codeBlock.dataset.indentWithTabs, | ||
lineNumbers: true, | ||
lineWrapping: true, | ||
mode: codeBlock.dataset.language, | ||
scrollbarStyle: 'native', | ||
spellcheck: false, | ||
tabSize: codeBlock.dataset.tabSize, | ||
theme: 'default', | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Form\Type; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextareaType; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\FormView; | ||
use Symfony\Component\OptionsResolver\Options; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* @author Yonel Ceruto <yonelceruto@gmail.com> | ||
*/ | ||
class CodeEditorType extends AbstractType | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildView(FormView $view, FormInterface $form, array $options): void | ||
{ | ||
$view->vars['height'] = $options['height']; | ||
$view->vars['tabSize'] = $options['tab_size']; | ||
$view->vars['indentWithTabs'] = $options['indent_with_tabs']; | ||
$view->vars['language'] = $options['language']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->setDefaults([ | ||
'height' => null, | ||
'tab_size' => 4, | ||
'indent_with_tabs' => false, | ||
// the code editor can't autodetect the language, so 'markdown' is used when | ||
// no language is selected explicitly (because it's the most similar to regular text) | ||
'language' => 'markdown', | ||
]); | ||
$resolver->setAllowedTypes('height', ['null', 'int']); | ||
$resolver->setAllowedTypes('tab_size', 'int'); | ||
$resolver->setAllowedTypes('indent_with_tabs', 'bool'); | ||
$resolver->setAllowedTypes('language', 'string'); | ||
$resolver->setAllowedValues('language', ['css', 'dockerfile', 'js', 'javascript', 'markdown', 'nginx', 'php', 'shell', 'sql', 'twig', 'xml', 'yaml-frontmatter', 'yaml']); | ||
|
||
// define some programming language shortcuts for better UX (e.g. 'js' === 'javascript') | ||
$resolver->setNormalizer('language', static function (Options $options, $language) { | ||
if ('js' === $language) { | ||
$language = 'javascript'; | ||
} | ||
|
||
return $language; | ||
}); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParent(): string | ||
{ | ||
return TextareaType::class; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getBlockPrefix(): string | ||
{ | ||
return 'easyadmin_code_editor'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Form\Type\Configurator; | ||
|
||
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CodeEditorType; | ||
use Symfony\Component\Form\FormConfigInterface; | ||
|
||
/** | ||
* This configurator is applied to any form field of type 'code_editor'. | ||
* | ||
* @author Javier Eguiluz <javier.eguiluz@gmail.com> | ||
*/ | ||
class CodeEditorTypeConfigurator implements TypeConfiguratorInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configure($name, array $options, array $metadata, FormConfigInterface $parentConfig): array | ||
{ | ||
if (isset($metadata['height'])) { | ||
$options['height'] = $metadata['height']; | ||
} | ||
if (isset($metadata['tab_size'])) { | ||
$options['tab_size'] = $metadata['tab_size']; | ||
} | ||
if (isset($metadata['indent_with_tabs'])) { | ||
$options['indent_with_tabs'] = $metadata['indent_with_tabs']; | ||
} | ||
if (isset($metadata['language'])) { | ||
$options['language'] = $metadata['language']; | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($type, array $options, array $metadata): bool | ||
{ | ||
return \in_array($type, ['code_editor', CodeEditorType::class], true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.