| |
| |
| |
| |
| |
|
|
| |
| if (typeof window !== 'undefined') { |
| window._nextjsDevtoolsStyleCache = window._nextjsDevtoolsStyleCache || { |
| pendingElements: [], |
| isObserving: false, |
| lastInsertedElement: null, |
| cachedShadowRoot: null, |
| } |
| } |
|
|
| |
| |
| |
| function getShadowRoot() { |
| const cache = window._nextjsDevtoolsStyleCache |
|
|
| |
| if (cache.cachedShadowRoot) { |
| return cache.cachedShadowRoot |
| } |
|
|
| |
| const portal = document.querySelector('nextjs-portal') |
| const shadowRoot = portal?.shadowRoot || null |
|
|
| if (shadowRoot) { |
| cache.cachedShadowRoot = shadowRoot |
| } |
|
|
| return shadowRoot |
| } |
|
|
| |
| |
| |
| |
| function insertElementIntoShadowRoot(element, shadowRoot) { |
| const cache = window._nextjsDevtoolsStyleCache |
|
|
| if (!cache.lastInsertedElement) { |
| shadowRoot.insertBefore(element, shadowRoot.firstChild) |
| } else if (cache.lastInsertedElement.nextSibling) { |
| shadowRoot.insertBefore(element, cache.lastInsertedElement.nextSibling) |
| } else { |
| shadowRoot.appendChild(element) |
| } |
|
|
| cache.lastInsertedElement = element |
| } |
|
|
| function flushCachedElements() { |
| const cache = window._nextjsDevtoolsStyleCache |
| const shadowRoot = getShadowRoot() |
|
|
| if (!shadowRoot) { |
| return |
| } |
|
|
| cache.pendingElements.forEach((element) => { |
| insertElementIntoShadowRoot(element, shadowRoot) |
| }) |
| cache.pendingElements = [] |
| } |
|
|
| function startObservingForPortal() { |
| const cache = window._nextjsDevtoolsStyleCache |
|
|
| if (cache.isObserving) { |
| return |
| } |
| cache.isObserving = true |
|
|
| |
| const shadowRoot = getShadowRoot() |
| if (shadowRoot) { |
| flushCachedElements() |
| return |
| } |
|
|
| |
| const observer = new MutationObserver((mutations) => { |
| if (mutations.length === 0 || mutations[0].addedNodes.length === 0) { |
| return |
| } |
|
|
| |
| |
| const mutationNode = mutations[0].addedNodes[0] |
| let portalNode = null |
| if ( |
| |
| mutationNode.tagName === 'SCRIPT' && |
| mutationNode.getAttribute('data-nextjs-dev-overlay') |
| ) { |
| portalNode = mutationNode.firstChild |
| } else if ( |
| |
| mutationNode.tagName === 'NEXTJS-PORTAL' |
| ) { |
| portalNode = mutationNode |
| } |
| if (!portalNode) { |
| return |
| } |
|
|
| |
| const checkShadowRoot = () => { |
| if (getShadowRoot()) { |
| flushCachedElements() |
| observer.disconnect() |
| cache.isObserving = false |
| } else { |
| |
| setTimeout(checkShadowRoot, 20) |
| } |
| } |
| checkShadowRoot() |
| }) |
|
|
| observer.observe(document.body, { |
| childList: true, |
| subtree: true, |
| }) |
| } |
|
|
| |
| |
| |
| function insertAtTop(element) { |
| |
| element.setAttribute('data-nextjs-dev-tool-style', 'true') |
|
|
| const shadowRoot = getShadowRoot() |
| if (shadowRoot) { |
| |
| insertElementIntoShadowRoot(element, shadowRoot) |
| } else { |
| |
| const cache = window._nextjsDevtoolsStyleCache |
| cache.pendingElements.push(element) |
|
|
| |
| startObservingForPortal() |
| } |
| } |
|
|
| module.exports = insertAtTop |
|
|