UNPKG

@sanity/visual-editing

Version:

[![npm stat](https://img.shields.io/npm/dm/@sanity/visual-editing.svg?style=flat-square)](https://npm-stat.com/charts.html?package=@sanity/visual-editing) [![npm version](https://img.shields.io/npm/v/@sanity/visual-editing.svg?style=flat-square)](https://

1 lines 3.03 kB
{"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, plugins}: 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 plugins={plugins}\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","renderVisualEditing","signal","components","history","refresh","zIndex","plugins","clearTimeout","addEventListener","setTimeout","unmount","parentNode","removeChild","document","createElement","body","insertBefore","nextSibling","createRoot","render","jsx","StrictMode","VisualEditing"],"mappings":";;AAUA,IAAIA,OAA2B,MAC3BC,OAAoB,MACpBC,UAAgD;AAE7C,SAASC,oBACdC,QACA;AAAA,EAACC;AAAAA,EAAYC;AAAAA,EAASC;AAAAA,EAASC;AAAAA,EAAQC;AAA6B,GAC9D;AAEFP,aAASQ,aAAaR,OAAO,GAEjCE,OAAOO,iBAAiB,SAAS,MAAM;AAErCT,cAAUU,WAAW,MAAM;AACrBX,eACFA,KAAKY,QAAQ,GACbZ,OAAO,OAELD,SACFA,KAAKc,WAAYC,YAAYf,IAAI,GAEjCA,OAAO;AAAA,OAER,GAAI;AAAA,EACR,CAAA,GAEIA,SACHA,OAAOgB,SAASC,cAAc,uBAAuB,GAErDD,SAASE,KAAKJ,WAAYK,aAAanB,MAAMgB,SAASE,KAAKE,WAAW,IAGnEnB,SACHA,OAAOoB,OAAAA,WAAWrB,IAAI,IAGxBC,KAAKqB,OACHC,2BAAA,IAACC,kBACC,EAAA,UAAAD,2BAAA;AAAA,IAACE,YAAA;AAAA,IAAA;AAAA,MACC;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MAEA,QAAQ;AAAA,IAAA;AAAA,KAEZ,CACF;AACF;;"}