|
@@ -0,0 +1,70 @@
|
|
|
+import { useEditor } from "@/modules/editor";
|
|
|
+import { DesignComp } from "@/modules/editor/defines/DesignTemp/DesignComp";
|
|
|
+import { Background, Layout } from "@/modules/editor/typings";
|
|
|
+import { cloneDeep } from "lodash";
|
|
|
+import { AnyFun } from "queenjs/typing";
|
|
|
+
|
|
|
+type IOptions<T, C> = {
|
|
|
+ value: T;
|
|
|
+ layout?: Layout;
|
|
|
+ background?: Background;
|
|
|
+ children?: C;
|
|
|
+};
|
|
|
+
|
|
|
+function createComp(
|
|
|
+ defaultOpts: any,
|
|
|
+ opts: any,
|
|
|
+ onCreate: (comp: DesignComp) => void
|
|
|
+) {
|
|
|
+ const defOpts = cloneDeep(defaultOpts);
|
|
|
+ if (defOpts.children) {
|
|
|
+ Object.entries(defOpts.children).forEach(([key, fn]: any) => {
|
|
|
+ defOpts.children[key] = fn();
|
|
|
+ });
|
|
|
+ }
|
|
|
+ const options = Object.assign(defOpts, opts);
|
|
|
+ const comp = new DesignComp(options);
|
|
|
+ onCreate(comp);
|
|
|
+ return comp.id;
|
|
|
+}
|
|
|
+
|
|
|
+function useCompData(compId: string) {
|
|
|
+ const editor = useEditor();
|
|
|
+ let data = editor.store.designCompMap.get(compId);
|
|
|
+ return data as any;
|
|
|
+}
|
|
|
+
|
|
|
+export function createCompHooks<T, C extends { [name: string]: AnyFun }>(
|
|
|
+ defaultOpts: IOptions<T, C>
|
|
|
+) {
|
|
|
+ function useChildCreator(key: keyof C) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ createComp: createComp.bind(null, defaultOpts),
|
|
|
+ useCompData: useCompData as (compId: string) => {
|
|
|
+ id: string;
|
|
|
+ value: T;
|
|
|
+ children: DeepObj<{ [name in keyof C]: ReturnType<C[name]> }>;
|
|
|
+ },
|
|
|
+ useChildCreator,
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
+export interface CompType {
|
|
|
+ id: string;
|
|
|
+}
|
|
|
+
|
|
|
+export class CompFactory {
|
|
|
+ static create(type: string): CompType {
|
|
|
+ return { id: type };
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+type DeepObj<T> = T extends object
|
|
|
+ ? T extends CompType
|
|
|
+ ? string
|
|
|
+ : T
|
|
|
+ : T extends Array<infer P>
|
|
|
+ ? DeepObj<P>[]
|
|
|
+ : T;
|