@contentstack/live-preview-utils
Version:
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.
1 lines • 2.58 kB
Source Map (JSON)
{"version":3,"sources":["../../../../src/visualBuilder/utils/getChildElements.ts"],"sourcesContent":["/**\n * Gets the first and second child elements of the parent element.\n * @param parentElement The parent element that contains the child elements.\n * @param parentCslpValue The cslp value of the parent element.\n * @returns The first and second child elements and a function to remove the clone.\n */\nexport default function getChildElements(\n parentElement: Element,\n parentCslpValue: string\n): [Element, Element, () => void] | [null, null, () => void] {\n const childElements = parentElement.querySelectorAll(\n `[data-cslp^=\"${parentCslpValue + \".\"}\"]`\n );\n\n // filter out elements that does not end with \".\" + number\n const filteredChildElements = Array.from(childElements).filter(\n (childElement) =>\n childElement.getAttribute(\"data-cslp\")?.match(/\\.\\d+$/) !== null\n );\n\n const firstChild = filteredChildElements.at(0);\n if (!firstChild) return [null, null, () => {}];\n\n const secondChild = filteredChildElements.at(1);\n if (secondChild) return [firstChild, secondChild, () => {}];\n\n // create a dummy clone to get the direction\n const firstChildClone = document.createElement(firstChild.tagName);\n firstChildClone.setAttribute(\n \"class\",\n firstChild.getAttribute(\"class\") ?? \"\"\n );\n\n const HIDE_ELEMENT_CSS =\n \"overflow: hidden !important; width: 0 !important; height: 0 !important; padding: 0 !important; border: 0 !important;\";\n firstChildClone.setAttribute(\"style\", HIDE_ELEMENT_CSS);\n\n parentElement.appendChild(firstChildClone);\n\n function removeClone() {\n parentElement.removeChild(firstChildClone);\n }\n\n return [firstChild, firstChildClone, removeClone];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAMe,SAAR,iBACH,eACA,iBACyD;AACzD,QAAM,gBAAgB,cAAc;AAAA,IAChC,gBAAgB,kBAAkB,GAAG;AAAA,EACzC;AAGA,QAAM,wBAAwB,MAAM,KAAK,aAAa,EAAE;AAAA,IACpD,CAAC,iBAAc;AAhBvB;AAiBY,iCAAa,aAAa,WAAW,MAArC,mBAAwC,MAAM,eAAc;AAAA;AAAA,EACpE;AAEA,QAAM,aAAa,sBAAsB,GAAG,CAAC;AAC7C,MAAI,CAAC,WAAY,QAAO,CAAC,MAAM,MAAM,MAAM;AAAA,EAAC,CAAC;AAE7C,QAAM,cAAc,sBAAsB,GAAG,CAAC;AAC9C,MAAI,YAAa,QAAO,CAAC,YAAY,aAAa,MAAM;AAAA,EAAC,CAAC;AAG1D,QAAM,kBAAkB,SAAS,cAAc,WAAW,OAAO;AACjE,kBAAgB;AAAA,IACZ;AAAA,IACA,WAAW,aAAa,OAAO,KAAK;AAAA,EACxC;AAEA,QAAM,mBACF;AACJ,kBAAgB,aAAa,SAAS,gBAAgB;AAEtD,gBAAc,YAAY,eAAe;AAEzC,WAAS,cAAc;AACnB,kBAAc,YAAY,eAAe;AAAA,EAC7C;AAEA,SAAO,CAAC,YAAY,iBAAiB,WAAW;AACpD;","names":[]}