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

core-data: remove dependency on block-editor #67870

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getBlockSupport,
store as blocksStore,
__unstableGetInnerBlocksProps as getInnerBlocksProps,
InnerBlocksContent,
} from '@wordpress/blocks';

/**
Expand Down Expand Up @@ -309,7 +310,7 @@ useInnerBlocksProps.save = getInnerBlocksProps;
ForwardedInnerBlocks.DefaultBlockAppender = DefaultBlockAppender;
ForwardedInnerBlocks.ButtonBlockAppender = ButtonBlockAppender;

ForwardedInnerBlocks.Content = () => useInnerBlocksProps.save().children;
ForwardedInnerBlocks.Content = InnerBlocksContent;

/**
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/inner-blocks/README.md
Expand Down
11 changes: 7 additions & 4 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import {
removeFormat,
} from '@wordpress/rich-text';
import { Popover } from '@wordpress/components';
import { getBlockBindingsSource } from '@wordpress/blocks';
import {
getBlockBindingsSource,
RichTextContent,
valueToHTMLString,
} from '@wordpress/blocks';
import deprecated from '@wordpress/deprecated';
import { __, sprintf } from '@wordpress/i18n';

Expand All @@ -37,7 +41,6 @@ import { useFormatTypes } from './use-format-types';
import { useEventListeners } from './event-listeners';
import FormatEdit from './format-edit';
import { getAllowedFormats } from './utils';
import { Content, valueToHTMLString } from './content';
import { withDeprecations } from './with-deprecations';
import { canBindBlock } from '../../utils/block-bindings';
import BlockContext from '../block-context';
Expand Down Expand Up @@ -504,7 +507,7 @@ export const PrivateRichText = withDeprecations(
forwardRef( RichTextWrapper )
);

PrivateRichText.Content = Content;
PrivateRichText.Content = RichTextContent;
PrivateRichText.isEmpty = ( value ) => {
return ! value || value.length === 0;
};
Expand Down Expand Up @@ -562,7 +565,7 @@ const PublicForwardedRichTextContainer = forwardRef( ( props, ref ) => {
return <PrivateRichText ref={ ref } { ...props } readOnly={ false } />;
} );

PublicForwardedRichTextContainer.Content = Content;
PublicForwardedRichTextContainer.Content = RichTextContent;
PublicForwardedRichTextContainer.isEmpty = ( value ) => {
return ! value || value.length === 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getBlockTransforms,
findTransform,
isUnmodifiedDefaultBlock,
RichTextContent,
} from '@wordpress/blocks';
import { useInstanceId, useMergeRefs } from '@wordpress/compose';
import {
Expand Down Expand Up @@ -42,7 +43,6 @@ import {
createLinkInParagraph,
} from './utils';
import EmbedHandlerPicker from './embed-handler-picker';
import { Content } from './content';
import RichText from './native';
import { withDeprecations } from './with-deprecations';
import { findSelection } from './event-listeners/input-rules';
Expand Down Expand Up @@ -679,7 +679,7 @@ export const PrivateRichText = withDeprecations(
forwardRef( RichTextWrapper )
);

PrivateRichText.Content = Content;
PrivateRichText.Content = RichTextContent;

PrivateRichText.isEmpty = ( value ) => {
return ! value || value.length === 0;
Expand Down
2 changes: 0 additions & 2 deletions packages/block-editor/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import * as globalStyles from './components/global-styles';
import { ExperimentalBlockEditorProvider } from './components/provider';
import { lock } from './lock-unlock';
import { getRichTextValues } from './components/rich-text/get-rich-text-values';
import ResizableBoxPopover from './components/resizable-box-popover';
import { default as PrivateQuickInserter } from './components/inserter/quick-inserter';
import {
Expand Down Expand Up @@ -59,7 +58,6 @@ lock( privateApis, {
ExperimentalBlockCanvas,
ExperimentalBlockEditorProvider,
getDuotoneFilter,
getRichTextValues,
PrivateQuickInserter,
extractWords,
getNormalizedSearchTerms,
Expand Down
2 changes: 0 additions & 2 deletions packages/block-editor/src/private-apis.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import * as globalStyles from './components/global-styles';
import { ExperimentalBlockEditorProvider } from './components/provider';
import { getRichTextValues } from './components/rich-text/get-rich-text-values';
import { lock } from './lock-unlock';
import { PrivateRichText } from './components/rich-text/';

Expand All @@ -14,6 +13,5 @@ export const privateApis = {};
lock( privateApis, {
...globalStyles,
ExperimentalBlockEditorProvider,
getRichTextValues,
PrivateRichText,
} );
16 changes: 16 additions & 0 deletions packages/blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ _Returns_

- `Array`: Block types that the blocks argument can be transformed to.

### getRichTextValues

Undocumented declaration.

### getSaveContent

Given a block type containing a save render implementation and attributes, returns the static markup to be saved.
Expand Down Expand Up @@ -365,6 +369,10 @@ _Returns_

- `boolean`: True if a block contains at least one child blocks with inserter support and false otherwise.

### InnerBlocksContent

Undocumented declaration.

### isReusableBlock

Determines whether or not the given block is a reusable block. This is a special block type that is used to point to a global block stored via the API.
Expand Down Expand Up @@ -681,6 +689,10 @@ _Parameters_
- _blockName_ `string`: Name of the block (example: “core/columns”).
- _variation_ `WPBlockVariation`: Object describing a block variation.

### RichTextContent

Undocumented declaration.

### serialize

Takes a block or set of blocks and returns the serialized post content.
Expand Down Expand Up @@ -1004,6 +1016,10 @@ _Returns_

- `[boolean,Array<LoggerItem>]`: validation results.

### valueToHTMLString

Undocumented declaration.

### withBlockContentContext

> **Deprecated**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,38 @@
* WordPress dependencies
*/
import { RawHTML } from '@wordpress/element';
import { children as childrenSource } from '@wordpress/blocks';
import deprecated from '@wordpress/deprecated';

