Skip to content

Commit

Permalink
rcl, various tag helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
markadrake committed Sep 2, 2023
1 parent 2039b5b commit d9229a2
Show file tree
Hide file tree
Showing 21 changed files with 176 additions and 56 deletions.
6 changes: 3 additions & 3 deletions .testing/Umbraco_10/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
},
"Umbraco.Web.UI": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:44390;http://localhost:37643",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"dotnetRunMessages": true
}
}
}
}
1 change: 1 addition & 0 deletions .testing/Umbraco_10/Umbraco_10.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
<RazorCompileOnBuild>false</RazorCompileOnBuild>
<RazorCompileOnPublish>false</RazorCompileOnPublish>
</PropertyGroup>
<PropertyGroup Condition=" '$(RunConfiguration)' == 'Umbraco.Web.UI' " />
</Project>
6 changes: 3 additions & 3 deletions .testing/Umbraco_12/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
},
"Umbraco.Web.UI": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:44308;http://localhost:39618",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"dotnetRunMessages": true
}
}
}
}
1 change: 1 addition & 0 deletions .testing/Umbraco_12/Umbraco_12.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
<RazorCompileOnPublish>false</RazorCompileOnPublish>
</PropertyGroup>

<PropertyGroup Condition=" '$(RunConfiguration)' == 'Umbraco.Web.UI' " />
</Project>
11 changes: 11 additions & 0 deletions .testing/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Setup a Test Umbraco Instance

1. Install a _specific version_ of `Umbraco.Templates`
2. Scaffold a new project based on these templates.

> The example below references the current LTS version of Umbraco, version 10.
```
dotnet new install Umbraco.Templates::10.0.0
dotnet new umbraco --name Umbraco_10
```
27 changes: 0 additions & 27 deletions Humble.Umbraco.Packages/Humble.RCL/Humble.RCL.csproj

This file was deleted.

11 changes: 11 additions & 0 deletions Humble.Umbraco.Packages/Humble.RCL/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
By default, static assets are made available in the following path:

```
/_content/_NAMESPACE_/_PATH_/_FILE_
```

By setting the following value in your `.csproj`, you can ensure that the path above matches the path in your project:

```
<StaticWebAssetBasePath>/</StaticWebAssetBasePath>
```

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions Humble.Umbraco.Packages/Humble.RCL/wwwroot/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alert("RCL is working!");
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace Humble.Umbraco.UI.TagHelpers;

