How to use @wordpress/core-data - 10 common examples

To help you get started, we’ve selected a few @wordpress/core-data examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github WordPress / gutenberg / packages / block-library / src / template-part / edit.js View on Github external
const [ status, setStatus ] = useEntityProp(
		'postType',
		'wp_template_part',
		'status'
	);
	const initialBlocks = useMemo( () => {
		if ( status !== 'publish' ) {
			// Publish if still an auto-draft.
			setStatus( 'publish' );
		}
		if ( typeof content !== 'function' ) {
			const parsedContent = parse( content );
			return parsedContent.length ? parsedContent : undefined;
		}
	}, [] );
	const [ blocks = initialBlocks, setBlocks ] = useEntityProp(
		'postType',
		'wp_template_part',
		'blocks'
	);
	const [ isDirty, isSaving, save ] = __experimentalUseEntitySaving(
		'postType',
		'wp_template_part',
		saveProps
	);
	const saveContent = useCallback( () => {
		_setContent( content( { blocks } ) );
		save();
	}, [ content, blocks ] );
	const setContent = useCallback( () => {
		_setContent( ( { blocks: blocksForSerialization = [] } ) =>
			serializeBlocks( blocksForSerialization )
github WordPress / gutenberg / packages / edit-site / src / components / save-button / index.js View on Github external
export default function SaveButton() {
	const [ , setStatus ] = useEntityProp( 'postType', 'wp_template', 'status' );
	const [ , setTitle ] = useEntityProp( 'postType', 'wp_template', 'title' );
	const [ slug ] = useEntityProp( 'postType', 'wp_template', 'slug' );
	// Publish template if not done yet.
	useEffect( () => {
		setStatus( 'publish' );
		setTitle( slug );
	}, [ slug ] );

	const { isDirty, isSaving } = useSelect( ( select ) => {
		const { getEntityRecordChangesByRecord, isSavingEntityRecord } = select(
			'core'
		);
		const entityRecordChangesByRecord = getEntityRecordChangesByRecord();
		const changedKinds = Object.keys( entityRecordChangesByRecord );
		return {
			isDirty: changedKinds.length > 0,
			isSaving: changedKinds.some( ( changedKind ) =>
				Object.keys(
github WordPress / gutenberg / packages / edit-site / src / components / save-button / index.js View on Github external
export default function SaveButton() {
	const [ , setStatus ] = useEntityProp( 'postType', 'wp_template', 'status' );
	const [ , setTitle ] = useEntityProp( 'postType', 'wp_template', 'title' );
	const [ slug ] = useEntityProp( 'postType', 'wp_template', 'slug' );
	// Publish template if not done yet.
	useEffect( () => {
		setStatus( 'publish' );
		setTitle( slug );
	}, [ slug ] );

	const { isDirty, isSaving } = useSelect( ( select ) => {
		const { getEntityRecordChangesByRecord, isSavingEntityRecord } = select(
			'core'
		);
		const entityRecordChangesByRecord = getEntityRecordChangesByRecord();
		const changedKinds = Object.keys( entityRecordChangesByRecord );
		return {
			isDirty: changedKinds.length > 0,
github WordPress / gutenberg / packages / editor / src / hooks / custom-sources-backwards-compatibility.js View on Github external
( BlockEdit ) => ( { attributes, setAttributes, ...props } ) => {
		const postType = useSelect( ( select ) => select( 'core/editor' ).getCurrentPostType(), [] );
		const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' );

		const mergedAttributes = useMemo(
			() => ( {
				...attributes,
				...mapValues( metaAttributes, ( metaKey ) => meta[ metaKey ] ),
			} ),
			[ attributes, meta ]
		);

		return (
			<BlockEdit
				attributes={ mergedAttributes }
				setAttributes={ ( nextAttributes ) => {
					const nextMeta = mapKeys(
						// Filter to intersection of keys between the updated
						// attributes and those with an associated meta key.
github WordPress / gutenberg / packages / block-library / src / template-part / edit.js View on Github external
function TemplatePart() {
	const [ content, _setContent ] = useEntityProp(
		'postType',
		'wp_template_part',
		'content'
	);
	const [ status, setStatus ] = useEntityProp(
		'postType',
		'wp_template_part',
		'status'
	);
	const initialBlocks = useMemo( () => {
		if ( status !== 'publish' ) {
			// Publish if still an auto-draft.
			setStatus( 'publish' );
		}
		if ( typeof content !== 'function' ) {
			const parsedContent = parse( content );
github WordPress / gutenberg / packages / block-library / src / template-part / edit.js View on Github external
const initialBlocks = useMemo( () => {
		if ( status !== 'publish' ) {
			// Publish if still an auto-draft.
			setStatus( 'publish' );
		}
		if ( typeof content !== 'function' ) {
			const parsedContent = parse( content );
			return parsedContent.length ? parsedContent : undefined;
		}
	}, [] );
	const [ blocks = initialBlocks, setBlocks ] = useEntityProp(
		'postType',
		'wp_template_part',
		'blocks'
	);
	const [ isDirty, isSaving, save ] = __experimentalUseEntitySaving(
		'postType',
		'wp_template_part',
		saveProps
	);
	const saveContent = useCallback( () => {
		_setContent( content( { blocks } ) );
		save();
	}, [ content, blocks ] );
	const setContent = useCallback( () => {
		_setContent( ( { blocks: blocksForSerialization = [] } ) =>
			serializeBlocks( blocksForSerialization )
		);
	}, [] );
	return (
		<>
			<Button
github WordPress / gutenberg / packages / block-library / src / site-title / edit.js View on Github external
export default function SiteTitleEdit() {
	const [ title, setTitle ] = useEntityProp( 'root', 'site', 'title' );
	return (
		<RichText
			tagName="h1"
			placeholder={ __( 'Site Title' ) }
			value={ title }
			onChange={ setTitle }
			allowedFormats={ [] }
		/>
	);
}
github WordPress / gutenberg / packages / block-library / src / post-content / edit.js View on Github external
export default function PostContentEdit() {
	const [ content, setContent ] = useEntityProp( 'postType', 'post', 'content' );
	const initialBlocks = useMemo( () => {
		const parsedContent = parse( content );
		return parsedContent.length ? parsedContent : undefined;
	}, [] );
	const [ blocks = initialBlocks, setBlocks ] = useEntityProp(
		'postType',
		'post',
		'blocks'
	);
	const [ isDirty, isSaving, save ] = useEntitySaving(
		'postType',
		'post',
		'content'
	);
	return (
		<>
			<Button
				isPrimary
				className="wp-block-custom-entity__save-button"
				disabled={ ! isDirty || ! content }
				isBusy={ isSaving }
github WordPress / gutenberg / packages / block-library / src / site-description / edit.js View on Github external
function SiteDescriptionEdit( {
	attributes,
	backgroundColor,
	className,
	fontSize,
	insertDefaultBlock,
	setAttributes,
	setBackgroundColor,
	setFontSize,
	setTextColor,
	textColor,

} ) {
	const [ description, setDescription ] = useEntityProp( 'root', 'site', 'description' );
	const [ isDirty, isSaving, save ] = __experimentalUseEntitySaving(
		'root',
		'site',
		'description'
	);

	const { customFontSize, textAlign } = attributes;
	const actualFontSize = customFontSize || fontSize.size;

	const preventNewlines = ( event ) => {
		if ( event.keyCode === ENTER ) {
			event.preventDefault();
			insertDefaultBlock();
		}
	};
github WordPress / gutenberg / packages / block-library / src / post-title / edit.js View on Github external
export default function PostTitleEdit() {
	const [ title, setTitle ] = useEntityProp( 'postType', 'post', 'title' );
	const [ , setSlug ] = useEntityProp( 'postType', 'post', 'slug' );
	const [ isDirty, isSaving, save ] = __experimentalUseEntitySaving(
		'postType',
		'post',
		saveProps
	);
	return (
		<>
			<Button
				isPrimary
				className="wp-block-custom-entity__save-button"
				disabled={ ! isDirty || ! title }
				isBusy={ isSaving }
				onClick={ save }
			>
				{ __( 'Update' ) }
			</Button>

@wordpress/core-data

Access to and manipulation of core WordPress entities.

GPL-2.0-or-later
Latest version published 20 days ago

Package Health Score

95 / 100
Full package analysis