Skip to content

Commit 944c02c

Browse files
Merge pull request #315 from contentstack/develop_v3
v3.0.2
2 parents a4bb83e + 6960555 commit 944c02c

22 files changed

Lines changed: 473 additions & 239 deletions

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021-2024 Contentstack
3+
Copyright (c) 2021-2025 Contentstack
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/live-preview-utils",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"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.",
55
"type": "module",
66
"types": "dist/legacy/index.d.ts",

src/configManager/__test__/handleUserConfig.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,63 @@ describe("handleInitData()", () => {
370370
expect(config.stackDetails.locale).toBe("en-us");
371371
});
372372
});
373+
374+
describe("handleInitData() - editButton configuration", () => {
375+
let config: DeepSignal<IConfig>;
376+
377+
beforeEach(() => {
378+
Config.reset();
379+
config = Config.get();
380+
});
381+
382+
afterAll(() => {
383+
Config.reset();
384+
});
385+
386+
test("should set editButton enable from initData", () => {
387+
const initData: Partial<IInitData> = {
388+
editButton: {
389+
enable: false,
390+
},
391+
};
392+
393+
handleInitData(initData);
394+
expect(config.editButton.enable).toBe(false);
395+
});
396+
397+
test("should set editButton exclude from initData if it is an array", () => {
398+
const initData: Partial<IInitData> = {
399+
editButton: {
400+
exclude: ["insideLivePreviewPortal"],
401+
},
402+
};
403+
404+
handleInitData(initData);
405+
expect(config.editButton.exclude).toEqual(["insideLivePreviewPortal"]);
406+
});
407+
408+
test("should set editButton position from initData", () => {
409+
const initData: Partial<IInitData> = {
410+
editButton: {
411+
position: "bottom",
412+
},
413+
};
414+
415+
handleInitData(initData);
416+
expect(config.editButton.position).toBe("bottom");
417+
});
418+
419+
test("should set editButton includeByQueryParameter from initData", () => {
420+
const initData: Partial<IInitData> = {
421+
editButton: {
422+
includeByQueryParameter: false,
423+
},
424+
};
425+
426+
handleInitData(initData);
427+
expect(config.editButton.includeByQueryParameter).toBe(false);
428+
});
429+
});
373430
});
374431

