diff --git a/local/seed/04-zh-cn-messages.sql b/local/seed/04-zh-cn-messages.sql
index b182294..234fc90 100644
--- a/local/seed/04-zh-cn-messages.sql
+++ b/local/seed/04-zh-cn-messages.sql
@@ -1497,6 +1497,8 @@ INSERT INTO message (code, content, area) SELECT 'PagesLeaveMessage', JSON_OBJEC
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '此页面的修改尚未保存。离开将丢失这些修改,确定离开吗?') WHERE LOWER(code) = LOWER('PagesLeaveMessage') AND area = 'A';
INSERT INTO message (code, content, area) SELECT 'PagesLeave', JSON_OBJECT('en_US', 'Leave', 'zh_CN', '离开'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesLeave') AND area = 'A');
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '离开') WHERE LOWER(code) = LOWER('PagesLeave') AND area = 'A';
+INSERT INTO message (code, content, area) SELECT 'PagesSaveToUpdate', JSON_OBJECT('en_US', 'Save to update', 'zh_CN', '保存后更新'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesSaveToUpdate') AND area = 'A');
+UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '保存后更新') WHERE LOWER(code) = LOWER('PagesSaveToUpdate') AND area = 'A';
INSERT INTO message (code, content, area) SELECT 'PagesChangesPublished', JSON_OBJECT('en_US', 'Changes published.', 'zh_CN', '修改已发布。'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesChangesPublished') AND area = 'A');
UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '修改已发布。') WHERE LOWER(code) = LOWER('PagesChangesPublished') AND area = 'A';
INSERT INTO message (code, content, area) SELECT 'PagesChangesDiscarded', JSON_OBJECT('en_US', 'Changes discarded.', 'zh_CN', '修改已放弃。'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesChangesDiscarded') AND area = 'A');
diff --git a/openspec/changes/add-page-visual-editor/tasks.md b/openspec/changes/add-page-visual-editor/tasks.md
index ede4ec6..e2f8900 100644
--- a/openspec/changes/add-page-visual-editor/tasks.md
+++ b/openspec/changes/add-page-visual-editor/tasks.md
@@ -22,8 +22,8 @@ Design: `design.md`. Every task is TDD. Work is serial in the main session, with
## 2. Canvas
-- [ ] 2.1 `PageCanvas`: the frame, the Desktop view (1280 px scaled) and the Phone view (390 px), live updates from the draft, and links and buttons inert.
-- [ ] 2.2 Edit-mode hooks in `CmsBlocks`: `EditContext`, `EditableText` for every plain text field with its server limit, image spots, `Section` outline and `data-index`. Public rendering stays unchanged: the existing `CmsBlocks*.test.tsx` stay green.
+- [x] 2.1 `PageCanvas`: the frame, the Desktop view (1280 px scaled) and the Phone view (390 px), live updates from the draft, and links and buttons inert.
+- [x] 2.2 Edit-mode hooks in `CmsBlocks`: `EditContext`, `EditableText` for every plain text field with its server limit, image spots, `Section` outline and `data-index`. Public rendering stays unchanged: the existing `CmsBlocks*.test.tsx` stay green.
- [ ] 2.3 Rich text in place: `RichTextEditor` bare mode with a floating toolbar (test: an unsafe link is refused with a message).
- [ ] 2.4 Custom HTML on the canvas: the saved-draft frame, the "Save to update" mark, and a reload after save.
diff --git a/solvway-admin-portal/app/components/pages/CmsBlocks.edit.test.tsx b/solvway-admin-portal/app/components/pages/CmsBlocks.edit.test.tsx
new file mode 100644
index 0000000..8d01f8f
--- /dev/null
+++ b/solvway-admin-portal/app/components/pages/CmsBlocks.edit.test.tsx
@@ -0,0 +1,104 @@
+import { fireEvent, render, screen, within } from "@testing-library/react";
+import { describe, it, expect, vi } from "vitest";
+import { LandingPage, type PublicBlock } from "./CmsBlocks";
+import { setField, type PageEdit } from "./CmsEdit";
+
+// add-page-visual-editor: the page in edit mode. Text edits in place, sections select, images open the picker, and
+// nothing navigates. Without `edit` the page renders exactly as before (CmsBlocks*.test.tsx).
+const company = { code: "INS", name: "INS Sdn Bhd" };
+const page = (blocks: PublicBlock[]) => ({ slug: "hotel", title: "Hotel day", blocks });
+
+function editor(overrides: Partial = {}): PageEdit {
+ return {
+ selected: null, select: vi.fn(), text: vi.fn(), html: vi.fn(), image: vi.fn(),
+ preview: () => undefined, stale: () => false, frameVersion: 0, chrome: () => null, ...overrides,
+ };
+}
+
+const typeInto = (el: HTMLElement, value: string) => { el.textContent = value; fireEvent.input(el); };
+
+describe("LandingPage in edit mode", () => {
+ it("edits a heading, a card title and a FAQ answer in place as plain text", () => {
+ const edit = editor();
+ render();
+ const heading = screen.getAllByRole("textbox", { name: "Heading" })[0];
+ expect(heading).toHaveAttribute("contenteditable", "plaintext-only");
+ expect(heading).toHaveTextContent("Hotel week");
+ typeInto(heading, "Hotel month");
+ expect(edit.text).toHaveBeenLastCalledWith(0, "heading", "Hotel month");
+
+ typeInto(screen.getAllByRole("textbox", { name: "Title" })[1], "Second");
+ expect(edit.text).toHaveBeenLastCalledWith(1, "cards.1.title", "Second");
+ typeInto(screen.getByRole("textbox", { name: "Answer 1" }), "Yes\nno");
+ expect(edit.text).toHaveBeenLastCalledWith(2, "items.0.answer", "Yes\nno");
+ });
+
+ it("offers an empty heading as a spot to type, and shows the limit once close to it", () => {
+ render();
+ expect(screen.getByText("100/120")).toBeInTheDocument();
+ expect(screen.getAllByRole("textbox", { name: "Heading" })[1]).toHaveTextContent("");
+ });
+
+ it("selects a section on click, outlines the selected one, and draws the editor's tools in it", () => {
+ const edit = editor({ selected: 1, chrome: (i) => tools {i} });
+ render();
+ const sections = document.querySelectorAll("[data-index]");
+ expect([...sections].map((s) => s.getAttribute("data-index"))).toEqual(["0", "1"]);
+ expect(sections[1]).toHaveAttribute("data-selected", "true");
+ expect(sections[0]).not.toHaveAttribute("data-selected");
+ expect(within(sections[1] as HTMLElement).getByText("tools 1")).toBeInTheDocument();
+ fireEvent.click(sections[0]);
+ expect(edit.select).toHaveBeenCalledWith(0);
+ });
+
+ it("keeps links and buttons from navigating, and edits a button's label", () => {
+ const edit = editor();
+ render();
+ const label = screen.getByRole("textbox", { name: "Button label" });
+ const link = label.closest("a")!;
+ expect(fireEvent.click(link)).toBe(false); // default prevented
+ typeInto(label, "Join now");
+ expect(edit.text).toHaveBeenLastCalledWith(0, "buttonLabel", "Join now");
+ });
+
+ it("opens the picker from an image spot and shows a chosen image before it is saved", () => {
+ const edit = editor({ preview: (i) => (i === 0 ? "data:image/png;base64,AAAA" : undefined) });
+ render();
+ const spots = screen.getAllByRole("button", { name: "Image" });
+ expect(spots).toHaveLength(2);
+ expect(spots[0].querySelector("img")).toHaveAttribute("src", "data:image/png;base64,AAAA");
+ fireEvent.click(spots[1]);
+ expect(edit.image).toHaveBeenCalledWith(1);
+ });
+
+ it("shows a Custom HTML block's saved version, selectable, marked while its HTML has unsaved edits", () => {
+ const edit = editor({ stale: (i) => i === 0, frameVersion: 3 });
+ render(x
" }])} edit={edit} preview frameBase="/pages/5/preview/frame" />);
+ expect(document.querySelector("iframe")).toHaveAttribute("src", "/pages/5/preview/frame/0?v=3");
+ expect(screen.getByText("Save to update")).toBeInTheDocument();
+ fireEvent.click(screen.getByRole("button", { name: "Custom HTML" }));
+ expect(edit.select).toHaveBeenCalledWith(0);
+ });
+
+ it("renders no editing without edit mode", () => {
+ render();
+ expect(document.querySelector("[contenteditable]")).toBeNull();
+ expect(document.querySelector("[data-index]")).toBeNull();
+ expect(screen.queryByRole("button")).toBeNull();
+ expect(screen.getByRole("heading", { level: 2, name: "Hotel week" })).toBeInTheDocument();
+ });
+});
+
+describe("setField", () => {
+ it("sets a top-level field or one inside a card or question, leaving the rest", () => {
+ const block = { type: "FEATURES" as const, heading: "H", cards: [{ title: "A", text: "a" }, { title: "B" }] };
+ expect(setField(block, "heading", "New")).toEqual({ ...block, heading: "New" });
+ expect(setField(block, "cards.1.title", "C").cards).toEqual([{ title: "A", text: "a" }, { title: "C" }]);
+ expect(block.cards[1].title).toBe("B");
+ expect(setField({ type: "FAQ", items: [{ question: "Q", answer: "A" }] }, "items.0.answer", "Z").items).toEqual([{ question: "Q", answer: "Z" }]);
+ });
+});
diff --git a/solvway-admin-portal/app/components/pages/CmsBlocks.tsx b/solvway-admin-portal/app/components/pages/CmsBlocks.tsx
index 95ab27e..7b381cb 100644
--- a/solvway-admin-portal/app/components/pages/CmsBlocks.tsx
+++ b/solvway-admin-portal/app/components/pages/CmsBlocks.tsx
@@ -4,6 +4,7 @@ import type { RegistrationInfo } from "@/app/lib/services/publicRegistration";
import { SafeHtml } from "./SafeHtml";
import { LandingForm } from "./LandingForm";
import { HtmlFrame } from "./HtmlFrame";
+import { EditContext, EditSection, HeroShade, HtmlSpot, ImageSpot, Text, stopNavigation, type PageEdit } from "./CmsEdit";
// Landing-page blocks, ported from babyboo's CmsBlocks minus the product blocks. HTML only goes through SafeHtml;
// images are this company's uploads; buttons render only for a safe link, checked again here. A Custom HTML block is
@@ -28,34 +29,34 @@ const BANDS: Record, string> = {
};
const bandOf = (look: unknown) => (LOOKS.includes(look as Look) && look !== "DEFAULT" ? (look as Exclude) : null);
-function ButtonLink({ block }: { block: CmsBlock }) {
- if (!block.buttonLabel || !isSafeLink(block.buttonUrl)) return null;
+function ButtonLink({ block, index, edit }: { block: CmsBlock; index: number; edit?: boolean }) {
+ // In edit mode a button with a label shows whatever its link, so its label can be typed in place.
+ if (!block.buttonLabel || (!edit && !isSafeLink(block.buttonUrl))) return null;
const external = /^https:/i.test(block.buttonUrl!);
const colours = bandOf(block.look) === "ACCENT" ? "bg-background text-foreground" : "bg-primary text-primary-foreground";
return (
- {block.buttonLabel}
+
);
}
-function BlockImage({ id, className }: { id?: number | null; className: string }) {
- // eslint-disable-next-line @next/next/no-img-element -- an uploaded image of unknown size
- return id ? : null;
-}
-
-function Block({ block, index, company, slug, preview }: { block: PublicBlock; index: number; company: string; slug: string; preview?: boolean }) {
+function Block({ block, index, company, slug, preview, edit }: { block: PublicBlock; index: number; company: string; slug: string; preview?: boolean; edit?: boolean }) {
+ const image = block.imageId ? imageSrc(block.imageId) : undefined;
+ const button = ;
switch (block.type) {
case "HERO":
return (
-
-
- {block.heading ?
{block.heading}
: null}
- {block.text ?
{block.text}
: null}
-
-
+
+
+
+
+
{button}
+
);
case "RICH_TEXT":
@@ -63,32 +64,43 @@ function Block({ block, index, company, slug, preview }: { block: PublicBlock; i
case "IMAGE_TEXT":
return (
-
+ {image || edit ? (
+
+
+
+ ) : null}
- {block.heading ?
{block.heading}
: null}
+
{block.html ? : null}
-
+ {button}
);
case "FAQ":
return block.items?.length ? (
- {block.heading ?
+ );
+}
+
+/** A Custom HTML block in edit mode: its saved version, covered so a click selects it, marked while unsaved. */
+export function HtmlSpot({ index, children }: { index: number; children: ReactNode }) {
+ const t = useT();
+ const edit = useEdit();
+ if (!edit) return <>{children}>;
+ return (
+
+
+ {children}
+
+
+ );
+}
+
+/** The hero's text area: shaded over an image, including one chosen in the editor and not saved yet. */
+export function HeroShade({ index, image, shaded, plain, children }: { index: number; image: boolean; shaded: string; plain: string; children: ReactNode }) {
+ const edit = useEdit();
+ return
{children}
;
+}
diff --git a/solvway-admin-portal/app/components/pages/PageCanvas.test.tsx b/solvway-admin-portal/app/components/pages/PageCanvas.test.tsx
new file mode 100644
index 0000000..c643aa5
--- /dev/null
+++ b/solvway-admin-portal/app/components/pages/PageCanvas.test.tsx
@@ -0,0 +1,17 @@
+import { render, waitFor, within } from "@testing-library/react";
+import { describe, it, expect } from "vitest";
+import { PageCanvas } from "./PageCanvas";
+
+// add-page-visual-editor: the canvas is a device screen, Desktop 1280 px (scaled to fit) or Phone 390 px.
+const frame = () => document.querySelector("iframe") as HTMLIFrameElement;
+
+describe("PageCanvas", () => {
+ it("shows the page on a 1280 px desktop screen or a 390 px phone screen", async () => {
+ const { rerender } = render(