Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/livePreview/__test__/live-preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ describe("incoming postMessage", () => {

describe("testing window event listeners", () => {
let addEventListenerMock: any;
let sendInitEvent = vi.fn().mockImplementation(mockLivePreviewInitEventListener);
const sendInitEvent = vi.fn().mockImplementation(mockLivePreviewInitEventListener);
let livePreviewInstance: LivePreview;

beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/livePreview/eventManager/postMessageEvent.hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Config, { setConfigFromParams } from "../../configManager/configManager";
import { ILivePreviewWindowType } from "../../types/types";
import { addParamsToUrl } from "../../utils";
import { addParamsToUrl, isOpeningInTimeline } from "../../utils";
import livePreviewPostMessage from "./livePreviewEventManager";
import { LIVE_PREVIEW_POST_MESSAGE_EVENTS } from "./livePreviewEventManager.constant";
import {
Expand Down Expand Up @@ -95,7 +95,7 @@ export function sendInitializeLivePreviewPostMessageEvent(): void {
// "init message did not contain contentTypeUid or entryUid."
// );
}
if (Config.get().ssr) {
if (Config.get().ssr || isOpeningInTimeline()) {
addParamsToUrl();
}
Config.set("windowType", windowType);
Expand Down
8 changes: 7 additions & 1 deletion src/utils/addLivePreviewQueryTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ export function addLivePreviewQueryTags(link: string): string {
const ctUid: string | null =
docUrl.searchParams.get("content_type_uid");
const entryUid: string | null = docUrl.searchParams.get("entry_uid");
if (livePreviewHash && ctUid && entryUid) {
const previewTimestamp: string | null = docUrl.searchParams.get("preview_timestamp");
if (livePreviewHash) {
newUrl.searchParams.set("live_preview", livePreviewHash);
}
if(ctUid && entryUid){
newUrl.searchParams.set("content_type_uid", ctUid);
newUrl.searchParams.set("entry_uid", entryUid);
}
if (previewTimestamp) {
newUrl.searchParams.set("preview_timestamp", previewTimestamp);
}
return newUrl.href;
} catch (error) {
PublicLogger.error("Error while adding live preview to URL");
Expand Down
15 changes: 11 additions & 4 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ export { addLivePreviewQueryTags };
export function addParamsToUrl() {
// Setting the query params to all the click events related to current domain
window.addEventListener("click", (event: any) => {
const target: any = event.target;
const targetHref: string | any = target.href;
const clickedElement = event.target;
Comment thread
KANE-99 marked this conversation as resolved.
const anchorElement = clickedElement.closest('a');

// Only proceed if the clicked element is either an anchor or a direct/indirect child of an anchor
if (!anchorElement || !anchorElement.contains(clickedElement)) {
return;
}

const targetHref: string | any = anchorElement.href;
const docOrigin: string = document.location.origin;
if (
targetHref &&
targetHref.includes(docOrigin) &&
!targetHref.includes("live_preview")
) {
const newUrl = addLivePreviewQueryTags(target.href);
event.target.href = newUrl || target.href;
const newUrl = addLivePreviewQueryTags(targetHref);
anchorElement.href = newUrl || targetHref;
}
});
}
Expand Down
Loading