feat: 增加可视化模板页面编辑器
This commit is contained in:
1 parent
9d2038cc93
commit
c5af790554
24 files changed
+773
-129
No files matched your search
@@ -38,10 +38,23 @@ export async function request<T>(
|
||||
}
|
||||
export async function loadWorkspace() {
|
||||
const data = await request<RuntimeWorkspace>("/public/workspace");
|
||||
if (data.publication?.status !== "published") throw new Error("品牌尚未发布");
|
||||
if (data.brand.visualTemplateKey !== TEMPLATE_KEY)
|
||||
const editorPreview =
|
||||
process.env.TARO_ENV === "h5" &&
|
||||
new URLSearchParams(window.location.search).get("editor") === "1";
|
||||
if (!editorPreview && data.publication?.status !== "published") throw new Error("品牌尚未发布");
|
||||
if (!editorPreview && data.brand.visualTemplateKey !== TEMPLATE_KEY)
|
||||
throw new Error("该品牌使用其他模板,请选择匹配的模板预览地址");
|
||||
return data;
|
||||
return {
|
||||
...data,
|
||||
brand: data.brand ?? {},
|
||||
templateData: data.templateData ?? {},
|
||||
offerings: data.offerings ?? [],
|
||||
inquiryForms: data.inquiryForms ?? [],
|
||||
trustItems: data.trustItems ?? [],
|
||||
trustPresentation: data.trustPresentation ?? {},
|
||||
capabilities: data.capabilities ?? [],
|
||||
theme: data.theme ?? { colors: {} },
|
||||
};
|
||||
}
|
||||
export async function ensureSession() {
|
||||
if (hasSession()) return;
|
||||
|
||||
@@ -1,22 +1,36 @@
|
||||
import { createContext, useCallback, useContext, useEffect, useState } from "react";
|
||||
import type { PropsWithChildren } from "react";
|
||||
import type { RuntimeWorkspace } from "@personal-brand/types";
|
||||
import { projectBrandAliases } from "@personal-brand/types";
|
||||
import { loadWorkspace } from "./api";
|
||||
const Context = createContext<{
|
||||
workspace?: RuntimeWorkspace;
|
||||
error: string;
|
||||
loading: boolean;
|
||||
setBrandDraft: (brand: Partial<RuntimeWorkspace["brand"]>) => void;
|
||||
reload: () => void;
|
||||
}>({ error: "", loading: true, reload() {} });
|
||||
}>({ error: "", loading: true, setBrandDraft() {}, reload() {} });
|
||||
export function WorkspaceProvider({ children }: PropsWithChildren) {
|
||||
const [workspace, setWorkspace] = useState<RuntimeWorkspace>();
|
||||
const [baseWorkspace, setBaseWorkspace] = useState<RuntimeWorkspace>();
|
||||
const [brandDraft, setBrandDraft] = useState<Partial<RuntimeWorkspace["brand"]>>({});
|
||||
const workspace = baseWorkspace
|
||||
? {
|
||||
...baseWorkspace,
|
||||
brand: projectBrandAliases(
|
||||
{ ...baseWorkspace.brand, ...brandDraft },
|
||||
brandDraft.config && typeof brandDraft.config === "object"
|
||||
? (brandDraft.config as Record<string, unknown>)
|
||||
: ((baseWorkspace.brand.config as Record<string, unknown> | undefined) ?? {}),
|
||||
),
|
||||
}
|
||||
: undefined;
|
||||
const [error, setError] = useState("");
|
||||
const [loading, setLoading] = useState(true);
|
||||
const reload = useCallback(() => {
|
||||
setLoading(true);
|
||||
setError("");
|
||||
void loadWorkspace()
|
||||
.then(setWorkspace)
|
||||
.then(setBaseWorkspace)
|
||||
.catch((error: unknown) =>
|
||||
setError(error instanceof Error ? error.message : "加载失败,请重试"),
|
||||
)
|
||||
@@ -24,7 +38,9 @@ export function WorkspaceProvider({ children }: PropsWithChildren) {
|
||||
}, []);
|
||||
useEffect(reload, [reload]);
|
||||
return (
|
||||
<Context.Provider value={{ workspace, error, loading, reload }}>{children}</Context.Provider>
|
||||
<Context.Provider value={{ workspace, error, loading, setBrandDraft, reload }}>
|
||||
{children}
|
||||
</Context.Provider>
|
||||
);
|
||||
}
|
||||
export const useWorkspace = () => useContext(Context);
|
||||
Reference in new issue
Block a user