UNPKG

@wordpress/block-library

Version:
8 lines (7 loc) 10.8 kB
{ "version": 3, "sources": ["../../../src/query/edit/query-content.jsx"], "sourcesContent": ["import { useSelect, useDispatch } from '@wordpress/data';\nimport { useInstanceId } from '@wordpress/compose';\nimport { useEffect, useCallback } from '@wordpress/element';\nimport {\n\tInspectorControls,\n\tuseBlockProps,\n\tstore as blockEditorStore,\n\tuseInnerBlocksProps,\n\tprivateApis as blockEditorPrivateApis,\n} from '@wordpress/block-editor';\nimport { __, sprintf } from '@wordpress/i18n';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { store as noticesStore } from '@wordpress/notices';\nimport EnhancedPaginationControl from './inspector-controls/enhanced-pagination-control';\nimport { unlock } from '../../lock-unlock';\nimport QueryInspectorControls from './inspector-controls';\nimport { getQueryContextFromTemplate, useUnsupportedBlocks } from '../utils';\nimport QueryToolbar from './query-toolbar';\n\nconst { HTMLElementControl } = unlock( blockEditorPrivateApis );\n\nconst DEFAULTS_POSTS_PER_PAGE = 3;\n\nexport default function QueryContent( {\n\tattributes,\n\tsetAttributes,\n\tclientId,\n\tcontext,\n\tname,\n\tisSelected,\n} ) {\n\tconst {\n\t\tqueryId,\n\t\tquery,\n\t\tenhancedPagination,\n\t\ttagName: TagName = 'div',\n\t\tquery: { inherit } = {},\n\t} = attributes;\n\tconst { templateSlug, postType } = context;\n\tconst { isSingular, templateType } =\n\t\tgetQueryContextFromTemplate( templateSlug );\n\tconst { __unstableMarkNextChangeAsNotPersistent } =\n\t\tuseDispatch( blockEditorStore );\n\tconst { createNotice } = useDispatch( noticesStore );\n\tconst unsupportedBlocks = useUnsupportedBlocks( clientId );\n\tconst instanceId = useInstanceId( QueryContent );\n\tconst blockProps = useBlockProps();\n\tconst innerBlocksProps = useInnerBlocksProps( blockProps );\n\tconst { postsPerPage } = useSelect( ( select ) => {\n\t\tconst { getSettings } = select( blockEditorStore );\n\t\tconst { getEntityRecord, getEntityRecordEdits, canUser } =\n\t\t\tselect( coreStore );\n\t\tconst settingPerPage = canUser( 'read', {\n\t\t\tkind: 'root',\n\t\t\tname: 'site',\n\t\t} )\n\t\t\t? +getEntityRecord( 'root', 'site' )?.posts_per_page\n\t\t\t: +getSettings().postsPerPage;\n\n\t\t// Gets changes made via the template area posts per page setting. These won't be saved\n\t\t// until the page is saved, but we should reflect this setting within the query loops\n\t\t// that inherit it.\n\t\tconst editedSettingPerPage = +getEntityRecordEdits( 'root', 'site' )\n\t\t\t?.posts_per_page;\n\n\t\treturn {\n\t\t\tpostsPerPage:\n\t\t\t\teditedSettingPerPage ||\n\t\t\t\tsettingPerPage ||\n\t\t\t\tDEFAULTS_POSTS_PER_PAGE,\n\t\t};\n\t}, [] );\n\n\t// Whether the \"exclude current\" filter applies: we're in a singular template\n\t// and the post type matches the query.\n\tconst shouldExcludeCurrentPost =\n\t\tisSingular &&\n\t\t! inherit &&\n\t\t( query.postType === postType || query.postType === templateType );\n\n\t// There are some effects running where some initialization logic is\n\t// happening and setting some values to some attributes (ex. queryId).\n\t// These updates can cause an `undo trap` where undoing will result in\n\t// resetting again, so we need to mark these changes as not persistent\n\t// with `__unstableMarkNextChangeAsNotPersistent`.\n\n\t// Changes in query property (which is an object) need to be in the same callback,\n\t// because updates are batched after the render and changes in different query properties\n\t// would cause to override previous wanted changes.\n\tconst updateQuery = useCallback(\n\t\t( newQuery ) =>\n\t\t\tsetAttributes( ( prevAttributes ) => ( {\n\t\t\t\tquery: { ...prevAttributes.query, ...newQuery },\n\t\t\t} ) ),\n\t\t[ setAttributes ]\n\t);\n\tuseEffect( () => {\n\t\tconst newQuery = {};\n\t\t// When we inherit from global query always need to set the `perPage`\n\t\t// based on the reading settings.\n\t\tif ( inherit && query.perPage !== postsPerPage ) {\n\t\t\tnewQuery.perPage = postsPerPage;\n\t\t} else if ( ! query.perPage && postsPerPage ) {\n\t\t\tnewQuery.perPage = postsPerPage;\n\t\t}\n\t\t// Remove the exclusion when it no longer applies, so the filters stay\n\t\t// clean. We never force it on: enabling the exclusion is left to the\n\t\t// user. An absent key is already clean — writing `null` into it would\n\t\t// change the serialized markup of every pre-existing Query block just\n\t\t// by opening the editor.\n\t\tif (\n\t\t\t! shouldExcludeCurrentPost &&\n\t\t\tquery.excludeCurrent !== undefined &&\n\t\t\tquery.excludeCurrent !== null\n\t\t) {\n\t\t\tnewQuery.excludeCurrent = null;\n\t\t}\n\t\tif ( !! Object.keys( newQuery ).length ) {\n\t\t\t__unstableMarkNextChangeAsNotPersistent();\n\t\t\tupdateQuery( newQuery );\n\t\t}\n\t}, [\n\t\tquery.perPage,\n\t\tquery.excludeCurrent,\n\t\tinherit,\n\t\tpostsPerPage,\n\t\tshouldExcludeCurrentPost,\n\t\t__unstableMarkNextChangeAsNotPersistent,\n\t\tupdateQuery,\n\t] );\n\t// We need this for multi-query block pagination.\n\t// Query parameters for each block are scoped to their ID.\n\tuseEffect( () => {\n\t\tif ( ! Number.isFinite( queryId ) ) {\n\t\t\t__unstableMarkNextChangeAsNotPersistent();\n\t\t\tsetAttributes( { queryId: instanceId } );\n\t\t}\n\t}, [\n\t\tqueryId,\n\t\tinstanceId,\n\t\t__unstableMarkNextChangeAsNotPersistent,\n\t\tsetAttributes,\n\t] );\n\tuseEffect( () => {\n\t\tif ( enhancedPagination && unsupportedBlocks.length ) {\n\t\t\t__unstableMarkNextChangeAsNotPersistent();\n\t\t\tsetAttributes( { enhancedPagination: false } );\n\t\t\tconst message = sprintf(\n\t\t\t\t/* translators: %s: A list of block titles. */\n\t\t\t\t__(\n\t\t\t\t\t`\"Reload full page\" was enabled because some blocks inside the Query block aren't supported: %s.`\n\t\t\t\t),\n\t\t\t\tunsupportedBlocks.join(\n\t\t\t\t\t/* translators: Used between list items, there is a space after the comma. */\n\t\t\t\t\t__( ', ' ) // eslint-disable-line @wordpress/i18n-no-flanking-whitespace\n\t\t\t\t)\n\t\t\t);\n\t\t\tcreateNotice( 'info', message, {\n\t\t\t\ttype: 'snackbar',\n\t\t\t\tid: 'query-enhanced-pagination-disabled',\n\t\t\t} );\n\t\t}\n\t}, [\n\t\tenhancedPagination,\n\t\tunsupportedBlocks,\n\t\t__unstableMarkNextChangeAsNotPersistent,\n\t\tsetAttributes,\n\t\tcreateNotice,\n\t] );\n\n\treturn (\n\t\t<>\n\t\t\t{ isSelected && (\n\t\t\t\t<QueryToolbar\n\t\t\t\t\tclientId={ clientId }\n\t\t\t\t\tattributes={ attributes }\n\t\t\t\t\thasInnerBlocks\n\t\t\t\t/>\n\t\t\t) }\n\t\t\t<InspectorControls>\n\t\t\t\t<QueryInspectorControls\n\t\t\t\t\tname={ name }\n\t\t\t\t\tattributes={ attributes }\n\t\t\t\t\tsetQuery={ updateQuery }\n\t\t\t\t\tsetAttributes={ setAttributes }\n\t\t\t\t\tclientId={ clientId }\n\t\t\t\t\tisSingular={ isSingular }\n\t\t\t\t\tshouldExcludeCurrentPost={ shouldExcludeCurrentPost }\n\t\t\t\t/>\n\t\t\t</InspectorControls>\n\t\t\t<InspectorControls group=\"advanced\">\n\t\t\t\t<HTMLElementControl\n\t\t\t\t\ttagName={ TagName }\n\t\t\t\t\tonChange={ ( value ) =>\n\t\t\t\t\t\tsetAttributes( { tagName: value } )\n\t\t\t\t\t}\n\t\t\t\t\tclientId={ clientId }\n\t\t\t\t\toptions={ [\n\t\t\t\t\t\t{ label: __( 'Default (<div>)' ), value: 'div' },\n\t\t\t\t\t\t{ label: '<main>', value: 'main' },\n\t\t\t\t\t\t{ label: '<section>', value: 'section' },\n\t\t\t\t\t\t{ label: '<aside>', value: 'aside' },\n\t\t\t\t\t] }\n\t\t\t\t/>\n\t\t\t\t<EnhancedPaginationControl\n\t\t\t\t\tenhancedPagination={ enhancedPagination }\n\t\t\t\t\tsetAttributes={ setAttributes }\n\t\t\t\t\tclientId={ clientId }\n\t\t\t\t/>\n\t\t\t</InspectorControls>\n\t\t\t<TagName { ...innerBlocksProps } />\n\t\t</>\n\t);\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AA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Ac,QAAQ,SAAS;AAAA,QAC9B,MAAM;AAAA,QACN,IAAI;AAAA,MACL,CAAE;AAAA,IACH;AAAA,EACD,GAAG;AAAA,IACF;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,CAAE;AAEF,SACC,4EACG;AAAA,kBACD;AAAA,MAAC,qBAAAC;AAAA,MAAA;AAAA,QACA;AAAA,QACA;AAAA,QACA,gBAAc;AAAA;AAAA,IACf;AAAA,IAED,4CAAC,yCACA;AAAA,MAAC,0BAAAC;AAAA,MAAA;AAAA,QACA;AAAA,QACA;AAAA,QACA,UAAW;AAAA,QACX;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA;AAAA,IACD,GACD;AAAA,IACA,6CAAC,yCAAkB,OAAM,YACxB;AAAA;AAAA,QAAC;AAAA;AAAA,UACA,SAAU;AAAA,UACV,UAAW,CAAE,UACZ,cAAe,EAAE,SAAS,MAAM,CAAE;AAAA,UAEnC;AAAA,UACA,SAAU;AAAA,YACT,EAAE,WAAO,gBAAI,iBAAkB,GAAG,OAAO,MAAM;AAAA,YAC/C,EAAE,OAAO,UAAU,OAAO,OAAO;AAAA,YACjC,EAAE,OAAO,aAAa,OAAO,UAAU;AAAA,YACvC,EAAE,OAAO,WAAW,OAAO,QAAQ;AAAA,UACpC;AAAA;AAAA,MACD;AAAA,MACA;AAAA,QAAC,mCAAAC;AAAA,QAAA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA;AAAA,MACD;AAAA,OACD;AAAA,IACA,4CAAC,WAAU,GAAG,kBAAmB;AAAA,KAClC;AAEF;", "names": ["blockEditorPrivateApis", "blockEditorStore", "noticesStore", "coreStore", "QueryToolbar", "QueryInspectorControls", "EnhancedPaginationControl"] }