|
|
import { select, subscribe } from '@wordpress/data'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function inIframe() { |
|
|
try { |
|
|
return window.self !== window.top; |
|
|
} catch ( e ) { |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function sendMessage( message ) { |
|
|
if ( ! window || ! window.parent ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
window.parent.postMessage( { ...message, type: 'gutenbergIframeMessage' }, '*' ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const isEditorReady = async () => |
|
|
new Promise( ( resolve ) => { |
|
|
const unsubscribe = subscribe( () => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const editorIsReady = select( 'core/editor' ).__unstableIsEditorReady |
|
|
? select( 'core/editor' ).__unstableIsEditorReady() |
|
|
: select( 'core/editor' ).isCleanNewPost(); |
|
|
if ( editorIsReady ) { |
|
|
unsubscribe(); |
|
|
resolve(); |
|
|
} |
|
|
} ); |
|
|
} ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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' ); |
|
|
|
|
|
|
|
|
const E2E_USER_AGENT = 'wp-e2e-tests'; |
|
|
|
|
|
export const isE2ETest = () => |
|
|
typeof window !== 'undefined' && window.navigator.userAgent.includes( E2E_USER_AGENT ); |
|
|
|