Skip to content

Commit

Permalink
🐛 Fix Layout breakout
Browse files Browse the repository at this point in the history
  • Loading branch information
koeeenig committed Nov 19, 2023
1 parent cf6eafa commit 9a56b2f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 53 deletions.
4 changes: 4 additions & 0 deletions src/BlazeKit.Site/Pages/(Public)/Items/Details/Page@.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h3>Details Page</h3>

<p>This site is an example for beaking out pof layouts. The site is located in Pages/(Public)/Items/Details which would imply that the layouts from (Public), Items and Details will be to the page.</p>
<p>Since we named the file Page@@.razor the layout inheritence will be omitted and the site will inherit the layout from Pages/Layout.razor</p>
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
Router.NavigateTo($"/items/details/{Id}", forceLoad: true);
}
}
<p>This route uses nested layouts and a aut-generated route parameter.</p>
<p>This route uses nested layouts and auto-generated route parameter.</p>
<p>The layout for <strong>Pages/(Public)/Items/Details/[Id]/Page.razor</strong> is composed from:</p>
<ul>
<li><i>Pages/(Public)/Items/Details/Layout.razor</i></li>
Expand Down
2 changes: 1 addition & 1 deletion src/BlazeKit.Site/Pages/(Public)/Items/Layout.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@inherits LayoutComponentBase
<h3>Layout in Foo</h3>
<h3>Layout in Items</h3>
@Body
@code {

Expand Down
113 changes: 65 additions & 48 deletions src/BlazeKit/Layout/ClosestLayout.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.IO;
using System.Linq;
Expand All @@ -9,39 +8,71 @@ public sealed class ClosestLayout : Lazy<string>
{
public ClosestLayout(string file, Action<string> log, bool skipSameDirectory = false, string root = "pages") : base(() =>
{
var rootFolder = file.ToLower().Substring(0, file.ToLower().IndexOf(root) + root.Length);
rootFolder = file.Substring(0, rootFolder.Length);
var name = "";
var fi = new FileInfo(file);
if (IsBreakout(fi)) {
log($"The layout file '{fi.Name}' breaks out of the inheritence chain");
// find breakout layout
var breakoutLayout = Path.GetFileNameWithoutExtension(fi.FullName).Split('@')[1];
var layoutPath = SegmentFolder(breakoutLayout, rootFolder);
//var layoutPath = fi.FullName.Substring(0,fi.FullName.IndexOf(breakoutLayout));
if(string.IsNullOrEmpty(layoutPath.Name))
{
try {
var rootFolder = file.ToLower().Substring(0, file.ToLower().IndexOf(root) + root.Length);
rootFolder = file.Substring(0, rootFolder.Length);
var name = "";
var fi = new FileInfo(file);
if (IsBreakout(fi)) {
log($"The Page/Layout '{fi.Name}' breaks out of the inheritence chain");
// find breakout layout
var breakoutLayout = Path.GetFileNameWithoutExtension(fi.FullName).Split('@')[1];
// sreach for layout at root
layoutPath = new FileInfo(file.ToLower().Substring(0, file.ToLower().IndexOf(root) + root.Length));
var layoutPath = new FileInfo(file.ToLower().Substring(0, file.ToLower().IndexOf(root) + root.Length));
layoutPath = new FileInfo(file.Substring(0, layoutPath.FullName.Length));
if(!string.IsNullOrEmpty(breakoutLayout))
{
layoutPath = SegmentFolder(breakoutLayout, rootFolder);
//var layoutPath = fi.FullName.Substring(0,fi.FullName.IndexOf(breakoutLayout));
if(string.IsNullOrEmpty(layoutPath.Name))
{
// sreach for layout at root
layoutPath = new FileInfo(file.ToLower().Substring(0, file.ToLower().IndexOf(root) + root.Length));
layoutPath = new FileInfo(file.Substring(0, layoutPath.FullName.Length));
} else
{
layoutPath = SegmentFolder(breakoutLayout, rootFolder);
}
log($"Searching for breakout layout in: {layoutPath}");
if(HasLayout(layoutPath.DirectoryName))
{
name = LayoutOf(layoutPath.DirectoryName).FullName;
} else
{
log($"Could not find breakout layout in: {layoutPath}");
var dir = fi.Directory;
if (skipSameDirectory && !IsRoot(dir.Name, root))
{
dir = dir.Parent;
}
else if (skipSameDirectory && IsRoot(dir.Name, root))
{
return "";
}
// check if the directory contains a Layout file
while (!HasLayout(dir.FullName))
{
dir = dir.Parent;
}
// we found a layout file
name = LayoutOf(dir.FullName).FullName;
}
} else {
name = LayoutOf(rootFolder).FullName;
}
} else
{
layoutPath = SegmentFolder(breakoutLayout, rootFolder);
}
log($"Searching for breakout layout in: {layoutPath}");
if(HasLayout(layoutPath.DirectoryName))
{
name = LayoutOf(layoutPath.DirectoryName).FullName;
} else
{
log($"Could not find breakout layout in: {layoutPath}");
var dir = fi.Directory;
if (skipSameDirectory && !IsRoot(dir.Name, root))
if(skipSameDirectory && !IsRoot(dir.Name,root))
{
dir = dir.Parent;
}
else if (skipSameDirectory && IsRoot(dir.Name, root))
} else if(skipSameDirectory && IsRoot(dir.Name,root))
{
return "";
}
Expand All @@ -54,31 +85,17 @@ public ClosestLayout(string file, Action<string> log, bool skipSameDirectory = f
// we found a layout file
name = LayoutOf(dir.FullName).FullName;
}
} else
{
var dir = fi.Directory;
if(skipSameDirectory && !IsRoot(dir.Name,root))
{
dir = dir.Parent;
} else if(skipSameDirectory && IsRoot(dir.Name,root))
{
return "";
}
// check if the directory contains a Layout file
while (!HasLayout(dir.FullName))
{
dir = dir.Parent;
}
// we found a layout file
name = LayoutOf(dir.FullName).FullName;
log($"Found layout file: {name}");
//var className = $"{new NamespaceSegments(name,root).Value}.{new SanititizedNamespace(Path.GetFileNameWithoutExtension(name)).Value}";
var className = new SanititizedNamespace($"{new NamespaceSegments(name, root).Value}.{Path.GetFileNameWithoutExtension(name)}").Value;
return className;
} catch(Exception ex)
{
log(ex.ToString());
throw new InvalidOperationException($"Could not find layout for {file}");
}
log($"Found layout file: {name}");
//var className = $"{new NamespaceSegments(name,root).Value}.{new SanititizedNamespace(Path.GetFileNameWithoutExtension(name)).Value}";
var className = new SanititizedNamespace($"{new NamespaceSegments(name, root).Value}.{Path.GetFileNameWithoutExtension(name)}").Value;
return className;
})
{ }

Expand Down
10 changes: 7 additions & 3 deletions src/BlazeKit/NamespaceSegments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ internal class NamespaceSegments : Lazy<string>
{
public NamespaceSegments(string path, string root) : base(() =>
{
var structure = path.Substring(path.ToLower().IndexOf(root));
var segments = structure.Split(new char[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries).Reverse().Skip(1).Reverse();//.Select(s => new SanititizedNamespace(s).Value);
return new SanititizedNamespace(string.Join(".", segments)).Value;
var result = new SanititizedNamespace(root).Value;
if(path.ToLower().IndexOf(root) >= 0) {
var structure = path.Substring(path.ToLower().IndexOf(root));
var segments = structure.Split(new char[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries).Reverse().Skip(1).Reverse();
result = new SanititizedNamespace(string.Join(".", segments)).Value;
}
return result;
})
{ }
}
Expand Down

0 comments on commit 9a56b2f

Please sign in to comment.