Skip to content

Commit f369aa4

Browse files
authored
refactor(PageStack): add page-stack js interop (#609)
* wip * πŸ†• feat(PageStack): add page-stack js interop
1 parent 9d1f2e9 commit f369aa4

8 files changed

Lines changed: 77 additions & 2 deletions

File tree

β€Žsrc/Component/BlazorComponent.Web/package.jsonβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"build:resize": "rollup --config rollup.config.resize.ts",
3131
"build:xgplayer": "rollup --config rollup.config.xgplayer.ts",
3232
"build:scrollToTarget": "rollup --config rollup.config.scrollToTargetJSInterop.ts",
33+
"build:pageStack": "rollup --config rollup.config.pageStack.ts",
3334
"build:sortable": "rollup --config rollup.config.sortable.ts",
3435
"build:transition": "rollup --config rollup.config.transition.ts"
3536
},
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { defineConfig } from "rollup";
2+
import { terser } from "rollup-plugin-terser";
3+
4+
import typescript from "@rollup/plugin-typescript";
5+
6+
export default defineConfig({
7+
input: "./src/components/page-stack/index.ts",
8+
output: [
9+
{
10+
file: "../../../../MASA.Blazor/Presets/PageStack/PPageStack.razor.js",
11+
format: "esm",
12+
sourcemap: true,
13+
},
14+
],
15+
plugins: [typescript(), terser()],
16+
watch: {
17+
exclude: "node_modules/**",
18+
},
19+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const dotnetRefs: { [prop: number]: DotNet.DotNetObject } = {};
2+
const clickHandlers: { [prop: number]: (e: MouseEvent) => void } = {};
3+
let nextId = 0;
4+
5+
export function attachListener(handle: DotNet.DotNetObject) {
6+
const id = nextId;
7+
const clickHandler = (event: MouseEvent) => onDocumentClick(id, event);
8+
clickHandlers[id] = clickHandler;
9+
document.addEventListener("click", clickHandler);
10+
dotnetRefs[id] = handle;
11+
return nextId++;
12+
}
13+
14+
async function onDocumentClick(id: number, event: MouseEvent) {
15+
const dotnet = dotnetRefs[id];
16+
if (dotnet === null) return;
17+
18+
const anchor = event.target.closest("a");
19+
if (!anchor) return;
20+
21+
const href = anchor.getAttribute("href");
22+
if (!href) return;
23+
24+
let strategy = anchor.getAttribute("data-page-stack-strategy");
25+
strategy = strategy === null ? null : strategy.toLowerCase();
26+
if (strategy === "" || strategy === "true" || strategy === "push") {
27+
await dotnet.invokeMethodAsync("Push", href);
28+
}
29+
}
30+
31+
export function detachListener(id: number) {
32+
const clickHandler = clickHandlers[id];
33+
document.removeEventListener("click", clickHandler);
34+
35+
dotnetRefs[id] && dotnetRefs[id].dispose();
36+
37+
delete clickHandlers[id];
38+
delete dotnetRefs[id];
39+
}

β€Žsrc/Component/BlazorComponent.Web/src/globals.d.tsβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ declare global {
188188
target: HTMLElement;
189189
}
190190

191+
interface MouseEvent {
192+
target: HTMLElement;
193+
}
194+
191195
interface MbEventTarget {
192196
elementReferenceId?: string;
193197
selector?: string;

β€Žsrc/Component/BlazorComponent.Web/src/interop.tsβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,3 +1533,11 @@ export function removeStopPropagationEvent(el: any, type: keyof HTMLElementEvent
15331533
export function historyBack() {
15341534
history.back();
15351535
}
1536+
1537+
export function historyGo(delta: number) {
1538+
history.go(delta);
1539+
}
1540+
1541+
export function historyReplace(href) {
1542+
history.replaceState(null, /*ignore title*/ '', href);
1543+
}

β€Žsrc/Component/BlazorComponent/JSInterop/JsInteropConstants.csβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,9 @@ public static class JsInteropConstants
153153
public static string RemoveStopPropagationEvent => $"{JsInteropFuncNamePrefix}removeStopPropagationEvent";
154154

155155
public static string HistoryBack => $"{JsInteropFuncNamePrefix}historyBack";
156+
157+
public static string HistoryGo => $"{JsInteropFuncNamePrefix}historyGo";
158+
159+
public static string HistoryReplace => $"{JsInteropFuncNamePrefix}historyReplace";
156160
}
157161
}

β€Žsrc/Component/BlazorComponent/wwwroot/js/blazor-component.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žsrc/Component/BlazorComponent/wwwroot/js/blazor-component.js.mapβ€Ž

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
Β (0)