diff --git a/local/seed/04-zh-cn-messages.sql b/local/seed/04-zh-cn-messages.sql index 234fc90..623ad32 100644 --- a/local/seed/04-zh-cn-messages.sql +++ b/local/seed/04-zh-cn-messages.sql @@ -1725,6 +1725,8 @@ INSERT INTO message (code, content, area) SELECT 'PagesLink', JSON_OBJECT('en_US UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '链接') WHERE LOWER(code) = LOWER('PagesLink') AND area = 'A'; INSERT INTO message (code, content, area) SELECT 'PagesLinkPrompt', JSON_OBJECT('en_US', 'Link address (https://, mailto:, tel:, / or #name)', 'zh_CN', '链接地址(https://、mailto:、tel:、/ 或 #名称)'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesLinkPrompt') AND area = 'A'); UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '链接地址(https://、mailto:、tel:、/ 或 #名称)') WHERE LOWER(code) = LOWER('PagesLinkPrompt') AND area = 'A'; +INSERT INTO message (code, content, area) SELECT 'PagesLinkRefused', JSON_OBJECT('en_US', 'That link is not allowed. Use https://, mailto:, tel:, a path starting with / or #name.', 'zh_CN', '不允许此链接。请使用 https://、mailto:、tel:、以 / 开头的路径或 #名称。'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesLinkRefused') AND area = 'A'); +UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '不允许此链接。请使用 https://、mailto:、tel:、以 / 开头的路径或 #名称。') WHERE LOWER(code) = LOWER('PagesLinkRefused') AND area = 'A'; INSERT INTO message (code, content, area) SELECT 'PagesClearFormatting', JSON_OBJECT('en_US', 'Clear formatting', 'zh_CN', '清除格式'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesClearFormatting') AND area = 'A'); UPDATE message SET content = JSON_SET(COALESCE(content, JSON_OBJECT()), '$.zh_CN', '清除格式') WHERE LOWER(code) = LOWER('PagesClearFormatting') AND area = 'A'; INSERT INTO message (code, content, area) SELECT 'PagesTooManyBlocks', JSON_OBJECT('en_US', 'A page can have at most 30 blocks.', 'zh_CN', '每个网页最多 30 个区块。'), 'A' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM message WHERE LOWER(code) = LOWER('PagesTooManyBlocks') AND area = 'A'); diff --git a/openspec/changes/add-page-visual-editor/tasks.md b/openspec/changes/add-page-visual-editor/tasks.md index e2f8900..6a0aaee 100644 --- a/openspec/changes/add-page-visual-editor/tasks.md +++ b/openspec/changes/add-page-visual-editor/tasks.md @@ -24,7 +24,7 @@ Design: `design.md`. Every task is TDD. Work is serial in the main session, with - [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). +- [x] 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. ## 3. Editor diff --git a/solvway-admin-portal/app/components/pages/CmsBlocks.edit.test.tsx b/solvway-admin-portal/app/components/pages/CmsBlocks.edit.test.tsx index 8d01f8f..c351e81 100644 --- a/solvway-admin-portal/app/components/pages/CmsBlocks.edit.test.tsx +++ b/solvway-admin-portal/app/components/pages/CmsBlocks.edit.test.tsx @@ -84,6 +84,15 @@ describe("LandingPage in edit mode", () => { expect(edit.select).toHaveBeenCalledWith(0); }); + it("edits rich text in place in the page's own style, for a Text block and an image-and-text block", async () => { + render(Body copy

" }, { type: "IMAGE_TEXT", heading: "Pic", html: "" }])} edit={editor()} preview />); + const boxes = await screen.findAllByRole("textbox", { name: "Content" }); + expect(boxes).toHaveLength(2); + expect(boxes[0]).toHaveAttribute("contenteditable", "true"); + expect(boxes[0]).toHaveTextContent("Body copy"); + expect(screen.getAllByText("Body copy")).toHaveLength(1); + }); + it("renders no editing without edit mode", () => { render(); expect(document.querySelector("[contenteditable]")).toBeNull(); diff --git a/solvway-admin-portal/app/components/pages/CmsBlocks.tsx b/solvway-admin-portal/app/components/pages/CmsBlocks.tsx index 7b381cb..1b0e7ed 100644 --- a/solvway-admin-portal/app/components/pages/CmsBlocks.tsx +++ b/solvway-admin-portal/app/components/pages/CmsBlocks.tsx @@ -4,7 +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"; +import { EditContext, EditSection, HeroShade, HtmlSpot, ImageSpot, RichSpot, 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 @@ -60,7 +60,9 @@ function Block({ block, index, company, slug, preview, edit }: { block: PublicBl ); case "RICH_TEXT": - return block.html ?
: null; + return block.html || edit ? ( +
{block.html ? : null}
+ ) : null; case "IMAGE_TEXT": return (
@@ -71,7 +73,7 @@ function Block({ block, index, company, slug, preview, edit }: { block: PublicBl ) : null}
- {block.html ? : null} + {block.html ? : null} {button}
diff --git a/solvway-admin-portal/app/components/pages/CmsEdit.tsx b/solvway-admin-portal/app/components/pages/CmsEdit.tsx index 40b1884..8d0b94e 100644 --- a/solvway-admin-portal/app/components/pages/CmsEdit.tsx +++ b/solvway-admin-portal/app/components/pages/CmsEdit.tsx @@ -1,6 +1,7 @@ "use client"; import { createContext, useContext, useLayoutEffect, useRef, type ElementType, type FormEvent, type KeyboardEvent, type MouseEvent, type ReactNode } from "react"; +import dynamic from "next/dynamic"; import { ImagePlus } from "lucide-react"; import { BLOCK_LABELS, type CmsBlock } from "@/app/lib/cmsPages"; import { useT } from "@/app/lib/i18n/MessagesProvider"; @@ -158,3 +159,18 @@ export function HeroShade({ index, image, shaded, plain, children }: { index: nu const edit = useEdit(); return
{children}
; } + +// Loaded only when the canvas shows rich text: visitors' pages never download the editor. +const RichTextEditor = dynamic(() => import("./RichTextEditor").then((m) => m.RichTextEditor), { ssr: false }); + +/** A block's rich text: the sanitised HTML on the public page (`children`); in edit mode, edited in place. */ +export function RichSpot({ index, html, className, children }: { index: number; html: string | undefined; className: string; children: ReactNode }) { + const t = useT(); + const edit = useEdit(); + if (!edit) return <>{children}; + return ( +
+ edit.html(index, value)} /> +
+ ); +} diff --git a/solvway-admin-portal/app/components/pages/RichTextEditor.test.tsx b/solvway-admin-portal/app/components/pages/RichTextEditor.test.tsx new file mode 100644 index 0000000..fa0294c --- /dev/null +++ b/solvway-admin-portal/app/components/pages/RichTextEditor.test.tsx @@ -0,0 +1,25 @@ +import { render, screen } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; +import { describe, it, expect, vi } from "vitest"; +import { RichTextEditor } from "./RichTextEditor"; + +// add-page-visual-editor: links follow the page link rule, and a refused one says why instead of vanishing. +describe("RichTextEditor", () => { + it("refuses an unsafe link with a message, and adds nothing", async () => { + const user = userEvent.setup(); + const onChange = vi.fn(); + vi.spyOn(window, "prompt").mockReturnValue("javascript:alert(1)"); + render(); + await screen.findByRole("textbox", { name: "Content" }); + await user.click(screen.getByRole("button", { name: "Link" })); + expect(screen.getByRole("alert")).toHaveTextContent("That link is not allowed"); + expect(onChange).not.toHaveBeenCalled(); + }); + + it("in bare mode drops the box and keeps the toolbar for when the text is being edited", async () => { + render( {}} />); + const box = await screen.findByRole("textbox", { name: "Content" }); + expect(box.closest(".border")).toBeNull(); + expect(screen.getByRole("toolbar", { name: "Formatting" })).toBeInTheDocument(); + }); +}); diff --git a/solvway-admin-portal/app/components/pages/RichTextEditor.tsx b/solvway-admin-portal/app/components/pages/RichTextEditor.tsx index 3a52642..ae0d1fe 100644 --- a/solvway-admin-portal/app/components/pages/RichTextEditor.tsx +++ b/solvway-admin-portal/app/components/pages/RichTextEditor.tsx @@ -1,6 +1,6 @@ "use client"; -import { useEffect, useRef, type ReactNode } from "react"; +import { useEffect, useRef, useState, type ReactNode } from "react"; import { EditorContent, useEditor, useEditorState, type Editor } from "@tiptap/react"; import StarterKit from "@tiptap/starter-kit"; import TextAlign from "@tiptap/extension-text-align"; @@ -15,15 +15,18 @@ import { isSafeLink } from "@/app/lib/cmsPages"; const htmlOf = (editor: Editor) => (editor.isEmpty ? "" : editor.getHTML()); -export function RichTextEditor({ id, label, value, onChange, disabled, invalid }: { +export function RichTextEditor({ id, label, value, onChange, disabled, invalid, bare }: { id?: string; label?: string; value: string; onChange: (html: string) => void; disabled?: boolean; invalid?: boolean; + /** On the page canvas (add-page-visual-editor): the text in the page's own style, the toolbar floating above it while edited. */ + bare?: boolean; }) { const t = useT(); + const [refused, setRefused] = useState(false); const emitted = useRef(value); const editor = useEditor({ immediatelyRender: false, @@ -37,7 +40,7 @@ export function RichTextEditor({ id, label, value, onChange, disabled, invalid } }), TextAlign.configure({ types: ["heading", "paragraph"], alignments: ["left", "center", "right"] }), ], - editorProps: { attributes: { ...(id ? { id } : {}), ...(label ? { "aria-label": label } : {}), ...(invalid ? { "aria-invalid": "true" } : {}), class: "min-h-[140px] px-3 py-2 text-sm outline-none" } }, + editorProps: { attributes: { ...(id ? { id } : {}), ...(label ? { "aria-label": label } : {}), ...(invalid ? { "aria-invalid": "true" } : {}), class: bare ? "min-h-[1.5em] outline-none" : "min-h-[140px] px-3 py-2 text-sm outline-none" } }, onUpdate: ({ editor }) => { const html = htmlOf(editor); emitted.current = html; @@ -76,13 +79,17 @@ export function RichTextEditor({ id, label, value, onChange, disabled, invalid } function addLink() { const url = window.prompt(t("PagesLinkPrompt")); + setRefused(!!url && !isSafeLink(url)); if (!url || !isSafeLink(url)) return; run((e) => e.chain().focus().extendMarkRange("link").setLink({ href: url }).run()); } + const toolbar = bare + ? "absolute bottom-full left-0 z-20 mb-2 hidden flex-wrap items-center gap-1 rounded-md border bg-popover p-1 text-popover-foreground shadow-md group-focus-within/rt:flex" + : "flex flex-wrap items-center gap-1"; return ( -
-
+
+