Skip to content

Commit

Permalink
🆕 feat(PageStack): add support for going back with specific delta and…
Browse files Browse the repository at this point in the history
… then replace with new uri (#2096)

* 🆕 feat(PageStack): add support for going back with delta and replace

* the replace method doesn't need debouncing
  • Loading branch information
capdiem committed Aug 12, 2024
1 parent 39193ec commit f6e8721
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 42 deletions.
20 changes: 11 additions & 9 deletions docs/Masa.Blazor.Docs/wwwroot/pages/labs/page-stack/en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ related:

Used to control the navigation of the page stack.

| Method name | Description |
|----------------|-----------------------------------------------------------------|
| `Push` | Push a new page onto the stack. |
| `Pop` | Pop the current page from the stack, equivalent to `GoBack(1)`. |
| `GoBack` | Return to the specified number of pages. |
| `GoBackToPage` | Return to the specified page. |
| `Replace` | Replace the current page. |
| `Clear` | Clear the current page stack. |
| `GoBackToTab` | Clear the current stack and jump to the specified tab. |
| Method name | Description |
|--------------------------|--------------------------------------------------------------------------|
| `Push` | Push a new page onto the stack. |
| `Pop` | Pop the current page from the stack, equivalent to `GoBack(1)`. |
| `GoBack` | Return to the specified number of pages. |
| `GoBackAndReplace` | Return to the specified number of pages, and the invoke `Replace` method.|
| `GoBackToPage` | Return to the specified page. |
| `GoBackToPageAndReplace` | Return to the specified page, and then invoke `Replace` method. |
| `Replace` | Replace the current page. |
| `Clear` | Clear the current page stack. |
| `GoBackToTab` | Clear the current stack and jump to the specified tab. |

```razor
@inject PageStackNavController NavController
Expand Down
20 changes: 11 additions & 9 deletions docs/Masa.Blazor.Docs/wwwroot/pages/labs/page-stack/zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ related:

用于控制页面堆栈的导航。

| 方法名 | 说明 |
|-----------|----------------------------|
| `Push` | 将新页面推入堆栈。 |
| `Pop` | 将当前页面弹出堆栈,同等于 `GoBack(1)`|
| `GoBack` | 返回到指定的前几个页面。 |
| `GoBackToPage` | 返回到指定页面。 |
| `Replace` | 替换当前页面。 |
| `Clear` | 清空当前页面堆栈。 |
| `GoBackToTab` | 清空当前堆栈并跳转到指定选项卡。 |
| 方法名 | 说明 |
|--------------------------|--------------------------------------------------------------------------|
| `Push` | 将新页面推入堆栈。 |
| `Pop` | 将当前页面弹出堆栈,同等于 `GoBack(1)`|
| `GoBack` | 返回到指定的前第几个页面。 |
| `GoBackAndReplace` | 返回到指定的前第几个页面后调用`Replace`方法。 |
| `GoBackToPage` | 返回到指定页面。 |
| `GoBackToPageAndRepalce` | 返回到指定页面后调用`Replace`方法。 |
| `Replace` | 替换当前页面。 |
| `Clear` | 清空当前页面堆栈。 |
| `GoBackToTab` | 清空当前堆栈并跳转到指定选项卡。 |

```razor
@inject PageStackNavController NavController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Masa.Blazor.Presets;

public class PageStackNavController(IJSRuntime jsRuntime)
public class PageStackNavController()
{
/// <summary>
/// Records the timestamp of the last action, shared by all actions.
Expand Down Expand Up @@ -64,17 +64,25 @@ public void Pop(object? state = null)
/// <param name="state"></param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public void GoBack(int delta = 1, object? state = null)
{
GoBackAndReplace(delta, null, state);
}

/// <summary>
/// Go back to specified number of steps in the page stack,
/// and then invoke the <see cref="Replace(string, object?)"/> method.
/// </summary>
/// <param name="delta"></param>
/// <param name="replaceUri"></param>
/// <param name="state"></param>
public void GoBackAndReplace(int delta, string? replaceUri, object? state = null)
{
if (delta < 1)
{
throw new ArgumentOutOfRangeException(nameof(delta), "The delta must be greater than or equal to 1.");
}

ExecuteIfTimeElapsed(() =>
{
StackPop?.Invoke(this, new PageStackPopEventArgs(delta, state));
_ = jsRuntime.InvokeVoidAsync(JsInteropConstants.HistoryGo, -delta);
});
ExecuteIfTimeElapsed(() => StackPop?.Invoke(this, new PageStackPopEventArgs(delta, replaceUri, state)));
}

/// <summary>
Expand All @@ -94,7 +102,7 @@ public void GoBackToPage(string absolutePath, object? state = null)
/// <param name="absolutePath"></param>
/// <param name="replaceUri"></param>
/// <param name="state"></param>
public void GoBackToPage(string absolutePath, string replaceUri, object? state = null)
public void GoBackToPageAndReplace(string absolutePath, string replaceUri, object? state = null)
{
ExecuteIfTimeElapsed(() => StackGoBackTo?.Invoke(this, new PageStackGoBackToPageEventArgs(absolutePath, state, replaceUri)));
}
Expand All @@ -106,7 +114,7 @@ public void GoBackToPage(string absolutePath, string replaceUri, object? state =
/// <param name="state"></param>
public void Replace(string relativeUri, object? state = null)
{
ExecuteIfTimeElapsed(() => StackReplace?.Invoke(this, new PageStackReplaceEventArgs(relativeUri, state)));
StackReplace?.Invoke(this, new PageStackReplaceEventArgs(relativeUri, state));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,8 @@ namespace Masa.Blazor.Presets.PageStack.NavController;

public class PageStackNavControllerFactory : IPageStackNavControllerFactory
{
private readonly IJSRuntime _jsRuntime;

internal readonly ConcurrentDictionary<string, Lazy<PageStackNavController>> _managers = new();

public PageStackNavControllerFactory(IJSRuntime jsRuntime)
{
_jsRuntime = jsRuntime;
}
private readonly ConcurrentDictionary<string, Lazy<PageStackNavController>> _managers = new();

public PageStackNavController Create(string name)
{
return _managers.GetOrAdd(name,
_ => new Lazy<PageStackNavController>(() => new PageStackNavController(_jsRuntime)))
.Value;
}
=> _managers.GetOrAdd(name, _ => new Lazy<PageStackNavController>(() => new PageStackNavController())).Value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ namespace Masa.Blazor.Presets.PageStack.NavController;

public class PageStackPopEventArgs : EventArgs
{
public PageStackPopEventArgs(int delta, object? state = null)
public PageStackPopEventArgs(int delta, string? replaceUri = null, object? state = null)
{
if (delta < 1)
{
throw new ArgumentOutOfRangeException(nameof(delta), "The delta must be greater than or equal to 1.");
}

Delta = delta;
ReplaceUri = replaceUri;
State = state;
}

public int Delta { get; }

public string? ReplaceUri { get; }

public object? State { get; }
}
12 changes: 10 additions & 2 deletions src/Masa.Blazor/Presets/PageStack/PPageStack.razor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Masa.Blazor.Presets.PageStack;
using Masa.Blazor.Presets.PageStack.NavController;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;

namespace Masa.Blazor.Presets;

Expand Down Expand Up @@ -148,11 +149,18 @@ private void InternalReplaceHandler(string relativeUri, object? state)
NavigationManager.Replace(relativeUri);
}

private void InternalPageStackNavManagerOnStackPop(object? sender, PageStackPopEventArgs e)
private async void InternalPageStackNavManagerOnStackPop(object? sender, PageStackPopEventArgs e)
{
_popstateByUserAction = true;

CloseTopPages(e.Delta, e.State);
await Js.InvokeVoidAsync(JsInteropConstants.HistoryGo, -e.Delta);

if (!string.IsNullOrWhiteSpace(e.ReplaceUri))
{
await Task.Delay(DelayForPageClosingAnimation);
InternalReplaceHandler(e.ReplaceUri, e.State);
}
}

private void InternalStackStackNavManagerOnStackPush(object? sender, PageStackPushEventArgs e)
Expand Down Expand Up @@ -194,7 +202,7 @@ private async void InternalPageStackNavManagerOnStackGoBackTo(object? sender, Pa

CloseTopPages(delta, e.State);

if (e.ReplaceUri is not null)
if (!string.IsNullOrWhiteSpace(e.ReplaceUri))
{
await Task.Delay(DelayForPageClosingAnimation);
InternalReplaceHandler(e.ReplaceUri, e.State);
Expand Down

0 comments on commit f6e8721

Please sign in to comment.