move the editor settings into editor self control.

This commit is contained in:
Ben
2024-07-02 09:50:53 +08:00
parent e00c261777
commit 6701e49f9a
11 changed files with 151 additions and 394 deletions

View File

@ -128,7 +128,7 @@
<div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="textEditorProvider" HelpText="Select the text editor provider for the site" ResourceKey="TextEditorProvider">Text Editor Provider: </Label>
<div class="col-sm-9">
<select id="textEditorProvider" class="form-select" value="@_textEditorProvider" required @onchange="TextEditorProviderChanged">
<select id="textEditorProvider" class="form-select" @bind="@_textEditorProvider" required>
@if (_textEditorProviders != null)
{
@foreach (var provider in _textEditorProviders)
@ -139,14 +139,6 @@
</select>
</div>
</div>
@if (_textEditorProviderSettings != null)
{
<div class="row mb-1 align-items-center">
<div class="col-sm-9 offset-sm-3">
@_textEditorProviderSettings
</div>
</div>
}
</div>
</Section>
<Section Name="FileExtensions" Heading="File Extensions" ResourceKey="FileExtensions">
@ -464,8 +456,6 @@
private string _connectionstring = string.Empty;
private string _textEditorProvider = "";
private IEnumerable<ITextEditorProvider> _textEditorProviders;
private RenderFragment _textEditorProviderSettings;
private ISettingsControl _textEditorProviderSettingsControl;
private string _createdby;
private DateTime _createdon;
private string _modifiedby;
@ -548,7 +538,6 @@
//text editor
_textEditorProvider = SettingService.GetSetting(settings, "TextEditorProvider", Constants.DefaultTextEditorProvider);
LoadTextEditorProviderSettingsControl();
// hosting model
_rendermode = site.RenderMode;
@ -732,11 +721,6 @@
await logger.LogInformation("Site Settings Saved {Site}", site);
if(_textEditorProviderSettingsControl != null)
{
await _textEditorProviderSettingsControl.UpdateSettings();
}
NavigationManager.NavigateTo(NavigateUrl(), true); // reload
}
}
@ -926,35 +910,4 @@
_aliasname = "";
StateHasChanged();
}
private void TextEditorProviderChanged(ChangeEventArgs e)
{
_textEditorProvider = e.Value.ToString();
LoadTextEditorProviderSettingsControl();
StateHasChanged();
}
private void LoadTextEditorProviderSettingsControl()
{
var provider = _textEditorProviders.FirstOrDefault(i => i.EditorType == _textEditorProvider);
var settingsType = provider != null && !string.IsNullOrEmpty(provider.SettingsType) ? Type.GetType(provider.SettingsType) : null;
if (settingsType != null)
{
_textEditorProviderSettings = builder =>
{
builder.OpenComponent(0, settingsType);
builder.AddComponentReferenceCapture(1, (c) =>
{
_textEditorProviderSettingsControl = (ISettingsControl)c;
});
builder.CloseComponent();
};
}
else
{
_textEditorProviderSettings = null;
_textEditorProviderSettingsControl = null;
}
}
}

View File

@ -2,7 +2,9 @@
@inherits ModuleControlBase
@implements ITextEditor
@inject ISettingService SettingService
@inject NavigationManager NavigationManager
@inject IStringLocalizer<QuillJSTextEditor> Localizer
@inject IStringLocalizer<SharedResources> SharedLocalizer
<div class="quill-text-editor">
<TabStrip ActiveTab="@_activetab">
@ -97,15 +99,69 @@
}
</TabPanel>
}
@if (_hasAdminPermission)
{
<TabPanel Name="Settings" Heading="Settings" ResourceKey="Settings" ResourceType="@resourceType">
<div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="AllowFileManagement" ResourceKey="AllowFileManagement" ResourceType="@resourceType" HelpText="Specify If Editors Can Upload and Select Files">Allow File Management: </Label>
<div class="col-sm-9">
<input type="checkbox" id="AllowFileManagement" class="form-check-input" @bind="_allowFileManagementSetting" />
</div>
</div>
<div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="AllowRawHtml" ResourceKey="AllowRawHtml" ResourceType="@resourceType" HelpText="Specify If Editors Can Enter Raw HTML">Allow Raw HTML: </Label>
<div class="col-sm-9">
<input type="checkbox" id="AllowRawHtml" class="form-check-input" @bind="_allowRawHtmlSetting" />
</div>
</div>
<div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="AllowRichText" ResourceKey="AllowRichText" ResourceType="@resourceType" HelpText="Specify If Editors Can Use Rich Text Editor">Allow Rich Text: </Label>
<div class="col-sm-9">
<input type="checkbox" id="AllowRichText" class="form-check-input" @bind="_allowRichTextSetting" />
</div>
</div>
<div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="Theme" ResourceKey="Theme" ResourceType="@resourceType" HelpText="Specify the Rich Text Editor's Theme">Theme: </Label>
<div class="col-sm-9">
<input type="text" id="Theme" class="form-control" @bind="_themeSetting" />
</div>
</div>
<div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="DebugLevel" ResourceKey="DebugLevel" ResourceType="@resourceType" HelpText="Specify the Debug Level">Debug Level: </Label>
<div class="col-sm-9">
<select id="DebugLevel" class="form-select" @bind="_debugLevelSetting">
@foreach (var level in _debugLevels)
{
<option value="@level">@level</option>
}
</select>
</div>
</div>
<div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="ToolbarContent" ResourceKey="ToolbarContent" ResourceType="@resourceType" HelpText="Specify the Toolbar Content">Toolbar Content: </Label>
<div class="col-sm-9">
<textarea id="ToolbarContent" class="form-control" @bind="_toolbarContentSetting" rows="5" />
</div>
</div>
<div class="row mb-1 align-items-center">
<div class="col-sm-9 offset-sm-3">
<button type="button" class="btn btn-success" @onclick="@(async () => await UpdateSettings())">@SharedLocalizer["Save"]</button>
</div>
</div>
</TabPanel>
}
</TabStrip>
</div>
@code {
private string resourceType = "Oqtane.Modules.Controls.QuillJSTextEditor, Oqtane.Client";
private bool _initialized = false;
private QuillEditorInterop interop;
private FileManager _fileManager;
private string _activetab = "Rich";
private bool _hasAdminPermission;
private bool _allowFileManagement = false;
private bool _allowRawHtml = false;
@ -113,6 +169,15 @@
private string _theme = "snow";
private string _debugLevel = "info";
private string _toolbarContent = string.Empty;
//adjust settings variables won't affect UI
private bool _allowFileManagementSetting = false;
private bool _allowRawHtmlSetting = false;
private bool _allowRichTextSetting = false;
private string _themeSetting = "snow";
private string _debugLevelSetting = "info";
private string _toolbarContentSetting = string.Empty;
private bool _settingsLoaded;
private ElementReference _editorElement;
@ -130,6 +195,8 @@
private bool _contentchanged = false;
private int _editorIndex;
private List<string> _debugLevels = new List<string> { "info", "log", "warn", "error" };
[Parameter]
public bool ReadOnly { get; set; }
@ -140,9 +207,7 @@
{
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" }
new Resource { ResourceType = ResourceType.Script, Bundle = "Quill", Url = "js/quill-interop.js", Location = ResourceLocation.Body }
};
protected override async Task OnInitializedAsync()
@ -367,12 +432,14 @@
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);
_allowFileManagementSetting = _allowFileManagement = SettingService.GetSetting(settings, "QuillTextEditor_AllowFileManagement", "true") == "true";
_allowRawHtmlSetting = _allowRawHtml = SettingService.GetSetting(settings, "QuillTextEditor_AllowRawHtml", "true") == "true";
_allowRichTextSetting = _allowRichText = SettingService.GetSetting(settings, "QuillTextEditor_AllowRichText", "true") == "true";
_themeSetting = _theme = SettingService.GetSetting(settings, "QuillTextEditor_Theme", "snow");
_debugLevelSetting = _debugLevel = SettingService.GetSetting(settings, "QuillTextEditor_DebugLevel", "info");
_toolbarContentSetting = _toolbarContent = SettingService.GetSetting(settings, "QuillTextEditor_ToolbarContent", string.Empty);
_hasAdminPermission = UserSecurity.IsAuthorized(PageState.User, RoleNames.Admin);
_settingsLoaded = true;
}
@ -381,4 +448,25 @@
AddModuleMessage(ex.Message, MessageType.Error);
}
}
private async Task UpdateSettings()
{
try
{
var settings = await SettingService.GetSiteSettingsAsync(PageState.Site.SiteId);
settings = SettingService.SetSetting(settings, "QuillTextEditor_AllowFileManagement", _allowFileManagementSetting.ToString().ToLower());
settings = SettingService.SetSetting(settings, "QuillTextEditor_AllowRawHtml", _allowRawHtmlSetting.ToString().ToLower());
settings = SettingService.SetSetting(settings, "QuillTextEditor_AllowRichText", _allowRichTextSetting.ToString().ToLower());
settings = SettingService.SetSetting(settings, "QuillTextEditor_Theme", _themeSetting);
settings = SettingService.SetSetting(settings, "QuillTextEditor_DebugLevel", _debugLevelSetting);
settings = SettingService.SetSetting(settings, "QuillTextEditor_ToolbarContent", _toolbarContentSetting);
await SettingService.UpdateSiteSettingsAsync(settings, PageState.Site.SiteId);
NavigationManager.NavigateTo(NavigationManager.Uri);
}
catch (Exception ex)
{
AddModuleMessage(ex.Message, MessageType.Error);
}
}
}

View File

@ -1,99 +0,0 @@
@namespace Oqtane.Modules.Controls
@inherits ModuleBase
@inject ISettingService SettingService
@implements Oqtane.Interfaces.ISettingsControl
@inject IStringLocalizer<QuillJSTextEditorSettings> Localizer
@inject IStringLocalizer<SharedResources> SharedLocalizer
<div class="container">
<div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="AllowFileManagement" ResourceKey="AllowFileManagement" ResourceType="@resourceType" HelpText="Specify If Editors Can Upload and Select Files">Allow File Management: </Label>
<div class="col-sm-9">
<input type="checkbox" id="AllowFileManagement" class="form-check-input" @bind="_allowFileManagement" />
</div>
</div>
<div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="AllowRawHtml" ResourceKey="AllowRawHtml" ResourceType="@resourceType" HelpText="Specify If Editors Can Enter Raw HTML">Allow Raw HTML: </Label>
<div class="col-sm-9">
<input type="checkbox" id="AllowRawHtml" class="form-check-input" @bind="_allowRawHtml" />
</div>
</div>
<div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="AllowRichText" ResourceKey="AllowRichText" ResourceType="@resourceType" HelpText="Specify If Editors Can Use Rich Text Editor">Allow Rich Text: </Label>
<div class="col-sm-9">
<input type="checkbox" id="AllowRichText" class="form-check-input" @bind="_allowRichText" />
</div>
</div>
<div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="Theme" ResourceKey="Theme" ResourceType="@resourceType" HelpText="Specify the Rich Text Editor's Theme">Theme: </Label>
<div class="col-sm-9">
<input type="text" id="Theme" class="form-control" @bind="_theme" />
</div>
</div>
<div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="DebugLevel" ResourceKey="DebugLevel" ResourceType="@resourceType" HelpText="Specify the Debug Level">Debug Level: </Label>
<div class="col-sm-9">
<select id="DebugLevel" class="form-select" @bind="_debugLevel">
@foreach (var level in _debugLevels)
{
<option value="@level">@level</option>
}
</select>
</div>
</div>
<div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="ToolbarContent" ResourceKey="ToolbarContent" ResourceType="@resourceType" HelpText="Specify the Toolbar Content">Toolbar Content: </Label>
<div class="col-sm-9">
<textarea id="ToolbarContent" class="form-control" @bind="_toolbarContent" rows="5" />
</div>
</div>
</div>
@code {
private string resourceType = "Oqtane.Modules.Controls.QuillJSTextEditorSettings, Oqtane.Client";
private bool _allowFileManagement;
private bool _allowRawHtml;
private bool _allowRichText;
private string _theme;
private string _debugLevel;
private string _toolbarContent;
private List<string> _debugLevels = new List<string> { "info", "log", "warn", "error" };
protected override async Task OnInitializedAsync()
{
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);
}
catch (Exception ex)
{
AddModuleMessage(ex.Message, MessageType.Error);
}
}
public async Task UpdateSettings()
{
try
{
var settings = await SettingService.GetSiteSettingsAsync(PageState.Site.SiteId);
settings = SettingService.SetSetting(settings, "QuillTextEditor_AllowFileManagement", _allowFileManagement.ToString().ToLower());
settings = SettingService.SetSetting(settings, "QuillTextEditor_AllowRawHtml", _allowRawHtml.ToString().ToLower());
settings = SettingService.SetSetting(settings, "QuillTextEditor_AllowRichText", _allowRichText.ToString().ToLower());
settings = SettingService.SetSetting(settings, "QuillTextEditor_Theme", _theme);
settings = SettingService.SetSetting(settings, "QuillTextEditor_DebugLevel", _debugLevel);
settings = SettingService.SetSetting(settings, "QuillTextEditor_ToolbarContent", _toolbarContent);
await SettingService.UpdateSiteSettingsAsync(settings, PageState.Site.SiteId);
}
catch (Exception ex)
{
AddModuleMessage(ex.Message, MessageType.Error);
}
}
}

View File

@ -4,6 +4,7 @@
@namespace Oqtane.Modules.Controls
@inherits ModuleControlBase
@inject IServiceProvider ServiceProvider
@inject ISettingService SettingService
@inject IStringLocalizer<RichTextEditor> Localizer
<div class="row" style="margin-bottom: 50px;">
@ -34,7 +35,7 @@
protected override async Task OnInitializedAsync()
{
_textEditorProvider = await GetTextEditorType(ServiceProvider, PageState.Site.SiteId);
_textEditorProvider = await GetTextEditorType();
}
protected override void OnParametersSet()
@ -55,22 +56,6 @@
await base.OnAfterRenderAsync(firstRender);
}
public override async Task<List<Resource>> GetResources(IServiceProvider serviceProvider, Page page)
{
var type = await GetTextEditorType(serviceProvider, page.SiteId);
if (!string.IsNullOrEmpty(type))
{
var editorType = Type.GetType(type);
if (editorType != null && editorType.GetInterfaces().Contains(typeof(IModuleControl)))
{
var control = Activator.CreateInstance(editorType) as IModuleControl;
return await control?.GetResources(serviceProvider, page);
}
}
return await base.GetResources(serviceProvider, page);
}
public async Task<string> GetHtml()
{
return await _textEditor.GetContent();
@ -124,21 +109,20 @@
}
}
private async Task<string> GetTextEditorType(IServiceProvider serviceProvider, int siteId)
private async Task<string> GetTextEditorType()
{
const string EditorSettingName = "TextEditorProvider";
if(!string.IsNullOrEmpty(Provider))
{
var provider = serviceProvider.GetServices<ITextEditorProvider>().FirstOrDefault(i => i.Name.Equals(Provider, StringComparison.OrdinalIgnoreCase));
var provider = ServiceProvider.GetServices<ITextEditorProvider>().FirstOrDefault(i => i.Name.Equals(Provider, StringComparison.OrdinalIgnoreCase));
if(provider != null)
{
return provider.EditorType;
}
}
var settingService = serviceProvider.GetService<ISettingService>();
var settings = await settingService.GetSiteSettingsAsync(siteId);
return settingService.GetSetting(settings, EditorSettingName, Constants.DefaultTextEditorProvider);
var settings = await SettingService.GetSiteSettingsAsync(PageState.Site.SiteId);
return SettingService.GetSetting(settings, EditorSettingName, Constants.DefaultTextEditorProvider);
}
}

View File

@ -51,6 +51,12 @@
public override string Title => "Edit Html/Text";
public override List<Resource> Resources => new List<Resource>()
{
new Resource { ResourceType = ResourceType.Stylesheet, Url = "css/quill/quill.bubble.css" },
new Resource { ResourceType = ResourceType.Stylesheet, Url = "css/quill/quill.snow.css" }
};
private RichTextEditor RichTextEditorHtml;
private string _content = null;
private string _createdby;
@ -60,8 +66,6 @@
private List<Models.HtmlText> _htmltexts;
private string _view = "";
public override List<string> ResourcesRegistrationTypes => new List<string> { typeof(RichTextEditor).FullName };
protected override async Task OnInitializedAsync()
{
try

View File

@ -50,8 +50,6 @@ namespace Oqtane.Modules
public virtual List<Resource> Resources { get; set; }
public virtual List<string> ResourcesRegistrationTypes { get; }
public virtual string RenderMode { get { return RenderModes.Interactive; } } // interactive by default
public virtual bool? Prerender { get { return null; } } // allows the Site Prerender property to be overridden
@ -73,21 +71,6 @@ namespace Oqtane.Modules
// base lifecycle method for handling JSInterop script registration
public virtual async Task<List<Resource>> GetResources(IServiceProvider serviceProvider, Page page)
{
var resources = Resources ?? new List<Resource>();
if(ResourcesRegistrationTypes != null)
{
foreach(var type in ResourcesRegistrationTypes)
{
resources.AddRange(await LoadResourcesFromType(serviceProvider, page, type));
}
}
return resources;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
@ -499,25 +482,6 @@ namespace Oqtane.Modules
}
}
private async Task<List<Resource>> LoadResourcesFromType(IServiceProvider serviceProvider, Page page, string typeName)
{
try
{
var type = Type.GetType(typeName, false, true);
if (type != null && type.GetInterfaces().Contains(typeof(IModuleControl)))
{
var control = Activator.CreateInstance(type) as IModuleControl;
return await control?.GetResources(serviceProvider, page);
}
}
catch(Exception ex)
{
//await Log(null, LogLevel.Error, string.Empty, ex, "Load Resources From Type {Type} Failed. {Message}", typeName, ex.Message);
}
return null;
}
[Obsolete("ContentUrl(int fileId) is deprecated. Use FileUrl(int fileId) instead.", false)]
public string ContentUrl(int fileid)
{