Beispiel #1
0
const onCancelClick = ( state, dispatch, ownProps ) => () => {
	if ( selectors.getTicketHasBeenCreated( state, ownProps ) ) {
		dispatch( actions.setTicketTempDetails( ownProps.clientId, {
			title: selectors.getTicketTitle( state, ownProps ),
			description: selectors.getTicketDescription( state, ownProps ),
			price: selectors.getTicketPrice( state, ownProps ),
			sku: selectors.getTicketSku( state, ownProps ),
			startDate: selectors.getTicketStartDate( state, ownProps ),
			startDateInput: selectors.getTicketStartDateInput( state, ownProps ),
			startDateMoment: selectors.getTicketStartDateMoment( state, ownProps ),
			endDate: selectors.getTicketEndDate( state, ownProps ),
			endDateInput: selectors.getTicketEndDateInput( state, ownProps ),
			endDateMoment: selectors.getTicketEndDateMoment( state, ownProps ),
			startTime: selectors.getTicketStartTime( state, ownProps ),
			endTime: selectors.getTicketEndTime( state, ownProps ),
			startTimeInput: selectors.getTicketStartTimeInput( state, ownProps ),
			endTimeInput: selectors.getTicketEndTimeInput( state, ownProps ),
			capacityType: selectors.getTicketCapacityType( state, ownProps ),
			capacity: selectors.getTicketCapacity( state, ownProps ),
		} ) );
		dispatch( actions.setTicketsTempSharedCapacity(
			selectors.getTicketsSharedCapacity( state ),
		) );
		dispatch( actions.setTicketHasChanges( ownProps.clientId, false ) );
	} else {
		dispatch( actions.removeTicketBlock( ownProps.clientId ) )
		wpDispatch( 'core/editor' ).removeBlocks( ownProps.clientId );
	}
	wpDispatch( 'core/editor' ).clearSelectedBlock();
};
	it( 'should render with result of query as custom prop name', () => {
		dispatch( 'core/viewport' ).setIsMatching( { '> wide': true } );
		const EnhancedComponent = withViewportMatch( { isWide: '> wide' } )( Component );
		const wrapper = mount( <EnhancedComponent /> );

		expect( wrapper.find( Component ).props() ).toEqual( { isWide: true } );

		wrapper.unmount();
	} );
	it( 'should not render if query does not match', () => {
		dispatch( 'core/viewport' ).setIsMatching( { '> wide': false } );
		const EnhancedComponent = ifViewportMatches( '> wide' )( Component );
		const wrapper = mount( <EnhancedComponent /> );

		expect( wrapper.find( Component ) ).toHaveLength( 0 );

		wrapper.unmount();
	} );
Beispiel #4
0
	onConfirmClick: () => {
		const { clientId } = ownProps;
		const { getBlockCount } = select( 'core/editor' );
		const { insertBlock } = wpDispatch( 'core/editor' );

		const nextChildPosition = getBlockCount( clientId );
		const block = createBlock( 'tribe/tickets-item', {} );
		insertBlock( block, nextChildPosition, clientId );
	},
	it( 'should render if query does match', () => {
		dispatch( 'core/viewport' ).setIsMatching( { '> wide': true } );
		const EnhancedComponent = ifViewportMatches( '> wide' )( Component );
		const wrapper = mount( <EnhancedComponent /> );

		expect( wrapper.find( Component ).childAt( 0 ).type() ).toBe( 'div' );

		wrapper.unmount();
	} );
Beispiel #6
0
		callback: createHigherOrderComponent( BlockEdit => withSelect( ( select, ownProps ) => {
			// Only proceed for image blocks with an assigned ID.
			if ( ownProps.name === 'core/image' && ownProps.attributes.id ) {
				const { getEditedPostAttribute } = select( 'core/editor' );

				if ( getEditedPostAttribute( 'type' ) === ARTWORK_POST_TYPE ) {

					if ( ! getEditedPostAttribute( 'featured_media' ) ) {
						// If this is an image block, if this post is an Artwork item, if there
						// is no existing featured image, AND if the image for this block has
						// been set, then automatically feature that image.
						dispatch( 'core/editor' ).editPost( {
							featured_media: ownProps.attributes.id,
						} );
					}
				}
			}
			return null;
		} )( BlockEdit ), 'autoFeatureFirstSelectedImage' ),
	return function() {
		// Collects identifiers of current fields so we can remove them later.
		const oldFieldIds = _.map( props.container.fields, 'id' );

		// Get a fresh copy of the container and fields.
		const { containers, fields } = normalizePreloadedState( _.get( window.cf, 'preloaded.containers', [] ) );
		const container = _.find( containers, [ 'id', props.id ] );
		const containerFields = _.filter( fields, [ 'container_id', props.id ] );

		// Replace the container and add the new fields.
		const { updateState, removeFields } = dispatch( 'carbon-fields/metaboxes' );

		updateState(
			_.keyBy( [ container ], 'id' ),
			_.keyBy( containerFields, 'id' )
		);

		removeFields( oldFieldIds );
	};
