From c036e2a8d15d7df266512dc8a08eb183b6b709d5 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Mon, 14 Jun 2021 07:42:57 +0200 Subject: [PATCH] Add `SourceCodeFormField` --- .../templates/__sourceCodeFormField.tpl | 18 ++ syncTemplates.json | 1 + .../acp/templates/__sourceCodeFormField.tpl | 18 ++ .../field/SourceCodeFormField.class.php | 223 ++++++++++++++++++ 4 files changed, 260 insertions(+) create mode 100644 com.woltlab.wcf/templates/__sourceCodeFormField.tpl create mode 100644 wcfsetup/install/files/acp/templates/__sourceCodeFormField.tpl create mode 100644 wcfsetup/install/files/lib/system/form/builder/field/SourceCodeFormField.class.php diff --git a/com.woltlab.wcf/templates/__sourceCodeFormField.tpl b/com.woltlab.wcf/templates/__sourceCodeFormField.tpl new file mode 100644 index 0000000000..6fbf197580 --- /dev/null +++ b/com.woltlab.wcf/templates/__sourceCodeFormField.tpl @@ -0,0 +1,18 @@ + + +{include file='codemirror' codemirrorMode=$field->getLanguage() codemirrorSelector='#'|concat:$field->getPrefixedId()} + + diff --git a/syncTemplates.json b/syncTemplates.json index 60709bd867..fcd4ed90c5 100644 --- a/syncTemplates.json +++ b/syncTemplates.json @@ -41,6 +41,7 @@ "__rowFormFieldContainer", "__singleMediaSelectionFormField", "__singleSelectionFormField", + "__sourceCodeFormField", "__suffixFormFieldContainer", "__tabFormContainer", "__tabMenuFormContainer", diff --git a/wcfsetup/install/files/acp/templates/__sourceCodeFormField.tpl b/wcfsetup/install/files/acp/templates/__sourceCodeFormField.tpl new file mode 100644 index 0000000000..6fbf197580 --- /dev/null +++ b/wcfsetup/install/files/acp/templates/__sourceCodeFormField.tpl @@ -0,0 +1,18 @@ + + +{include file='codemirror' codemirrorMode=$field->getLanguage() codemirrorSelector='#'|concat:$field->getPrefixedId()} + + diff --git a/wcfsetup/install/files/lib/system/form/builder/field/SourceCodeFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/SourceCodeFormField.class.php new file mode 100644 index 0000000000..9d1fde27d1 --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/field/SourceCodeFormField.class.php @@ -0,0 +1,223 @@ + + * @package WoltLabSuite\Core\System\Form\Builder\Field + * @since 5.5 + */ +class SourceCodeFormField extends AbstractFormField implements + IAttributeFormField, + IAutoFocusFormField, + ICssClassFormField, + IImmutableFormField, + IMaximumLengthFormField, + IMinimumLengthFormField, + INullableFormField +{ + use TAttributeFormField; + use TAutoFocusFormField; + use TCssClassFormField; + use TImmutableFormField; + use TMaximumLengthFormField; + use TMinimumLengthFormField; + use TNullableFormField; + + /** + * @var string|null + */ + protected $language; + + /** + * @inheritDoc + */ + protected $templateName = '__sourceCodeFormField'; + + public const LANGUAGES = [ + // Languages supported by CodeMirror itself. + 'apl', + 'asciiarmor', + 'asn.1', + 'asterisk', + 'brainfuck', + 'clike', + 'clojure', + 'cmake', + 'cobol', + 'coffeescript', + 'commonlisp', + 'crystal', + 'css', + 'cypher', + 'd', + 'dart', + 'diff', + 'django', + 'dockerfile', + 'dtd', + 'dylan', + 'ebnf', + 'ecl', + 'eiffel', + 'elm', + 'erlang', + 'factor', + 'fcl', + 'forth', + 'fortran', + 'gas', + 'gfm', + 'gherkin', + 'go', + 'groovy', + 'haml', + 'handlebars', + 'haskell', + 'haskell-literate', + 'haxe', + 'htmlembedded', + 'htmlmixed', + 'http', + 'idl', + 'javascript', + 'jinja2', + 'jsx', + 'julia', + 'livescript', + 'lua', + 'markdown', + 'mathematica', + 'mbox', + 'mirc', + 'mllike', + 'modelica', + 'mscgen', + 'mumps', + 'nginx', + 'nsis', + 'ntriples', + 'octave', + 'oz', + 'pascal', + 'pegjs', + 'perl', + 'php', + 'pig', + 'powershell', + 'properties', + 'protobuf', + 'pug', + 'puppet', + 'python', + 'q', + 'r', + 'rpm', + 'rst', + 'ruby', + 'rust', + 'sas', + 'sass', + 'scheme', + 'shell', + 'sieve', + 'slim', + 'smalltalk', + 'smarty', + 'solr', + 'soy', + 'sparql', + 'spreadsheet', + 'sql', + 'stex', + 'stylus', + 'swift', + 'tcl', + 'textile', + 'tiddlywiki', + 'tiki', + 'toml', + 'tornado', + 'troff', + 'ttcn', + 'ttcn-cfg', + 'turtle', + 'twig', + 'vb', + 'vbscript', + 'velocity', + 'verilog', + 'vhdl', + 'vue', + 'wast', + 'webidl', + 'xml', + 'xquery', + 'yacas', + 'yaml', + 'yaml-frontmatter', + 'z80', + + // Additional language added/supported by us. + 'smartymixed', + ]; + + /** + * Returns the source code language used or `null` if no language is specified. + * + * By default, `null` is returned. + */ + public function getLanguage(): ?string + { + return $this->language; + } + + /** + * Sets the source code language used or unset a previously set language if `null` is given. + * + * @throws \InvalidArgumentException if given language is unsupported + */ + public function language(?string $language): self + { + if (!\in_array($language, self::LANGUAGES)) { + throw new \InvalidArgumentException("Unsupported language '{$language}' given."); + } + + $this->language = $language; + + return $this; + } + + /** + * @inheritDoc + */ + public function readValue() + { + if ($this->getDocument()->hasRequestData($this->getPrefixedId())) { + $this->value = $this->getDocument()->getRequestData($this->getPrefixedId()); + } + } + + /** + * @inheritDoc + */ + public function validate() + { + if ($this->value === null || $this->value === '') { + if ($this->isRequired()) { + $this->addValidationError(new FormFieldValidationError('empty')); + } + } else { + $this->validateMinimumLength($this->value); + $this->validateMaximumLength($this->value); + } + + parent::validate(); + } +} -- 2.20.1