{"version":3,"file":"renderVisualEditing.cjs","sources":["../../src/ui/renderVisualEditing.tsx"],"sourcesContent":["/**\n * The purpose of this file is to contain the logic for rendering the <VisualEditing />\n * component in a way that is easy to lazy load for the `enableVisualEditing` function.\n */\n\nimport {StrictMode} from 'react'\nimport {createRoot, type Root} from 'react-dom/client'\nimport type {VisualEditingOptions} from '../types'\nimport {VisualEditing} from './VisualEditing'\n\nlet node: HTMLElement | null = null\nlet root: Root | null = null\nlet cleanup: ReturnType<typeof setTimeout> | null = null\n\nexport function renderVisualEditing(\n  signal: AbortSignal,\n  {components, history, refresh, zIndex}: VisualEditingOptions,\n): void {\n  // Cancel pending cleanups, this is useful to avoid overlays blinking as the parent app transition between URLs, or hot module reload is happening\n  if (cleanup) clearTimeout(cleanup)\n  // Setup a cleanup function listener right away, as the signal might abort in-between the next steps\n  signal.addEventListener('abort', () => {\n    // Handle React StrictMode, delay cleanup for a second in case it's a rerender\n    cleanup = setTimeout(() => {\n      if (root) {\n        root.unmount()\n        root = null\n      }\n      if (node) {\n        node.parentNode!.removeChild(node)\n\n        node = null\n      }\n    }, 1000)\n  })\n\n  if (!node) {\n    node = document.createElement('sanity-visual-editing')\n    // render sanity-visual-editing after closing </body> tag\n    document.body.parentNode!.insertBefore(node, document.body.nextSibling)\n  }\n\n  if (!root) {\n    root = createRoot(node)\n  }\n\n  root.render(\n    <StrictMode>\n      <VisualEditing\n        components={components}\n        history={history}\n        refresh={refresh}\n        zIndex={zIndex}\n        // Disabling the portal, as this function is already making sure the overlays render in the right spot\n        portal={false}\n      />\n    </StrictMode>,\n  )\n}\n"],"names":["node","root","cleanup","exports","renderVisualEditing","signal","components","history","refresh","zIndex","clearTimeout","addEventListener","setTimeout","unmount","parentNode","removeChild","document","createElement","body","insertBefore","nextSibling","createRoot","render","jsx","StrictMode","children","jsxRuntime","VisualEditing","portal"],"mappings":"iIAUA,IAAIA,EAA2B,KAC3BC,EAAoB,KACpBC,EAAgD,KA8CpDC,QAAAC,oBA5CO,SACLC,GACAC,WAACA,EAAAA,QAAYC,EAAAA,QAASC,EAAAA,OAASC,IAG3BP,GAASQ,aAAaR,GAE1BG,EAAOM,iBAAiB,SAAS,KAE/BT,EAAUU,YAAW,KAEjBX,IAAAA,EAAKY,UACLZ,EAAO,MAELD,IACFA,EAAKc,WAAYC,YAAYf,GAE7BA,EAAO,KAAA,GAER,IAAI,IAGJA,IACHA,EAAOgB,SAASC,cAAc,yBAE9BD,SAASE,KAAKJ,WAAYK,aAAanB,EAAMgB,SAASE,KAAKE,cAGxDnB,IACHA,EAAOoB,EAAAA,WAAWrB,IAGpBC,EAAKqB,OACHC,EAAAA,IAACC,aACC,CAAAC,SAAAC,EAAAH,IAACI,EAAAA,cAAA,CACCrB,aACAC,UACAC,UACAC,SAEAmB,QAAQ,MAIhB"}