Beispiel #8
0
const onCancelClick = ( state, dispatch ) => () => {
	dispatch( actions.setRSVPTempDetails( {
		tempTitle: selectors.getRSVPTitle( state ),
		tempDescription: selectors.getRSVPDescription( state ),
		tempCapacity: selectors.getRSVPCapacity( state ),
		tempNotGoingResponses: selectors.getRSVPNotGoingResponses( state ),
		tempStartDate: selectors.getRSVPStartDate( state ),
		tempStartDateInput: selectors.getRSVPStartDateInput( state ),
		tempStartDateMoment: selectors.getRSVPStartDateMoment( state ),
		tempEndDate: selectors.getRSVPEndDate( state ),
		tempEndDateInput: selectors.getRSVPEndDateInput( state ),
		tempEndDateMoment: selectors.getRSVPEndDateMoment( state ),
		tempStartTime: selectors.getRSVPStartTime( state ),
		tempEndTime: selectors.getRSVPEndTime( state ),
		tempStartTimeInput: selectors.getRSVPStartTimeInput( state ),
		tempEndTimeInput: selectors.getRSVPEndTimeInput( state ),
	} ) );
	dispatch( actions.setRSVPHasChanges( false ) );
	wpDispatch( 'core/editor' ).clearSelectedBlock();
};
export function registerFormatType( name, settings ) {
	settings = {
		name,
		...settings,
	};

	if ( typeof settings.name !== 'string' ) {
		window.console.error(
			'Format names must be strings.'
		);
		return;
	}

	if ( ! /^[a-z][a-z0-9-]*\/[a-z][a-z0-9-]*$/.test( settings.name ) ) {
		window.console.error(
			'Format names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-format'
		);
		return;
	}

	if ( select( 'core/rich-text' ).getFormatType( settings.name ) ) {
		window.console.error(
			'Format "' + settings.name + '" is already registered.'
		);
		return;
	}

	if (
		typeof settings.tagName !== 'string' ||
		settings.tagName === ''
	) {
		window.console.error(
			'Format tag names must be a string.'
		);
		return;
	}

	if (
		( typeof settings.className !== 'string' || settings.className === '' ) &&
		settings.className !== null
	) {
		window.console.error(
			'Format class names must be a string, or null to handle bare elements.'
		);
		return;
	}

	if ( ! /^[_a-zA-Z]+[a-zA-Z0-9-]*$/.test( settings.className ) ) {
		window.console.error(
			'A class name must begin with a letter, followed by any number of hyphens, letters, or numbers.'
		);
		return;
	}

	if ( settings.className === null ) {
		const formatTypeForBareElement = select( 'core/rich-text' )
			.getFormatTypeForBareElement( settings.tagName );

		if ( formatTypeForBareElement ) {
			window.console.error(
				`Format "${ formatTypeForBareElement.name }" is already registered to handle bare tag name "${ settings.tagName }".`
			);
			return;
		}
	} else {
		const formatTypeForClassName = select( 'core/rich-text' )
			.getFormatTypeForClassName( settings.className );

		if ( formatTypeForClassName ) {
			window.console.error(
				`Format "${ formatTypeForClassName.name }" is already registered to handle class name "${ settings.className }".`
			);
			return;
		}
	}

	if ( ! ( 'title' in settings ) || settings.title === '' ) {
		window.console.error(
			'The format "' + settings.name + '" must have a title.'
		);
		return;
	}

	if ( 'keywords' in settings && settings.keywords.length > 3 ) {
		window.console.error(
			'The format "' + settings.name + '" can have a maximum of 3 keywords.'
		);
		return;
	}

	if ( typeof settings.title !== 'string' ) {
		window.console.error(
			'Format titles must be strings.'
		);
		return;
	}

	dispatch( 'core/rich-text' ).addFormatTypes( settings );

	const getFunctionStackMemoized = memize( ( previousStack = EMPTY_ARRAY, newFunction ) => {
		return [
			...previousStack,
			newFunction,
		];
	} );

	if (
		settings.__experimentalGetPropsForEditableTreePreparation
	) {
		addFilter( 'experimentalRichText', name, ( OriginalComponent ) => {
			let Component = OriginalComponent;
			if (
				settings.__experimentalCreatePrepareEditableTree ||
				settings.__experimentalCreateFormatToValue ||
				settings.__experimentalCreateValueToFormat
			) {
				Component = ( props ) => {
					const additionalProps = {};

					if ( settings.__experimentalCreatePrepareEditableTree ) {
						additionalProps.prepareEditableTree = getFunctionStackMemoized(
							props.prepareEditableTree,
							settings.__experimentalCreatePrepareEditableTree( props[ `format_${ name }` ], {
								richTextIdentifier: props.identifier,
								blockClientId: props.clientId,
							} )
						);
					}

					if ( settings.__experimentalCreateOnChangeEditableValue ) {
						const dispatchProps = Object.keys( props ).reduce( ( accumulator, propKey ) => {
							const propValue = props[ propKey ];
							const keyPrefix = `format_${ name }_dispatch_`;
							if ( propKey.startsWith( keyPrefix ) ) {
								const realKey = propKey.replace( keyPrefix, '' );

								accumulator[ realKey ] = propValue;
							}

							return accumulator;
						}, {} );

						additionalProps.onChangeEditableValue = getFunctionStackMemoized(
							props.onChangeEditableValue,
							settings.__experimentalCreateOnChangeEditableValue( {
								...props[ `format_${ name }` ],
								...dispatchProps,
							}, {
								richTextIdentifier: props.identifier,
								blockClientId: props.clientId,
							} )
						);
					}

					return <OriginalComponent
						{ ...props }
						{ ...additionalProps }
					/>;
				};
			}

			const hocs = [
				withSelect( ( sel, { clientId, identifier } ) => ( {
					[ `format_${ name }` ]: settings.__experimentalGetPropsForEditableTreePreparation(
						sel,
						{
							richTextIdentifier: identifier,
							blockClientId: clientId,
						}
					),
				} ) ),
			];

			if ( settings.__experimentalGetPropsForEditableTreeChangeHandler ) {
				hocs.push( withDispatch( ( disp, { clientId, identifier } ) => {
					const dispatchProps = settings.__experimentalGetPropsForEditableTreeChangeHandler(
						disp,
						{
							richTextIdentifier: identifier,
							blockClientId: clientId,
						}
					);

					return mapKeys( dispatchProps, ( value, key ) => {
						return `format_${ name }_dispatch_${ key }`;
					} );
				} ) );
			}

			return compose( hocs )( Component );
		} );
	}

	return settings;
}
	registerBlockType( `carbon-fields/${ name }`, {
		title: container.title,
		icon: getBlockSetting( 'icon' ),
		category: getBlockSetting( 'category.slug' ),
		keywords: getBlockSetting( 'keywords', [] ),
		description: getBlockSetting( 'description', '' ),
		attributes: {
			data: {
				type: 'object',
				default: fields
			}
		},
		supports: {
			tabs: isPlainObject( getBlockSetting( 'tabs' ) ),
			preview: getBlockSetting( 'preview' ),
			alignWide: false,
			anchor: false,
			html: false
		},
		edit: BlockEdit,
		save: BlockSave
	} );
} );

