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 2.21 kB
{"version":3,"file":"enableVisualEditing.cjs","sources":["../../src/ui/enableVisualEditing.tsx"],"sourcesContent":["import type {DisableVisualEditing, VisualEditingOptions} from '../types'\n\n/**\n * Enables Visual Editing overlay in a page with sourcemap encoding.\n *\n * This will overlay UI on hovered elements that deep-links to Sanity Studio.\n * @public\n */\nexport function enableVisualEditing(options: VisualEditingOptions = {}): DisableVisualEditing {\n const controller = new AbortController()\n // Lazy load everything, react, the app, all of it\n import('./renderVisualEditing').then(({renderVisualEditing}) => {\n const {signal} = controller\n /**\n * Due to lazy loading it's possible for the following to happen, and is a consequence of dynamic ESM imports not being cancellable natively:\n * 1. Userland calls `const disableVisualEditing = enableVisualEditing()` and the dynamic ESM import is started.\n * 2. The dynamic import uses the network, and it takes a while to load.\n * 3. The user navigates to a different page in the app that doesn't need Visual Editing, for example a login page.\n * 4. Since the app is no longer in a state where Visual Editing is needed, the user calls `disableVisualEditing()`.\n * 5. The dynamic import eventually resolves and this function is called.\n * When this happens we want to skip calling `renderVisualEditing` since we know it's no longer needed.\n */\n if (signal.aborted) return\n\n // Hand off to the render function with the signal, which will be subscribed to for detecting when to cancel the rendering if needed and unmount the app.\n renderVisualEditing(signal, options)\n })\n\n return () => {\n controller.abort()\n }\n}\n"],"names":["enableVisualEditing","options","controller","AbortController","then","renderVisualEditing","signal","aborted","abort"],"mappings":";AAQgBA,SAAAA,oBAAoBC,UAAgC,IAA0B;AACtFC,QAAAA,aAAa,IAAIC,gBAAgB;AAEhC,SAAA,QAAA,QAAA,EAAA,KAAA,WAAA;AAAA,WAAA,QAAA,2BAAuB;AAAA,KAAEC,KAAK,CAAC;AAAA,IAACC;AAAAA,EAAAA,MAAyB;AACxD,UAAA;AAAA,MAACC;AAAAA,IAAAA,IAAUJ;AAUbI,WAAOC,WAGXF,oBAAoBC,QAAQL,OAAO;AAAA,EACpC,CAAA,GAEM,MAAM;AACXC,eAAWM,MAAM;AAAA,EACnB;AACF;;"}