Skip to content

Commit 1304bb0

Browse files
authored
Merge pull request #606 from contentstack/develop_v4
Develop v4
2 parents 7121495 + 91c6c6e commit 1304bb0

19 files changed

Lines changed: 678 additions & 84 deletions

src/common/inIframe.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ export function inIframe(): boolean {
88
}
99
}
1010

11+
export function inVisualEditor(): boolean{
12+
try {
13+
return inIframe() && window?.name == 'visual-editor'
14+
} catch (e) {
15+
return false;
16+
}
17+
}
18+
1119
export function isOpeningInNewTab(): boolean {
1220
try {
1321
if(hasWindow()) {

src/configManager/__test__/configManager.test.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Config, { updateConfigFromUrl } from "../configManager";
1+
import Config, { updateConfigFromUrl, syncToStackSdk } from "../configManager";
22
import { getDefaultConfig, getUserInitData } from "../config.default";
33
import { DeepSignal } from "deepsignal";
44
import { IConfig } from "../../types/types";
@@ -102,6 +102,73 @@ describe("config default flags", () => {
102102
});
103103
});
104104

105+
describe("syncToStackSdk", () => {
106+
beforeEach(() => {
107+
Config.reset();
108+
});
109+
110+
afterAll(() => {
111+
Config.reset();
112+
});
113+
114+
test("should set hash, stackSdkLivePreview.hash and stackSdkLivePreview.live_preview when hash is provided", () => {
115+
syncToStackSdk({ hash: "abc123" });
116+
117+
const config = Config.get();
118+
expect(config.stackSdk.live_preview.hash).toBe("abc123");
119+
expect(config.stackSdk.live_preview.live_preview).toBe("abc123");
120+
expect(config.stackSdk.live_preview.content_type_uid).toBeUndefined();
121+
expect(config.stackSdk.live_preview.entry_uid).toBeUndefined();
122+
});
123+
124+
test("should set content_type_uid on stackSdk when contentTypeUid is provided", () => {
125+
syncToStackSdk({ contentTypeUid: "blog" });
126+
127+
const config = Config.get();
128+
expect(config.stackSdk.live_preview.content_type_uid).toBe("blog");
129+
expect(config.stackSdk.live_preview.hash).toBeUndefined();
130+
expect(config.stackSdk.live_preview.entry_uid).toBeUndefined();
131+
});
132+
133+
test("should set entry_uid on stackSdk when entryUid is provided", () => {
134+
syncToStackSdk({ entryUid: "entry-42" });
135+
136+
const config = Config.get();
137+
expect(config.stackSdk.live_preview.entry_uid).toBe("entry-42");
138+
expect(config.stackSdk.live_preview.hash).toBeUndefined();
139+
expect(config.stackSdk.live_preview.content_type_uid).toBeUndefined();
140+
});
141+
142+
test("should set all three fields when all params are provided", () => {
143+
syncToStackSdk({ hash: "h1", contentTypeUid: "page", entryUid: "e1" });
144+
145+
const config = Config.get();
146+
expect(config.stackSdk.live_preview.hash).toBe("h1");
147+
expect(config.stackSdk.live_preview.live_preview).toBe("h1");
148+
expect(config.stackSdk.live_preview.content_type_uid).toBe("page");
149+
expect(config.stackSdk.live_preview.entry_uid).toBe("e1");
150+
});
151+
152+
test("should skip falsy values — null and undefined are ignored", () => {
153+
syncToStackSdk({ hash: null, contentTypeUid: undefined, entryUid: null });
154+
155+
const config = Config.get();
156+
expect(config.stackSdk.live_preview.hash).toBeUndefined();
157+
expect(config.stackSdk.live_preview.content_type_uid).toBeUndefined();
158+
expect(config.stackSdk.live_preview.entry_uid).toBeUndefined();
159+
});
160+
161+
test("should not overwrite existing stackSdk values for keys not passed", () => {
162+
syncToStackSdk({ hash: "first", contentTypeUid: "ct1", entryUid: "e1" });
163+
syncToStackSdk({ hash: "second" });
164+
165+
const config = Config.get();
166+
expect(config.stackSdk.live_preview.hash).toBe("second");
167+
expect(config.stackSdk.live_preview.content_type_uid).toBe("ct1");
168+
expect(config.stackSdk.live_preview.entry_uid).toBe("e1");
169+
});
170+
});
171+
105172
describe("update config from url", () => {
106173
let config: DeepSignal<IConfig>;
107174

src/configManager/config.default.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,6 @@ export function getDefaultConfig(): IConfig {
118118
payload: [],
119119
},
120120
enableLivePreviewOutsideIframe: undefined,
121+
pageContext: null,
121122
};
122123
}

src/configManager/configManager.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,50 @@ export function setConfigFromParams(
7373
const content_type_uid = urlParams.get("content_type_uid");
7474
const entry_uid = urlParams.get("entry_uid");
7575

76-
const stackSdkLivePreview = Config.get().stackSdk.live_preview;
77-
7876
if (live_preview) {
7977
Config.set("hash", live_preview);
80-
stackSdkLivePreview.hash = live_preview;
81-
stackSdkLivePreview.live_preview = live_preview;
8278
}
8379

8480
if (content_type_uid) {
8581
Config.set("stackDetails.contentTypeUid", content_type_uid);
86-
stackSdkLivePreview.content_type_uid = content_type_uid;
8782
}
8883

8984
if (entry_uid) {
9085
Config.set("stackDetails.entryUid", entry_uid);
91-
stackSdkLivePreview.entry_uid = entry_uid;
86+
}
87+
88+
syncToStackSdk({
89+
hash: live_preview,
90+
contentTypeUid: content_type_uid,
91+
entryUid: entry_uid,
92+
});
93+
}
94+
95+
/**
96+
* Syncs hash, contentTypeUid, and entryUid into the user's stackSdk.live_preview object.
97+
* Auto-effects via deepsignal were ruled out because Config.reset() replaces the deepSignal
98+
* instance, which would blind any bound effect. Explicit sync is the safe alternative.
99+
*/
100+
export function syncToStackSdk({
101+
hash,
102+
contentTypeUid,
103+
entryUid,
104+
}: {
105+
hash?: string | null;
106+
contentTypeUid?: string | null;
107+
entryUid?: string | null;
108+
}): void {
109+
const stackSdkLivePreview = Config.get().stackSdk.live_preview;
110+
111+
if (hash) {
112+
stackSdkLivePreview.hash = hash;
113+
stackSdkLivePreview.live_preview = hash;
114+
}
115+
if (contentTypeUid) {
116+
stackSdkLivePreview.content_type_uid = contentTypeUid;
117+
}
118+
if (entryUid) {
119+
stackSdkLivePreview.entry_uid = entryUid;
92120
}
93121

94122
Config.set("stackSdk.live_preview", stackSdkLivePreview);

0 commit comments

Comments
 (0)