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

Site Editor: use EditorProvider instead of custom logic #56000

Merged
merged 14 commits into from
Nov 16, 2023
23 changes: 2 additions & 21 deletions packages/block-editor/src/components/provider/use-block-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,11 @@ export default function useBlockSync( {
resetBlocks,
resetSelection,
replaceInnerBlocks,
selectBlock,
setHasControlledInnerBlocks,
__unstableMarkNextChangeAsNotPersistent,
} = registry.dispatch( blockEditorStore );
const {
hasSelectedBlock,
getBlockName,
getBlocks,
getSelectionStart,
getSelectionEnd,
getBlock,
} = registry.select( blockEditorStore );
const { getBlockName, getBlocks, getSelectionStart, getSelectionEnd } =
registry.select( blockEditorStore );
const isControlled = useSelect(
( select ) => {
return (
Expand Down Expand Up @@ -180,9 +173,6 @@ export default function useBlockSync( {
// bound sync, unset the outbound value to avoid considering it in
// subsequent renders.
pendingChanges.current.outgoing = [];
const hadSelection = hasSelectedBlock();
const selectionAnchor = getSelectionStart();
const selectionFocus = getSelectionEnd();
setControlledBlocks();

if ( controlledSelection ) {
Expand All @@ -191,15 +181,6 @@ export default function useBlockSync( {
controlledSelection.selectionEnd,
controlledSelection.initialPosition
);
} else {
const selectionStillExists = getBlock(
selectionAnchor.clientId
);
if ( hadSelection && ! selectionStillExists ) {
selectBlock( clientId );
} else {
resetSelection( selectionAnchor, selectionFocus );
}
}
}
}, [ controlledBlocks, clientId ] );
Expand Down
9 changes: 8 additions & 1 deletion packages/core-data/src/entity-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ export function useEntityBlockEditor( kind, name, { id: _id } = {} ) {
const id = _id ?? providerId;
const { content, editedBlocks, meta } = useSelect(
( select ) => {
if ( ! id ) {
return {};
}
const { getEditedEntityRecord } = select( STORE_NAME );
const editedRecord = getEditedEntityRecord( kind, name, id );
return {
Expand All @@ -169,14 +172,18 @@ export function useEntityBlockEditor( kind, name, { id: _id } = {} ) {
useDispatch( STORE_NAME );

const blocks = useMemo( () => {
if ( ! id ) {
return undefined;
}

if ( editedBlocks ) {
return editedBlocks;
}

return content && typeof content !== 'function'
? parse( content )
: EMPTY_ARRAY;
}, [ editedBlocks, content ] );
}, [ id, editedBlocks, content ] );

const updateFootnotes = useCallback(
( _blocks ) => updateFootnotesFromMeta( _blocks, meta ),
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading