Merge pull request #2824 from sbwalker/dev

optimize JavaScript handling
This commit is contained in:
Shaun Walker 2023-05-18 14:36:19 -04:00 committed by GitHub
commit 2137b79f6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 84 additions and 123 deletions

View File

@ -1,33 +0,0 @@
@using System.ComponentModel
@using Oqtane.Shared
@inject SiteState SiteState
@((MarkupString)content)
@code {
private string content = "";
protected override void OnInitialized()
{
((INotifyPropertyChanged)SiteState.Properties).PropertyChanged += PropertyChanged;
}
private void PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "BodyContent":
if (content != SiteState.Properties.BodyContent)
{
content = SiteState.Properties.BodyContent;
StateHasChanged();
}
break;
}
}
public void Dispose()
{
((INotifyPropertyChanged)SiteState.Properties).PropertyChanged -= PropertyChanged;
}
}

View File

@ -19,22 +19,30 @@
switch (e.PropertyName) switch (e.PropertyName)
{ {
case "PageTitle": case "PageTitle":
if (title != SiteState.Properties.PageTitle)
{
title = "\n<title>" + SiteState.Properties.PageTitle + "</title>"; title = "\n<title>" + SiteState.Properties.PageTitle + "</title>";
StateHasChanged(); StateHasChanged();
}
break; break;
case "HeadContent": case "HeadContent":
if (content != SiteState.Properties.HeadContent) content = RemoveScripts(SiteState.Properties.HeadContent) + "\n";
{
content = SiteState.Properties.HeadContent + "\n";
StateHasChanged(); StateHasChanged();
}
break; break;
} }
} }
private string RemoveScripts(string headcontent)
{
if (!string.IsNullOrEmpty(headcontent))
{
var index = headcontent.IndexOf("<script");
while (index >= 0)
{
headcontent = headcontent.Remove(index, headcontent.IndexOf("</script>") + 9 - index);
index = headcontent.IndexOf("<script");
}
}
return headcontent;
}
public void Dispose() public void Dispose()
{ {
((INotifyPropertyChanged)SiteState.Properties).PropertyChanged -= PropertyChanged; ((INotifyPropertyChanged)SiteState.Properties).PropertyChanged -= PropertyChanged;

View File

@ -290,18 +290,6 @@ namespace Oqtane.Modules
} }
} }
public void AddBodyContent(string content)
{
if (string.IsNullOrEmpty(SiteState.Properties.BodyContent))
{
SiteState.Properties.BodyContent = content;
}
else if (!SiteState.Properties.BodyContent.Contains(content))
{
SiteState.Properties.BodyContent += content;
}
}
// logging methods // logging methods
public async Task Log(Alias alias, LogLevel level, string function, Exception exception, string message, params object[] args) public async Task Log(Alias alias, LogLevel level, string function, Exception exception, string message, params object[] args)
{ {

View File

@ -16,7 +16,6 @@ using Microsoft.JSInterop;
using Oqtane.Documentation; using Oqtane.Documentation;
using Oqtane.Modules; using Oqtane.Modules;
using Oqtane.Services; using Oqtane.Services;
using Oqtane.Shared;
using Oqtane.UI; using Oqtane.UI;
namespace Oqtane.Client namespace Oqtane.Client

View File

@ -41,11 +41,11 @@
// head content // head content
if (!string.IsNullOrEmpty(PageState.Site.HeadContent)) if (!string.IsNullOrEmpty(PageState.Site.HeadContent))
{ {
headcontent += RemoveScripts(PageState.Site.HeadContent) + "\n"; headcontent += ProcessHeadContent(PageState.Site.HeadContent, "site") + "\n";
} }
if (!string.IsNullOrEmpty(PageState.Page.HeadContent)) if (!string.IsNullOrEmpty(PageState.Page.HeadContent))
{ {
headcontent += RemoveScripts(PageState.Page.HeadContent) + "\n"; headcontent += ProcessHeadContent(PageState.Page.HeadContent, $"page{PageState.Page.PageId}") + "\n";
} }
// stylesheets // stylesheets
foreach (Resource resource in PageState.Page.Resources.Where(item => item.ResourceType == ResourceType.Stylesheet)) foreach (Resource resource in PageState.Page.Resources.Where(item => item.ResourceType == ResourceType.Stylesheet))
@ -55,13 +55,6 @@
} }
SiteState.Properties.HeadContent = headcontent; SiteState.Properties.HeadContent = headcontent;
// set page body content
var bodycontent = "";
if (bodycontent != "")
{
SiteState.Properties.BodyContent = bodycontent;
}
DynamicComponent = builder => DynamicComponent = builder =>
{ {
var themeType = Type.GetType(PageState.Page.ThemeType); var themeType = Type.GetType(PageState.Page.ThemeType);
@ -72,10 +65,9 @@
protected override async Task OnAfterRenderAsync(bool firstRender) protected override async Task OnAfterRenderAsync(bool firstRender)
{ {
if (!firstRender) if (!string.IsNullOrEmpty(PageState.Page.HeadContent) && PageState.Page.HeadContent.Contains("<script"))
{
if (PageState.Page.HeadContent != null && PageState.Page.HeadContent.Contains("<script"))
{ {
// inject scripts into page
var interop = new Interop(JSRuntime); var interop = new Interop(JSRuntime);
var count = 0; var count = 0;
var index = PageState.Page.HeadContent.IndexOf("<script"); var index = PageState.Page.HeadContent.IndexOf("<script");
@ -126,23 +118,31 @@
id = $"page{PageState.Page.PageId}-script{count}"; id = $"page{PageState.Page.PageId}-script{count}";
} }
index = script.IndexOf(">") + 1; index = script.IndexOf(">") + 1;
await interop.IncludeScript(id, "", "", "", script.Substring(index, script.IndexOf("</script>") - index), "head"); await interop.IncludeScript(id, "", "", "", "", script.Substring(index, script.IndexOf("</script>") - index), "head");
} }
index = PageState.Page.HeadContent.IndexOf("<script", index + 1); index = PageState.Page.HeadContent.IndexOf("<script", index + 1);
} }
} }
} }
}
private string RemoveScripts(string headcontent) private string ProcessHeadContent(string headcontent, string id)
{ {
if (headcontent != null) // iterate scripts
if (!string.IsNullOrEmpty(headcontent))
{ {
var count = 0;
var index = headcontent.IndexOf("<script"); var index = headcontent.IndexOf("<script");
while (index >= 0) while (index >= 0)
{ {
headcontent = headcontent.Remove(index, headcontent.IndexOf("</script>") + 9 - index); var script = headcontent.Substring(index, headcontent.IndexOf("</script>", index) + 9 - index);
index = headcontent.IndexOf("<script"); if (!script.Contains("src=") && !script.Contains("id="))
{
count += 1;
id += $"-script{count}";
headcontent = headcontent.Replace(script, script.Replace("<script", $"<script id=\"{id}\""));
index += id.Length;
}
index = headcontent.IndexOf("<script", index + 1);
} }
} }
return headcontent; return headcontent;

View File

@ -55,7 +55,6 @@
@Html.Raw(Model.PWAScript) @Html.Raw(Model.PWAScript)
} }
@Html.Raw(Model.BodyResources) @Html.Raw(Model.BodyResources)
<component type="typeof(Oqtane.Body)" render-mode="@((RenderMode)Enum.Parse(typeof(RenderMode), Model.RenderMode, true))" />
} }
else else
{ {

View File

@ -430,8 +430,8 @@ namespace Oqtane.Pages
private void ProcessHeadContent(string headcontent, string id) private void ProcessHeadContent(string headcontent, string id)
{ {
// iterate scripts // add scripts to page
if (headcontent != null) if (!string.IsNullOrEmpty(headcontent))
{ {
var count = 0; var count = 0;
var index = headcontent.IndexOf("<script"); var index = headcontent.IndexOf("<script");