-
Notifications
You must be signed in to change notification settings - Fork 1
feat(VB-2354): notify the editor when the entries on the page change #648
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 |
|---|---|---|
| @@ -1,34 +1,46 @@ | ||
| import { extractDetailsFromCslp, isValidCslp } from "../../cslp/cslpdata"; | ||
|
|
||
| type EntryIdentifiers = { | ||
| entriesInCurrentPage: { | ||
| entryUid: string; | ||
| contentTypeUid: string; | ||
| locale: string; | ||
| }[]; | ||
| } | ||
| export type EntryIdentifier = { | ||
| entryUid: string; | ||
| contentTypeUid: string; | ||
| locale: string; | ||
| }; | ||
|
|
||
| export type EntryIdentifiers = { | ||
| entriesInCurrentPage: EntryIdentifier[]; | ||
| }; | ||
|
|
||
| /** | ||
| * Distinct entries rendered on the page, read from `data-cslp`. The same entry | ||
| * in two locales is two results: the editor keeps one channel per entry+locale. | ||
| */ | ||
| export function getEntryIdentifiersInCurrentPage(): EntryIdentifiers { | ||
| const elementsWithCslp = Array.from( | ||
| document.querySelectorAll("[data-cslp]") | ||
| ); | ||
| const uniqueEntriesMap = new Map<string, { entryUid: string, contentTypeUid: string, locale: string}>(); | ||
| const uniqueEntriesMap = new Map<string, EntryIdentifier>(); | ||
| elementsWithCslp.forEach((element) => { | ||
| const cslpValue = element.getAttribute("data-cslp"); | ||
| if (!isValidCslp(cslpValue)) return; | ||
| const cslpData = extractDetailsFromCslp(cslpValue); | ||
| uniqueEntriesMap.set(cslpData.entry_uid, | ||
| { | ||
| entryUid: cslpData.entry_uid, | ||
| contentTypeUid: cslpData.content_type_uid, | ||
| locale: cslpData.locale | ||
| } | ||
| ); | ||
| uniqueEntriesMap.set(`${cslpData.entry_uid}.${cslpData.locale}`, { | ||
|
Contributor
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. The dedup key moved from Correct for channel subscriptions, but the callers were written against the old shape. Worth a check on |
||
| entryUid: cslpData.entry_uid, | ||
| contentTypeUid: cslpData.content_type_uid, | ||
| locale: cslpData.locale, | ||
| }); | ||
| }); | ||
|
|
||
| const uniqueEntriesArray = Array.from(uniqueEntriesMap.values()); | ||
|
|
||
| return { | ||
| entriesInCurrentPage: uniqueEntriesArray, | ||
| entriesInCurrentPage: Array.from(uniqueEntriesMap.values()), | ||
| }; | ||
| } | ||
|
|
||
| /** Order-independent fingerprint of the entry set, used to notify only on change. */ | ||
| export function getEntryIdentifiersSignature( | ||
| entries: EntryIdentifier[] | ||
| ): string { | ||
| return entries | ||
| .map((entry) => `${entry.entryUid}.${entry.locale}`) | ||
| .sort() | ||
| .join("|"); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ export enum VisualBuilderPostMessageEvents { | |
|
|
||
| // FROM visual builder | ||
| GET_ALL_ENTRIES_IN_CURRENT_PAGE = "get-entries-in-current-page", | ||
| ENTRIES_IN_PAGE_CHANGED = "entries-in-page-changed", | ||
|
Contributor
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. Can we make this
Worth settling now: this string is the wire contract on an SDK that customers pin, so renaming after release means supporting both forever. |
||
| HIDE_FOCUS_OVERLAY = "hide-focus-overlay", | ||
| SHOW_DRAFT_FIELDS = "show-draft-fields", | ||
| REMOVE_DRAFT_FIELDS = "remove-draft-fields", | ||
|
|
||
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.
The observer is
{ childList: true, subtree: true }, so attribute-only changes never reachnotifyEntriesInPageIfChanged.useRecalculateVariantDataCSLPValuesrewritesdata-cslpin place on a variant switch. Same nodes, different entry uids, no mutation record, no push. The editor would keep the old entries' channels and miss the new ones.Either add
attributes: true, attributeFilter: ["data-cslp"]to the observe call, or callnotifyEntriesInPageIfChanged()at the end of the recalculation path.