{% import '_includes/forms.twig' as forms %} {% import 'codeeditor/codeEditor.twig' as codeEditor %} {% set readOnly = readOnly ?? false %} {% set baseMonacoOptions = { tabSize: 2, showUnused: false, } %} {% if readOnly %} {% set baseMonacoOptions = baseMonacoOptions|merge({ domReadOnly: true, readOnly: true }) %} {% endif %} {% set baseCodeEditorOptions = { wrapperClass: 'monaco-editor-background-frame', } %} {% if ckeConfig.uid %} {{ hiddenInput('uid', ckeConfig.uid) }} {% endif %} {{ forms.textField({ first: true, label: 'Name'|t('app'), id: 'name', name: 'name', value: ckeConfig.name, autofocus: true, required: true, errors: ckeConfig.getErrors('name'), data: {'error-key': 'name'}, disabled: readOnly, }) }} {% embed '_includes/forms/field.twig' with { label: 'Toolbar'|t('ckeditor'), instructions: 'Drag toolbar items into the editor.'|t('ckeditor'), id: 'toolbar', ckeConfig: ckeConfig, errors: ckeConfig.getErrors('toolbar'), data: {'error-key': 'toolbar'}, } %} {% block input %}
{{ hiddenInput('toolbar', ckeConfig.toolbar|json_encode) }}
{% endblock %} {% endembed %} {{ forms.checkboxSelectField({ label: 'Heading Levels'|t('ckeditor'), instructions: 'Choose which heading levels should be available to fields.'|t('ckeditor'), id: 'heading-levels', name: 'headingLevels', options: [ {label: 'Heading {level}'|t('ckeditor', {level: 1}), value: '1'}, {label: 'Heading {level}'|t('ckeditor', {level: 2}), value: '2'}, {label: 'Heading {level}'|t('ckeditor', {level: 3}), value: '3'}, {label: 'Heading {level}'|t('ckeditor', {level: 4}), value: '4'}, {label: 'Heading {level}'|t('ckeditor', {level: 5}), value: '5'}, {label: 'Heading {level}'|t('ckeditor', {level: 6}), value: '6'}, ], values: ckeConfig.headingLevels, disabled: readOnly, }) }} {% embed '_includes/forms/field.twig' with { label: 'Config Options'|t('ckeditor'), instructions: 'Define custom [config options]({link}) which should be merged with the default config.'|t('ckeditor', { link: 'https://ckeditor.com/docs/ckeditor5/latest/api/module_core_editor_editorconfig-EditorConfig.html', }), id: 'config-options', ckeConfig: ckeConfig, errors: ckeConfig.getErrors('json')|merge(ckeConfig.getErrors('js')), data: {'error-key': 'json'}, } %} {% block input %} {% import 'codeeditor/codeEditor.twig' as codeEditor %} {% set lang = ckeConfig.js ? 'js' : 'json' %} {% set isJson = lang == 'json' %} {% set isJs = lang == 'js' %}
{{ tag('button', { type: 'button', class: ['btn', 'small', isJson ? 'active']|filter, aria: {pressed: isJson ? 'true'}|filter, data: {language: 'json'}, text: 'JSON', }) }} {{ tag('button', { type: 'button', class: ['btn', 'small', isJs ? 'active' : null]|filter, aria: {pressed: isJs ? 'true'}|filter, data: {language: 'js'}, text: 'JavaScript', }) }}
{% set classes = [] %} {% if not isJson %} {% set classes = classes|merge(['hidden']) %} {% endif %} {% if readOnly %} {% set classes = classes|merge(['disabled']) %} {% endif %} {% tag 'div' with { id: 'config-options-json-container', class: classes|join(' '), } %} {{ codeEditor.textarea( { id: 'config-options-json', name: 'json', value: isJson ? (ckeConfig.json ?? "{\n \n}"), }, 'ckeditor:EditorConfigJson', baseMonacoOptions|merge({ language: 'json', }), baseCodeEditorOptions|push(ckeConfig.hasErrors('json') ? 'has-errors'), ) }} {% endtag %} {% set classes = [] %} {% if not isJs %} {% set classes = classes|merge(['hidden']) %} {% endif %} {% if readOnly %} {% set classes = classes|merge(['disabled']) %} {% endif %} {% tag 'div' with { id: 'config-options-js-container', class: classes|join(' ') } %} {{ codeEditor.textarea( { id: 'config-options-js', name: 'js', value: isJs ? (ckeConfig.js ?? "return {\n \n}"), }, 'ckeditor:EditorConfigJs', baseMonacoOptions|merge({ language: 'javascript', }), baseCodeEditorOptions, ) }} {% endtag %} {% endblock %} {% endembed %} {% js %} (() => { // Register the config options JSON schema const jsonSchemaUri = {{ jsonSchemaUri|json_encode|raw }}; const schema = JSON.parse( JSON.stringify({{ jsonSchema|json_encode|raw }}) .replace( new RegExp(Craft.escapeRegex(JSON.stringify('__PLUGIN_LIST__')), 'g'), JSON.stringify(CKEditor5.craftcms.pluginNames()) ) ); monaco.languages.json.jsonDefaults.setDiagnosticsOptions({ validate: true, schemas: [ { uri: jsonSchemaUri, fileMatch: [jsonSchemaUri], schema: schema, }, ], }); })() {% endjs %} {{ codeEditor.textareaField( { label: 'Custom Styles'|t('ckeditor'), instructions: 'Define CSS styles that should be registered for editors, such as [style classes]({url}).'|t('ckeditor', { url: 'https://ckeditor.com/docs/ckeditor5/latest/features/style.html', }), id: 'css', name: 'css', value: ckeConfig.css, }, 'CodeEditor', baseMonacoOptions|merge({ language: 'css', }), baseCodeEditorOptions|merge({ wrapperClass: readOnly ? 'disabled' : null, }), ) }}