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

Edit Widgets: Replace store name string with exposed store definition #28044

Merged
merged 1 commit into from
Jan 9, 2021
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
9 changes: 5 additions & 4 deletions packages/edit-widgets/src/blocks/legacy-widget/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import LegacyWidgetEditHandler from './handler';
import WidgetSelectControl from './widget-select-control';
import WidgetPreview from './widget-preview';
import LegacyWidgetInspectorCard from './inspector-card';
import { store as editWidgetsStore } from '../../../store';

function LegacyWidgetEdit( {
attributes,
Expand Down Expand Up @@ -191,11 +192,11 @@ function LegacyWidgetEdit( {

export default withSelect( ( select, { clientId, attributes } ) => {
const { widgetClass, referenceWidgetName } = attributes;
let widgetId = select( 'core/edit-widgets' ).getWidgetIdForClientId(
let widgetId = select( editWidgetsStore ).getWidgetIdForClientId(
clientId
);
const widget = select( 'core/edit-widgets' ).getWidget( widgetId );
const widgetArea = select( 'core/edit-widgets' ).getWidgetAreaForClientId(
const widget = select( editWidgetsStore ).getWidget( widgetId );
const widgetArea = select( editWidgetsStore ).getWidgetAreaForClientId(
clientId
);
const editorSettings = select( 'core/block-editor' ).getSettings();
Expand All @@ -214,7 +215,7 @@ export default withSelect( ( select, { clientId, attributes } ) => {
let isDuplicateReferenceWidget = false;
if ( referenceWidgetName ) {
const referenceWidgetBlocks = select(
'core/edit-widgets'
editWidgetsStore
).getReferenceWidgetBlocks( referenceWidgetName );
if ( clientId !== referenceWidgetBlocks[ 0 ].clientId ) {
isDuplicateReferenceWidget = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { SelectControl } from '@wordpress/components';

/**
* Internal dependencies
*/
import { store as editWidgetsStore } from '../../../store';

export default function WidgetSelectControl( {
availableLegacyWidgets,
currentWidget,
Expand All @@ -19,7 +24,7 @@ export default function WidgetSelectControl( {
emptyLabel = __( 'There are no widgets available.' ),
} ) {
const usedReferenceWidgetNames = useSelect( ( select ) =>
select( 'core/edit-widgets' )
select( editWidgetsStore )
.getReferenceWidgetBlocks()
.map( ( { attributes } ) => attributes?.referenceWidgetName )
);
Expand Down
5 changes: 3 additions & 2 deletions packages/edit-widgets/src/blocks/widget-area/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Panel, PanelBody } from '@wordpress/components';
* Internal dependencies
*/
import WidgetAreaInnerBlocks from './inner-blocks';
import { store as editWidgetsStore } from '../../../store';

/** @typedef {import('@wordpress/element').RefObject} RefObject */

Expand All @@ -25,10 +26,10 @@ export default function WidgetAreaEdit( {
} ) {
const isOpen = useSelect(
( select ) =>
select( 'core/edit-widgets' ).getIsWidgetAreaOpen( clientId ),
select( editWidgetsStore ).getIsWidgetAreaOpen( clientId ),
[ clientId ]
);
const { setIsWidgetAreaOpen } = useDispatch( 'core/edit-widgets' );
const { setIsWidgetAreaOpen } = useDispatch( editWidgetsStore );

const wrapper = useRef();
const setOpen = useCallback(
Expand Down
7 changes: 4 additions & 3 deletions packages/edit-widgets/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,24 @@ import SaveButton from '../save-button';
import UndoButton from './undo-redo/undo';
import RedoButton from './undo-redo/redo';
import useLastSelectedWidgetArea from '../../hooks/use-last-selected-widget-area';
import { store as editWidgetsStore } from '../../store';

function Header() {
const inserterButton = useRef();
const isLargeViewport = useViewportMatch( 'medium' );
const widgetAreaClientId = useLastSelectedWidgetArea();
const isLastSelectedWidgetAreaOpen = useSelect(
( select ) =>
select( 'core/edit-widgets' ).getIsWidgetAreaOpen(
select( editWidgetsStore ).getIsWidgetAreaOpen(
widgetAreaClientId
),
[ widgetAreaClientId ]
);
const isInserterOpened = useSelect( ( select ) =>
select( 'core/edit-widgets' ).isInserterOpened()
select( editWidgetsStore ).isInserterOpened()
);
const { setIsWidgetAreaOpen, setIsInserterOpened } = useDispatch(
'core/edit-widgets'
editWidgetsStore
);
const { selectBlock } = useDispatch( 'core/block-editor' );
const handleClick = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import {
import { useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { store as editWidgetsStore } from '../../store';

function KeyboardShortcuts() {
const { redo, undo } = useDispatch( 'core' );
const { saveEditedWidgetAreas } = useDispatch( 'core/edit-widgets' );
const { saveEditedWidgetAreas } = useDispatch( editWidgetsStore );

useShortcut(
'core/edit-widgets/undo',
Expand Down
7 changes: 4 additions & 3 deletions packages/edit-widgets/src/components/layout/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { __ } from '@wordpress/i18n';
import Header from '../header';
import WidgetAreasBlockEditorContent from '../widget-areas-block-editor-content';
import useWidgetLibraryInsertionPoint from '../../hooks/use-widget-library-insertion-point';
import { store as editWidgetsStore } from '../../store';

const interfaceLabels = {
/* translators: accessibility text for the widgets screen top bar landmark region. */
Expand All @@ -40,15 +41,15 @@ function Interface( { blockEditorSettings } ) {
const isMobileViewport = useViewportMatch( 'medium', '<' );
const isHugeViewport = useViewportMatch( 'huge', '>=' );
const { setIsInserterOpened, closeGeneralSidebar } = useDispatch(
'core/edit-widgets'
editWidgetsStore
);
const { rootClientId, insertionIndex } = useWidgetLibraryInsertionPoint();

const { hasSidebarEnabled, isInserterOpened } = useSelect( ( select ) => ( {
hasSidebarEnabled: !! select(
interfaceStore
).getActiveComplementaryArea( 'core/edit-widgets' ),
isInserterOpened: !! select( 'core/edit-widgets' ).isInserterOpened(),
).getActiveComplementaryArea( editWidgetsStore ),
isInserterOpened: !! select( editWidgetsStore ).isInserterOpened(),
} ) );
const ref = useRef();

Expand Down
5 changes: 3 additions & 2 deletions packages/edit-widgets/src/components/save-button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ import { useDispatch, useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import { store as editWidgetsStore } from '../../store';

function SaveButton() {
const { hasEditedWidgetAreaIds, isSaving } = useSelect( ( select ) => {
const { getEditedWidgetAreas, isSavingWidgetAreas } = select(
'core/edit-widgets'
editWidgetsStore
);

return {
hasEditedWidgetAreaIds: getEditedWidgetAreas()?.length > 0,
isSaving: isSavingWidgetAreas(),
};
}, [] );
const { saveEditedWidgetAreas } = useDispatch( 'core/edit-widgets' );
const { saveEditedWidgetAreas } = useDispatch( editWidgetsStore );

return (
<Button
Expand Down
9 changes: 5 additions & 4 deletions packages/edit-widgets/src/components/sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ const WIDGET_AREAS_IDENTIFIER = 'edit-widgets/block-areas';
* Internal dependencies
*/
import WidgetAreas from './widget-areas';
import { store as editWidgetsStore } from '../../store';

function ComplementaryAreaTab( { identifier, label, isActive } ) {
const { enableComplementaryArea } = useDispatch( interfaceStore );
return (
<Button
onClick={ () =>
enableComplementaryArea( 'core/edit-widgets', identifier )
enableComplementaryArea( editWidgetsStore, identifier )
}
className={ classnames( 'edit-widgets-sidebar__panel-tab', {
'is-active': isActive,
Expand Down Expand Up @@ -73,7 +74,7 @@ export default function Sidebar() {

const selectedBlock = getSelectedBlock();

let activeArea = getActiveComplementaryArea( 'core/edit-widgets' );
let activeArea = getActiveComplementaryArea( editWidgetsStore );
if ( ! activeArea ) {
if ( selectedBlock ) {
activeArea = BLOCK_INSPECTOR_IDENTIFIER;
Expand Down Expand Up @@ -117,7 +118,7 @@ export default function Sidebar() {
isGeneralSidebarOpen
) {
enableComplementaryArea(
'core/edit-widgets',
editWidgetsStore,
BLOCK_INSPECTOR_IDENTIFIER
);
}
Expand All @@ -127,7 +128,7 @@ export default function Sidebar() {
isGeneralSidebarOpen
) {
enableComplementaryArea(
'core/edit-widgets',
editWidgetsStore,
WIDGET_AREAS_IDENTIFIER
);
}
Expand Down
7 changes: 6 additions & 1 deletion packages/edit-widgets/src/components/sidebar/widget-areas.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import { blockDefault } from '@wordpress/icons';
import { BlockIcon } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { store as editWidgetsStore } from '../../store';

export default function WidgetAreas( { selectedWidgetAreaId } ) {
const widgetAreas = useSelect(
( select ) => select( 'core/edit-widgets' ).getWidgetAreas(),
( select ) => select( editWidgetsStore ).getWidgetAreas(),
[]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import KeyboardShortcuts from '../keyboard-shortcuts';
import { useEntityBlockEditor } from '@wordpress/core-data';
import { buildWidgetAreasPostId, KIND, POST_TYPE } from '../../store/utils';
import useLastSelectedWidgetArea from '../../hooks/use-last-selected-widget-area';
import { store as editWidgetsStore } from '../../store';

export default function WidgetAreasBlockEditorProvider( {
blockEditorSettings,
Expand All @@ -35,16 +36,16 @@ export default function WidgetAreasBlockEditorProvider( {
select( 'core' ).canUser( 'create', 'media' ),
true
),
widgetAreas: select( 'core/edit-widgets' ).getWidgetAreas(),
widgets: select( 'core/edit-widgets' ).getWidgets(),
widgetAreas: select( editWidgetsStore ).getWidgetAreas(),
widgets: select( editWidgetsStore ).getWidgets(),
reusableBlocks: select( 'core' ).getEntityRecords(
'postType',
'wp_block'
),
} ),
[]
);
const { setIsInserterOpened } = useDispatch( 'core/edit-widgets' );
const { setIsInserterOpened } = useDispatch( editWidgetsStore );

const settings = useMemo( () => {
let mediaUploadBlockEditor;
Expand Down
7 changes: 4 additions & 3 deletions packages/edit-widgets/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
POST_TYPE,
WIDGET_AREA_ENTITY_TYPE,
} from './utils';
import { STORE_NAME as editWidgetsStoreName } from './constants';

/**
* Persists a stub post with given ID to core data store. The post is meant to be in-memory only and
Expand All @@ -49,7 +50,7 @@ export const persistStubPost = function* ( id, blocks ) {

export function* saveEditedWidgetAreas() {
const editedWidgetAreas = yield select(
'core/edit-widgets',
editWidgetsStoreName,
'getEditedWidgetAreas'
);
if ( ! editedWidgetAreas?.length ) {
Expand Down Expand Up @@ -97,7 +98,7 @@ export function* saveWidgetAreas( widgetAreas ) {
}

export function* saveWidgetArea( widgetAreaId ) {
const widgets = yield select( 'core/edit-widgets', 'getWidgets' );
const widgets = yield select( editWidgetsStoreName, 'getWidgets' );
const widgetIdToClientId = yield getWidgetToClientIdMapping();
const clientIdToWidgetId = invert( widgetIdToClientId );

Expand Down Expand Up @@ -336,6 +337,6 @@ export function* closeGeneralSidebar() {
yield dispatch(
interfaceStore.name,
'disableComplementaryArea',
'core/edit-widgets'
editWidgetsStoreName
);
}
4 changes: 4 additions & 0 deletions packages/edit-widgets/src/store/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Module Constants
*/
export const STORE_NAME = 'core/edit-widgets';
3 changes: 2 additions & 1 deletion packages/edit-widgets/src/store/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
KIND,
WIDGET_AREA_ENTITY_TYPE,
} from './utils';
import { STORE_NAME as editWidgetsStoreName } from './constants';

/**
* Trigger an API Fetch request.
Expand Down Expand Up @@ -190,6 +191,6 @@ const controls = {
};

const getState = ( registry ) =>
registry.stores[ 'core/edit-widgets' ].store.getState();
registry.stores[ editWidgetsStoreName ].store.getState();

export default controls;
6 changes: 1 addition & 5 deletions packages/edit-widgets/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ import * as selectors from './selectors';
import * as actions from './actions';
import controls from './controls';
import './batch-support';

/**
* Module Constants
*/
const STORE_NAME = 'core/edit-widgets';
import { STORE_NAME } from './constants';

/**
* Block editor data store configuration.
Expand Down
13 changes: 7 additions & 6 deletions packages/edit-widgets/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
POST_TYPE,
WIDGET_AREA_ENTITY_TYPE,
} from './utils';
import { STORE_NAME as editWidgetsStoreName } from './constants';

export const getWidgets = createRegistrySelector( ( select ) => () => {
const widgets = select( 'core' ).getEntityRecords(
Expand All @@ -38,7 +39,7 @@ export const getWidgets = createRegistrySelector( ( select ) => () => {
*/
export const getWidget = createRegistrySelector(
( select ) => ( state, id ) => {
const widgets = select( 'core/edit-widgets' ).getWidgets();
const widgets = select( editWidgetsStoreName ).getWidgets();
return widgets[ id ];
}
);
Expand Down Expand Up @@ -66,7 +67,7 @@ export const getWidgetIdForClientId = ( state, clientId ) => {
*/
export const getWidgetAreaForClientId = createRegistrySelector(
( select ) => ( state, clientId ) => {
const widgetAreas = select( 'core/edit-widgets' ).getWidgetAreas();
const widgetAreas = select( editWidgetsStoreName ).getWidgetAreas();
for ( const widgetArea of widgetAreas ) {
const post = select( 'core' ).getEditedEntityRecord(
KIND,
Expand All @@ -83,7 +84,7 @@ export const getWidgetAreaForClientId = createRegistrySelector(

export const getEditedWidgetAreas = createRegistrySelector(
( select ) => ( state, ids ) => {
let widgetAreas = select( 'core/edit-widgets' ).getWidgetAreas();
let widgetAreas = select( editWidgetsStoreName ).getWidgetAreas();
if ( ! widgetAreas ) {
return [];
}
Expand Down Expand Up @@ -119,7 +120,7 @@ export const getEditedWidgetAreas = createRegistrySelector(
export const getReferenceWidgetBlocks = createRegistrySelector(
( select ) => ( state, referenceWidgetName = null ) => {
const results = [];
const widgetAreas = select( 'core/edit-widgets' ).getWidgetAreas();
const widgetAreas = select( editWidgetsStoreName ).getWidgetAreas();
for ( const _widgetArea of widgetAreas ) {
const post = select( 'core' ).getEditedEntityRecord(
KIND,
Expand All @@ -142,7 +143,7 @@ export const getReferenceWidgetBlocks = createRegistrySelector(
);

export const isSavingWidgetAreas = createRegistrySelector( ( select ) => () => {
const widgetAreasIds = select( 'core/edit-widgets' )
const widgetAreasIds = select( editWidgetsStoreName )
.getWidgetAreas()
?.map( ( { id } ) => id );
if ( ! widgetAreasIds ) {
Expand All @@ -161,7 +162,7 @@ export const isSavingWidgetAreas = createRegistrySelector( ( select ) => () => {
}

const widgetIds = [
...Object.keys( select( 'core/edit-widgets' ).getWidgets() ),
...Object.keys( select( editWidgetsStoreName ).getWidgets() ),
undefined, // account for new widgets without an ID
];
for ( const id of widgetIds ) {
Expand Down