<script nonce={{ csp_nonce() if csp_nonce else "" }}>
    {% set const_name = 'editor_' + target_id | replace('-', '_') %}
    {% if use_htmx %}
    var const_name = htmx.find("#{{ target_id }}")
    {% else %}
    var const_name = document.querySelector("#{{ target_id }}")
    {% endif %}

    {% if enable_watchdog %}
    var watchdog = new CKSource.EditorWatchdog();
    window.watchdog = watchdog;
    watchdog.setCreator((element, config) => {
        return CKSource.Editor
            .create(element, config)
            .then(editor => {
                return editor;
            })
    });
    watchdog.setDestructor(editor => {
        return editor.destroy();
    });
    watchdog.on('error', handleError);
    watchdog
    {% else %}
    {% set editor_class = editor_type.split('-')[0] %}
    { { editor_class | capitalize() + 'Editor' } }
    {% endif %}
            .create(const_name, {
        licenseKey: "{{ license_key or '' }}",
                {% if upload_endpoint %}
    simpleUpload: {
        uploadUrl: '{{ url_for(upload_endpoint) }}',
            withCredentials: true,
                {% if enable_csrf %}
    headers: {
        'X-CSRF-TOKEN': '{{ csrf_token() }}',
                                    }
    {% endif %}
                }
    {% endif %}
            })
    {% if not enable_watchdog %}
            .then(NewEditor => {
        window.editor = NewEditor;
    })
    {% endif %}
            .catch (handleError);

    function handleError(error) {
        console.error('Oops, something went wrong!');
        console.error(error);
    }
</script>