File size: 2,852 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
import { select, subscribe } from '@wordpress/data';
/**
* Checks self and top to determine if we are being loaded in an iframe.
* Can't use window.frameElement because we are being embedded from a different origin.
* @returns {boolean} Whether this script is loaded in a iframe.
*/
export function inIframe() {
try {
return window.self !== window.top;
} catch ( e ) {
return true;
}
}
/**
* Sends a message object to the parent. The object is extended to include a type that
* identifies the source as Gutenberg related.
* @param {Object} message object containing the action to be performed on the parent and any require options
*/
export function sendMessage( message ) {
if ( ! window || ! window.parent ) {
return;
}
window.parent.postMessage( { ...message, type: 'gutenbergIframeMessage' }, '*' );
}
/**
* Indicates if the block editor has been initialized.
* @returns {Promise} Promise that resolves when the editor has been initialized.
*/
export const isEditorReady = async () =>
new Promise( ( resolve ) => {
const unsubscribe = subscribe( () => {
// Calypso sends the message as soon as the iframe is loaded, so we
// need to be sure that the editor is initialized and the core blocks
// registered. There is an unstable selector for that, so we use
// `isCleanNewPost` otherwise which is triggered when everything is
// initialized if the post is new.
const editorIsReady = select( 'core/editor' ).__unstableIsEditorReady
? select( 'core/editor' ).__unstableIsEditorReady()
: select( 'core/editor' ).isCleanNewPost();
if ( editorIsReady ) {
unsubscribe();
resolve();
}
} );
} );
/**
* Indicates if the block editor has been initialized with blocks.
* @returns {Promise} Promise that resolves when the editor has been initialized with blocks.
*/
export const isEditorReadyWithBlocks = async () =>
new Promise( ( resolve ) => {
const unsubscribe = subscribe( () => {
const isCleanNewPost = select( 'core/editor' ).isCleanNewPost();
if ( isCleanNewPost ) {
unsubscribe();
resolve( false );
}
const blocks = select( 'core/editor' ).getBlocks();
if ( blocks.length > 0 ) {
unsubscribe();
resolve( true );
}
} );
} );
export const getPostTypeRecords = async ( name ) =>
new Promise( ( resolve ) => {
const unsubscribe = subscribe( () => {
const records = select( 'core' ).getEntityRecords( 'postType', name, { per_page: -1 } );
if ( records !== null ) {
unsubscribe();
resolve( records );
}
} );
} );
export const getPages = async () => getPostTypeRecords( 'page' );
// All end-to-end tests use a custom user agent containing this string.
const E2E_USER_AGENT = 'wp-e2e-tests';
export const isE2ETest = () =>
typeof window !== 'undefined' && window.navigator.userAgent.includes( E2E_USER_AGENT );
|