{% if enableTinyMce|default %}
    <script>
        {
            const config = {
                min_height: 336,
                language: '{{ tinyMceLanguage|e('js') }}',
                element_format: 'html',
                document_base_url: '{{ (app.request.schemeAndHttpHost ~ app.request.basePath ~ '/')|e('js') }}',
                entities: '160,nbsp,60,lt,62,gt,173,shy',
                license_key: 'gpl',
                branding: false,
                promotion: false,
                skin: (document.documentElement.dataset.colorScheme === 'dark' ? 'oxide-dark' : 'oxide'),
                setup: function (editor) {
                    editor.getElement().removeAttribute('required');
                    document.querySelectorAll('[accesskey]').forEach(function (el) {
                        editor.addShortcut('access+' + el.accessKey, el.id, () => el.click());
                    });
                },
                init_instance_callback: function (editor) {
                    if (document.activeElement && document.activeElement.id && document.activeElement.id == editor.id) {
                        editor.editorManager.get(editor.id).focus();
                    }
                    editor.on('focus', () => window.dispatchEvent(new Event('store-scroll-offset')));
                },

                {% block picker %}
                file_picker_callback: function (callback, value, meta) {
                    Backend.openModalSelector({
                        'id': 'tl_listing',
                        'title': document.getElement('.tox-dialog__title').get('text'),
                        'url': Contao.routes.backend_picker + '?context=' + (meta.filetype == 'file' ? 'link' : 'file') + '&amp;extras[fieldType]=radio&amp;extras[filesOnly]=true&amp;extras[source]={{ source|e('js') }}&amp;value=' + value + '&amp;popup=1',
                        'callback': function (table, val) {
                            document.getElement('.tox-dialog input')?.focus();
                            callback(val.join(','));
                        }
                    });
                },
                file_picker_types: '{{ fileBrowserTypes|e('js') }}',
                {% endblock %}

                {% block content_css %}
                content_css: document.documentElement.dataset.colorScheme === 'dark' ? '{{ asset('tinymce-dark.css', 'system/themes/' ~ theme)|e('js') }}' : '{{ asset('tinymce.css', 'system/themes/' ~ theme)|e('js') }}',
                {% endblock %}

                {% block plugins %}
                plugins: 'autosave charmap code fullscreen help image importcss link lists searchreplace stripnbsp table visualblocks visualchars',
                {% endblock %}

                {% block valid_elements %}
                extended_valid_elements: 'q[cite|class|title],article,section,hgroup,figure,figcaption',
                {% endblock %}

                {% block menubar %}
                menubar: 'file edit insert view format table help',
                {% endblock %}

                {% block toolbar %}
                toolbar: 'link unlink | image | blocks | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | code',
                {% endblock %}

                {% block contextmenu %}
                contextmenu: false,
                {% endblock %}

                {% block paste_data_images %}
                paste_data_images: false,
                {% endblock %}

                {% block cache_suffix %}
                cache_suffix: '?v={{ asset_version('js/tinymce.min.js', 'contao-components/tinymce4')|e('js') }}',
                {% endblock %}

                {% block custom %}
                {% endblock %}

                {% if readonly|default %}
                readonly: true,
                {% endif %}

                browser_spellcheck: true,
                importcss_append: true
            };

            // Add Stimulus data and controller reference
            const target = document.querySelector('#{{ selector|e('js') }}');
            target.tinymceConfig = config;
            target.setAttribute('data-action', 'turbo:before-visit@window->contao--tinymce#leave');
            target.setAttribute('data-controller', `${target.getAttribute('data-controller') || ''} contao--tinymce`.trim());
        }
    </script>
{% endif %}
