@wordpress/core-commands 1.8.0 → 1.8.2

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.
@@ -7,10 +7,75 @@ exports.useAdminNavigationCommands = useAdminNavigationCommands;
7
7
  var _commands = require("@wordpress/commands");
8
8
  var _i18n = require("@wordpress/i18n");
9
9
  var _icons = require("@wordpress/icons");
10
+ var _url = require("@wordpress/url");
11
+ var _coreData = require("@wordpress/core-data");
12
+ var _data = require("@wordpress/data");
13
+ var _element = require("@wordpress/element");
14
+ var _notices = require("@wordpress/notices");
15
+ var _router = require("@wordpress/router");
16
+ var _lockUnlock = require("./lock-unlock");
10
17
  /**
11
18
  * WordPress dependencies
12
19
  */
13
20
 
21
+ /**
22
+ * Internal dependencies
23
+ */
24
+
25
+ const {
26
+ useHistory
27
+ } = (0, _lockUnlock.unlock)(_router.privateApis);
28
+ function useAddNewPageCommand() {
29
+ const isSiteEditor = (0, _url.getPath)(window.location.href)?.includes('site-editor.php');
30
+ const history = useHistory();
31
+ const isBlockBasedTheme = (0, _data.useSelect)(select => {
32
+ return select(_coreData.store).getCurrentTheme()?.is_block_theme;
33
+ }, []);
34
+ const {
35
+ saveEntityRecord
36
+ } = (0, _data.useDispatch)(_coreData.store);
37
+ const {
38
+ createErrorNotice
39
+ } = (0, _data.useDispatch)(_notices.store);
40
+ const createPageEntity = (0, _element.useCallback)(async ({
41
+ close
42
+ }) => {
43
+ try {
44
+ const page = await saveEntityRecord('postType', 'page', {
45
+ status: 'draft'
46
+ }, {
47
+ throwOnError: true
48
+ });
49
+ if (page?.id) {
50
+ history.push({
51
+ postId: page.id,
52
+ postType: 'page',
53
+ canvas: 'edit'
54
+ });
55
+ }
56
+ } catch (error) {
57
+ const errorMessage = error.message && error.code !== 'unknown_error' ? error.message : (0, _i18n.__)('An error occurred while creating the item.');
58
+ createErrorNotice(errorMessage, {
59
+ type: 'snackbar'
60
+ });
61
+ } finally {
62
+ close();
63
+ }
64
+ }, [createErrorNotice, history, saveEntityRecord]);
65
+ const commands = (0, _element.useMemo)(() => {
66
+ const addNewPage = isSiteEditor && isBlockBasedTheme ? createPageEntity : () => document.location.href = 'post-new.php?post_type=page';
67
+ return [{
68
+ name: 'core/add-new-page',
69
+ label: (0, _i18n.__)('Add new page'),
70
+ icon: _icons.plus,
71
+ callback: addNewPage
72
+ }];
73
+ }, [createPageEntity, isSiteEditor, isBlockBasedTheme]);
74
+ return {
75
+ isLoading: false,
76
+ commands
77
+ };
78
+ }
14
79
  function useAdminNavigationCommands() {
15
80
  (0, _commands.useCommand)({
16
81
  name: 'core/add-new-post',
@@ -20,13 +85,9 @@ function useAdminNavigationCommands() {
20
85
  document.location.href = 'post-new.php';
21
86
  }
22
87
  });
23
- (0, _commands.useCommand)({
88
+ (0, _commands.useCommandLoader)({
24
89
  name: 'core/add-new-page',
25
- label: (0, _i18n.__)('Add new page'),
26
- icon: _icons.plus,
27
- callback: () => {
28
- document.location.href = 'post-new.php?post_type=page';
29
- }
90
+ hook: useAddNewPageCommand
30
91
  });
31
92
  }
32
93
  //# sourceMappingURL=admin-navigation-commands.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_commands","require","_i18n","_icons","useAdminNavigationCommands","useCommand","name","label","__","icon","plus","callback","document","location","href"],"sources":["@wordpress/core-commands/src/admin-navigation-commands.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCommand } from '@wordpress/commands';\nimport { __ } from '@wordpress/i18n';\nimport { plus } from '@wordpress/icons';\n\nexport function useAdminNavigationCommands() {\n\tuseCommand( {\n\t\tname: 'core/add-new-post',\n\t\tlabel: __( 'Add new post' ),\n\t\ticon: plus,\n\t\tcallback: () => {\n\t\t\tdocument.location.href = 'post-new.php';\n\t\t},\n\t} );\n\tuseCommand( {\n\t\tname: 'core/add-new-page',\n\t\tlabel: __( 'Add new page' ),\n\t\ticon: plus,\n\t\tcallback: () => {\n\t\t\tdocument.location.href = 'post-new.php?post_type=page';\n\t\t},\n\t} );\n}\n"],"mappings":";;;;;;AAGA,IAAAA,SAAA,GAAAC,OAAA;AACA,IAAAC,KAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AALA;AACA;AACA;;AAKO,SAASG,0BAA0BA,CAAA,EAAG;EAC5C,IAAAC,oBAAU,EAAE;IACXC,IAAI,EAAE,mBAAmB;IACzBC,KAAK,EAAE,IAAAC,QAAE,EAAE,cAAe,CAAC;IAC3BC,IAAI,EAAEC,WAAI;IACVC,QAAQ,EAAEA,CAAA,KAAM;MACfC,QAAQ,CAACC,QAAQ,CAACC,IAAI,GAAG,cAAc;IACxC;EACD,CAAE,CAAC;EACH,IAAAT,oBAAU,EAAE;IACXC,IAAI,EAAE,mBAAmB;IACzBC,KAAK,EAAE,IAAAC,QAAE,EAAE,cAAe,CAAC;IAC3BC,IAAI,EAAEC,WAAI;IACVC,QAAQ,EAAEA,CAAA,KAAM;MACfC,QAAQ,CAACC,QAAQ,CAACC,IAAI,GAAG,6BAA6B;IACvD;EACD,CAAE,CAAC;AACJ","ignoreList":[]}
1
+ {"version":3,"names":["_commands","require","_i18n","_icons","_url","_coreData","_data","_element","_notices","_router","_lockUnlock","useHistory","unlock","routerPrivateApis","useAddNewPageCommand","isSiteEditor","getPath","window","location","href","includes","history","isBlockBasedTheme","useSelect","select","coreStore","getCurrentTheme","is_block_theme","saveEntityRecord","useDispatch","createErrorNotice","noticesStore","createPageEntity","useCallback","close","page","status","throwOnError","id","push","postId","postType","canvas","error","errorMessage","message","code","__","type","commands","useMemo","addNewPage","document","name","label","icon","plus","callback","isLoading","useAdminNavigationCommands","useCommand","useCommandLoader","hook"],"sources":["@wordpress/core-commands/src/admin-navigation-commands.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCommand, useCommandLoader } from '@wordpress/commands';\nimport { __ } from '@wordpress/i18n';\nimport { plus } from '@wordpress/icons';\nimport { getPath } from '@wordpress/url';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { useCallback, useMemo } from '@wordpress/element';\nimport { store as noticesStore } from '@wordpress/notices';\nimport { privateApis as routerPrivateApis } from '@wordpress/router';\n\n/**\n * Internal dependencies\n */\nimport { unlock } from './lock-unlock';\n\nconst { useHistory } = unlock( routerPrivateApis );\n\nfunction useAddNewPageCommand() {\n\tconst isSiteEditor = getPath( window.location.href )?.includes(\n\t\t'site-editor.php'\n\t);\n\tconst history = useHistory();\n\tconst isBlockBasedTheme = useSelect( ( select ) => {\n\t\treturn select( coreStore ).getCurrentTheme()?.is_block_theme;\n\t}, [] );\n\tconst { saveEntityRecord } = useDispatch( coreStore );\n\tconst { createErrorNotice } = useDispatch( noticesStore );\n\n\tconst createPageEntity = useCallback(\n\t\tasync ( { close } ) => {\n\t\t\ttry {\n\t\t\t\tconst page = await saveEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'page',\n\t\t\t\t\t{\n\t\t\t\t\t\tstatus: 'draft',\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tthrowOnError: true,\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t\tif ( page?.id ) {\n\t\t\t\t\thistory.push( {\n\t\t\t\t\t\tpostId: page.id,\n\t\t\t\t\t\tpostType: 'page',\n\t\t\t\t\t\tcanvas: 'edit',\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t} catch ( error ) {\n\t\t\t\tconst errorMessage =\n\t\t\t\t\terror.message && error.code !== 'unknown_error'\n\t\t\t\t\t\t? error.message\n\t\t\t\t\t\t: __( 'An error occurred while creating the item.' );\n\n\t\t\t\tcreateErrorNotice( errorMessage, {\n\t\t\t\t\ttype: 'snackbar',\n\t\t\t\t} );\n\t\t\t} finally {\n\t\t\t\tclose();\n\t\t\t}\n\t\t},\n\t\t[ createErrorNotice, history, saveEntityRecord ]\n\t);\n\n\tconst commands = useMemo( () => {\n\t\tconst addNewPage =\n\t\t\tisSiteEditor && isBlockBasedTheme\n\t\t\t\t? createPageEntity\n\t\t\t\t: () =>\n\t\t\t\t\t\t( document.location.href =\n\t\t\t\t\t\t\t'post-new.php?post_type=page' );\n\t\treturn [\n\t\t\t{\n\t\t\t\tname: 'core/add-new-page',\n\t\t\t\tlabel: __( 'Add new page' ),\n\t\t\t\ticon: plus,\n\t\t\t\tcallback: addNewPage,\n\t\t\t},\n\t\t];\n\t}, [ createPageEntity, isSiteEditor, isBlockBasedTheme ] );\n\n\treturn {\n\t\tisLoading: false,\n\t\tcommands,\n\t};\n}\n\nexport function useAdminNavigationCommands() {\n\tuseCommand( {\n\t\tname: 'core/add-new-post',\n\t\tlabel: __( 'Add new post' ),\n\t\ticon: plus,\n\t\tcallback: () => {\n\t\t\tdocument.location.href = 'post-new.php';\n\t\t},\n\t} );\n\n\tuseCommandLoader( {\n\t\tname: 'core/add-new-page',\n\t\thook: useAddNewPageCommand,\n\t} );\n}\n"],"mappings":";;;;;;AAGA,IAAAA,SAAA,GAAAC,OAAA;AACA,IAAAC,KAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AACA,IAAAG,IAAA,GAAAH,OAAA;AACA,IAAAI,SAAA,GAAAJ,OAAA;AACA,IAAAK,KAAA,GAAAL,OAAA;AACA,IAAAM,QAAA,GAAAN,OAAA;AACA,IAAAO,QAAA,GAAAP,OAAA;AACA,IAAAQ,OAAA,GAAAR,OAAA;AAKA,IAAAS,WAAA,GAAAT,OAAA;AAhBA;AACA;AACA;;AAWA;AACA;AACA;;AAGA,MAAM;EAAEU;AAAW,CAAC,GAAG,IAAAC,kBAAM,EAAEC,mBAAkB,CAAC;AAElD,SAASC,oBAAoBA,CAAA,EAAG;EAC/B,MAAMC,YAAY,GAAG,IAAAC,YAAO,EAAEC,MAAM,CAACC,QAAQ,CAACC,IAAK,CAAC,EAAEC,QAAQ,CAC7D,iBACD,CAAC;EACD,MAAMC,OAAO,GAAGV,UAAU,CAAC,CAAC;EAC5B,MAAMW,iBAAiB,GAAG,IAAAC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BAA+B;IACpC,OAAO,CACN;MACCkC,IAAI,EAAE,mBAAmB;MACzBC,KAAK,EAAE,IAAAP,QAAE,EAAE,cAAe,CAAC;MAC3BQ,IAAI,EAAEC,WAAI;MACVC,QAAQ,EAAEN;IACX,CAAC,CACD;EACF,CAAC,EAAE,CAAEnB,gBAAgB,EAAEjB,YAAY,EAAEO,iBAAiB,CAAG,CAAC;EAE1D,OAAO;IACNoC,SAAS,EAAE,KAAK;IAChBT;EACD,CAAC;AACF;AAEO,SAASU,0BAA0BA,CAAA,EAAG;EAC5C,IAAAC,oBAAU,EAAE;IACXP,IAAI,EAAE,mBAAmB;IACzBC,KAAK,EAAE,IAAAP,QAAE,EAAE,cAAe,CAAC;IAC3BQ,IAAI,EAAEC,WAAI;IACVC,QAAQ,EAAEA,CAAA,KAAM;MACfL,QAAQ,CAAClC,QAAQ,CAACC,IAAI,GAAG,cAAc;IACxC;EACD,CAAE,CAAC;EAEH,IAAA0C,0BAAgB,EAAE;IACjBR,IAAI,EAAE,mBAAmB;IACzBS,IAAI,EAAEhD;EACP,CAAE,CAAC;AACJ","ignoreList":[]}
@@ -1,9 +1,74 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import { useCommand } from '@wordpress/commands';
4
+ import { useCommand, useCommandLoader } from '@wordpress/commands';
5
5
  import { __ } from '@wordpress/i18n';
6
6
  import { plus } from '@wordpress/icons';
7
+ import { getPath } from '@wordpress/url';
8
+ import { store as coreStore } from '@wordpress/core-data';
9
+ import { useSelect, useDispatch } from '@wordpress/data';
10
+ import { useCallback, useMemo } from '@wordpress/element';
11
+ import { store as noticesStore } from '@wordpress/notices';
12
+ import { privateApis as routerPrivateApis } from '@wordpress/router';
13
+
14
+ /**
15
+ * Internal dependencies
16
+ */
17
+ import { unlock } from './lock-unlock';
18
+ const {
19
+ useHistory
20
+ } = unlock(routerPrivateApis);
21
+ function useAddNewPageCommand() {
22
+ const isSiteEditor = getPath(window.location.href)?.includes('site-editor.php');
23
+ const history = useHistory();
24
+ const isBlockBasedTheme = useSelect(select => {
25
+ return select(coreStore).getCurrentTheme()?.is_block_theme;
26
+ }, []);
27
+ const {
28
+ saveEntityRecord
29
+ } = useDispatch(coreStore);
30
+ const {
31
+ createErrorNotice
32
+ } = useDispatch(noticesStore);
33
+ const createPageEntity = useCallback(async ({
34
+ close
35
+ }) => {
36
+ try {
37
+ const page = await saveEntityRecord('postType', 'page', {
38
+ status: 'draft'
39
+ }, {
40
+ throwOnError: true
41
+ });
42
+ if (page?.id) {
43
+ history.push({
44
+ postId: page.id,
45
+ postType: 'page',
46
+ canvas: 'edit'
47
+ });
48
+ }
49
+ } catch (error) {
50
+ const errorMessage = error.message && error.code !== 'unknown_error' ? error.message : __('An error occurred while creating the item.');
51
+ createErrorNotice(errorMessage, {
52
+ type: 'snackbar'
53
+ });
54
+ } finally {
55
+ close();
56
+ }
57
+ }, [createErrorNotice, history, saveEntityRecord]);
58
+ const commands = useMemo(() => {
59
+ const addNewPage = isSiteEditor && isBlockBasedTheme ? createPageEntity : () => document.location.href = 'post-new.php?post_type=page';
60
+ return [{
61
+ name: 'core/add-new-page',
62
+ label: __('Add new page'),
63
+ icon: plus,
64
+ callback: addNewPage
65
+ }];
66
+ }, [createPageEntity, isSiteEditor, isBlockBasedTheme]);
67
+ return {
68
+ isLoading: false,
69
+ commands
70
+ };
71
+ }
7
72
  export function useAdminNavigationCommands() {
8
73
  useCommand({
9
74
  name: 'core/add-new-post',
@@ -13,13 +78,9 @@ export function useAdminNavigationCommands() {
13
78
  document.location.href = 'post-new.php';
14
79
  }
15
80
  });
16
- useCommand({
81
+ useCommandLoader({
17
82
  name: 'core/add-new-page',
18
- label: __('Add new page'),
19
- icon: plus,
20
- callback: () => {
21
- document.location.href = 'post-new.php?post_type=page';
22
- }
83
+ hook: useAddNewPageCommand
23
84
  });
24
85
  }
25
86
  //# sourceMappingURL=admin-navigation-commands.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["useCommand","__","plus","useAdminNavigationCommands","name","label","icon","callback","document","location","href"],"sources":["@wordpress/core-commands/src/admin-navigation-commands.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCommand } from '@wordpress/commands';\nimport { __ } from '@wordpress/i18n';\nimport { plus } from '@wordpress/icons';\n\nexport function useAdminNavigationCommands() {\n\tuseCommand( {\n\t\tname: 'core/add-new-post',\n\t\tlabel: __( 'Add new post' ),\n\t\ticon: plus,\n\t\tcallback: () => {\n\t\t\tdocument.location.href = 'post-new.php';\n\t\t},\n\t} );\n\tuseCommand( {\n\t\tname: 'core/add-new-page',\n\t\tlabel: __( 'Add new page' ),\n\t\ticon: plus,\n\t\tcallback: () => {\n\t\t\tdocument.location.href = 'post-new.php?post_type=page';\n\t\t},\n\t} );\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,UAAU,QAAQ,qBAAqB;AAChD,SAASC,EAAE,QAAQ,iBAAiB;AACpC,SAASC,IAAI,QAAQ,kBAAkB;AAEvC,OAAO,SAASC,0BAA0BA,CAAA,EAAG;EAC5CH,UAAU,CAAE;IACXI,IAAI,EAAE,mBAAmB;IACzBC,KAAK,EAAEJ,EAAE,CAAE,cAAe,CAAC;IAC3BK,IAAI,EAAEJ,IAAI;IACVK,QAAQ,EAAEA,CAAA,KAAM;MACfC,QAAQ,CAACC,QAAQ,CAACC,IAAI,GAAG,cAAc;IACxC;EACD,CAAE,CAAC;EACHV,UAAU,CAAE;IACXI,IAAI,EAAE,mBAAmB;IACzBC,KAAK,EAAEJ,EAAE,CAAE,cAAe,CAAC;IAC3BK,IAAI,EAAEJ,IAAI;IACVK,QAAQ,EAAEA,CAAA,KAAM;MACfC,QAAQ,CAACC,QAAQ,CAACC,IAAI,GAAG,6BAA6B;IACvD;EACD,CAAE,CAAC;AACJ","ignoreList":[]}
1
+ {"version":3,"names":["useCommand","useCommandLoader","__","plus","getPath","store","coreStore","useSelect","useDispatch","useCallback","useMemo","noticesStore","privateApis","routerPrivateApis","unlock","useHistory","useAddNewPageCommand","isSiteEditor","window","location","href","includes","history","isBlockBasedTheme","select","getCurrentTheme","is_block_theme","saveEntityRecord","createErrorNotice","createPageEntity","close","page","status","throwOnError","id","push","postId","postType","canvas","error","errorMessage","message","code","type","commands","addNewPage","document","name","label","icon","callback","isLoading","useAdminNavigationCommands","hook"],"sources":["@wordpress/core-commands/src/admin-navigation-commands.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCommand, useCommandLoader } from '@wordpress/commands';\nimport { __ } from '@wordpress/i18n';\nimport { plus } from '@wordpress/icons';\nimport { getPath } from '@wordpress/url';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { useCallback, useMemo } from '@wordpress/element';\nimport { store as noticesStore } from '@wordpress/notices';\nimport { privateApis as routerPrivateApis } from '@wordpress/router';\n\n/**\n * Internal dependencies\n */\nimport { unlock } from './lock-unlock';\n\nconst { useHistory } = unlock( routerPrivateApis );\n\nfunction useAddNewPageCommand() {\n\tconst isSiteEditor = getPath( window.location.href )?.includes(\n\t\t'site-editor.php'\n\t);\n\tconst history = useHistory();\n\tconst isBlockBasedTheme = useSelect( ( select ) => {\n\t\treturn select( coreStore ).getCurrentTheme()?.is_block_theme;\n\t}, [] );\n\tconst { saveEntityRecord } = useDispatch( coreStore );\n\tconst { createErrorNotice } = useDispatch( noticesStore );\n\n\tconst createPageEntity = useCallback(\n\t\tasync ( { close } ) => {\n\t\t\ttry {\n\t\t\t\tconst page = await saveEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'page',\n\t\t\t\t\t{\n\t\t\t\t\t\tstatus: 'draft',\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tthrowOnError: true,\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t\tif ( page?.id ) {\n\t\t\t\t\thistory.push( {\n\t\t\t\t\t\tpostId: page.id,\n\t\t\t\t\t\tpostType: 'page',\n\t\t\t\t\t\tcanvas: 'edit',\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t} catch ( error ) {\n\t\t\t\tconst errorMessage =\n\t\t\t\t\terror.message && error.code !== 'unknown_error'\n\t\t\t\t\t\t? error.message\n\t\t\t\t\t\t: __( 'An error occurred while creating the item.' );\n\n\t\t\t\tcreateErrorNotice( errorMessage, {\n\t\t\t\t\ttype: 'snackbar',\n\t\t\t\t} );\n\t\t\t} finally {\n\t\t\t\tclose();\n\t\t\t}\n\t\t},\n\t\t[ createErrorNotice, history, saveEntityRecord ]\n\t);\n\n\tconst commands = useMemo( () => {\n\t\tconst addNewPage =\n\t\t\tisSiteEditor && isBlockBasedTheme\n\t\t\t\t? createPageEntity\n\t\t\t\t: () =>\n\t\t\t\t\t\t( document.location.href =\n\t\t\t\t\t\t\t'post-new.php?post_type=page' );\n\t\treturn [\n\t\t\t{\n\t\t\t\tname: 'core/add-new-page',\n\t\t\t\tlabel: __( 'Add new page' ),\n\t\t\t\ticon: plus,\n\t\t\t\tcallback: addNewPage,\n\t\t\t},\n\t\t];\n\t}, [ createPageEntity, isSiteEditor, isBlockBasedTheme ] );\n\n\treturn {\n\t\tisLoading: false,\n\t\tcommands,\n\t};\n}\n\nexport function useAdminNavigationCommands() {\n\tuseCommand( {\n\t\tname: 'core/add-new-post',\n\t\tlabel: __( 'Add new post' ),\n\t\ticon: plus,\n\t\tcallback: () => {\n\t\t\tdocument.location.href = 'post-new.php';\n\t\t},\n\t} );\n\n\tuseCommandLoader( {\n\t\tname: 'core/add-new-page',\n\t\thook: useAddNewPageCommand,\n\t} );\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,UAAU,EAAEC,gBAAgB,QAAQ,qBAAqB;AAClE,SAASC,EAAE,QAAQ,iBAAiB;AACpC,SAASC,IAAI,QAAQ,kBAAkB;AACvC,SAASC,OAAO,QAAQ,gBAAgB;AACxC,SAASC,KAAK,IAAIC,SAAS,QAAQ,sBAAsB;AACzD,SAASC,SAAS,EAAEC,WAAW,QAAQ,iBAAiB;AAC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1.8.2",
4
4
  "description": "WordPress core reusable commands.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -28,18 +28,19 @@
28
28
  "sideEffects": false,
29
29
  "dependencies": {
30
30
  "@babel/runtime": "^7.16.0",
31
- "@wordpress/block-editor": "^14.3.0",
32
- "@wordpress/commands": "^1.8.0",
33
- "@wordpress/compose": "^7.8.0",
34
- "@wordpress/core-data": "^7.8.0",
35
- "@wordpress/data": "^10.8.0",
36
- "@wordpress/element": "^6.8.0",
37
- "@wordpress/html-entities": "^4.8.0",
38
- "@wordpress/i18n": "^5.8.0",
39
- "@wordpress/icons": "^10.8.0",
40
- "@wordpress/private-apis": "^1.8.0",
41
- "@wordpress/router": "^1.8.0",
42
- "@wordpress/url": "^4.8.0"
31
+ "@wordpress/block-editor": "^14.3.2",
32
+ "@wordpress/commands": "^1.8.2",
33
+ "@wordpress/compose": "^7.8.2",
34
+ "@wordpress/core-data": "^7.8.2",
35
+ "@wordpress/data": "^10.8.2",
36
+ "@wordpress/element": "^6.8.1",
37
+ "@wordpress/html-entities": "^4.8.1",
38
+ "@wordpress/i18n": "^5.8.1",
39
+ "@wordpress/icons": "^10.8.1",
40
+ "@wordpress/notices": "^5.8.2",
41
+ "@wordpress/private-apis": "^1.8.1",
42
+ "@wordpress/router": "^1.8.1",
43
+ "@wordpress/url": "^4.8.1"
43
44
  },
44
45
  "peerDependencies": {
45
46
  "react": "^18.0.0",
@@ -48,5 +49,5 @@
48
49
  "publishConfig": {
49
50
  "access": "public"
50
51
  },
51
- "gitHead": "cecf5e14d317aa67407f77a7e5c8b6a43016bd42"
52
+ "gitHead": "51204ac9382d0551d8fdebd3c8d4623dabfa9f3c"
52
53
  }
@@ -1,9 +1,92 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import { useCommand } from '@wordpress/commands';
4
+ import { useCommand, useCommandLoader } from '@wordpress/commands';
5
5
  import { __ } from '@wordpress/i18n';
6
6
  import { plus } from '@wordpress/icons';
7
+ import { getPath } from '@wordpress/url';
8
+ import { store as coreStore } from '@wordpress/core-data';
9
+ import { useSelect, useDispatch } from '@wordpress/data';
10
+ import { useCallback, useMemo } from '@wordpress/element';
11
+ import { store as noticesStore } from '@wordpress/notices';
12
+ import { privateApis as routerPrivateApis } from '@wordpress/router';
13
+
14
+ /**
15
+ * Internal dependencies
16
+ */
17
+ import { unlock } from './lock-unlock';
18
+
19
+ const { useHistory } = unlock( routerPrivateApis );
20
+
21
+ function useAddNewPageCommand() {
22
+ const isSiteEditor = getPath( window.location.href )?.includes(
23
+ 'site-editor.php'
24
+ );
25
+ const history = useHistory();
26
+ const isBlockBasedTheme = useSelect( ( select ) => {
27
+ return select( coreStore ).getCurrentTheme()?.is_block_theme;
28
+ }, [] );
29
+ const { saveEntityRecord } = useDispatch( coreStore );
30
+ const { createErrorNotice } = useDispatch( noticesStore );
31
+
32
+ const createPageEntity = useCallback(
33
+ async ( { close } ) => {
34
+ try {
35
+ const page = await saveEntityRecord(
36
+ 'postType',
37
+ 'page',
38
+ {
39
+ status: 'draft',
40
+ },
41
+ {
42
+ throwOnError: true,
43
+ }
44
+ );
45
+ if ( page?.id ) {
46
+ history.push( {
47
+ postId: page.id,
48
+ postType: 'page',
49
+ canvas: 'edit',
50
+ } );
51
+ }
52
+ } catch ( error ) {
53
+ const errorMessage =
54
+ error.message && error.code !== 'unknown_error'
55
+ ? error.message
56
+ : __( 'An error occurred while creating the item.' );
57
+
58
+ createErrorNotice( errorMessage, {
59
+ type: 'snackbar',
60
+ } );
61
+ } finally {
62
+ close();
63
+ }
64
+ },
65
+ [ createErrorNotice, history, saveEntityRecord ]
66
+ );
67
+
68
+ const commands = useMemo( () => {
69
+ const addNewPage =
70
+ isSiteEditor && isBlockBasedTheme
71
+ ? createPageEntity
72
+ : () =>
73
+ ( document.location.href =
74
+ 'post-new.php?post_type=page' );
75
+ return [
76
+ {
77
+ name: 'core/add-new-page',
78
+ label: __( 'Add new page' ),
79
+ icon: plus,
80
+ callback: addNewPage,
81
+ },
82
+ ];
83
+ }, [ createPageEntity, isSiteEditor, isBlockBasedTheme ] );
84
+
85
+ return {
86
+ isLoading: false,
87
+ commands,
88
+ };
89
+ }
7
90
 
8
91
  export function useAdminNavigationCommands() {
9
92
  useCommand( {
@@ -14,12 +97,9 @@ export function useAdminNavigationCommands() {
14
97
  document.location.href = 'post-new.php';
15
98
  },
16
99
  } );
17
- useCommand( {
100
+
101
+ useCommandLoader( {
18
102
  name: 'core/add-new-page',
19
- label: __( 'Add new page' ),
20
- icon: plus,
21
- callback: () => {
22
- document.location.href = 'post-new.php?post_type=page';
23
- },
103
+ hook: useAddNewPageCommand,
24
104
  } );
25
105
  }