1const CLOSE_BUTTON_RENDER_DELAY=50; 2const modalHandlers=new Map(); 3function getDocumentElement(){ 4if(!document.body.classList.contains('wp-admin')&&!document.body.classList.contains('block-editor-page') ){ 5return document; 6} 7let document_element=document; 8const getEditorIframe=document.querySelectorAll('iframe[name="editor-canvas"]'); 9if(getEditorIframe?.length){ 10const iframeDocument = 11getEditorIframe?.[ 0 ]?.contentWindow?.document||getEditorIframe?.[ 0 ]?.contentDocument; 12if(iframeDocument){ 13document_element=iframeDocument; 14}} 15return document_element; 16} 17function attachGlobalModalHandlers(){ 18attachKeyboardHandlersToDocument(document); 19const iframe=document.querySelector('iframe[name="editor-canvas"]'); 20if(iframe){ 21const iframeDoc=iframe.contentWindow?.document||iframe.contentDocument; 22if(iframeDoc){ 23attachKeyboardHandlersToDocument(iframeDoc); 24}else{ 25iframe.addEventListener('load', ()=> { 26const iframeDocLoaded=iframe.contentWindow?.document||iframe.contentDocument; 27if(iframeDocLoaded){ 28attachKeyboardHandlersToDocument(iframeDocLoaded); 29}}); 30}}else{ 31setTimeout(()=> { 32const delayedIframe=document.querySelector('iframe[name="editor-canvas"]'); 33if(delayedIframe){ 34const iframeDoc=delayedIframe.contentWindow?.document||delayedIframe.contentDocument; 35if(iframeDoc&&!iframeDoc._spectraHandlersAttached){ 36attachKeyboardHandlersToDocument(iframeDoc); 37}} 38}, 1000); 39}} 40function attachKeyboardHandlersToDocument(doc){ 41if(doc._spectraHandlersAttached){ 42return; 43} 44doc._spectraHandlersAttached=true; 45doc.addEventListener('keydown',(e)=> { 46if(( e.target.classList.contains('modal-trigger-element') || 47e.target.getAttribute('data-wp-on--click')==='spectra/modal::actions.toggle' || 48e.target.getAttribute('data-wp-on--click')==='spectra/modal::actions.open') ){ 49if(e.key==='Enter'||e.key===' '){ 50e.preventDefault(); 51e.target.click(); 52}} 53else if(e.target.classList.contains('spectra-modal-popup-close') || 54e.target.getAttribute('data-wp-on--click')==='spectra/modal::actions.close'){ 55if(e.key==='Enter'||e.key===' '){ 56e.preventDefault(); 57e.target.click(); 58}} 59}); 60doc.addEventListener('keydown',(e)=> { 61if(e.key==='Escape'){ 62let activeModal=document.querySelector('.spectra-modal-popup.active'); 63if(!activeModal){ 64const iframe=document.querySelector('iframe[name="editor-canvas"]'); 65if(iframe){ 66const iframeDoc=iframe.contentWindow?.document||iframe.contentDocument; 67if(iframeDoc){ 68activeModal=iframeDoc.querySelector('.spectra-modal-popup.active'); 69}} 70} 71if(activeModal&&activeModal.id){ 72const escPress=activeModal.getAttribute('data-esc-press'); 73if(escPress!=='true'){ 74return; 75} 76e.preventDefault(); 77e.stopPropagation(); 78if(typeof closeModal==='function'){ 79closeModal(activeModal.id); 80}else{ 81activeModal.classList.remove('active'); 82}} 83}}); 84} 85if(document.readyState==='loading'){ 86document.addEventListener('DOMContentLoaded', attachGlobalModalHandlers); 87}else{ 88attachGlobalModalHandlers(); 89} 90function setupModalInitializationListener(){ 91const handleInitialization=(event)=> { 92const { 93blockId, 94overlayClick, 95escPress, 96}=event.detail; 97const args={ 98blockId, 99overlayClick: overlayClick||false, 100escPress: escPress||false, 101}; 102cleanupModalHandlers(blockId); 103attachOverlayClickClose(args); 104attachEscKeyClose(args); 105attachKeyboardHandlers(args); 106attachCloseButtonKeyboard(args); 107}; 108document.addEventListener('spectra:modal:initialized', handleInitialization); 109const iframe=document.querySelector('iframe[name="editor-canvas"]'); 110if(iframe){ 111const iframeDoc=iframe.contentWindow?.document||iframe.contentDocument; 112if(iframeDoc){ 113iframeDoc.addEventListener('spectra:modal:initialized', handleInitialization); 114}} 115} 116if(document.readyState==='loading'){ 117document.addEventListener('DOMContentLoaded', setupModalInitializationListener); 118}else{ 119setupModalInitializationListener(); 120} 121function cleanupModalHandlers(blockId){ 122const handlers=modalHandlers.get(blockId); 123if(handlers){ 124handlers.forEach(( { element, event, handler })=> { 125element.removeEventListener(event, handler); 126}); 127modalHandlers.delete(blockId); 128}} 129function storeHandler(blockId, element, event, handler){ 130if(!modalHandlers.has(blockId) ){ 131modalHandlers.set(blockId, []); 132} 133modalHandlers.get(blockId).push({ element, event, handler }); 134element.addEventListener(event, handler); 135} 136function attachOverlayClickClose(args){ 137const modal=getDocumentElement().getElementById(args.blockId); 138if(!modal){ 139return; 140} 141if(args.overlayClick){ 142const handler=(e)=> { 143if(e.currentTarget===modal&&e.target===modal){ 144closeModal(args.blockId); 145return; 146} 147const popup=modal.querySelector('.spectra-modal-popup'); 148if(!popup||!popup.classList.contains('active') ) return; 149const popupWrap=popup.querySelector('.spectra-modal-popup-wrap'); 150if(!popupWrap) return; 151if(e.target===popup){ 152closeModal(args.blockId); 153}}; 154storeHandler(args.blockId, modal, 'click', handler); 155const popup=modal.querySelector('.spectra-modal-popup'); 156if(popup){ 157const popupHandler=(e)=> { 158const popupWrap=popup.querySelector('.spectra-modal-popup-wrap'); 159if(popupWrap&&e.target===popup){ 160e.stopPropagation(); 161closeModal(args.blockId); 162}}; 163storeHandler(args.blockId, popup, 'click', popupHandler); 164}} 165} 166function attachEscKeyClose(args){ 167if(args.escPress){ 168const handler=(e)=> { 169if(e.key==='Escape'){ 170const modal=getDocumentElement().getElementById(args.blockId); 171if(modal&&modal.classList.contains('active') ){ 172closeModal(args.blockId); 173}} 174}; 175storeHandler(args.blockId, getDocumentElement(), 'keydown', handler); 176}} 177function closeModal(blockId){ 178const modal=getDocumentElement().getElementById(blockId); 179if(! modal) return; 180modal.classList.remove('active'); 181const popup=modal.querySelector('.spectra-modal-popup'); 182if(popup){ 183popup.classList.remove('active'); 184popup.setAttribute('aria-hidden', 'true'); 185popup.removeAttribute('aria-modal'); 186} 187const state=modalHandlers.get(blockId )?.state; 188if(state?.previouslyFocusedElement){ 189setTimeout(()=> { 190if(state.previouslyFocusedElement&&typeof state.previouslyFocusedElement.focus==='function'){ 191state.previouslyFocusedElement.focus(); 192}}, 0); 193} 194const event=new CustomEvent('spectra:modal:closed', { 195detail: { blockId }, 196bubbles: true 197}); 198document.dispatchEvent(event); 199} 200function attachKeyboardHandlers(args){ 201if(!modalHandlers.has(args.blockId) ){ 202modalHandlers.set(args.blockId, []); 203} 204modalHandlers.get(args.blockId).state={ 205previouslyFocusedElement: null, 206isFirstTab: true 207}; 208const keydownHandler=(e)=> { 209const modal=getDocumentElement().getElementById(args.blockId); 210if(! modal||! modal.classList.contains('active') ) return; 211const popup=modal.querySelector('.spectra-modal-popup'); 212if(popup&&popup.getAttribute('aria-hidden')==='true') return; 213const state=modalHandlers.get(args.blockId )?.state; 214if(! state) return; 215const focusable=modal.querySelectorAll('a, button, input, textarea, select, [tabindex]:not
215([tabindex="-1"])' 216); 217if(!focusable.length) return; 218const first=focusable[0]; 219const last=focusable[focusable.length - 1]; 220const activeElement=modal.ownerDocument.activeElement; 221if(e.key==='Tab'){ 222const modalTrigger=modal.getAttribute('data-modal-trigger')||'manual'; 223if(state.isFirstTab&&modalTrigger==='automatic'&&!modal.contains(activeElement) ){ 224e.preventDefault(); 225first.focus(); 226state.isFirstTab=false; 227return; 228} 229if(e.shiftKey&&activeElement===first){ 230e.preventDefault(); 231last.focus(); 232}else if(!e.shiftKey&&activeElement===last){ 233e.preventDefault(); 234first.focus(); 235}else if(!modal.contains(activeElement) ){ 236e.preventDefault(); 237if(e.shiftKey){ 238last.focus(); 239}else{ 240first.focus(); 241}} 242}else if(e.key==='Escape'&&args.escPress){ 243closeModal(args.blockId); 244if(state.previouslyFocusedElement){ 245state.previouslyFocusedElement.focus(); 246}} 247}; 248storeHandler(args.blockId, getDocumentElement(), 'keydown', keydownHandler); 249const focusinHandler=(e)=> { 250const modal=getDocumentElement().getElementById(args.blockId); 251if(! modal||! modal.classList.contains('active') ) return; 252if(!modal.contains(e.target) ){ 253e.preventDefault(); 254e.stopPropagation(); 255const focusable=modal.querySelectorAll('a, button, input, textarea, select, [tabindex]:not([tabindex="-1"])' 256); 257if(focusable.length > 0){ 258focusable[0].focus(); 259}} 260}; 261storeHandler(args.blockId, getDocumentElement(), 'focusin', focusinHandler); 262const openHandler=(event)=> { 263if(event.detail?.blockId&&event.detail.blockId!==args.blockId) return; 264const modal=getDocumentElement().getElementById(args.blockId); 265if(!modal||!modal.classList.contains('active') ) return; 266const state=modalHandlers.get(args.blockId )?.state; 267if(! state) return; 268const focusable=modal.querySelectorAll('a, button, input, textarea, select, [tabindex]:not([tabindex="-1"])' 269); 270if(!focusable.length) return; 271const activeElement=modal.ownerDocument.activeElement; 272if(event.detail?.triggerElement){ 273state.previouslyFocusedElement=event.detail.triggerElement; 274}else{ 275state.previouslyFocusedElement=activeElement; 276} 277state.isFirstTab=true; 278const modalTrigger=event.detail?.modalTrigger||modal.getAttribute('data-modal-trigger')||'manual'; 279if(modalTrigger==='automatic'){ 280}}; 281storeHandler(args.blockId, getDocumentElement(), 'spectra:modal:opened', openHandler); 282} 283function attachCloseButtonKeyboard(args){ 284const modal=getDocumentElement().getElementById(args.blockId); 285if(!modal) return; 286setTimeout(()=> { 287const closeButtons=modal.querySelectorAll('.spectra-modal-popup-close'); 288closeButtons.forEach(button=> { 289if(button.tagName!=='BUTTON'&&button.tagName!=='A'&&!button.hasAttribute('tabindex') ){ 290button.setAttribute('tabindex', '0'); 291} 292if(button.tagName!=='BUTTON'&&!button.hasAttribute('role') ){ 293button.setAttribute('role', 'button'); 294} 295const handler=(e)=> { 296if(e.key==='Enter'||e.key===' '){ 297e.preventDefault(); 298e.stopPropagation(); 299button.click(); 300closeModal(args.blockId); 301}}; 302storeHandler(args.blockId, button, 'keydown', handler); 303}); 304}, CLOSE_BUTTON_RENDER_DELAY); 305};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.