-
Notifications
You must be signed in to change notification settings - Fork 1
fix(discussions): emit REQUEST_DISCUSSION_HIGHLIGHTS when iframe CSLPs mutate #583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop_v4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,14 @@ import { DATA_CSLP_ATTR_SELECTOR } from "../utils/constants"; | |
| import { visualBuilderStyles } from "../visualBuilder.style"; | ||
| import { isValidCslp } from "../../cslp/cslpdata"; | ||
| import { setHighlightVariantFields } from "./useVariantsPostMessageEvent"; | ||
| import visualBuilderPostMessage from "../utils/visualBuilderPostMessage"; | ||
| import { VisualBuilderPostMessageEvents } from "../utils/types/postMessage.types"; | ||
|
|
||
| const VARIANT_UPDATE_DELAY_MS: Readonly<number> = 8000; | ||
| // Debounce window after the last observed mutation before asking the visual | ||
| // editor to re-send discussion highlights. Short enough to feel responsive; | ||
| // long enough to coalesce a burst of mutations into a single request. | ||
| const DISCUSSION_HIGHLIGHTS_DEBOUNCE_MS: Readonly<number> = 200; | ||
|
|
||
| type OnAudienceModeVariantPatchUpdate = { | ||
| highlightVariantFields: boolean; | ||
|
|
@@ -32,6 +38,21 @@ export function updateVariantClasses(): void { | |
| const variant = VisualBuilder.VisualBuilderGlobalState.value.variant; | ||
| const observers: MutationObserver[] = []; | ||
|
|
||
| // Ask the visual editor to re-send discussion highlights once the current | ||
| // burst of data-cslp mutations has settled. The VB owns the field-path list | ||
| // (it can be per-variant) so the iframe cannot re-mount comment icons on | ||
| // its own — it can only request a refresh. | ||
| let highlightsRequestTimer: ReturnType<typeof setTimeout> | null = null; | ||
| const requestDiscussionHighlights = () => { | ||
| if (highlightsRequestTimer !== null) clearTimeout(highlightsRequestTimer); | ||
| highlightsRequestTimer = setTimeout(() => { | ||
| highlightsRequestTimer = null; | ||
| visualBuilderPostMessage?.send( | ||
| VisualBuilderPostMessageEvents.REQUEST_DISCUSSION_HIGHLIGHTS | ||
| ); | ||
| }, DISCUSSION_HIGHLIGHTS_DEBOUNCE_MS); | ||
| }; | ||
|
|
||
| // Helper function to update element classes | ||
| const updateElementClasses = ( | ||
| element: HTMLElement, | ||
|
|
@@ -156,6 +177,7 @@ export function updateVariantClasses(): void { | |
| DATA_CSLP_ATTR_SELECTOR | ||
| ); | ||
| updateElementClasses(element, dataCslp || "", observer); | ||
| requestDiscussionHighlights(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can debounce it right? I don't want us to spam, postmessages from LP SDK through mutation observer
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's already debounced — If you'd prefer a longer window (say 300ms or 500ms) for extra safety against very chunky React renders, easy to bump. |
||
| } | ||
| }); | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { describe, it, expect, beforeEach, afterEach } from "vitest"; | ||
| import { | ||
| updateHighlightedCommentIconPosition, | ||
| removeAllHighlightedCommentIcons, | ||
| } from "../generateHighlightedComment"; | ||
|
|
||
| describe("updateHighlightedCommentIconPosition", () => { | ||
| let container: HTMLDivElement; | ||
|
|
||
| beforeEach(() => { | ||
| container = document.createElement("div"); | ||
| container.className = "visual-builder__container"; | ||
| document.body.appendChild(container); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| document.body.removeChild(container); | ||
| }); | ||
|
|
||
| const makeIcon = (fieldPath: string): HTMLDivElement => { | ||
| const icon = document.createElement("div"); | ||
| icon.className = "highlighted-comment collab-icon"; | ||
| icon.setAttribute("field-path", fieldPath); | ||
| icon.style.position = "fixed"; | ||
| icon.style.top = "100px"; | ||
| icon.style.left = "200px"; | ||
| container.appendChild(icon); | ||
| return icon; | ||
| }; | ||
|
|
||
| const makeTarget = (cslpValue: string): HTMLDivElement => { | ||
| const el = document.createElement("div"); | ||
| el.setAttribute("data-cslp", cslpValue); | ||
| container.appendChild(el); | ||
| return el; | ||
| }; | ||
|
|
||
| it("keeps an icon whose target element is still present", () => { | ||
| const cslp = "content.entry.en-us.field"; | ||
| makeTarget(cslp); | ||
| makeIcon(cslp); | ||
|
|
||
| updateHighlightedCommentIconPosition(); | ||
|
|
||
| expect( | ||
| document.querySelector(`.highlighted-comment[field-path="${cslp}"]`), | ||
| ).not.toBeNull(); | ||
| }); | ||
|
|
||
| it("removes an orphaned icon when its target element is no longer found", () => { | ||
| // Stale CSLP with no matching element — simulates the element having | ||
| // been mutated to a variant CSLP value. | ||
| const staleCslp = "content.entry.en-us.old_field"; | ||
| makeIcon(staleCslp); | ||
|
|
||
| updateHighlightedCommentIconPosition(); | ||
|
|
||
| expect( | ||
| document.querySelector(`.highlighted-comment[field-path="${staleCslp}"]`), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("removes only orphaned icons and keeps valid ones", () => { | ||
| const validCslp = "content.entry.en-us.valid"; | ||
| const staleCslp = "content.entry.en-us.stale"; | ||
|
|
||
| makeTarget(validCslp); | ||
| makeIcon(validCslp); | ||
| makeIcon(staleCslp); | ||
|
|
||
| updateHighlightedCommentIconPosition(); | ||
|
|
||
| expect( | ||
| document.querySelector(`.highlighted-comment[field-path="${validCslp}"]`), | ||
| ).not.toBeNull(); | ||
| expect( | ||
| document.querySelector(`.highlighted-comment[field-path="${staleCslp}"]`), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("does not throw when there are no icons", () => { | ||
| expect(() => updateHighlightedCommentIconPosition()).not.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("removeAllHighlightedCommentIcons", () => { | ||
| it("removes every .highlighted-comment element", () => { | ||
| ["a", "b", "c"].forEach((cslp) => { | ||
| const icon = document.createElement("div"); | ||
| icon.className = "highlighted-comment collab-icon"; | ||
| icon.setAttribute("field-path", cslp); | ||
| document.body.appendChild(icon); | ||
| }); | ||
|
|
||
| expect(document.querySelectorAll(".highlighted-comment").length).toBe(3); | ||
|
|
||
| removeAllHighlightedCommentIcons(); | ||
|
|
||
| expect(document.querySelectorAll(".highlighted-comment").length).toBe(0); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have used debounce from lodash instead of custom logic.