Skip to content

Commit d8ca105

Browse files
t-hamanoMamaduka
andauthored
Core Data: Create icons entity (#75773)
* Create icons entity * Just use useSelect * Make getIcons selector public * Remove getIcons selector * Minor cleanup --------- Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
1 parent 90a61de commit d8ca105

11 files changed

Lines changed: 65 additions & 78 deletions

File tree

‎packages/block-library/src/icon/components/custom-inserter/icon-grid.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import HtmlRenderer from '../../../utils/html-renderer';
1212
export default function IconGrid( { icons, onChange, attributes } ) {
1313
return (
1414
<div className="wp-block-icon__inserter-grid">
15-
{ icons.length === 0 ? (
15+
{ ! icons?.length ? (
1616
<div className="wp-block-icon__inserter-grid-no-results">
1717
<p>{ __( 'No results found.' ) }</p>
1818
</div>

‎packages/block-library/src/icon/components/custom-inserter/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import IconGrid from './icon-grid';
1313
import { normalizeSearchInput } from '../../../utils/search-patterns';
1414

1515
export default function CustomInserterModal( {
16-
icons,
16+
icons = [],
1717
setInserterOpen,
1818
attributes,
1919
setAttributes,

‎packages/block-library/src/icon/edit.js‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,17 @@ import {
2525
__experimentalGetSpacingClassesAndStyles as useSpacingProps,
2626
getDimensionsClassesAndStyles as useDimensionsProps,
2727
} from '@wordpress/block-editor';
28-
import { useSelect } from '@wordpress/data';
29-
import { store as coreDataStore } from '@wordpress/core-data';
3028
import { useState } from '@wordpress/element';
3129
import { SVG, Rect, Path } from '@wordpress/primitives';
30+
import { useSelect } from '@wordpress/data';
31+
import { store as coreDataStore } from '@wordpress/core-data';
3232

3333
/**
3434
* Internal dependencies
3535
*/
3636
import { useToolsPanelDropdownMenuProps } from '../utils/hooks';
3737
import HtmlRenderer from '../utils/html-renderer';
3838
import { CustomInserterModal } from './components';
39-
import { unlock } from '../lock-unlock';
4039

4140
const IconPlaceholder = ( { className, style } ) => (
4241
<SVG
@@ -70,14 +69,25 @@ export function Edit( { attributes, setAttributes } ) {
7069
const borderProps = useBorderProps( attributes );
7170
const dimensionsProps = useDimensionsProps( attributes );
7271

73-
const allIcons = useSelect( ( select ) => {
74-
return unlock( select( coreDataStore ) ).getIcons();
75-
}, [] );
72+
const { selectedIcon, allIcons = [] } = useSelect(
73+
( select ) => {
74+
const { getEntityRecord, getEntityRecords } =
75+
select( coreDataStore );
76+
return {
77+
selectedIcon: icon
78+
? getEntityRecord( 'root', 'icon', icon )
79+
: null,
80+
allIcons: isInserterOpen
81+
? getEntityRecords( 'root', 'icon', {
82+
per_page: -1,
83+
} )
84+
: undefined,
85+
};
86+
},
87+
[ isInserterOpen, icon ]
88+
);
7689

77-
const iconToDisplay =
78-
allIcons?.length > 0
79-
? allIcons?.find( ( { name } ) => name === icon )?.content
80-
: '';
90+
const iconToDisplay = selectedIcon?.content || '';
8191

8292
const blockControls = (
8393
<>

‎packages/core-data/src/entities.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ export const rootEntitiesConfig = [
226226
plural: 'fontCollections',
227227
key: 'slug',
228228
},
229+
{
230+
label: __( 'Icons' ),
231+
name: 'icon',
232+
kind: 'root',
233+
baseURL: '/wp/v2/icons',
234+
baseURLParams: { context: 'view' },
235+
plural: 'icons',
236+
key: 'name',
237+
},
229238
].map( ( entity ) => {
230239
const syncEnabledRootEntities = new Set( [ 'comment' ] );
231240

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import type { Context, OmitNevers } from './helpers';
5+
6+
import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records';
7+
8+
declare module './base-entity-records' {
9+
export namespace BaseEntityRecords {
10+
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
11+
export interface Icon< C extends Context > {
12+
/**
13+
* The icon name.
14+
*/
15+
name: string;
16+
/**
17+
* The icon content (SVG markup).
18+
*/
19+
content: string;
20+
/**
21+
* The icon label.
22+
*/
23+
label: string;
24+
}
25+
}
26+
}
27+
28+
export type Icon< C extends Context = 'edit' > = OmitNevers<
29+
_BaseEntityRecords.Icon< C >
30+
>;

‎packages/core-data/src/entity-types/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
} from './font-collection';
1313
import type { FontFamily, FontFace, WpFontFamily } from './font-family';
1414
import type { GlobalStylesRevision } from './global-styles-revision';
15+
import type { Icon } from './icon';
1516
import type { MenuLocation } from './menu-location';
1617
import type { NavMenu } from './nav-menu';
1718
import type { NavMenuItem } from './nav-menu-item';
@@ -45,6 +46,7 @@ export type {
4546
FontFace,
4647
FontFamily,
4748
GlobalStylesRevision,
49+
Icon,
4850
MenuLocation,
4951
NavMenu,
5052
NavMenuItem,
@@ -109,6 +111,7 @@ export interface PerPackageEntityRecords< C extends Context > {
109111
| Comment< C >
110112
| FontCollection< C >
111113
| GlobalStylesRevision< C >
114+
| Icon< C >
112115
| MenuLocation< C >
113116
| NavMenu< C >
114117
| NavMenuItem< C >

‎packages/core-data/src/private-actions.js‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,3 @@ export function receiveEditorAssets( assets ) {
160160
assets,
161161
};
162162
}
163-
164-
/**
165-
* Returns an action object used to receive icons.
166-
*
167-
* @param {Array} icons List of icons.
168-
*
169-
* @return {Object} Action object.
170-
*/
171-
export function receiveIcons( icons ) {
172-
return {
173-
type: 'RECEIVE_ICONS',
174-
icons,
175-
};
176-
}

‎packages/core-data/src/private-selectors.ts‎

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ import { createSelector, createRegistrySelector } from '@wordpress/data';
66
/**
77
* Internal dependencies
88
*/
9-
import {
10-
getDefaultTemplateId,
11-
getEntityRecord,
12-
type State,
13-
type Icon,
14-
} from './selectors';
9+
import { getDefaultTemplateId, getEntityRecord, type State } from './selectors';
1510
import { STORE_NAME } from './name';
1611
import { unlock } from './lock-unlock';
1712
import { getSyncManager } from './sync';
@@ -309,13 +304,3 @@ export function getEditorSettings(
309304
export function getEditorAssets( state: State ): Record< string, any > | null {
310305
return state.editorAssets;
311306
}
312-
313-
/**
314-
* Returns the list of available icons.
315-
*
316-
* @param state Data state.
317-
* @return The list of icons or empty array if not loaded.
318-
*/
319-
export function getIcons( state: State ): Icon[] {
320-
return state.icons ?? [];
321-
}

‎packages/core-data/src/reducer.js‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -660,22 +660,6 @@ export function editorAssets( state = null, action ) {
660660
return state;
661661
}
662662

663-
/**
664-
* Reducer managing icons.
665-
*
666-
* @param {Array} state Current state.
667-
* @param {Object} action Action object.
668-
*
669-
* @return {Array} Updated state.
670-
*/
671-
export function icons( state = [], action ) {
672-
switch ( action.type ) {
673-
case 'RECEIVE_ICONS':
674-
return action.icons;
675-
}
676-
return state;
677-
}
678-
679663
/**
680664
* Reducer managing sync connection states for entities.
681665
* Keyed by "kind/name:id" (e.g., "postType/post:123").
@@ -725,6 +709,5 @@ export default combineReducers( {
725709
registeredPostMeta,
726710
editorSettings,
727711
editorAssets,
728-
icons,
729712
syncConnectionStatuses,
730713
} );

‎packages/core-data/src/resolvers.js‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,15 +1255,3 @@ export const getEditorAssets =
12551255
} );
12561256
dispatch.receiveEditorAssets( assets );
12571257
};
1258-
1259-
/**
1260-
* Requests icons from the REST API.
1261-
*/
1262-
export const getIcons =
1263-
() =>
1264-
async ( { dispatch } ) => {
1265-
const icons = await apiFetch( {
1266-
path: '/wp/v2/icons',
1267-
} );
1268-
dispatch.receiveIcons( icons );
1269-
};

0 commit comments

Comments
 (0)