public class HelloWorldTagHelper : TagHelper
{
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "p";
output.Content.SetContent("Hello, World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Common;
using Umbraco.Extensions;

namespace Humble.Umbraco.UI.TagHelpers;

[HtmlTargetElement("humble-parser", TagStructure = TagStructure.NormalOrSelfClosing)]
public class HumbleParserTagHelper : TagHelper
{
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly UmbracoHelper _umbracoHelper;

public HumbleParserTagHelper(IUmbracoContextAccessor umbracoContextAccessor, UmbracoHelper umbracoHelper)
{
_umbracoContextAccessor = umbracoContextAccessor;
_umbracoHelper = umbracoHelper;
}

public IHtmlEncodedString Content { get; set; }

public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = null;
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
var contentService = umbracoContext.Content;

if (contentService == null)
{
return;
}

IPublishedContent content = _umbracoHelper.AssignedContentItem; // Set this based on your requirements

var replacePattern = new Regex(@"{{([a-zA-Z]+)}}", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
var newContents = Content.ToString();

var matches = replacePattern.Matches(newContents);

foreach (Match match in matches)
{
string key = match.Groups[1].Value;
var typeProperty = content.GetType().GetProperty(key, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);

if (typeProperty != null)
{
newContents = newContents.Replace(match.Value, typeProperty.GetValue(content, null).ToString());
continue;
}

var contentProperty = content.GetProperty(key);
if (contentProperty == null) continue;

var value = contentProperty.GetValue();
if (value == null) continue;

newContents = newContents.Replace(match.Value, value.ToString());
}

output.Content.SetHtmlContent(newContents);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```
<humble-parser
content="@yourHtmlEncodedString" />
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Umbraco.Cms.Core.Models;
using Umbraco.Extensions;

namespace Humble.Umbraco.UI.TagHelpers;

[HtmlTargetElement("humble-picture", TagStructure = TagStructure.NormalOrSelfClosing)]
public class PictureTagHelper : TagHelper
{
public List<MediaWithCrops> MediaWithCropsList { get; set; }

public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var content = await output.GetChildContentAsync();
var mediaSources = new List<string>();

foreach (var mediaWithCrops in MediaWithCropsList)
{
// Determine which breakpoint to use based on the width of the image
string breakpoint = "s";

// Get the URL for the specific breakpoint from the LocalCrops
var cropData = mediaWithCrops.LocalCrops.GetCrop(breakpoint);
var mediaUrl = mediaWithCrops.Url();

// Create a <source> element for each media source
var mediaSource = $"<source media='(min-width: {cropData?.Width}px)' srcset='{mediaUrl}'>";
mediaSources.Add(mediaSource);
}

// Generate the <picture> element
var pictureElement = $"<picture>{string.Join("", mediaSources)}</picture>";
output.TagName = null;
output.Content.SetHtmlContent(pictureElement);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```
<humble-picture
media-with-crops-list="@Model.MediaWithCropsList" />
```
12 changes: 6 additions & 6 deletions Humble.Umbraco.sln
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Umbraco_12", ".testing\Umbr
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Humble.Umbraco.NamingConventions", "Humble.Umbraco.Packages\Humble.Umbraco.NamingConventions\Humble.Umbraco.NamingConventions.csproj", "{7A12A70D-4167-406F-B89E-D496B00ACADE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Humble.RCL", "Humble.Umbraco.Packages\Humble.RCL\Humble.RCL.csproj", "{1B7D7EAA-8632-491B-9588-F430B20BDC24}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Humble.RCL", "Humble.Umbraco.Packages\Humble.RCL\Humble.RCL.csproj", "{D005DB84-F65B-4655-8813-CC1CA4C9E485}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -105,10 +105,10 @@ Global
{7A12A70D-4167-406F-B89E-D496B00ACADE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A12A70D-4167-406F-B89E-D496B00ACADE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A12A70D-4167-406F-B89E-D496B00ACADE}.Release|Any CPU.Build.0 = Release|Any CPU
{1B7D7EAA-8632-491B-9588-F430B20BDC24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1B7D7EAA-8632-491B-9588-F430B20BDC24}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B7D7EAA-8632-491B-9588-F430B20BDC24}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B7D7EAA-8632-491B-9588-F430B20BDC24}.Release|Any CPU.Build.0 = Release|Any CPU
{D005DB84-F65B-4655-8813-CC1CA4C9E485}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D005DB84-F65B-4655-8813-CC1CA4C9E485}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D005DB84-F65B-4655-8813-CC1CA4C9E485}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D005DB84-F65B-4655-8813-CC1CA4C9E485}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -126,7 +126,7 @@ Global
{A3F697D6-0EFF-4E28-BF5B-8257A4E397D0} = {B0AC1BBF-9853-40FA-8A89-90174EA13EA5}
{5B36BA76-E0EF-4520-978F-55E3C13ED916} = {B0AC1BBF-9853-40FA-8A89-90174EA13EA5}
{7A12A70D-4167-406F-B89E-D496B00ACADE} = {46774B56-D02A-4E25-972B-BC0B5443FC14}
{1B7D7EAA-8632-491B-9588-F430B20BDC24} = {46774B56-D02A-4E25-972B-BC0B5443FC14}
{D005DB84-F65B-4655-8813-CC1CA4C9E485} = {46774B56-D02A-4E25-972B-BC0B5443FC14}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F0A5EF84-2D31-4F02-9A33-4FC3B05F11D9}
Expand Down
2 changes: 2 additions & 0 deletions Humble.Umbraco/Humble.Umbraco.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<Import Project="..\Humble.Umbraco.Packages\Humble.Umbraco.RazorBlockPreview\build\Humble.Umbraco.RazorBlockPreview.targets" />
<ItemGroup>
<ProjectReference Include="..\Humble.Umbraco.Packages\Humble.RCL\Humble.RCL.csproj" />
<ProjectReference Include="..\Humble.Umbraco.Packages\Humble.Umbraco.RazorBlockPreview\Humble.Umbraco.RazorBlockPreview.csproj" />
</ItemGroup>

Expand Down Expand Up @@ -84,4 +85,5 @@
<PropertyGroup>
<RazorCompileOnBuild>false</RazorCompileOnBuild>
</PropertyGroup>
<PropertyGroup Condition=" '$(RunConfiguration)' == 'Umbraco.Web.UI.NetCore' " />
</Project>
4 changes: 2 additions & 2 deletions Humble.Umbraco/Parsers/RteParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static IHtmlEncodedString Parse(this IHtmlEncodedString Html, IPublishedC
// Exit: string contents are null or empty
if (Html == null) return Html;

// Exit: no current asigned content item
// Exit: no assigned content item
if (Content == null) return Html;

// Find matches
Expand Down Expand Up @@ -57,4 +57,4 @@ public static IHtmlEncodedString Parse(this IHtmlEncodedString Html, IPublishedC
return new HtmlEncodedString(newContents);
}
}
}
}
6 changes: 3 additions & 3 deletions Humble.Umbraco/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
},
"Umbraco.Web.UI.NetCore": {
"commandName": "Project",
"applicationUrl": "https://localhost:44307;http://localhost:47012",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:44307;http://localhost:47012"
}
}
}
}
}
4 changes: 2 additions & 2 deletions Humble.Umbraco/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void ConfigureServices(IServiceCollection services)
.AddHtmlMinification()
.AddXmlMinification()
.AddHttpCompression();

services
.AddUmbraco(_env, _config)
.AddBackOffice()
Expand Down Expand Up @@ -109,4 +109,4 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
var settings = Model.Settings;
}

@Html.Raw(content.Copy.Parse(Umbraco.AssignedContentItem))
@Html.Raw(content.Copy.Parse(Umbraco.AssignedContentItem))
@*
<humble-parser content="content.Copy"></humble-parser>
*@

0 comments on commit d9229a2

Please sign in to comment.