375432
describe("handleClientUrlParams()", () => {

src/cslp/cslpdata.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ function getParentPathDetails(
109109

110110
/**
111111
* Returns metadata for a multiple field in a content entry.
112+
* @summary ONLY USE THESE RETURNED VALUES WHEN FIELD IS MULTIPLE
113+
* @summary IT GIVES WRONG DATA IF FIELD IS NOT MULTIPLE
112114
* @param content_type_uid - The UID of the content type.
113115
* @param entry_uid - The UID of the content entry.
114116
* @param locale - The locale of the content entry.

src/livePreview/editButton/__test__/editButton.test.ts

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { Mock } from "vitest";
1+
import { Mock, vitest } from "vitest";
22
import { DOMRect } from "../../../__test__/utils";
33
import {
44
createMultipleEditButton,
55
createSingularEditButton,
66
getEditButtonPosition,
7+
shouldRenderEditButton
78
} from "../editButton";
9+
import Config from "../../../configManager/configManager";
10+
import * as inIframe from "../../../common/inIframe";
11+
import { PublicLogger } from "../../../logger/logger";
812

913
let editCallback: Mock<(e: MouseEvent) => void> | undefined;
1014
let linkCallback: Mock<(e: MouseEvent) => void> | undefined;
@@ -241,3 +245,75 @@ describe("Edit button for Link", () => {
241245
expect(linkCallback).toBeCalled();
242246
});
243247
});
248+
249+
describe("shouldRenderEditButton", () => {
250+
test("should return true if the config has enabled as true", () => {
251+
vitest.spyOn(Config, "get").mockReturnValue({ editButton: { enable: true } });
252+
expect(shouldRenderEditButton()).toBe(true);
253+
});
254+
test("should return false if the config has enabled as false", () => {
255+
vitest.spyOn(Config, "get").mockReturnValue({ editButton: { enable: false } });
256+
expect(shouldRenderEditButton()).toBe(false);
257+
});
258+
259+
describe("includeByQueryParameter", () => {
260+
test("should log error and return false if enable key is undefined", () => {
261+
const loggerSpy = vitest.spyOn(PublicLogger, "error");
262+
vitest.spyOn(Config, "get").mockReturnValue({ editButton: {} });
263+
expect(shouldRenderEditButton()).toBe(false);
264+
expect(loggerSpy).toHaveBeenCalledWith("enable key is required inside editButton object");
265+
});
266+
267+
test("should return true if cslp-buttons query parameter is true", () => {
268+
vitest.spyOn(Config, "get").mockReturnValue({ editButton: { enable: true } });
269+
vitest.spyOn(window, "location", "get").mockReturnValue({
270+
href: "http://example.com?cslp-buttons=true",
271+
} as Location);
272+
expect(shouldRenderEditButton()).toBe(true);
273+
});
274+
275+
test("should return false if cslp-buttons query parameter is false", () => {
276+
vitest.spyOn(Config, "get").mockReturnValue({ editButton: { enable: true } });
277+
vitest.spyOn(window, "location", "get").mockReturnValue({
278+
href: "http://example.com?cslp-buttons=false",
279+
} as Location);
280+
expect(shouldRenderEditButton()).toBe(false);
281+
});
282+
283+
test("should return true if cslp-buttons query parameter is not present", () => {
284+
vitest.spyOn(Config, "get").mockReturnValue({ editButton: { enable: true } });
285+
vitest.spyOn(window, "location", "get").mockReturnValue({
286+
href: "http://example.com",
287+
} as Location);
288+
expect(shouldRenderEditButton()).toBe(true);
289+
});
290+
})
291+
describe("exclude", () => {
292+
test("should return false if the config has exclude as `insideLivePreviewPortal` and the element is inside live preview portal", () => {
293+
vitest.spyOn(inIframe, "inIframe").mockReturnValue(true);
294+
vitest.spyOn(Config, "get").mockReturnValue({ editButton: { enable: true, exclude: ["insideLivePreviewPortal"] } });
295+
expect(shouldRenderEditButton()).toBe(false);
296+
});
297+
test("should return true if the config has exclude as `insideLivePreviewPortal` and the element is not inside live preview portal", () => {
298+
vitest.spyOn(inIframe, "inIframe").mockReturnValue(false);
299+
vitest.spyOn(Config, "get").mockReturnValue({ editButton: { enable: true, exclude: ["insideLivePreviewPortal"] } });
300+
expect(shouldRenderEditButton()).toBe(true);
301+
});
302+
test("should return false if the config has exclude as `outsideLivePreviewPortal` and the element is not inside live preview portal", () => {
303+
vitest.spyOn(inIframe, "inIframe").mockReturnValue(false);
304+
vitest.spyOn(Config, "get").mockReturnValue({ editButton: { enable: true, exclude: ["outsideLivePreviewPortal"] } });
305+
expect(shouldRenderEditButton()).toBe(false);
306+
});
307+
test("should return true if the config has exclude as `outsideLivePreviewPortal` and the element is inside live preview portal", () => {
308+
vitest.spyOn(inIframe, "inIframe").mockReturnValue(true);
309+
vitest.spyOn(Config, "get").mockReturnValue({ editButton: { enable: true, exclude: ["outsideLivePreviewPortal"] } });
310+
expect(shouldRenderEditButton()).toBe(true);
311+
});
312+
})
313+
314+
test("should return false if the website is rendered in Builder", () => {
315+
vitest.spyOn(Config, "get").mockReturnValue({ editButton: { enable: true }, windowType: "builder" });
316+
vitest.spyOn(inIframe, "inIframe").mockReturnValue(true);
317+
expect(shouldRenderEditButton()).toBe(false);
318+
})
319+
})

src/livePreview/editButton/__test__/shouldRenderEditButton.test.ts

Lines changed: 0 additions & 120 deletions
This file was deleted.

src/livePreview/editButton/editButton.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ export function shouldRenderEditButton(): boolean {
225225

226226
// case outside live preview
227227
if (
228+
!iFrameCheck &&
228229
config.editButton.exclude?.find(
229230
(exclude) => exclude === "outsideLivePreviewPortal"
230231
)

src/timeline/compare/compare.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const voidElements = new Set([
1818
"source",
1919
"track",
2020
"wbr",
21+
"video"
2122
]);
2223

2324
const LEAF_CSLP_SELECTOR = "[data-cslp]:not(:has([data-cslp]))";

src/visualBuilder/components/FieldRevert/FieldRevertComponent.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ export const BASE_VARIANT_STATUS: IVariantStatus = {
4444
};
4545

4646
export async function getFieldVariantStatus(
47-
fieldPathWithIndex: string
47+
fieldMetadata: CslpData
4848
): Promise<IVariantStatus | undefined> {
4949
try {
5050
const result = await visualBuilderPostMessage?.send<IVariantStatus>(
5151
"get-field-variant-status",
52-
fieldPathWithIndex
52+
fieldMetadata
5353
);
5454
return result;
5555
} catch (error) {

0 commit comments

Comments
 (0)