@if (!defined('LOAD_TINYMCE'))
    @push('js')
        <script src="/assets/plugins/tinymce/jquery.tinymce.min.js"></script>
        <script src="/assets/plugins/tinymce/tinymce.min.js"></script>
        @component('boilerplate::minify')
            <script>
                tinymce.defaultSettings = {
                    path_absolute: "/",
                    plugins: "autolink autoresize fullscreen codemirror link lists table media preview image paste customalign",
                    toolbar: "insertfile undo redo | styleselect | bold italic underline | customalignleft aligncenter customalignright | link media image | bullist numlist | table | code fullscreen",
                    contextmenu: "link image imagetools table spellchecker bold italic underline",
                    sticky_toolbar_container: '.tox-editor-header',
                    toolbar_drawer: "sliding",
                    sticky_offset: $('nav.main-header').outerHeight(),
                    codemirror: {
                        config: {
                            theme: 'storm'
                        }
                    },
                    menubar: false,
                    removed_menuitems: 'newdocument',
                    remove_linebreaks: false,
                    forced_root_block: false,
                    force_p_newlines: true,
                    relative_urls: false,
                    verify_html: false,
                    branding: false,
                    statusbar: false,
                    browser_spellcheck: true,
                    encoding: 'UTF-8',
                    image_uploadtab: false,
                    paste_preprocess: function(plugin, args) {
                        args.content = args.content.replace(
                            /<(\/)*(\\?xml:|meta|link|span|font|del|ins|st1:|[ovwxp]:)((.|\s)*?)>/gi, ''); // Unwanted tags
                        args.content = args.content.replace(/\s(class|style|type|start)=("(.*?)"|(\w*))/gi,
                            ''); // Unwanted attributes
                        args.content = args.content.replace(
                            /<(p|a|div|span|strike|strong|i|u)[^>]*?>(\s|&nbsp;|<br\/>|\r|\n)*?<\/(p|a|div|span|strike|strong|i|u)>/gi,
                            ''); // Empty tags
                    },
                    skin: "oxide",
                    content_css: 'default',
                    language: '{{ App::getLocale() }}',
                    file_picker_callback: function(callback, value, meta) {
                        var x = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName(
                            'body')[0].clientWidth;
                        var y = window.innerHeight || document.documentElement.clientHeight || document
                            .getElementsByTagName('body')[0].clientHeight;

                        var cmsURL = tinymce.defaultSettings.path_absolute + 'filemanager?editor=' + meta.fieldname;
                        if (meta.filetype == 'image') {
                            cmsURL = cmsURL + "&type=Images";
                        } else {
                            cmsURL = cmsURL + "&type=Files";
                        }

                        tinyMCE.activeEditor.windowManager.openUrl({
                            url: cmsURL,
                            title: 'Filemanager',
                            width: x * 0.8,
                            height: y * 0.8,
                            resizable: "yes",
                            close_previous: "no",
                            onMessage: (api, message) => {
                                callback(message.content);
                            }
                        });
                    }
                };

                // Prevent Bootstrap dialog from blocking focusin
                $(document).on('focusin', function(e) {
                    if ($(e.target).closest(".tox-tinymce, .tox-tinymce-aux, .moxman-window, .tam-assetmanager-root")
                        .length) {
                        e.stopImmediatePropagation();
                    }
                });

                function initEditor(sel, options) {
                    var selector = (typeof(sel) == 'undefined') ? '.editor' : sel;
                    var options = (typeof(options) == 'undefined') ? {} : options;
                    var setup = {
                        setup: function(ed) {
                            if ($('#' + ed.id).prop('readonly')) {
                                ed.settings.readonly = true;
                            }
                        }
                    };
                    options = Object.assign(options, setup);

                    for (var i = tinymce.editors.length - 1; i > -1; i--) {
                        tinyMCE.execCommand("mceRemoveEditor", true, tinymce.editors[i].id);
                    }
                    $(selector).tinymce(options);
                }
            </script>
        @endcomponent
    @endpush
    @php(define('LOAD_TINYMCE', true))
@endif
