@using System.Text.RegularExpressions @namespace Oqtane.Modules.Controls @inherits ModuleControlBase @inject ISettingService SettingService @inject IStringLocalizer Localizer
@if (AllowRichText) { @if (_richfilemanager) {
}
@if (AllowFileManagement) { } @if (_richfilemanager) { @((MarkupString)"  ") }
@if (ToolbarContent != null) { @ToolbarContent } else { }
} @if (AllowRawHtml) { @if (_rawfilemanager) {
}
@if (AllowFileManagement) { } @if (_rawfilemanager) { @((MarkupString)"  ") }
@if (ReadOnly) { } else { }
}
@code { private ElementReference _editorElement; private ElementReference _toolBar; private bool _richfilemanager = false; private FileManager _fileManager; private string _richhtml = string.Empty; private string _originalrichhtml = string.Empty; private bool _rawfilemanager = false; private string _rawhtml = string.Empty; private string _originalrawhtml = string.Empty; private string _message = string.Empty; private string _activetab = "Rich"; [Parameter] public string Content { get; set; } [Parameter] public bool ReadOnly { get; set; } = false; [Parameter] public string Placeholder { get; set; } = "Enter Your Content..."; [Parameter] public bool AllowFileManagement { get; set; } = true; [Parameter] public bool AllowRichText { get; set; } = true; [Parameter] public bool AllowRawHtml { get; set; } = true; // parameters only applicable to rich text editor [Parameter] public RenderFragment ToolbarContent { get; set; } [Parameter] public string Theme { get; set; } = "snow"; [Parameter] public string DebugLevel { get; set; } = "info"; public override List Resources => new List() { new Resource { ResourceType = ResourceType.Script, Bundle = "Quill", Url = "js/quill.min.js" }, new Resource { ResourceType = ResourceType.Script, Bundle = "Quill", Url = "js/quill-blot-formatter.min.js" }, new Resource { ResourceType = ResourceType.Script, Bundle = "Quill", Url = "js/quill-interop.js" } }; protected override void OnParametersSet() { _richhtml = Content; _rawhtml = Content; _originalrawhtml = _rawhtml; // preserve for comparison later _originalrichhtml = ""; // Quill wraps content in

tags which can be used as a signal to set the active tab if (!string.IsNullOrEmpty(Content) && !Content.StartsWith("

") && AllowRawHtml) { _activetab = "Raw"; } } protected override async Task OnAfterRenderAsync(bool firstRender) { await base.OnAfterRenderAsync(firstRender); if (AllowRichText) { var interop = new RichTextEditorInterop(JSRuntime); if (firstRender) { await interop.CreateEditor( _editorElement, _toolBar, ReadOnly, Placeholder, Theme, DebugLevel); } await interop.LoadEditorContent(_editorElement, _richhtml); if (string.IsNullOrEmpty(_originalrichhtml)) { // preserve a copy of the rich text content (Quill sanitizes content so we need to retrieve it from the editor) _originalrichhtml = await interop.GetHtml(_editorElement); } } } public void CloseRichFileManager() { _richfilemanager = false; _message = string.Empty; StateHasChanged(); } public void CloseRawFileManager() { _rawfilemanager = false; _message = string.Empty; StateHasChanged(); } public async Task GetHtml() { // evaluate raw html content as first priority if (_rawhtml != _originalrawhtml) { return _rawhtml; } else { var richhtml = ""; if (AllowRichText) { // return rich text content if it has changed var interop = new RichTextEditorInterop(JSRuntime); richhtml = await interop.GetHtml(_editorElement); } // rich text value will only be blank if AllowRichText is disabled or the JS Interop method failed if (richhtml != _originalrichhtml && !string.IsNullOrEmpty(richhtml) && !string.IsNullOrEmpty(_originalrichhtml)) { return richhtml; } else { // return original raw html content return _originalrawhtml; } } } public async Task InsertRichImage() { _message = string.Empty; if (_richfilemanager) { var file = _fileManager.GetFile(); if (file != null) { var interop = new RichTextEditorInterop(JSRuntime); await interop.InsertImage(_editorElement, file.Url, ((!string.IsNullOrEmpty(file.Description)) ? file.Description : file.Name)); _richhtml = await interop.GetHtml(_editorElement); _richfilemanager = false; } else { _message = Localizer["Message.Require.Image"]; } } else { _richfilemanager = true; } StateHasChanged(); } public async Task InsertRawImage() { _message = string.Empty; if (_rawfilemanager) { var file = _fileManager.GetFile(); if (file != null) { var interop = new Interop(JSRuntime); int pos = await interop.GetCaretPosition("rawhtmleditor"); var image = "\"""; _rawhtml = _rawhtml.Substring(0, pos) + image + _rawhtml.Substring(pos); _rawfilemanager = false; } else { _message = Localizer["Message.Require.Image"]; } } else { _rawfilemanager = true; } StateHasChanged(); } }