/**
* Internal dependencies
*/
import RichText from './';
import childrenSource from './children';
import { getInnerBlocksProps } from './serializer';

/**
* Internal dependencies
*/
import { getMultilineTag } from './utils';
export function InnerBlocksContent() {
return getInnerBlocksProps().children;
}

function isEmpty( value ) {
return ! value || value.length === 0;
}

function getMultilineTag( multiline ) {
if ( multiline !== true && multiline !== 'p' && multiline !== 'li' ) {
return undefined;
}

return multiline === true ? 'p' : multiline;
}

export function valueToHTMLString( value, multiline ) {
if ( RichText.isEmpty( value ) ) {
if ( isEmpty( value ) ) {
const multilineTag = getMultilineTag( multiline );
return multilineTag ? `<${ multilineTag }></${ multilineTag }>` : '';
}

if ( Array.isArray( value ) ) {
deprecated( 'wp.blockEditor.RichText value prop as children type', {
deprecated( 'wp.blocks.RichTextContent value prop as children type', {
since: '6.1',
version: '6.3',
alternative: 'value prop as string',
Expand All @@ -42,7 +53,7 @@ export function valueToHTMLString( value, multiline ) {
return value.toHTMLString();
}

export function Content( {
export function RichTextContent( {
value,
tagName: Tag,
multiline,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
* WordPress dependencies
*/
import { RawHTML, StrictMode, Fragment } from '@wordpress/element';
import {
getSaveElement,
__unstableGetBlockProps as getBlockProps,
} from '@wordpress/blocks';
import { RichTextData } from '@wordpress/rich-text';

/**
* Internal dependencies
*/
import InnerBlocks from '../inner-blocks';
import { Content } from './content';
import { getSaveElement, getBlockProps } from './serializer';
import { InnerBlocksContent, RichTextContent } from './content';

/*
* This function is similar to `@wordpress/element`'s `renderToString` function,
Expand Down Expand Up @@ -42,9 +38,9 @@ function addValuesForElement( element, values, innerBlocks ) {
return addValuesForElements( props.children, values, innerBlocks );
case RawHTML:
return;
case InnerBlocks.Content:
case InnerBlocksContent:
return addValuesForBlocks( values, innerBlocks );
case Content:
case RichTextContent:
values.push( props.value );
return;
}
Expand Down Expand Up @@ -85,7 +81,7 @@ function addValuesForBlocks( values, blocks ) {
// Instead of letting save elements use `useInnerBlocksProps.save`,
// force them to use InnerBlocks.Content instead so we can intercept
// a single component.
<InnerBlocks.Content />
<InnerBlocksContent />
);
addValuesForElement( saveElement, values, innerBlocks );
}
Expand Down
6 changes: 6 additions & 0 deletions packages/blocks/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ export {
__EXPERIMENTAL_ELEMENTS,
__EXPERIMENTAL_PATHS_WITH_OVERRIDE,
} from './constants';
export {
RichTextContent,
InnerBlocksContent,
valueToHTMLString,
} from './content';
export { getRichTextValues } from './get-rich-text-values';

export const privateApis = {};
lock( privateApis, { isContentBlock } );
1 change: 0 additions & 1 deletion packages/core-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"dependencies": {
"@babel/runtime": "7.25.7",
"@wordpress/api-fetch": "*",
"@wordpress/block-editor": "*",
"@wordpress/blocks": "*",
"@wordpress/compose": "*",
"@wordpress/data": "*",
Expand Down
30 changes: 5 additions & 25 deletions packages/core-data/src/footnotes/get-rich-text-values-cached.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
/**
* WordPress dependencies
*/
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import { unlock } from '../lock-unlock';

// TODO: The following line should have been:
//
// const unlockedApis = unlock( blockEditorPrivateApis );
//
// But there are hidden circular dependencies in RNMobile code, specifically in
// certain native components in the `components` package that depend on
// `block-editor`. What follows is a workaround that defers the `unlock` call
// to prevent native code from failing.
//
// Fix once https://github.com/WordPress/gutenberg/issues/52692 is closed.
let unlockedApis;
import { getRichTextValues } from '@wordpress/blocks';

const cache = new WeakMap();

export default function getRichTextValuesCached( block ) {
if ( ! unlockedApis ) {
unlockedApis = unlock( blockEditorPrivateApis );
}

if ( ! cache.has( block ) ) {
const values = unlockedApis.getRichTextValues( [ block ] );
let values = cache.get( block );
if ( ! values ) {
values = getRichTextValues( [ block ] );
cache.set( block, values );
}
return cache.get( block );
return values;
}
Loading