diff --git a/src/ui/cms/index.ts b/src/ui/cms/index.ts index 03e4a164..9be5e41a 100644 --- a/src/ui/cms/index.ts +++ b/src/ui/cms/index.ts @@ -1,7 +1,5 @@ import { createSender } from '../../channel'; -export const registerCmsElement = createSender('cmsRegisterElement'); - export type cmsRegisterElement = { responseType: void, @@ -28,3 +26,26 @@ export type cmsRegisterElement = { [key: string]: unknown, }, }; + +export const registerCmsElement = createSender('cmsRegisterElement'); + +export const isCmsLocation = (name: string, type: 'preview' | 'component' | 'config'): { isLocation: false } | {isLocation: true, dataSetId: string} => { + const params = new URLSearchParams(window.location.search); + + const locationId = params.get('location-id'); + if (!locationId) { + return { isLocation: false }; + } + + const locationParts = locationId.split('-'); + + const [elementName, elementType, id] = locationParts; + if (elementName !== name || elementType !== type || !id) { + return { isLocation: false }; + } + + return { + isLocation: true, + dataSetId: `${elementName}__${type}-${id}`, + }; +};