diff --git a/openspec/changes/add-page-anchor-links/tasks.md b/openspec/changes/add-page-anchor-links/tasks.md
index 394e0ae..a788d2a 100644
--- a/openspec/changes/add-page-anchor-links/tasks.md
+++ b/openspec/changes/add-page-anchor-links/tasks.md
@@ -10,7 +10,7 @@ Design: `design.md`. Every task is TDD. Work is serial in the main session, with
## 2. Portal
- [x] 2.1 Link rules: `isSafeLink`, the rich-text editor's `SAFE_LINK`, a sanitiser test that `#name` hrefs survive; block ids in `CmsBlocks` (pattern-checked).
-- [ ] 2.2 Frame bridge: click listener in the frame script, `frameAnchor()` message check, `HtmlFrame` scroll (own frame only).
+- [x] 2.2 Frame bridge: click listener in the frame script, `frameAnchor()` message check, `HtmlFrame` scroll (own frame only).
- [ ] 2.3 Editor: "Link name" field per block with field errors; label changes in `defaultMessages.ts` and `zh_CN.seed.json`, then `npm run i18n:sql`; `sql/01-anchor-labels.sql` (new labels, plus English and Chinese updates of `PagesBadLink` and `PagesLinkPrompt`) and `sql/99-rollback.sql`.
- Verify (2.1 to 2.3): `cd solvway-admin-portal && npx vitest run; echo VITEST=$? && npx tsc --noEmit && npm run lint`
diff --git a/solvway-admin-portal/app/components/pages/CmsBlocks.html.test.tsx b/solvway-admin-portal/app/components/pages/CmsBlocks.html.test.tsx
index e130051..fbd92cb 100644
--- a/solvway-admin-portal/app/components/pages/CmsBlocks.html.test.tsx
+++ b/solvway-admin-portal/app/components/pages/CmsBlocks.html.test.tsx
@@ -1,5 +1,5 @@
import { act, render, screen } from "@testing-library/react";
-import { describe, it, expect } from "vitest";
+import { describe, it, expect, vi } from "vitest";
import { LandingPage, type PublicBlock } from "./CmsBlocks";
import { FRAME_SANDBOX } from "@/app/lib/frameHtml";
@@ -26,6 +26,25 @@ describe("LandingPage: Custom HTML", () => {
expect(frame()).toHaveAttribute("id", "story");
});
+ it("scrolls the page for a #name link in its own frame: to the frame's target, else to the named block", () => {
+ const scrollTo = vi.spyOn(window, "scrollTo").mockImplementation(() => {});
+ const into = vi.fn();
+ Element.prototype.scrollIntoView = into;
+ render();
+ vi.spyOn(frame(), "getBoundingClientRect").mockReturnValue({ top: 100 } as DOMRect);
+ const ask = (data: unknown, source: MessageEventSource | null) => act(() => { window.dispatchEvent(new MessageEvent("message", { data, source })); });
+ ask({ type: "colite-frame-anchor", name: "signup", y: null }, window);
+ expect(into).not.toHaveBeenCalled();
+ ask({ type: "colite-frame-anchor", name: "signup", y: null }, frame().contentWindow);
+ expect(into).toHaveBeenCalledTimes(1);
+ expect(into.mock.contexts[0]).toBe(document.getElementById("signup"));
+ ask({ type: "colite-frame-anchor", name: "top", y: 300 }, frame().contentWindow);
+ expect(scrollTo).toHaveBeenCalledWith({ top: 400, behavior: "smooth" });
+ ask({ type: "colite-frame-anchor", name: "nowhere", y: null }, frame().contentWindow);
+ expect(into).toHaveBeenCalledTimes(1);
+ expect(scrollTo).toHaveBeenCalledTimes(1);
+ });
+
it("uses the preview's frame address when given", () => {
render();
expect(frame()).toHaveAttribute("src", "/pages/5/preview/frame/0");
diff --git a/solvway-admin-portal/app/components/pages/HtmlFrame.tsx b/solvway-admin-portal/app/components/pages/HtmlFrame.tsx
index 1ca0e79..6ab5533 100644
--- a/solvway-admin-portal/app/components/pages/HtmlFrame.tsx
+++ b/solvway-admin-portal/app/components/pages/HtmlFrame.tsx
@@ -1,16 +1,22 @@
"use client";
import { useEffect, useRef, useState } from "react";
-import { FRAME_SANDBOX, nextFrameHeight, START_HEIGHT, type FrameHeight } from "@/app/lib/frameHtml";
+import { FRAME_SANDBOX, frameAnchor, nextFrameHeight, START_HEIGHT, type FrameHeight } from "@/app/lib/frameHtml";
-/** A Custom HTML block: the sealed frame, sized by height reports from that frame's own window only. */
+/** A Custom HTML block: the sealed frame, sized by height reports and scrolled to by #name requests from that frame's own window only. */
export function HtmlFrame({ id, src, title }: { id?: string; src: string; title: string }) {
const ref = useRef(null);
const [size, setSize] = useState({ height: START_HEIGHT, steps: [] });
useEffect(() => {
const onMessage = (e: MessageEvent) => {
- if (!ref.current || e.source !== ref.current.contentWindow) return;
- setSize((s) => nextFrameHeight(s, e.data));
+ const frame = ref.current;
+ if (!frame || e.source !== frame.contentWindow) return;
+ const anchor = frameAnchor(e.data);
+ if (!anchor) return setSize((s) => nextFrameHeight(s, e.data));
+ // add-page-anchor-links: the frame's own target first, else a named block of this page.
+ if (anchor.y !== null) return window.scrollTo({ top: frame.getBoundingClientRect().top + window.scrollY + anchor.y, behavior: "smooth" });
+ const block = document.getElementById(anchor.name);
+ if (block?.closest("main")) block.scrollIntoView({ behavior: "smooth" });
};
window.addEventListener("message", onMessage);
return () => window.removeEventListener("message", onMessage);
diff --git a/solvway-admin-portal/app/lib/frameHtml.anchor.test.ts b/solvway-admin-portal/app/lib/frameHtml.anchor.test.ts
new file mode 100644
index 0000000..0cbd446
--- /dev/null
+++ b/solvway-admin-portal/app/lib/frameHtml.anchor.test.ts
@@ -0,0 +1,39 @@
+// @vitest-environment jsdom
+import { describe, it, expect, vi, afterEach } from "vitest";
+import { frameDocument } from "./frameHtml";
+
+// add-page-anchor-links: the injected frame script turns clicks on #name links into requests to the page.
+function runFrameScript(body: string) {
+ document.body.innerHTML = body;
+ const code = frameDocument("").match(/`;
+export const FRAME_ANCHOR_MESSAGE = "colite-frame-anchor";
+/** A block link name, as in cmsPages (kept here as a string for the frame script). */
+const ANCHOR_SOURCE = "^[a-z0-9][a-z0-9-]{0,39}$";
+const ANCHOR = new RegExp(ANCHOR_SOURCE);
-/** The pasted HTML with the height script before its last