From fd2e970d8b719e2ee99d665e0cdc4e53d7d34bb3 Mon Sep 17 00:00:00 2001 From: que01 Date: Fri, 9 Oct 2026 15:12:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E9=A2=84=E8=A7=88=E5=BA=95=E9=83=A8=E6=8C=89=E9=92=AE=E6=BA=A2?= =?UTF-8?q?=E5=87=BA=E4=B8=8E=E5=92=A8=E8=AF=A2=E8=A1=A8=E5=8D=95=E7=BC=BA?= =?UTF-8?q?=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../brand/ComponentTemplatePreview.css | 1 + .../features/brand/component-renderers.jsx | 14 +++- .../features/brand/taro-web-components.jsx | 71 ++++++++++++++++++- .../src/features/brand/taro-web-runtime.jsx | 3 + apps/admin/template-preview-css.mjs | 10 ++- apps/admin/vite.config.ts | 1 + apps/taro/src/components/Inquiry.tsx | 11 ++- apps/taro/test/browser/admin-editor.spec.ts | 48 ++++++++++++- 8 files changed, 151 insertions(+), 8 deletions(-) diff --git a/apps/admin/src/features/brand/ComponentTemplatePreview.css b/apps/admin/src/features/brand/ComponentTemplatePreview.css index d3a41ba..d1b28ec 100644 --- a/apps/admin/src/features/brand/ComponentTemplatePreview.css +++ b/apps/admin/src/features/brand/ComponentTemplatePreview.css @@ -35,6 +35,7 @@ } .component-template-preview__canvas { + position: relative; container-type: inline-size; width: min(100%, 390px); min-height: calc(100% - 48px); diff --git a/apps/admin/src/features/brand/component-renderers.jsx b/apps/admin/src/features/brand/component-renderers.jsx index d15929a..7500eed 100644 --- a/apps/admin/src/features/brand/component-renderers.jsx +++ b/apps/admin/src/features/brand/component-renderers.jsx @@ -1,19 +1,29 @@ import { TemplatePage as FoodTemplatePage } from "../../../../../packages/template-acheng-campus-food/taro/index.tsx"; import { TemplatePage as EditorialTemplatePage } from "../../../../../packages/template-linjing-editorial/taro/index.tsx"; import React from "react"; +import { Inquiry } from "../../../../taro/src/components/Inquiry.tsx"; +import { Records } from "../../../../taro/src/components/Records.tsx"; +import { Success } from "../../../../taro/src/components/Success.tsx"; import { TemplateAssetContext } from "./taro-web-components.jsx"; +function pageContent({ workspace, page }) { + if (page === "inquiry") return ; + if (page === "records") return ; + if (page === "success") return ; + return null; +} + function FoodPreview(props) { return ( - + {pageContent(props)} ); } function EditorialPreview(props) { return ( - + {pageContent(props)} ); } diff --git a/apps/admin/src/features/brand/taro-web-components.jsx b/apps/admin/src/features/brand/taro-web-components.jsx index b91c6fc..71b93e3 100644 --- a/apps/admin/src/features/brand/taro-web-components.jsx +++ b/apps/admin/src/features/brand/taro-web-components.jsx @@ -27,9 +27,76 @@ const element = (tag) => export const View = element("div"); export const Text = element("span"); export const Button = element("button"); -export const Input = React.forwardRef((props, ref) => - React.createElement("input", { ...props, ref }), +export const Input = React.forwardRef(({ onInput, ...props }, ref) => + React.createElement("input", { + ...props, + ref, + onInput: (event) => onInput?.({ detail: { value: event.currentTarget.value } }), + }), ); +export const Textarea = React.forwardRef(({ onInput, maxlength, ...props }, ref) => + React.createElement("textarea", { + ...props, + ref, + maxLength: maxlength, + onInput: (event) => onInput?.({ detail: { value: event.currentTarget.value } }), + }), +); +export function Picker({ children, mode, range = [], value, onChange }) { + return ( +
+ {children} + {mode === "date" ? ( + onChange?.({ detail: { value: event.currentTarget.value } })} + style={{ position: "absolute", inset: 0, width: "100%", opacity: 0, cursor: "pointer" }} + /> + ) : ( + + )} +
+ ); +} +const CheckboxContext = React.createContext(null); +export function CheckboxGroup({ children, onChange }) { + const [selected, setSelected] = React.useState([]); + const update = (value, checked) => { + const next = checked ? [...selected, value] : selected.filter((item) => item !== value); + setSelected(next); + onChange?.({ detail: { value: next } }); + }; + return ( + +
{children}
+
+ ); +} +export function Checkbox({ value, checked }) { + const update = React.useContext(CheckboxContext); + return ( + update?.(value, event.currentTarget.checked)} + /> + ); +} +export const Label = element("label"); export const Image = React.forwardRef(({ mode, src, style, ...props }, ref) => { const key = React.useContext(TemplateAssetContext); return React.createElement("img", { diff --git a/apps/admin/src/features/brand/taro-web-runtime.jsx b/apps/admin/src/features/brand/taro-web-runtime.jsx index c75f4e6..d8248e7 100644 --- a/apps/admin/src/features/brand/taro-web-runtime.jsx +++ b/apps/admin/src/features/brand/taro-web-runtime.jsx @@ -1,4 +1,7 @@ +import React from "react"; const noop = () => Promise.resolve(); +export const useRouter = () => ({ params: {} }); +export const useDidShow = (callback) => React.useEffect(callback, []); const Taro = { setStorageSync() {}, getStorageSync() { diff --git a/apps/admin/template-preview-css.mjs b/apps/admin/template-preview-css.mjs index 3bcaed5..5211caf 100644 --- a/apps/admin/template-preview-css.mjs +++ b/apps/admin/template-preview-css.mjs @@ -4,7 +4,10 @@ export function templatePreviewCss() { postcssPlugin: "template-preview-css", Once(root) { const file = root.source?.input.file || ""; - if (!file.includes("/packages/template-") || !file.endsWith("/taro/style.css")) return; + const templateStyle = + file.includes("/packages/template-") && file.endsWith("/taro/style.css"); + const sharedFormStyle = file.endsWith("/apps/taro/src/components/forms.css"); + if (!templateStyle && !sharedFormStyle) return; root.walkRules((rule) => { rule.selector = rule.selector .split(",") @@ -21,6 +24,11 @@ export function templatePreviewCss() { .join(", "); }); root.walkDecls((declaration) => { + // 组件预览中的固定按钮必须以页面画布为定位基准。 + if (declaration.prop === "position" && declaration.value === "fixed") + declaration.value = "absolute"; + // 模板页面的视口高度按手机画布宽度计算,避免引用 Admin 浏览器高度。 + declaration.value = declaration.value.replace(/100vh/g, "178cqw"); declaration.value = declaration.value.replace( /(-?(?:[0-9]*[.])?[0-9]+)px/g, (_, value) => Number((Number(value) / 7.5).toFixed(8)) + "cqw", diff --git a/apps/admin/vite.config.ts b/apps/admin/vite.config.ts index 026eb07..2060b88 100644 --- a/apps/admin/vite.config.ts +++ b/apps/admin/vite.config.ts @@ -14,6 +14,7 @@ export default defineConfig({ }, define: { "process.env.TARO_PLATFORM": JSON.stringify("web"), + "process.env.TARO_ENV": JSON.stringify("h5"), PREVIEW_ALL_TEMPLATES: JSON.stringify(true), TEMPLATE_MANIFEST: JSON.stringify({ navigation: [] }), }, diff --git a/apps/taro/src/components/Inquiry.tsx b/apps/taro/src/components/Inquiry.tsx index f0a9f90..1df51a3 100644 --- a/apps/taro/src/components/Inquiry.tsx +++ b/apps/taro/src/components/Inquiry.tsx @@ -17,7 +17,13 @@ import type { Values } from "../runtime/content"; import { ensureSession, request, brandSlug } from "../runtime/api"; import { takeQuery, navigate } from "../runtime/navigation"; import "./forms.css"; -export function Inquiry({ workspace }: { workspace: RuntimeWorkspace }) { +export function Inquiry({ + workspace, + preview = false, +}: { + workspace: RuntimeWorkspace; + preview?: boolean; +}) { const router = useRouter(); const [offeringId, setOfferingId] = useState(router.params.offeringId || ""); const form: RuntimeInquiryForm | undefined = @@ -28,7 +34,7 @@ export function Inquiry({ workspace }: { workspace: RuntimeWorkspace }) { const [sending, setSending] = useState(false); const busy = useRef(false); const p = presentation(workspace, "inquiry"); - const food = TEMPLATE_KEY === "acheng-campus-food"; + const food = workspace.brand.visualTemplateKey === "acheng-campus-food"; useDidShow(() => { const query = takeQuery("inquiry"); if (query.offeringId) setOfferingId(query.offeringId); @@ -40,6 +46,7 @@ export function Inquiry({ workspace }: { workspace: RuntimeWorkspace }) { const setValue = (key: string, value: string | string[]) => setValues((current) => ({ ...current, [key]: value })); async function submit() { + if (preview) return; if (busy.current) return; if (!form) { setError("咨询表单暂不可用,请稍后重试"); diff --git a/apps/taro/test/browser/admin-editor.spec.ts b/apps/taro/test/browser/admin-editor.spec.ts index 975bb40..6c5fe2e 100644 --- a/apps/taro/test/browser/admin-editor.spec.ts +++ b/apps/taro/test/browser/admin-editor.spec.ts @@ -76,7 +76,19 @@ const runtimeWorkspace = { }, }, ], - inquiryForms: [], + inquiryForms: [ + { + key: "brand-inquiry", + version: "1", + purpose: "inquiry", + target: "brand", + submissionAction: "create-inquiry", + fields: [ + { key: "name", label: "称呼", type: "text", required: true }, + { key: "message", label: "需求说明", type: "textarea", required: true }, + ], + }, + ], trustItems: [{ ...adminTrust }], trustPresentation: {}, capabilities: [], @@ -89,6 +101,7 @@ const templateContent = { { key: "offerings", page: "offerings", label: "套餐餐品" }, { key: "service", page: "service", label: "出摊服务" }, { key: "detail", page: "detail", label: "详情页" }, + { key: "inquiry", page: "inquiry", label: "定制/咨询" }, ], assetSlots: [], trustContentTypes: [ @@ -362,12 +375,45 @@ test("Admin 按通用预览目标筛选字段并同步品牌草稿", async ({ pa await page.getByText("出摊服务", { exact: true }).click(); await expect(preview.locator(".schedule-card")).toContainText("周一到周五营业"); await page.getByText("详情页", { exact: true }).click(); + const detailBounds = await preview.evaluate((element) => { + const canvas = element.querySelector(".component-template-preview__canvas")!; + const bar = element.querySelector(".food-detail .bar")!; + const canvasBox = canvas.getBoundingClientRect(); + const barBox = bar.getBoundingClientRect(); + return { + position: getComputedStyle(bar).position, + left: barBox.left - canvasBox.left, + right: canvasBox.right - barBox.right, + bottom: canvasBox.bottom - barBox.bottom, + }; + }); + expect(detailBounds.position).toBe("absolute"); + expect(detailBounds.left).toBeGreaterThanOrEqual(-1); + expect(detailBounds.right).toBeGreaterThanOrEqual(-1); + expect(detailBounds.bottom).toBeGreaterThanOrEqual(-1); + await preview.locator(".food-detail .bar").scrollIntoViewIfNeeded(); + await expect(preview.locator(".food-detail .bar button")).toBeVisible(); + await preview.screenshot({ path: "../../.codex-tasks/preview-proportion/detail.png" }); await preview.locator('[data-editor-target="acheng-campus-food.detail.detailCopy"]').click(); await expect(preview.locator(".component-template-preview__outline.is-selected")).toHaveAttribute( "data-selected-target", "acheng-campus-food.detail.detailCopy", ); await expect(page.getByLabel("价格标题")).toBeVisible(); + await page.getByText("定制/咨询", { exact: true }).click(); + await expect(preview.locator(".inquiry-form .form-field")).toHaveCount(2); + await expect(preview.locator(".inquiry-form")).toContainText("称呼"); + await expect(preview.locator(".inquiry-form")).toContainText("需求说明"); + await preview.locator(".inquiry-form").scrollIntoViewIfNeeded(); + await preview.screenshot({ path: "../../.codex-tasks/preview-proportion/inquiry.png" }); + const formOverflow = await preview + .locator(".inquiry-form") + .evaluate((form) => form.scrollWidth - form.clientWidth); + expect(formOverflow).toBeLessThanOrEqual(1); + await page.getByRole("button", { name: "浏览页面" }).click(); + await preview.locator(".inquiry-form input").first().fill("预览访客"); + await expect(preview.locator(".inquiry-form input").first()).toHaveValue("预览访客"); + await page.getByRole("button", { name: "点选编辑" }).click(); await page.getByText("套餐餐品", { exact: true }).click(); await expect .poll(() =>