| 1 |
( function( window, document ) { |
| 2 |
const paletteConfig = window.mainwpCommandPalette || {}; |
| 3 |
|
| 4 |
function getDispatcher() { |
| 5 |
if ( |
| 6 |
! window.wp || |
| 7 |
! window.wp.data || |
| 8 |
typeof window.wp.data.dispatch !== 'function' |
| 9 |
) { |
| 10 |
return null; |
| 11 |
} |
| 12 |
|
| 13 |
return window.wp.data.dispatch( 'core/commands' ); |
| 14 |
} |
| 15 |
|
| 16 |
function applyCommands() { |
| 17 |
const dispatcher = getDispatcher(); |
| 18 |
|
| 19 |
if ( |
| 20 |
! dispatcher || |
| 21 |
typeof dispatcher.registerCommand !== 'function' || |
| 22 |
typeof dispatcher.unregisterCommand !== 'function' |
| 23 |
) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
if ( Array.isArray( paletteConfig.unregister ) ) { |
| 28 |
paletteConfig.unregister.forEach( ( commandName ) => { |
| 29 |
if ( commandName ) { |
| 30 |
dispatcher.unregisterCommand( commandName ); |
| 31 |
} |
| 32 |
} ); |
| 33 |
} |
| 34 |
|
| 35 |
if ( ! Array.isArray( paletteConfig.commands ) ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
paletteConfig.commands.forEach( ( command ) => { |
| 40 |
if ( ! command || ! command.name || ! command.label || ! command.url ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
dispatcher.registerCommand( { |
| 45 |
name: command.name, |
| 46 |
label: command.label, |
| 47 |
searchLabel: command.searchLabel || command.label, |
| 48 |
keywords: Array.isArray( command.keywords ) ? command.keywords : [], |
| 49 |
category: 'view', |
| 50 |
callback: ( { close } = {} ) => { |
| 51 |
if ( typeof close === 'function' ) { |
| 52 |
close(); |
| 53 |
} |
| 54 |
|
| 55 |
window.location.assign( command.url ); |
| 56 |
}, |
| 57 |
} ); |
| 58 |
} ); |
| 59 |
} |
| 60 |
|
| 61 |
function scheduleApply() { |
| 62 |
applyCommands(); |
| 63 |
window.setTimeout( applyCommands, 0 ); |
| 64 |
window.setTimeout( applyCommands, 150 ); |
| 65 |
window.setTimeout( applyCommands, 500 ); |
| 66 |
window.setTimeout( applyCommands, 1500 ); |
| 67 |
} |
| 68 |
|
| 69 |
if ( document.readyState === 'loading' ) { |
| 70 |
document.addEventListener( 'DOMContentLoaded', scheduleApply, { once: true } ); |
| 71 |
} else { |
| 72 |
scheduleApply(); |
| 73 |
} |
| 74 |
|
| 75 |
window.addEventListener( 'load', applyCommands, { once: true } ); |
| 76 |
} )( window, document ); |
| 77 |
|