UNPKG

@knxcloud/lowcode-hooks

Version:
1 lines 4.49 kB
{"version":3,"file":"lowcode-hooks.mjs","sources":["../src/current-node.ts","../src/renderer-context.ts"],"sourcesContent":["import type {\n GlobalEvent,\n IPublicModelNode,\n IPublicTypeDisposable,\n} from '@alilc/lowcode-types';\nimport type { InjectionKey } from 'vue';\nimport type { DesignMode } from './renderer-context';\nimport { inject } from 'vue';\n\nexport type IPublicTypePropChangeOptions = Omit<\n GlobalEvent.Node.Prop.ChangeOptions,\n 'node'\n>;\n\nexport interface INode extends IPublicModelNode {\n onVisibleChange(func: (flag: boolean) => any): () => void;\n onPropChange(func: (info: IPublicTypePropChangeOptions) => void): IPublicTypeDisposable;\n onChildrenChange(\n fn: (param?: { type: string; node: INode } | undefined) => void,\n ): IPublicTypeDisposable | undefined;\n}\n\nexport interface EnvNode {\n mode: DesignMode;\n node: INode | null;\n isDesignerEnv: boolean;\n}\n\nexport interface DesignerEnvNode extends EnvNode {\n mode: 'design';\n node: INode;\n isDesignerEnv: true;\n}\n\nexport interface LiveEnvNode extends EnvNode {\n mode: 'live';\n node: null;\n isDesignerEnv: false;\n}\n\nexport type CurrentNode = DesignerEnvNode | LiveEnvNode;\n\nexport function getCurrentNodeKey(): InjectionKey<CurrentNode> {\n let key = (window as any).__currentNode;\n if (!key) {\n key = Symbol('__currentNode');\n (window as any).__currentNode = key;\n }\n return key;\n}\n\nexport function useCurrentNode(): CurrentNode {\n const key = getCurrentNodeKey();\n return inject(\n key,\n () => {\n return {\n mode: 'live',\n node: null,\n isDesignerEnv: false,\n } as LiveEnvNode;\n },\n true,\n );\n}\n","import type { IPublicTypeNodeSchema } from '@alilc/lowcode-types';\nimport type { Component, ComponentPublicInstance, InjectionKey } from 'vue';\nimport type { INode } from './current-node';\nimport { inject, getCurrentInstance } from 'vue';\n\nexport type DesignMode = 'live' | 'design';\n\nexport interface RendererContext {\n readonly components: Record<string, Component<any, any, any>>;\n readonly designMode: DesignMode;\n readonly thisRequiredInJSE: boolean;\n getNode(id: string): INode | null;\n rerender(): void;\n wrapLeafComp<C extends object, L extends object>(name: string, comp: C, leaf: L): L;\n triggerCompGetCtx(schema: IPublicTypeNodeSchema, val: ComponentPublicInstance): void;\n}\n\nexport function getRendererContextKey(): InjectionKey<RendererContext> {\n let key = (window as any).__rendererContext;\n if (!key) {\n key = Symbol('__rendererContext');\n (window as any).__rendererContext = key;\n }\n return key;\n}\n\nexport function useRendererContext(): RendererContext {\n const key = getRendererContextKey();\n return inject(\n key,\n () => {\n const props = getCurrentInstance()?.props ?? {};\n return {\n rerender: () => void 0,\n thisRequiredInJSE: true,\n components: getPropValue(props, 'components', {}),\n designMode: getPropValue<DesignMode>(props, 'designMode', 'live'),\n getNode: getPropValue(props, 'getNode', () => null),\n wrapLeafComp: <T extends object, L extends object>(_: string, __: T, leaf: L) =>\n leaf,\n triggerCompGetCtx: getPropValue(props, 'triggerCompGetCtx', () => void 0),\n };\n },\n true,\n );\n}\n\nfunction getPropValue<T>(\n props: Record<string, unknown>,\n key: string,\n defaultValue: T,\n): T {\n return (props[key] || props[`__${key}`] || defaultValue) as T;\n}\n"],"names":[],"mappings":";AA0CO,SAAS,oBAA+C;AAC7D,MAAI,MAAO,OAAe;AAC1B,MAAI,CAAC,KAAK;AACR,UAAM,OAAO,eAAe;AAC3B,WAAe,gBAAgB;AAAA,EAClC;AACO,SAAA;AACT;AAEO,SAAS,iBAA8B;AAC5C,QAAM,MAAM;AACL,SAAA;AAAA,IACL;AAAA,IACA,MAAM;AACG,aAAA;AAAA,QACL,MAAM;AAAA,QACN,MAAM;AAAA,QACN,eAAe;AAAA,MAAA;AAAA,IAEnB;AAAA,IACA;AAAA,EAAA;AAEJ;AC/CO,SAAS,wBAAuD;AACrE,MAAI,MAAO,OAAe;AAC1B,MAAI,CAAC,KAAK;AACR,UAAM,OAAO,mBAAmB;AAC/B,WAAe,oBAAoB;AAAA,EACtC;AACO,SAAA;AACT;AAEO,SAAS,qBAAsC;AACpD,QAAM,MAAM;AACL,SAAA;AAAA,IACL;AAAA,IACA,MAAM;;AACJ,YAAM,SAAQ,oCAAA,mBAAsB,UAAtB,YAA+B,CAAA;AACtC,aAAA;AAAA,QACL,UAAU,MAAM;AAAA,QAChB,mBAAmB;AAAA,QACnB,YAAY,aAAa,OAAO,cAAc,CAAA,CAAE;AAAA,QAChD,YAAY,aAAyB,OAAO,cAAc,MAAM;AAAA,QAChE,SAAS,aAAa,OAAO,WAAW,MAAM,IAAI;AAAA,QAClD,cAAc,CAAqC,GAAW,IAAO,SACnE;AAAA,QACF,mBAAmB,aAAa,OAAO,qBAAqB,MAAM,MAAM;AAAA,MAAA;AAAA,IAE5E;AAAA,IACA;AAAA,EAAA;AAEJ;AAEA,SAAS,aACP,OACA,KACA,cACG;AACH,SAAQ,MAAM,GAAG,KAAK,MAAM,KAAK,GAAG,EAAE,KAAK;AAC7C;"}