import type { BuilderRenderState, RegisteredComponent, RegisteredComponents } from '../../context/types'; import { serializeIncludingFunctions } from '../../functions/register-component'; import { copyPlainData } from '../../helpers/copy-plain-data'; import type { BuilderContent } from '../../types/builder-content'; import type { ComponentInfo } from '../../types/components'; import type { Dictionary, Nullable } from '../../types/typescript'; import type { ContentProps } from './content.types'; // Registrations are usually module constants, so their derived forms are built once per object. const SERIALIZED_COMPONENT_INFOS = new WeakMap(); const REGISTERED_COMPONENT_ENTRIES = new WeakMap(); const getSerializedComponentInfo = (registration: RegisteredComponent): ComponentInfo => { const cached = SERIALIZED_COMPONENT_INFOS.get(registration); if (cached) return cached; const { component: _, ...rest } = registration; const info: ComponentInfo = serializeIncludingFunctions((rest as ComponentInfo)); SERIALIZED_COMPONENT_INFOS.set(registration, info); return info; }; export const getRegisteredComponents = (registrations: RegisteredComponent[], wrapComponent: (component: any) => any): RegisteredComponents => { const registeredComponents: RegisteredComponents = {}; for (const registration of registrations) { let entry = REGISTERED_COMPONENT_ENTRIES.get(registration); if (!entry) { entry = ({ component: wrapComponent(registration.component), ...getSerializedComponentInfo(registration) } as RegisteredComponent); REGISTERED_COMPONENT_ENTRIES.set(registration, entry); } registeredComponents[registration.name] = entry; } return registeredComponents; }; export const getComponentInfos = (registrations: RegisteredComponent[]): Dictionary => { const componentInfos: Dictionary = {}; for (const registration of registrations) { componentInfos[registration.name] = getSerializedComponentInfo(registration); } return componentInfos; }; export const getRootStateInitialValue = ({ content, data, locale }: Pick) => { const defaultValues: BuilderRenderState = {}; const seen = new WeakMap(); const initialState = copyPlainData(content?.data?.state || {}, seen); // set default values for content state inputs content?.data?.inputs?.forEach(input => { if (input.name && input.defaultValue !== undefined) { defaultValues[input.name] = copyPlainData(input.defaultValue, seen); } }); return { ...defaultValues, ...initialState, ...data, ...(locale ? { locale } : {}) }; }; export const getContentInitialValue = ({ content, data }: Pick): Nullable => { return !content ? undefined : { ...content, data: { ...content?.data, ...data }, meta: content?.meta }; }; /** * A state object whose reads and writes go to whatever `getRootState` returns * when they happen, for jsCode functions that outlive the root state object * they were defined against. */ export const getLiveRootState = (getRootState: () => BuilderRenderState): BuilderRenderState => new Proxy(({} as BuilderRenderState), { get: (_, prop) => Reflect.get(getRootState(), prop), set: (_, prop, value) => Reflect.set(getRootState(), prop, value), has: (_, prop) => Reflect.has(getRootState(), prop), deleteProperty: (_, prop) => Reflect.deleteProperty(getRootState(), prop), ownKeys: () => Reflect.ownKeys(getRootState()), getOwnPropertyDescriptor: (_, prop) => { const descriptor = Reflect.getOwnPropertyDescriptor(getRootState(), prop); return descriptor && { ...descriptor, configurable: true }; } })