@wordpress/core-commands 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import { useAddPostTypeCommands } from './add-post-type-commands';
5
+ import { useSiteEditorNavigationCommands } from './site-editor-navigation-commands';
6
+ import { lock } from './lock-unlock';
7
+
8
+ function useCommands() {
9
+ useAddPostTypeCommands();
10
+ useSiteEditorNavigationCommands();
11
+ }
12
+
13
+ export const privateApis = {};
14
+ lock( privateApis, {
15
+ useCommands,
16
+ } );
@@ -0,0 +1,129 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { privateApis } from '@wordpress/commands';
5
+ import { __ } from '@wordpress/i18n';
6
+ import { useMemo } from '@wordpress/element';
7
+ import { useSelect } from '@wordpress/data';
8
+ import { store as coreStore } from '@wordpress/core-data';
9
+ import { post, page, layout, symbolFilled } from '@wordpress/icons';
10
+ import { privateApis as routerPrivateApis } from '@wordpress/router';
11
+ import { getQueryArg, addQueryArgs, getPath } from '@wordpress/url';
12
+
13
+ /**
14
+ * Internal dependencies
15
+ */
16
+ import { unlock } from './lock-unlock';
17
+
18
+ const { useCommandLoader } = unlock( privateApis );
19
+ const { useHistory } = unlock( routerPrivateApis );
20
+
21
+ const icons = {
22
+ post,
23
+ page,
24
+ wp_template: layout,
25
+ wp_template_part: symbolFilled,
26
+ };
27
+
28
+ const getNavigationCommandLoaderPerPostType = ( postType ) =>
29
+ function useNavigationCommandLoader( { search } ) {
30
+ const history = useHistory();
31
+ const supportsSearch = ! [ 'wp_template', 'wp_template_part' ].includes(
32
+ postType
33
+ );
34
+ const deps = supportsSearch ? [ search ] : [];
35
+ const { records, isLoading } = useSelect( ( select ) => {
36
+ const { getEntityRecords } = select( coreStore );
37
+ const query = supportsSearch
38
+ ? {
39
+ search: !! search ? search : undefined,
40
+ per_page: 10,
41
+ orderby: search ? 'relevance' : 'date',
42
+ }
43
+ : {
44
+ per_page: -1,
45
+ };
46
+ return {
47
+ records: getEntityRecords( 'postType', postType, query ),
48
+ isLoading: ! select( coreStore ).hasFinishedResolution(
49
+ 'getEntityRecords',
50
+ [ 'postType', postType, query ]
51
+ ),
52
+ // We're using the string literal to check whether we're in the site editor.
53
+ /* eslint-disable-next-line @wordpress/data-no-store-string-literals */
54
+ isSiteEditor: !! select( 'edit-site' ),
55
+ };
56
+ }, deps );
57
+
58
+ const commands = useMemo( () => {
59
+ return ( records ?? [] ).slice( 0, 10 ).map( ( record ) => {
60
+ const isSiteEditor = getPath( window.location.href )?.includes(
61
+ 'site-editor.php'
62
+ );
63
+ const extraArgs = isSiteEditor
64
+ ? { canvas: getQueryArg( window.location.href, 'canvas' ) }
65
+ : {};
66
+ return {
67
+ name: record.title?.rendered + ' ' + record.id,
68
+ label: record.title?.rendered
69
+ ? record.title?.rendered
70
+ : __( '(no title)' ),
71
+ icon: icons[ postType ],
72
+ callback: ( { close } ) => {
73
+ const args = {
74
+ postType,
75
+ postId: record.id,
76
+ ...extraArgs,
77
+ };
78
+ const targetUrl = addQueryArgs(
79
+ 'site-editor.php',
80
+ args
81
+ );
82
+ if ( isSiteEditor ) {
83
+ history.push( args );
84
+ } else {
85
+ document.location = targetUrl;
86
+ }
87
+ close();
88
+ },
89
+ };
90
+ } );
91
+ }, [ records, history ] );
92
+
93
+ return {
94
+ commands,
95
+ isLoading,
96
+ };
97
+ };
98
+
99
+ const usePageNavigationCommandLoader =
100
+ getNavigationCommandLoaderPerPostType( 'page' );
101
+ const usePostNavigationCommandLoader =
102
+ getNavigationCommandLoaderPerPostType( 'post' );
103
+ const useTemplateNavigationCommandLoader =
104
+ getNavigationCommandLoaderPerPostType( 'wp_template' );
105
+ const useTemplatePartNavigationCommandLoader =
106
+ getNavigationCommandLoaderPerPostType( 'wp_template_part' );
107
+
108
+ export function useSiteEditorNavigationCommands() {
109
+ useCommandLoader( {
110
+ name: 'core/edit-site/navigate-pages',
111
+ group: __( 'Pages' ),
112
+ hook: usePageNavigationCommandLoader,
113
+ } );
114
+ useCommandLoader( {
115
+ name: 'core/edit-site/navigate-posts',
116
+ group: __( 'Posts' ),
117
+ hook: usePostNavigationCommandLoader,
118
+ } );
119
+ useCommandLoader( {
120
+ name: 'core/edit-site/navigate-templates',
121
+ group: __( 'Templates' ),
122
+ hook: useTemplateNavigationCommandLoader,
123
+ } );
124
+ useCommandLoader( {
125
+ name: 'core/edit-site/navigate-template-parts',
126
+ group: __( 'Template Parts' ),
127
+ hook: useTemplatePartNavigationCommandLoader,
128
+ } );
129
+ }