@model string
@inject IPermissionService permissionService
@inject AdminAreaSettings adminAreaSettings
@inject ITinyMceHelper tinyMceHelper
@using Nop.Web.Areas.Admin.Helpers
@using Nop.Services.Security
@{
    // tinyMCE language
    var language = await tinyMceHelper.GetTinyMceLanguageAsync();

    var allowRoxyFileman = await permissionService.AuthorizeAsync(StandardPermissionProvider.HtmlEditorManagePictures);

    var random = CommonHelper.GenerateRandomInteger();

    //extend editor with additional settings
    //Sample setting value (below):
    //settings.plugins.push('print'); settings.toolbar += ' | print';
    var additionalEditorSettings = adminAreaSettings.RichEditorAdditionalSettings;
    //is java-script supported?
    var allowJavaScript = adminAreaSettings.RichEditorAllowJavaScript;

    //allow HTML body? Full page? - http://www.tinymce.com/wiki.php/Plugin:fullpage
    //false by default
    var enableFullPage = Convert.ToBoolean(ViewData["nop.RichEditor.EnableFullPage"] ?? false);

    var extended_valid_elements = new List<string>();
    var valid_children = new List<string>();
    
    if(allowJavaScript)
    {
        extended_valid_elements.Add("script[charset|defer|language|src|type]");
        valid_children.Add("+body[script]");
    }

    if (adminAreaSettings.RichEditorAllowStyleTag)
    {
        extended_valid_elements.Add("style[dir<ltr?rtl|lang|media|title|type]");
        extended_valid_elements.Add("link[dir<ltr?rtl|href|hreflang|lang|media|rel|rev|title|target|type]");

        valid_children.Add("+body[style]");
        valid_children.Add("+body[link]");
    }
}
<script src="~/lib_npm/tinymce/tinymce.min.js" asp-location="Head"></script>

<script>
    $(function() {
        @* wooncherk contribution *@
        function RoxyFileBrowser@(random)(callback, value, type) {
            //we manually generate the configuration file to ensure that it works fine in virtual directory
            $.ajax({
                cache: false,
                type: "GET",
                url: "@Url.Action("CreateConfiguration", "RoxyFileman")",
                success: function (data, textStatus, jqXHR) {
                    var roxyFileman = '@Url.Content("~/lib/Roxy_Fileman/index.html")';
                    roxyFileman += (roxyFileman.indexOf("?") < 0 ? "?" : "&") + "type=" + type.filetype;
                    if (value)
                        roxyFileman += '&value=' + value; // a link to already chosen image if it exists
                    if (tinyMCE.activeEditor.settings.language)
                        roxyFileman += '&langCode=' + tinyMCE.activeEditor.settings.language;
                    const instanceApi = tinyMCE.activeEditor.windowManager.openUrl({
                        title: 'Roxy Fileman',
                        url: roxyFileman,
                        width: 850,
                        height: 650,
                        plugins: "media",
                        onMessage: function(dialogApi, details) {
                            callback(details.content);
                            instanceApi.close();
                        }
                    });
                    return false;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    $('#createConfigurationFailed-info').text(errorThrown);
                    $("#createConfigurationFailed").trigger("click");
                }
            });
        }

        var defaultEditorSettings@(random) = {
            selector: "#@Html.IdForModel()",
            fontsize_formats: "8pt 9pt 10pt 11pt 12pt 26pt 36pt",
            height: 200,
            width: '100%',
            resize: true,
            min_height: 200,
            max_height: '100%',
            plugins: [
                "advlist autolink autoresize directionality lists link image charmap preview anchor",
                "searchreplace visualblocks code fullscreen",
                "insertdatetime media table paste@(enableFullPage ? " fullpage" : null)"
            ],
            toolbar: "ltr rtl | insertfile undo redo | styleselect | fontselect | fontsizeselect | bold italic | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
            relative_urls: false,
            suffix: '.min',
            base_url: '@Url.Content("~/lib_npm/tinymce")',
            @if (allowRoxyFileman)
            {
                <text>
                    //picture manager
                    file_picker_callback: RoxyFileBrowser@(random),
                </text>
            }
            @if (!string.IsNullOrEmpty(language))
            {
                <text>
                    //language
                    language: "@language",
                </text>
            }
            //we should set "convert_urls" set to "false" (because of "relative_urls" requirement)
            //otherwise, it'll break existing links when editing message template
            convert_urls: false,
            //ensure that special chars are not encoded. For example, ä, ö and ü should NOT become "&auml;", "&ouml;" and "&uuml;"
            //Otherwise, database searches become impossible when words contain a character of this kind
            entity_encoding: "named",
            entities: '160,nbsp',
            @if (allowJavaScript)
            {
                <text>
                    allow_script_urls: true,
                </text>
            }
            else
            {
                <text>
                    invalid_elements: "script",
                </text>
            }
            @if (extended_valid_elements.Any())
            {
                <text>
                    extended_valid_elements: "@Html.Raw(string.Join(',', extended_valid_elements))",
                </text>
            }
            @if (valid_children.Any())
            {
                <text>
                    valid_children: "@Html.Raw(string.Join(',', valid_children))"
                </text>
            }
        };

        function getAdditionalEditorSettings@(random)(settings) {
            @Html.Raw(additionalEditorSettings)
            return settings;
        }

        tinyMCE.init(getAdditionalEditorSettings@(random)(defaultEditorSettings@(random)));
    });
</script>

<textarea asp-for="@Model">@ViewData.TemplateInfo.FormattedModelValue</textarea>
<nop-alert asp-alert-id="createConfigurationFailed" />