@namespace Oqtane.Modules.Controls @inherits ModuleControlBase @implements ITextEditor @inject ISettingService SettingService @inject IStringLocalizer Localizer
@if (_allowRichText) { @if (_richfilemanager) {
}
@if (_allowFileManagement) { } @if (_richfilemanager) { @((MarkupString)"  ") }
@if (!string.IsNullOrEmpty(_toolbarContent)) { @((MarkupString)_toolbarContent) } else { }
} @if (_allowRawHtml) { @if (_rawfilemanager) {
}
@if (_allowFileManagement) { } @if (_rawfilemanager) { @((MarkupString)"  ") }
@if (ReadOnly) { } else { }
}
@code { private bool _initialized = false; private QuillEditorInterop interop; private FileManager _fileManager; private string _activetab = "Rich"; private bool _allowFileManagement = false; private bool _allowRawHtml = false; private bool _allowRichText = false; private string _theme = "snow"; private string _debugLevel = "info"; private string _toolbarContent = string.Empty; private bool _settingsLoaded; private ElementReference _editorElement; private ElementReference _toolBar; private bool _richfilemanager = false; private string _richhtml = string.Empty; private string _originalrichhtml = string.Empty; private bool _rawfilemanager = false; private string _rawhtmlid = "RawHtmlEditor_" + Guid.NewGuid().ToString("N"); private string _rawhtml = string.Empty; private string _originalrawhtml = string.Empty; private string _message = string.Empty; private bool _contentchanged = false; private int _editorIndex; [Parameter] public bool ReadOnly { get; set; } [Parameter] public string Placeholder { get; set; } public override List Resources { get; set; } = new List() { new Resource { ResourceType = ResourceType.Script, Bundle = "Quill", Url = "js/quill.min.js", Location = ResourceLocation.Body }, new Resource { ResourceType = ResourceType.Script, Bundle = "Quill", Url = "js/quill-blot-formatter.min.js", Location = ResourceLocation.Body }, new Resource { ResourceType = ResourceType.Script, Bundle = "Quill", Url = "js/quill-interop.js", Location = ResourceLocation.Body }, new Resource { ResourceType = ResourceType.Stylesheet, Url = "css/quill/quill.bubble.css" }, new Resource { ResourceType = ResourceType.Stylesheet, Url = "css/quill/quill.snow.css" } }; protected override async Task OnInitializedAsync() { interop = new QuillEditorInterop(JSRuntime); if (string.IsNullOrEmpty(Placeholder)) { Placeholder = Localizer["Placeholder"]; } await LoadSettings(); } protected override void OnParametersSet() { if (!_allowRichText) { _activetab = "Raw"; } } protected override async Task OnAfterRenderAsync(bool firstRender) { await base.OnAfterRenderAsync(firstRender); if (_allowRichText) { if (_settingsLoaded && !_initialized) { await interop.CreateEditor( _editorElement, _toolBar, ReadOnly, Placeholder, _theme, _debugLevel); await interop.LoadEditorContent(_editorElement, _richhtml); // preserve a copy of the content (Quill sanitizes content so we need to retrieve it from the editor as it may have been modified) _originalrichhtml = await interop.GetHtml(_editorElement); _initialized = true; } else { if (_initialized) { if (_contentchanged) { // reload editor if Content passed to component has changed await interop.LoadEditorContent(_editorElement, _richhtml); _originalrichhtml = await interop.GetHtml(_editorElement); _contentchanged = false; } else { // preserve changed content on re-render event var richhtml = await interop.GetHtml(_editorElement); if (richhtml != _richhtml) { _richhtml = richhtml; await interop.LoadEditorContent(_editorElement, _richhtml); } } } } } } public void Initialize(string content) { _richhtml = content; _rawhtml = content; _originalrichhtml = ""; _richhtml = content; if (!_contentchanged) { _contentchanged = content != _originalrawhtml; } _originalrawhtml = _rawhtml; // preserve for comparison later StateHasChanged(); } public async Task GetContent() { // evaluate raw html content as first priority if (_rawhtml != _originalrawhtml) { return _rawhtml; } else { var richhtml = ""; if (_allowRichText) { richhtml = await interop.GetHtml(_editorElement); } if (richhtml != _originalrichhtml && !string.IsNullOrEmpty(richhtml)) { // convert Quill's empty content to empty string if (richhtml == "


") { richhtml = string.Empty; } return richhtml; } else { // return original raw html content return _originalrawhtml; } } } 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) { richhtml = await interop.GetHtml(_editorElement); } if (richhtml != _originalrichhtml && !string.IsNullOrEmpty(richhtml)) { // convert Quill's empty content to empty string if (richhtml == "


") { richhtml = string.Empty; } 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) { await interop.InsertImage(_editorElement, file.Url, ((!string.IsNullOrEmpty(file.Description)) ? file.Description : file.Name), _editorIndex); _richhtml = await interop.GetHtml(_editorElement); _richfilemanager = false; } else { _message = Localizer["Message.Require.Image"]; } } else { _editorIndex = await interop.GetCurrentCursor(_editorElement); _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(_rawhtmlid); var image = "\"""; _rawhtml = _rawhtml.Substring(0, pos) + image + _rawhtml.Substring(pos); _rawfilemanager = false; } else { _message = Localizer["Message.Require.Image"]; } } else { _rawfilemanager = true; } StateHasChanged(); } private async Task LoadSettings() { try { var settings = await SettingService.GetSiteSettingsAsync(PageState.Site.SiteId); _allowFileManagement = SettingService.GetSetting(settings, "QuillTextEditor_AllowFileManagement", "true") == "true"; _allowRawHtml = SettingService.GetSetting(settings, "QuillTextEditor_AllowRawHtml", "true") == "true"; _allowRichText = SettingService.GetSetting(settings, "QuillTextEditor_AllowRichText", "true") == "true"; _theme = SettingService.GetSetting(settings, "QuillTextEditor_Theme", "snow"); _debugLevel = SettingService.GetSetting(settings, "QuillTextEditor_DebugLevel", "info"); _toolbarContent = SettingService.GetSetting(settings, "QuillTextEditor_ToolbarContent", string.Empty); _settingsLoaded = true; } catch (Exception ex) { AddModuleMessage(ex.Message, MessageType.Error); } } }