Skip to content

Commit

Permalink
Lock apis
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed May 11, 2023
1 parent 2cdf417 commit da68075
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 70 deletions.
4 changes: 0 additions & 4 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,6 @@ _Returns_

- `string`: returns the cssUnit value in a simple px format.

### getRichTextValues

Undocumented declaration.

### getSpacingPresetCssVar

Converts a spacing preset into a custom value.
Expand Down
1 change: 0 additions & 1 deletion packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export {
RichTextShortcut,
RichTextToolbarButton,
__unstableRichTextInputEvent,
getRichTextValues,
} from './rich-text';
export { default as ToolSelector } from './tool-selector';
export { default as __experimentalUnitControl } from './unit-control';
Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,14 +465,14 @@ function findContent( blocks, richTextValues = [] ) {

for ( const block of blocks ) {
if (
block.type?.__unstableIsRichTextContent ===
block?.type?.__unstableIsRichTextContent ===
ForwardedRichTextContainer.Content.__unstableIsRichTextContent
) {
richTextValues.push( block.props.value );
continue;
}

if ( block.props?.children ) {
if ( block?.props?.children ) {
findContent( block.props.children, richTextValues );
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/block-editor/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import * as globalStyles from './components/global-styles';
import { ExperimentalBlockEditorProvider } from './components/provider';
import { lock } from './lock-unlock';
import { horKey } from './store/reducer';
import { getRichTextValues } from './components/rich-text';
import OffCanvasEditor from './components/off-canvas-editor';
import ResizableBoxPopover from './components/resizable-box-popover';
import { ComposedPrivateInserter as PrivateInserter } from './components/inserter';
Expand All @@ -19,6 +21,8 @@ export const privateApis = {};
lock( privateApis, {
...globalStyles,
ExperimentalBlockEditorProvider,
horKey,
getRichTextValues,
OffCanvasEditor,
PrivateInserter,
PrivateListView,
Expand Down
16 changes: 6 additions & 10 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,16 +745,12 @@ const withResetControlledBlocks = ( reducer ) => ( state, action ) => {
return reducer( state, action );
};

const withBlockFilters = ( reducer ) => ( state, action ) => {
const newState = reducer( state, action );
const filteredState = applyFilters(
'blockEditor.higherOrderReducer',
newState,
reducer
);
// The random number is used so it can't be used without the unlocker.
// eslint-disable-next-line no-restricted-syntax
export const horKey = 'blockEditor.higherOrderReducer' + Math.random();

return filteredState;
};
const withHorFilters = ( reducer ) => ( state, action ) =>
applyFilters( horKey, reducer( state, action ), reducer );

/**
* Reducer returning the blocks state.
Expand All @@ -774,7 +770,7 @@ export const blocks = pipe(
withPersistentBlockChange,
withIgnoredBlockChange,
withResetControlledBlocks,
withBlockFilters
withHorFilters
)( {
// The state is using a Map instead of a plain object for performance reasons.
// You can run the "./test/performance.js" unit test to check the impact
Expand Down
109 changes: 56 additions & 53 deletions packages/block-library/src/footnotes/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,79 @@
import {
RichText,
useBlockProps,
getRichTextValues,
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import { addFilter } from '@wordpress/hooks';
import { createBlock } from '@wordpress/blocks';
import { useRefEffect } from '@wordpress/compose';

addFilter(
'blockEditor.higherOrderReducer',
'core/footnotes',
( state, reducer ) => {
const blocks = state.tree.get( '' )?.innerBlocks;
/**
* Internal dependencies
*/
import { unlock } from '../private-apis';

if ( ! blocks ) return state;
const { horKey, getRichTextValues } = unlock( blockEditorPrivateApis );

const content = getRichTextValues( blocks ).join( '' );
addFilter( horKey, 'core/footnotes', ( state, reducer ) => {
const blocks = state.tree.get( '' )?.innerBlocks;

if ( ! content ) return state;
if ( content.indexOf( 'data-fn' ) === -1 ) return state;
if ( ! blocks ) return state;

// This can be avoided when
// https://github.com/WordPress/gutenberg/pull/43204 lands. We can then
// get the order directly from the rich text values.
const regex = /data-fn="([^"]+)"/g;
const newOrder = [];
let match;
while ( ( match = regex.exec( content ) ) !== null ) {
newOrder.push( match[ 1 ] );
}
const content = getRichTextValues( blocks ).join( '' );

const fnBlock = Array.from( state.byClientId.values() ).find(
( block ) => block.name === 'core/footnotes'
);
if ( ! content ) return state;
if ( content.indexOf( 'data-fn' ) === -1 ) return state;

// This can be avoided when
// https://github.com/WordPress/gutenberg/pull/43204 lands. We can then
// get the order directly from the rich text values.
const regex = /data-fn="([^"]+)"/g;
const newOrder = [];
let match;
while ( ( match = regex.exec( content ) ) !== null ) {
newOrder.push( match[ 1 ] );
}

const currentOrder = fnBlock
? state.attributes
.get( fnBlock.clientId )
.footnotes.map( ( fn ) => fn.id )
: [];
const fnBlock = Array.from( state.byClientId.values() ).find(
( block ) => block.name === 'core/footnotes'
);

if ( currentOrder.join( '' ) === newOrder.join( '' ) ) return state;
const currentOrder = fnBlock
? state.attributes
.get( fnBlock.clientId )
.footnotes.map( ( fn ) => fn.id )
: [];

if ( ! fnBlock ) {
const rootBlock = Array.from( state.byClientId.values() ).find(
( block ) => block.name === 'core/post-content'
);
return reducer( state, {
type: 'INSERT_BLOCKS',
blocks: [ createBlock( 'core/footnotes', { footnotes: [] } ) ],
rootClientId: rootBlock?.clientId,
} );
}
if ( currentOrder.join( '' ) === newOrder.join( '' ) ) return state;

if ( ! fnBlock ) {
const rootBlock = Array.from( state.byClientId.values() ).find(
( block ) => block.name === 'core/post-content'
);
return reducer( state, {
type: 'UPDATE_BLOCK_ATTRIBUTES',
clientIds: [ fnBlock.clientId ],
attributes: {
footnotes: newOrder.map( ( id ) => {
return (
state.attributes
.get( fnBlock.clientId )
.footnotes.find( ( fn ) => fn.id === id ) || {
id,
content: '',
}
);
} ),
},
type: 'INSERT_BLOCKS',
blocks: [ createBlock( 'core/footnotes', { footnotes: [] } ) ],
rootClientId: rootBlock?.clientId,
} );
}
);

return reducer( state, {
type: 'UPDATE_BLOCK_ATTRIBUTES',
clientIds: [ fnBlock.clientId ],
attributes: {
footnotes: newOrder.map( ( id ) => {
return (
state.attributes
.get( fnBlock.clientId )
.footnotes.find( ( fn ) => fn.id === id ) || {
id,
content: '',
}
);
} ),
},
} );
} );

export default function FootnotesEdit( { attributes, setAttributes } ) {
const ref = useRefEffect( ( node ) => {
Expand Down

0 comments on commit da68075

Please sign in to comment.