From e1da695164f30484f48510632703546ae9284e43 Mon Sep 17 00:00:00 2001 From: Kirtesh Suthar Date: Fri, 20 Dec 2024 18:22:06 +0530 Subject: [PATCH] feat: add config to exported live preview object --- package.json | 2 +- .../contentstack-live-preview-HOC.test.ts | 2 ++ src/preview/contentstack-live-preview-HOC.ts | 29 +++++++++++++++---- src/types/types.ts | 2 ++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index b841385..2580007 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/live-preview-utils", - "version": "3.0.1", + "version": "3.1.0", "description": "Contentstack provides the Live Preview SDK to establish a communication channel between the various Contentstack SDKs and your website, transmitting live changes to the preview pane.", "type": "module", "types": "dist/legacy/index.d.ts", diff --git a/src/preview/__test__/contentstack-live-preview-HOC.test.ts b/src/preview/__test__/contentstack-live-preview-HOC.test.ts index 000c2a8..5664ab7 100644 --- a/src/preview/__test__/contentstack-live-preview-HOC.test.ts +++ b/src/preview/__test__/contentstack-live-preview-HOC.test.ts @@ -190,6 +190,8 @@ describe("Live Preview HOC config", () => { expect(Config.get().hash).toBe("test"); expect(Config.get().stackDetails.contentTypeUid).toBe("test"); expect(Config.get().stackDetails.entryUid).toBe("test"); + expect(ContentstackLivePreview.config.stackDetails.contentTypeUid).toBe("test"); + expect(ContentstackLivePreview.config.stackDetails.entryUid).toBe("test"); Object.defineProperty(window, "location", { writable: true, diff --git a/src/preview/contentstack-live-preview-HOC.ts b/src/preview/contentstack-live-preview-HOC.ts index 69762bd..e89daf2 100644 --- a/src/preview/contentstack-live-preview-HOC.ts +++ b/src/preview/contentstack-live-preview-HOC.ts @@ -1,10 +1,9 @@ +import { cloneDeep, isEmpty, pick } from "lodash-es"; import { v4 as uuidv4 } from "uuid"; - -import { isEmpty } from "lodash-es"; import { getUserInitData } from "../configManager/config.default"; import Config, { updateConfigFromUrl } from "../configManager/configManager"; -import { VisualBuilder } from "../visualBuilder"; import LivePreview from "../livePreview/live-preview"; +import { handlePageTraversal } from "../livePreview/onPageTraversal"; import { removeFromOnChangeSubscribers } from "../livePreview/removeFromOnChangeSubscribers"; import { OnEntryChangeCallback, @@ -14,9 +13,9 @@ import { OnEntryChangeUnsubscribeParameters, } from "../livePreview/types/onEntryChangeCallback.type"; import { PublicLogger } from "../logger/logger"; -import type { IInitData } from "../types/types"; -import { handlePageTraversal } from "../livePreview/onPageTraversal"; import { handleWebCompare } from "../timeline/compare/compare"; +import type { IExportedConfig, IInitData } from "../types/types"; +import { VisualBuilder } from "../visualBuilder"; class ContentstackLivePreview { private static previewConstructors: @@ -71,6 +70,26 @@ class ContentstackLivePreview { return Config.get().hash; } + static get config(): IExportedConfig { + if (!ContentstackLivePreview.isInitialized()) { + updateConfigFromUrl(); // check if we could extract from the URL + } + const config = Config.get(); + const clonedConfig = cloneDeep(config); + const configToShare = pick(clonedConfig, [ + 'ssr', + 'enable', + 'cleanCslpOnProduction', + 'stackDetails', + 'clientUrlParams', + 'windowType', + 'hash', + 'editButton', + 'mode', + ]); + return configToShare; + } + private static isInitialized(): boolean { return !isEmpty(ContentstackLivePreview.previewConstructors); } diff --git a/src/types/types.ts b/src/types/types.ts index 4bad3b2..1b2c859 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -191,3 +191,5 @@ export interface IVisualBuilderInitEvent { masterLocale: string; }; } + +export type IExportedConfig = Pick \ No newline at end of file