/**
 * Load the definitions in store.
 */
dispatch( 'carbon-fields/blocks' ).setupContainerDefinitions( containerDefinitions );
dispatch( 'carbon-fields/blocks' ).setupFieldDefinitions( fieldDefinitions );
Beispiel #11
0
		}, new window.FormData() );
		additionalData.forEach( ( [ key, value ] ) => formData.append( key, value ) );

		// Save the metaboxes
		apiFetch( {
			url: window._wpMetaBoxUrl,
			method: 'POST',
			body: formData,
			parse: false,
		} )
			.then( () => store.dispatch( metaBoxUpdatesSuccess() ) );
	},
	SWITCH_MODE( action ) {
		// Unselect blocks when we switch to the code editor.
		if ( action.mode !== 'visual' ) {
			dispatch( 'core/editor' ).clearSelectedBlock();
		}

		const message = action.mode === 'visual' ? __( 'Visual editor selected' ) : __( 'Code editor selected' );
		speak( message, 'assertive' );
	},
	INIT( _, store ) {
		// Select the block settings tab when the selected block changes
		subscribe( onChangeListener(
			() => !! select( 'core/editor' ).getBlockSelectionStart(),
			( hasBlockSelection ) => {
				if ( ! select( 'core/edit-post' ).isEditorSidebarOpened() ) {
					return;
				}
				if ( hasBlockSelection ) {
					store.dispatch( openGeneralSidebar( 'edit-post/block' ) );
const fetchSharedBlocks = once( () => dispatch( 'core/editor' ).fetchSharedBlocks() );