Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] trunk from WordPress:trunk #123

Merged
merged 6 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions bin/api-docs/gen-components-docs/get-tags-from-storybook.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* External dependencies
*/
import fs from 'node:fs/promises';
import babel from '@babel/core';

/**
* Returns `meta.tags` from a Storybook file.
*
* @param {string} filePath
* @return {Promise<string[]>} Array of tags.
*/
export async function getTagsFromStorybook( filePath ) {
const fileContent = await fs.readFile( filePath, 'utf8' );
const parsedFile = babel.parse( fileContent, {
filename: filePath,
} );

const meta = parsedFile.program.body.find(
( node ) =>
node.type === 'VariableDeclaration' &&
node.declarations[ 0 ].id.name === 'meta'
);

return (
meta.declarations[ 0 ].init.properties
.find( ( node ) => node.key.name === 'tags' )
?.value.elements.map( ( node ) => node.value ) ?? []
);
}
9 changes: 9 additions & 0 deletions bin/api-docs/gen-components-docs/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import path from 'path';
*/
import { generateMarkdownDocs } from './markdown/index.mjs';
import { getDescriptionsForSubcomponents } from './get-subcomponent-descriptions.mjs';
import { getTagsFromStorybook } from './get-tags-from-storybook.mjs';

const MANIFEST_GLOB = 'packages/components/src/**/docs-manifest.json';

Expand Down Expand Up @@ -113,9 +114,17 @@ await Promise.all(
} ) ?? []
);

const tags = await getTagsFromStorybook(
path.resolve(
path.dirname( manifestPath ),
'stories/index.story.tsx'
)
);

