feat: 增加可视化模板页面编辑器
This commit is contained in:
1 parent
9d2038cc93
commit
c5af790554
24 files changed
+773
-129
No files matched your search
@@ -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