const docs = generateMarkdownDocs( {
typeDocs,
subcomponentTypeDocs,
tags,
} );
const outputFile = path.resolve(
path.dirname( manifestPath ),
Expand Down
11 changes: 10 additions & 1 deletion bin/api-docs/gen-components-docs/markdown/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ json2md.converters.md = ( input ) => {
return input?.trim() || '';
};

export function generateMarkdownDocs( { typeDocs, subcomponentTypeDocs } ) {
export function generateMarkdownDocs( {
typeDocs,
subcomponentTypeDocs,
tags,
} ) {
const mainDocsJson = [
{ h1: typeDocs.displayName },
'<!-- This file is generated automatically and cannot be edited directly. Make edits via TypeScript types and TSDocs. -->',
tags.includes( 'status-private' ) && [
{
p: '🔒 This component is locked as a [private API](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-private-apis/). We do not yet recommend using this outside of the Gutenberg project.',
},
],
{
p: `<p class="callout callout-info">See the <a href="https://wordpress.github.io/gutenberg/?path=/docs/components-${ typeDocs.displayName.toLowerCase() }--docs">WordPress Storybook</a> for more detailed, interactive documentation.</p>`,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import {
import AspectRatioTool from '../aspect-ratio-tool';

export default {
title: 'BlockEditor (Private APIs)/DimensionsTool/AspectRatioTool',
title: 'BlockEditor/DimensionsTool/AspectRatioTool',
component: AspectRatioTool,
tags: [ 'status-private' ],
argTypes: {
panelId: { control: false },
onChange: { action: 'changed' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import {
import DimensionsTool from '..';

export default {
title: 'BlockEditor (Private APIs)/DimensionsTool',
title: 'BlockEditor/DimensionsTool/DimensionsTool',
component: DimensionsTool,
tags: [ 'status-private' ],
argTypes: {
panelId: { control: false },
onChange: { action: 'changed' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import {
import ScaleTool from '../scale-tool';

export default {
title: 'BlockEditor (Private APIs)/DimensionsTool/ScaleTool',
title: 'BlockEditor/DimensionsTool/ScaleTool',
component: ScaleTool,
tags: [ 'status-private' ],
argTypes: {
panelId: { control: false },
onChange: { action: 'changed' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import {
import WidthHeightTool from '../width-height-tool';

export default {
title: 'BlockEditor (Private APIs)/DimensionsTool/WidthHeightTool',
title: 'BlockEditor/DimensionsTool/WidthHeightTool',
component: WidthHeightTool,
tags: [ 'status-private' ],
argTypes: {
panelId: { control: false },
onChange: { action: 'changed' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import fastDeepEqual from 'fast-deep-equal/es6';
* WordPress dependencies
*/
import { useRef, useLayoutEffect } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { useRegistry } from '@wordpress/data';
import { synchronizeBlocksWithTemplate } from '@wordpress/blocks';

/**
Expand Down Expand Up @@ -42,21 +42,22 @@ export default function useInnerBlockTemplateSync(
) {
// Instead of adding a useSelect mapping here, please add to the useSelect
// mapping in InnerBlocks! Every subscription impacts performance.

const {
getBlocks,
getSelectedBlocksInitialCaretPosition,
isBlockSelected,
} = useSelect( blockEditorStore );
const { replaceInnerBlocks, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );
const registry = useRegistry();

// Maintain a reference to the previous value so we can do a deep equality check.
const existingTemplateRef = useRef( null );

useLayoutEffect( () => {
let isCancelled = false;

const {
getBlocks,
getSelectedBlocksInitialCaretPosition,
isBlockSelected,
} = registry.select( blockEditorStore );
const { replaceInnerBlocks, __unstableMarkNextChangeAsNotPersistent } =
registry.dispatch( blockEditorStore );

// There's an implicit dependency between useInnerBlockTemplateSync and useNestedSettingsUpdate
// The former needs to happen after the latter and since the latter is using microtasks to batch updates (performance optimization),
// we need to schedule this one in a microtask as well.
Expand Down Expand Up @@ -110,5 +111,11 @@ export default function useInnerBlockTemplateSync(
return () => {
isCancelled = true;
};
}, [ template, templateLock, clientId ] );
}, [
template,
templateLock,
clientId,
registry,
templateInsertUpdatesSelection,
] );
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import {
import ResolutionTool from '..';

export default {
title: 'BlockEditor (Private APIs)/ResolutionControl',
title: 'BlockEditor/ResolutionControl',
component: ResolutionTool,
tags: [ 'status-private' ],
argTypes: {
panelId: { control: false },
onChange: { action: 'changed' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import TextAlignmentControl from '../';
const meta = {
title: 'BlockEditor/TextAlignmentControl',
component: TextAlignmentControl,
tags: [ 'status-private' ],
parameters: {
docs: {
canvas: { sourceState: 'shown' },
Expand Down
2 changes: 2 additions & 0 deletions packages/components/src/badge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<!-- This file is generated automatically and cannot be edited directly. Make edits via TypeScript types and TSDocs. -->

🔒 This component is locked as a [private API](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-private-apis/). We do not yet recommend using this outside of the Gutenberg project.

<p class="callout callout-info">See the <a href="https://wordpress.github.io/gutenberg/?path=/docs/components-badge--docs">WordPress Storybook</a> for more detailed, interactive documentation.</p>

## Props
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/badge/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import type { Meta, StoryObj } from '@storybook/react';
*/
import Badge from '..';

const meta = {
const meta: Meta< typeof Badge > = {
component: Badge,
title: 'Components/Containers/Badge',
id: 'components-badge',
tags: [ 'status-private' ],
} satisfies Meta< typeof Badge >;
};

export default meta;

Expand Down
26 changes: 24 additions & 2 deletions packages/core-data/src/private-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { createSelector, createRegistrySelector } from '@wordpress/data';
/**
* Internal dependencies
*/
import { getDefaultTemplateId, getEntityRecord, type State } from './selectors';
import {
canUser,
getDefaultTemplateId,
getEntityRecord,
type State,
} from './selectors';
import { STORE_NAME } from './name';
import { unlock } from './lock-unlock';

Expand Down Expand Up @@ -134,6 +139,13 @@ interface SiteData {
export const getHomePage = createRegistrySelector( ( select ) =>
createSelector(
() => {
const canReadSiteData = select( STORE_NAME ).canUser( 'read', {
kind: 'root',
name: 'site',
} );
if ( ! canReadSiteData ) {
return null;
}
const siteData = select( STORE_NAME ).getEntityRecord(
'root',
'site'
Expand All @@ -156,7 +168,10 @@ export const getHomePage = createRegistrySelector( ( select ) =>
return { postType: 'wp_template', postId: frontPageTemplateId };
},
( state ) => [
getEntityRecord( state, 'root', 'site' ),
canUser( state, 'read', {
kind: 'root',
name: 'site',
} ) && getEntityRecord( state, 'root', 'site' ),
getDefaultTemplateId( state, {
slug: 'front-page',
} ),
Expand All @@ -165,6 +180,13 @@ export const getHomePage = createRegistrySelector( ( select ) =>
);

export const getPostsPageId = createRegistrySelector( ( select ) => () => {
const canReadSiteData = select( STORE_NAME ).canUser( 'read', {
kind: 'root',
name: 'site',
} );
if ( ! canReadSiteData ) {
return null;
}
const siteData = select( STORE_NAME ).getEntityRecord( 'root', 'site' ) as
| SiteData
| undefined;
Expand Down
3 changes: 2 additions & 1 deletion packages/editor/src/components/document-bar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { store as commandsStore } from '@wordpress/commands';
import { useRef, useEffect } from '@wordpress/element';
import { useReducedMotion } from '@wordpress/compose';
import { decodeEntities } from '@wordpress/html-entities';
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';

/**
* Internal dependencies
Expand Down Expand Up @@ -200,7 +201,7 @@ export default function DocumentBar( props ) {
<Text size="body" as="h1">
<span className="editor-document-bar__post-title">
{ title
? decodeEntities( title )
? stripHTML( title )
: __( 'No title' ) }
</span>
{ pageTypeBadge && (
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/components/post-card-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';

/**
* Internal dependencies
Expand Down Expand Up @@ -94,7 +94,7 @@ export default function PostCardPanel( {
labels?.name
);
} else if ( postTitle ) {
title = decodeEntities( postTitle );
title = stripHTML( postTitle );
}

return (
Expand Down
7 changes: 5 additions & 2 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export const ExperimentalEditorProvider = withRegistryProvider(
BlockEditorProviderComponent = ExperimentalBlockEditorProvider,
__unstableTemplate: template,
} ) => {
const hasTemplate = !! template;
const {
editorSettings,
selection,
Expand Down Expand Up @@ -195,15 +196,17 @@ export const ExperimentalEditorProvider = withRegistryProvider(
isReady: __unstableIsEditorReady(),
mode: getRenderingMode(),
defaultMode:
postTypeObject?.default_rendering_mode ?? 'post-only',
hasTemplate && postTypeObject?.default_rendering_mode
? postTypeObject?.default_rendering_mode
: 'post-only',
selection: getEditorSelection(),
postTypeEntities:
post.type === 'wp_template'
? getEntitiesConfig( 'postType' )
: null,
};
},
[ post.type ]
[ post.type, hasTemplate ]
);

const shouldRenderTemplate = !! template && mode !== 'post-only';
Expand Down
Loading