@wordpress/fields 0.5.0 → 0.5.1-next.cd6172eb0.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.
Files changed (125) hide show
  1. package/README.md +29 -0
  2. package/build/actions/duplicate-template-part.js +68 -0
  3. package/build/actions/duplicate-template-part.js.map +1 -0
  4. package/build/actions/index.js +7 -0
  5. package/build/actions/index.js.map +1 -1
  6. package/build/actions/permanently-delete-post.js +0 -1
  7. package/build/actions/permanently-delete-post.js.map +1 -1
  8. package/build/actions/rename-post.js +2 -2
  9. package/build/actions/rename-post.js.map +1 -1
  10. package/build/actions/reset-post.js +3 -3
  11. package/build/actions/reset-post.js.map +1 -1
  12. package/build/actions/restore-post.js +0 -1
  13. package/build/actions/restore-post.js.map +1 -1
  14. package/build/actions/trash-post.js +0 -1
  15. package/build/actions/trash-post.js.map +1 -1
  16. package/build/actions/utils.js +4 -12
  17. package/build/actions/utils.js.map +1 -1
  18. package/build/components/create-template-part-modal/index.js +213 -0
  19. package/build/components/create-template-part-modal/index.js.map +1 -0
  20. package/build/components/create-template-part-modal/utils.js +64 -0
  21. package/build/components/create-template-part-modal/utils.js.map +1 -0
  22. package/build/fields/index.js +7 -0
  23. package/build/fields/index.js.map +1 -1
  24. package/build/fields/slug/slug-view.js +1 -1
  25. package/build/fields/slug/slug-view.js.map +1 -1
  26. package/build/fields/slug/utils.js +3 -0
  27. package/build/fields/slug/utils.js.map +1 -1
  28. package/build/fields/template/index.js +28 -0
  29. package/build/fields/template/index.js.map +1 -0
  30. package/build/fields/template/template-edit.js +154 -0
  31. package/build/fields/template/template-edit.js.map +1 -0
  32. package/build/fields/title/title-view.js +1 -1
  33. package/build/fields/title/title-view.js.map +1 -1
  34. package/build/index.js +13 -0
  35. package/build/index.js.map +1 -1
  36. package/build/mutation/index.js +0 -1
  37. package/build/mutation/index.js.map +1 -1
  38. package/build/types.js.map +1 -1
  39. package/build-module/actions/duplicate-template-part.js +63 -0
  40. package/build-module/actions/duplicate-template-part.js.map +1 -0
  41. package/build-module/actions/index.js +1 -0
  42. package/build-module/actions/index.js.map +1 -1
  43. package/build-module/actions/permanently-delete-post.js +0 -1
  44. package/build-module/actions/permanently-delete-post.js.map +1 -1
  45. package/build-module/actions/rename-post.js +3 -3
  46. package/build-module/actions/rename-post.js.map +1 -1
  47. package/build-module/actions/reset-post.js +4 -4
  48. package/build-module/actions/reset-post.js.map +1 -1
  49. package/build-module/actions/restore-post.js +0 -1
  50. package/build-module/actions/restore-post.js.map +1 -1
  51. package/build-module/actions/trash-post.js +0 -1
  52. package/build-module/actions/trash-post.js.map +1 -1
  53. package/build-module/actions/utils.js +4 -11
  54. package/build-module/actions/utils.js.map +1 -1
  55. package/build-module/components/create-template-part-modal/index.js +204 -0
  56. package/build-module/components/create-template-part-modal/index.js.map +1 -0
  57. package/build-module/components/create-template-part-modal/utils.js +55 -0
  58. package/build-module/components/create-template-part-modal/utils.js.map +1 -0
  59. package/build-module/fields/index.js +1 -0
  60. package/build-module/fields/index.js.map +1 -1
  61. package/build-module/fields/slug/slug-view.js +1 -1
  62. package/build-module/fields/slug/slug-view.js.map +1 -1
  63. package/build-module/fields/slug/utils.js +3 -0
  64. package/build-module/fields/slug/utils.js.map +1 -1
  65. package/build-module/fields/template/index.js +21 -0
  66. package/build-module/fields/template/index.js.map +1 -0
  67. package/build-module/fields/template/template-edit.js +144 -0
  68. package/build-module/fields/template/template-edit.js.map +1 -0
  69. package/build-module/fields/title/title-view.js +1 -1
  70. package/build-module/fields/title/title-view.js.map +1 -1
  71. package/build-module/index.js +1 -0
  72. package/build-module/index.js.map +1 -1
  73. package/build-module/mutation/index.js +0 -1
  74. package/build-module/mutation/index.js.map +1 -1
  75. package/build-module/types.js.map +1 -1
  76. package/build-style/style-rtl.css +74 -0
  77. package/build-style/style.css +74 -0
  78. package/build-types/actions/duplicate-template-part.d.ts +11 -0
  79. package/build-types/actions/duplicate-template-part.d.ts.map +1 -0
  80. package/build-types/actions/index.d.ts +1 -0
  81. package/build-types/actions/index.d.ts.map +1 -1
  82. package/build-types/actions/rename-post.d.ts.map +1 -1
  83. package/build-types/actions/reset-post.d.ts.map +1 -1
  84. package/build-types/actions/utils.d.ts +7 -8
  85. package/build-types/actions/utils.d.ts.map +1 -1
  86. package/build-types/components/create-template-part-modal/index.d.ts +34 -0
  87. package/build-types/components/create-template-part-modal/index.d.ts.map +1 -0
  88. package/build-types/components/create-template-part-modal/utils.d.ts +4 -0
  89. package/build-types/components/create-template-part-modal/utils.d.ts.map +1 -0
  90. package/build-types/fields/index.d.ts +1 -0
  91. package/build-types/fields/index.d.ts.map +1 -1
  92. package/build-types/fields/slug/utils.d.ts.map +1 -1
  93. package/build-types/fields/template/index.d.ts +8 -0
  94. package/build-types/fields/template/index.d.ts.map +1 -0
  95. package/build-types/fields/template/template-edit.d.ts +4 -0
  96. package/build-types/fields/template/template-edit.d.ts.map +1 -0
  97. package/build-types/fields/title/title-view.d.ts.map +1 -1
  98. package/build-types/index.d.ts +2 -1
  99. package/build-types/index.d.ts.map +1 -1
  100. package/build-types/lock-unlock.d.ts +1 -1
  101. package/build-types/lock-unlock.d.ts.map +1 -1
  102. package/build-types/types.d.ts +3 -0
  103. package/build-types/types.d.ts.map +1 -1
  104. package/package.json +24 -23
  105. package/src/actions/duplicate-template-part.tsx +72 -0
  106. package/src/actions/index.ts +1 -0
  107. package/src/actions/rename-post.tsx +3 -6
  108. package/src/actions/reset-post.tsx +4 -9
  109. package/src/actions/utils.ts +7 -15
  110. package/src/components/create-template-part-modal/index.tsx +274 -0
  111. package/src/components/create-template-part-modal/style.scss +63 -0
  112. package/src/components/create-template-part-modal/test/utils.js +60 -0
  113. package/src/components/create-template-part-modal/utils.js +68 -0
  114. package/src/fields/index.ts +1 -0
  115. package/src/fields/slug/slug-view.tsx +1 -1
  116. package/src/fields/slug/utils.ts +4 -0
  117. package/src/fields/template/index.ts +22 -0
  118. package/src/fields/template/style.scss +23 -0
  119. package/src/fields/template/template-edit.tsx +210 -0
  120. package/src/fields/title/title-view.tsx +3 -4
  121. package/src/index.ts +2 -1
  122. package/src/style.scss +2 -0
  123. package/src/types.ts +3 -0
  124. package/tsconfig.json +2 -0
  125. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,213 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.CreateTemplatePartModalContents = CreateTemplatePartModalContents;
7
+ exports.default = CreateTemplatePartModal;
8
+ var _components = require("@wordpress/components");
9
+ var _compose = require("@wordpress/compose");
10
+ var _coreData = require("@wordpress/core-data");
11
+ var _data = require("@wordpress/data");
12
+ var _element = require("@wordpress/element");
13
+ var _i18n = require("@wordpress/i18n");
14
+ var _icons = require("@wordpress/icons");
15
+ var _notices = require("@wordpress/notices");
16
+ var _blocks = require("@wordpress/blocks");
17
+ var _utils = require("./utils");
18
+ var _jsxRuntime = require("react/jsx-runtime");
19
+ /**
20
+ * WordPress dependencies
21
+ */
22
+
23
+ // @ts-ignore
24
+
25
+ /**
26
+ * Internal dependencies
27
+ */
28
+
29
+ /**
30
+ * A React component that renders a modal for creating a template part. The modal displays a title and the contents for creating the template part.
31
+ * This component should not live in this package, it should be moved to a dedicated package responsible for managing template.
32
+ * @param {Object} props The component props.
33
+ * @param props.modalTitle
34
+ */
35
+ function CreateTemplatePartModal({
36
+ modalTitle,
37
+ ...restProps
38
+ }) {
39
+ const defaultModalTitle = (0, _data.useSelect)(select =>
40
+ // @ts-ignore
41
+ select(_coreData.store).getPostType('wp_template_part')?.labels?.add_new_item, []);
42
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Modal, {
43
+ title: modalTitle || defaultModalTitle,
44
+ onRequestClose: restProps.closeModal,
45
+ overlayClassName: "fields-create-template-part-modal",
46
+ focusOnMount: "firstContentElement",
47
+ size: "medium",
48
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(CreateTemplatePartModalContents, {
49
+ ...restProps
50
+ })
51
+ });
52
+ }
53
+ const getTemplatePartIcon = iconName => {
54
+ if ('header' === iconName) {
55
+ return _icons.header;
56
+ } else if ('footer' === iconName) {
57
+ return _icons.footer;
58
+ } else if ('sidebar' === iconName) {
59
+ return _icons.sidebar;
60
+ }
61
+ return _icons.symbolFilled;
62
+ };
63
+
64
+ /**
65
+ * A React component that renders the content of a model for creating a template part.
66
+ * This component should not live in this package; it should be moved to a dedicated package responsible for managing template.
67
+ *
68
+ * @param {Object} props - The component props.
69
+ * @param {string} [props.defaultArea=uncategorized] - The default area for the template part.
70
+ * @param {Array} [props.blocks=[]] - The blocks to be included in the template part.
71
+ * @param {string} [props.confirmLabel='Add'] - The label for the confirm button.
72
+ * @param {Function} props.closeModal - Function to close the modal.
73
+ * @param {Function} props.onCreate - Function to call when the template part is successfully created.
74
+ * @param {Function} [props.onError] - Function to call when there is an error creating the template part.
75
+ * @param {string} [props.defaultTitle=''] - The default title for the template part.
76
+ */
77
+ function CreateTemplatePartModalContents({
78
+ defaultArea = 'uncategorized',
79
+ blocks = [],
80
+ confirmLabel = (0, _i18n.__)('Add'),
81
+ closeModal,
82
+ onCreate,
83
+ onError,
84
+ defaultTitle = ''
85
+ }) {
86
+ const {
87
+ createErrorNotice
88
+ } = (0, _data.useDispatch)(_notices.store);
89
+ const {
90
+ saveEntityRecord
91
+ } = (0, _data.useDispatch)(_coreData.store);
92
+ const existingTemplateParts = (0, _utils.useExistingTemplateParts)();
93
+ const [title, setTitle] = (0, _element.useState)(defaultTitle);
94
+ const [area, setArea] = (0, _element.useState)(defaultArea);
95
+ const [isSubmitting, setIsSubmitting] = (0, _element.useState)(false);
96
+ const instanceId = (0, _compose.useInstanceId)(CreateTemplatePartModal);
97
+ const defaultTemplatePartAreas = (0, _data.useSelect)(select => {
98
+ const areas =
99
+ // @ts-expect-error getEntityRecord is not typed with unstableBase as argument.
100
+ select(_coreData.store).getEntityRecord('root', '__unstableBase')?.default_template_part_areas || [];
101
+ return areas.map(item => {
102
+ return {
103
+ ...item,
104
+ icon: getTemplatePartIcon(item.icon)
105
+ };
106
+ });
107
+ }, []);
108
+ async function createTemplatePart() {
109
+ if (!title || isSubmitting) {
110
+ return;
111
+ }
112
+ try {
113
+ setIsSubmitting(true);
114
+ const uniqueTitle = (0, _utils.getUniqueTemplatePartTitle)(title, existingTemplateParts);
115
+ const cleanSlug = (0, _utils.getCleanTemplatePartSlug)(uniqueTitle);
116
+ const templatePart = await saveEntityRecord('postType', 'wp_template_part', {
117
+ slug: cleanSlug,
118
+ title: uniqueTitle,
119
+ content: (0, _blocks.serialize)(blocks),
120
+ area
121
+ }, {
122
+ throwOnError: true
123
+ });
124
+ await onCreate(templatePart);
125
+
126
+ // TODO: Add a success notice?
127
+ } catch (error) {
128
+ const errorMessage = error instanceof Error && 'code' in error && error.message && error.code !== 'unknown_error' ? error.message : (0, _i18n.__)('An error occurred while creating the template part.');
129
+ createErrorNotice(errorMessage, {
130
+ type: 'snackbar'
131
+ });
132
+ onError?.();
133
+ } finally {
134
+ setIsSubmitting(false);
135
+ }
136
+ }
137
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("form", {
138
+ onSubmit: async event => {
139
+ event.preventDefault();
140
+ await createTemplatePart();
141
+ },
142
+ children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalVStack, {
143
+ spacing: "4",
144
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.TextControl, {
145
+ __next40pxDefaultSize: true,
146
+ __nextHasNoMarginBottom: true,
147
+ label: (0, _i18n.__)('Name'),
148
+ value: title,
149
+ onChange: setTitle,
150
+ required: true
151
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.BaseControl, {
152
+ __nextHasNoMarginBottom: true,
153
+ label: (0, _i18n.__)('Area'),
154
+ id: `fields-create-template-part-modal__area-selection-${instanceId}`,
155
+ className: "fields-create-template-part-modal__area-base-control",
156
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalRadioGroup, {
157
+ label: (0, _i18n.__)('Area'),
158
+ className: "fields-create-template-part-modal__area-radio-group",
159
+ id: `fields-create-template-part-modal__area-selection-${instanceId}`,
160
+ onChange: value => value && typeof value === 'string' ? setArea(value) : () => void 0,
161
+ checked: area,
162
+ children: defaultTemplatePartAreas.map(({
163
+ icon,
164
+ label,
165
+ area: value,
166
+ description
167
+ }) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalRadio, {
168
+ __next40pxDefaultSize: true,
169
+ value: value,
170
+ className: "fields-create-template-part-modal__area-radio",
171
+ children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.Flex, {
172
+ align: "start",
173
+ justify: "start",
174
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.FlexItem, {
175
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Icon, {
176
+ icon: icon
177
+ })
178
+ }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.FlexBlock, {
179
+ className: "fields-create-template-part-modal__option-label",
180
+ children: [label, /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
181
+ children: description
182
+ })]
183
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.FlexItem, {
184
+ className: "fields-create-template-part-modal__checkbox",
185
+ children: area === value && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Icon, {
186
+ icon: _icons.check
187
+ })
188
+ })]
189
+ })
190
+ }, label))
191
+ })
192
+ }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalHStack, {
193
+ justify: "right",
194
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
195
+ __next40pxDefaultSize: true,
196
+ variant: "tertiary",
197
+ onClick: () => {
198
+ closeModal();
199
+ },
200
+ children: (0, _i18n.__)('Cancel')
201
+ }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
202
+ __next40pxDefaultSize: true,
203
+ variant: "primary",
204
+ type: "submit",
205
+ "aria-disabled": !title || isSubmitting,
206
+ isBusy: isSubmitting,
207
+ children: confirmLabel
208
+ })]
209
+ })]
210
+ })
211
+ });
212
+ }
213
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_components","require","_compose","_coreData","_data","_element","_i18n","_icons","_notices","_blocks","_utils","_jsxRuntime","CreateTemplatePartModal","modalTitle","restProps","defaultModalTitle","useSelect","select","coreStore","getPostType","labels","add_new_item","jsx","Modal","title","onRequestClose","closeModal","overlayClassName","focusOnMount","size","children","CreateTemplatePartModalContents","getTemplatePartIcon","iconName","headerIcon","footerIcon","sidebarIcon","symbolFilledIcon","defaultArea","blocks","confirmLabel","__","onCreate","onError","defaultTitle","createErrorNotice","useDispatch","noticesStore","saveEntityRecord","existingTemplateParts","useExistingTemplateParts","setTitle","useState","area","setArea","isSubmitting","setIsSubmitting","instanceId","useInstanceId","defaultTemplatePartAreas","areas","getEntityRecord","default_template_part_areas","map","item","icon","createTemplatePart","uniqueTitle","getUniqueTemplatePartTitle","cleanSlug","getCleanTemplatePartSlug","templatePart","slug","content","serialize","throwOnError","error","errorMessage","Error","message","code","type","onSubmit","event","preventDefault","jsxs","__experimentalVStack","spacing","TextControl","__next40pxDefaultSize","__nextHasNoMarginBottom","label","value","onChange","required","BaseControl","id","className","__experimentalRadioGroup","checked","description","__experimentalRadio","Flex","align","justify","FlexItem","Icon","FlexBlock","check","__experimentalHStack","Button","variant","onClick","isBusy"],"sources":["@wordpress/fields/src/components/create-template-part-modal/index.tsx"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport {\n\tIcon,\n\tBaseControl,\n\tTextControl,\n\tFlex,\n\tFlexItem,\n\tFlexBlock,\n\tButton,\n\tModal,\n\t__experimentalRadioGroup as RadioGroup,\n\t__experimentalRadio as Radio,\n\t__experimentalHStack as HStack,\n\t__experimentalVStack as VStack,\n} from '@wordpress/components';\nimport { useInstanceId } from '@wordpress/compose';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport { useState } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\nimport {\n\tcheck,\n\tfooter as footerIcon,\n\theader as headerIcon,\n\tsidebar as sidebarIcon,\n\tsymbolFilled as symbolFilledIcon,\n} from '@wordpress/icons';\nimport { store as noticesStore } from '@wordpress/notices';\n// @ts-ignore\nimport { serialize } from '@wordpress/blocks';\n\n/**\n * Internal dependencies\n */\nimport {\n\tgetCleanTemplatePartSlug,\n\tgetUniqueTemplatePartTitle,\n\tuseExistingTemplateParts,\n} from './utils';\n\ntype CreateTemplatePartModalContentsProps = {\n\tdefaultArea?: string;\n\tblocks: any[];\n\tconfirmLabel?: string;\n\tcloseModal: () => void;\n\tonCreate: ( templatePart: any ) => void;\n\tonError?: () => void;\n\tdefaultTitle?: string;\n};\n\n/**\n * A React component that renders a modal for creating a template part. The modal displays a title and the contents for creating the template part.\n * This component should not live in this package, it should be moved to a dedicated package responsible for managing template.\n * @param {Object} props The component props.\n * @param props.modalTitle\n */\nexport default function CreateTemplatePartModal( {\n\tmodalTitle,\n\t...restProps\n}: {\n\tmodalTitle: string;\n} & CreateTemplatePartModalContentsProps ) {\n\tconst defaultModalTitle = useSelect(\n\t\t( select ) =>\n\t\t\t// @ts-ignore\n\t\t\tselect( coreStore ).getPostType( 'wp_template_part' )?.labels\n\t\t\t\t?.add_new_item,\n\t\t[]\n\t);\n\treturn (\n\t\t<Modal\n\t\t\ttitle={ modalTitle || defaultModalTitle }\n\t\t\tonRequestClose={ restProps.closeModal }\n\t\t\toverlayClassName=\"fields-create-template-part-modal\"\n\t\t\tfocusOnMount=\"firstContentElement\"\n\t\t\tsize=\"medium\"\n\t\t>\n\t\t\t{ /* @ts-ignore */ }\n\t\t\t<CreateTemplatePartModalContents { ...restProps } />\n\t\t</Modal>\n\t);\n}\n\nconst getTemplatePartIcon = ( iconName: string ) => {\n\tif ( 'header' === iconName ) {\n\t\treturn headerIcon;\n\t} else if ( 'footer' === iconName ) {\n\t\treturn footerIcon;\n\t} else if ( 'sidebar' === iconName ) {\n\t\treturn sidebarIcon;\n\t}\n\treturn symbolFilledIcon;\n};\n\n/**\n * A React component that renders the content of a model for creating a template part.\n * This component should not live in this package; it should be moved to a dedicated package responsible for managing template.\n *\n * @param {Object} props - The component props.\n * @param {string} [props.defaultArea=uncategorized] - The default area for the template part.\n * @param {Array} [props.blocks=[]] - The blocks to be included in the template part.\n * @param {string} [props.confirmLabel='Add'] - The label for the confirm button.\n * @param {Function} props.closeModal - Function to close the modal.\n * @param {Function} props.onCreate - Function to call when the template part is successfully created.\n * @param {Function} [props.onError] - Function to call when there is an error creating the template part.\n * @param {string} [props.defaultTitle=''] - The default title for the template part.\n */\nexport function CreateTemplatePartModalContents( {\n\tdefaultArea = 'uncategorized',\n\tblocks = [],\n\tconfirmLabel = __( 'Add' ),\n\tcloseModal,\n\tonCreate,\n\tonError,\n\tdefaultTitle = '',\n}: CreateTemplatePartModalContentsProps ) {\n\tconst { createErrorNotice } = useDispatch( noticesStore );\n\tconst { saveEntityRecord } = useDispatch( coreStore );\n\tconst existingTemplateParts = useExistingTemplateParts();\n\n\tconst [ title, setTitle ] = useState( defaultTitle );\n\tconst [ area, setArea ] = useState( defaultArea );\n\tconst [ isSubmitting, setIsSubmitting ] = useState( false );\n\tconst instanceId = useInstanceId( CreateTemplatePartModal );\n\n\tconst defaultTemplatePartAreas = useSelect( ( select ) => {\n\t\tconst areas =\n\t\t\t// @ts-expect-error getEntityRecord is not typed with unstableBase as argument.\n\t\t\tselect( coreStore ).getEntityRecord< {\n\t\t\t\tdefault_template_part_areas: Array< {\n\t\t\t\t\tarea: string;\n\t\t\t\t\tlabel: string;\n\t\t\t\t\ticon: string;\n\t\t\t\t\tdescription: string;\n\t\t\t\t} >;\n\t\t\t} >( 'root', '__unstableBase' )?.default_template_part_areas || [];\n\n\t\treturn areas.map( ( item ) => {\n\t\t\treturn { ...item, icon: getTemplatePartIcon( item.icon ) };\n\t\t} );\n\t}, [] );\n\n\tasync function createTemplatePart() {\n\t\tif ( ! title || isSubmitting ) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tsetIsSubmitting( true );\n\t\t\tconst uniqueTitle = getUniqueTemplatePartTitle(\n\t\t\t\ttitle,\n\t\t\t\texistingTemplateParts\n\t\t\t);\n\t\t\tconst cleanSlug = getCleanTemplatePartSlug( uniqueTitle );\n\n\t\t\tconst templatePart = await saveEntityRecord(\n\t\t\t\t'postType',\n\t\t\t\t'wp_template_part',\n\t\t\t\t{\n\t\t\t\t\tslug: cleanSlug,\n\t\t\t\t\ttitle: uniqueTitle,\n\t\t\t\t\tcontent: serialize( blocks ),\n\t\t\t\t\tarea,\n\t\t\t\t},\n\t\t\t\t{ throwOnError: true }\n\t\t\t);\n\t\t\tawait onCreate( templatePart );\n\n\t\t\t// TODO: Add a success notice?\n\t\t} catch ( error ) {\n\t\t\tconst errorMessage =\n\t\t\t\terror instanceof Error &&\n\t\t\t\t'code' in error &&\n\t\t\t\terror.message &&\n\t\t\t\terror.code !== 'unknown_error'\n\t\t\t\t\t? error.message\n\t\t\t\t\t: __(\n\t\t\t\t\t\t\t'An error occurred while creating the template part.'\n\t\t\t\t\t );\n\n\t\t\tcreateErrorNotice( errorMessage, { type: 'snackbar' } );\n\n\t\t\tonError?.();\n\t\t} finally {\n\t\t\tsetIsSubmitting( false );\n\t\t}\n\t}\n\treturn (\n\t\t<form\n\t\t\tonSubmit={ async ( event ) => {\n\t\t\t\tevent.preventDefault();\n\t\t\t\tawait createTemplatePart();\n\t\t\t} }\n\t\t>\n\t\t\t<VStack spacing=\"4\">\n\t\t\t\t<TextControl\n\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t__nextHasNoMarginBottom\n\t\t\t\t\tlabel={ __( 'Name' ) }\n\t\t\t\t\tvalue={ title }\n\t\t\t\t\tonChange={ setTitle }\n\t\t\t\t\trequired\n\t\t\t\t/>\n\t\t\t\t<BaseControl\n\t\t\t\t\t__nextHasNoMarginBottom\n\t\t\t\t\tlabel={ __( 'Area' ) }\n\t\t\t\t\tid={ `fields-create-template-part-modal__area-selection-${ instanceId }` }\n\t\t\t\t\tclassName=\"fields-create-template-part-modal__area-base-control\"\n\t\t\t\t>\n\t\t\t\t\t<RadioGroup\n\t\t\t\t\t\tlabel={ __( 'Area' ) }\n\t\t\t\t\t\tclassName=\"fields-create-template-part-modal__area-radio-group\"\n\t\t\t\t\t\tid={ `fields-create-template-part-modal__area-selection-${ instanceId }` }\n\t\t\t\t\t\tonChange={ ( value ) =>\n\t\t\t\t\t\t\tvalue && typeof value === 'string'\n\t\t\t\t\t\t\t\t? setArea( value )\n\t\t\t\t\t\t\t\t: () => void 0\n\t\t\t\t\t\t}\n\t\t\t\t\t\tchecked={ area }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ defaultTemplatePartAreas.map(\n\t\t\t\t\t\t\t( { icon, label, area: value, description } ) => (\n\t\t\t\t\t\t\t\t<Radio\n\t\t\t\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\t\t\t\tkey={ label }\n\t\t\t\t\t\t\t\t\tvalue={ value }\n\t\t\t\t\t\t\t\t\tclassName=\"fields-create-template-part-modal__area-radio\"\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t<Flex align=\"start\" justify=\"start\">\n\t\t\t\t\t\t\t\t\t\t<FlexItem>\n\t\t\t\t\t\t\t\t\t\t\t<Icon icon={ icon } />\n\t\t\t\t\t\t\t\t\t\t</FlexItem>\n\t\t\t\t\t\t\t\t\t\t<FlexBlock className=\"fields-create-template-part-modal__option-label\">\n\t\t\t\t\t\t\t\t\t\t\t{ label }\n\t\t\t\t\t\t\t\t\t\t\t<div>{ description }</div>\n\t\t\t\t\t\t\t\t\t\t</FlexBlock>\n\n\t\t\t\t\t\t\t\t\t\t<FlexItem className=\"fields-create-template-part-modal__checkbox\">\n\t\t\t\t\t\t\t\t\t\t\t{ area === value && (\n\t\t\t\t\t\t\t\t\t\t\t\t<Icon icon={ check } />\n\t\t\t\t\t\t\t\t\t\t\t) }\n\t\t\t\t\t\t\t\t\t\t</FlexItem>\n\t\t\t\t\t\t\t\t\t</Flex>\n\t\t\t\t\t\t\t\t</Radio>\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t) }\n\t\t\t\t\t</RadioGroup>\n\t\t\t\t</BaseControl>\n\t\t\t\t<HStack justify=\"right\">\n\t\t\t\t\t<Button\n\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\tvariant=\"tertiary\"\n\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\tcloseModal();\n\t\t\t\t\t\t} }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ __( 'Cancel' ) }\n\t\t\t\t\t</Button>\n\t\t\t\t\t<Button\n\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\tvariant=\"primary\"\n\t\t\t\t\t\ttype=\"submit\"\n\t\t\t\t\t\taria-disabled={ ! title || isSubmitting }\n\t\t\t\t\t\tisBusy={ isSubmitting }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ confirmLabel }\n\t\t\t\t\t</Button>\n\t\t\t\t</HStack>\n\t\t\t</VStack>\n\t\t</form>\n\t);\n}\n"],"mappings":";;;;;;;AAGA,IAAAA,WAAA,GAAAC,OAAA;AAcA,IAAAC,QAAA,GAAAD,OAAA;AACA,IAAAE,SAAA,GAAAF,OAAA;AACA,IAAAG,KAAA,GAAAH,OAAA;AACA,IAAAI,QAAA,GAAAJ,OAAA;AACA,IAAAK,KAAA,GAAAL,OAAA;AACA,IAAAM,MAAA,GAAAN,OAAA;AAOA,IAAAO,QAAA,GAAAP,OAAA;AAEA,IAAAQ,OAAA,GAAAR,OAAA;AAKA,IAAAS,MAAA,GAAAT,OAAA;AAIiB,IAAAU,WAAA,GAAAV,OAAA;AAxCjB;AACA;AACA;;AA4BA;;AAGA;AACA;AACA;;AAiBA;AACA;AACA;AACA;AACA;AACA;AACe,SAASW,uBAAuBA,CAAE;EAChDC,UAAU;EACV,GAA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SAAS,EAAC,6CAA6C;gBAAAnE,QAAA,EAC9DuB,IAAI,KAAKuC,KAAK,iBACf,IAAAjF,WAAA,CAAAW,GAAA,EAACtB,WAAA,CAAA0G,IAAI;kBAACzC,IAAI,EAAG2C;gBAAO,CAAE;cACtB,CACQ,CAAC;YAAA,CACN;UAAC,GAlBDjB,KAmBA,CAET;QAAC,CACU;MAAC,CACD,CAAC,eACd,IAAAhF,WAAA,CAAA0E,IAAA,EAACrF,WAAA,CAAA6G,oBAAM;QAACL,OAAO,EAAC,OAAO;QAAA1E,QAAA,gBACtB,IAAAnB,WAAA,CAAAW,GAAA,EAACtB,WAAA,CAAA8G,MAAM;UACNrB,qBAAqB;UACrBsB,OAAO,EAAC,UAAU;UAClBC,OAAO,EAAGA,CAAA,KAAM;YACftF,UAAU,CAAC,CAAC;UACb,CAAG;UAAAI,QAAA,EAED,IAAAW,QAAE,EAAE,QAAS;QAAC,CACT,CAAC,eACT,IAAA9B,WAAA,CAAAW,GAAA,EAACtB,WAAA,CAAA8G,MAAM;UACNrB,qBAAqB;UACrBsB,OAAO,EAAC,SAAS;UACjB9B,IAAI,EAAC,QAAQ;UACb,iBAAgB,CAAEzD,KAAK,IAAI+B,YAAc;UACzC0D,MAAM,EAAG1D,YAAc;UAAAzB,QAAA,EAErBU;QAAY,CACP,CAAC;MAAA,CACF,CAAC;IAAA,CACF;EAAC,CACJ,CAAC;AAET","ignoreList":[]}
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useExistingTemplateParts = exports.getUniqueTemplatePartTitle = exports.getCleanTemplatePartSlug = void 0;
7
+ var _changeCase = require("change-case");
8
+ var _data = require("@wordpress/data");
9
+ var _coreData = require("@wordpress/core-data");
10
+ /**
11
+ * External dependencies
12
+ */
13
+
14
+ /**
15
+ * WordPress dependencies
16
+ */
17
+
18
+ /**
19
+ * Internal dependencies
20
+ */
21
+
22
+ const useExistingTemplateParts = () => {
23
+ var _useSelect;
24
+ return (_useSelect = (0, _data.useSelect)(select => select(_coreData.store).getEntityRecords('postType', 'wp_template_part', {
25
+ per_page: -1
26
+ }), [])) !== null && _useSelect !== void 0 ? _useSelect : [];
27
+ };
28
+
29
+ /**
30
+ * Return a unique template part title based on
31
+ * the given title and existing template parts.
32
+ *
33
+ * @param {string} title The original template part title.
34
+ * @param {Object} templateParts The array of template part entities.
35
+ * @return {string} A unique template part title.
36
+ */
37
+ exports.useExistingTemplateParts = useExistingTemplateParts;
38
+ const getUniqueTemplatePartTitle = (title, templateParts) => {
39
+ const lowercaseTitle = title.toLowerCase();
40
+ const existingTitles = templateParts.map(templatePart => templatePart.title.rendered.toLowerCase());
41
+ if (!existingTitles.includes(lowercaseTitle)) {
42
+ return title;
43
+ }
44
+ let suffix = 2;
45
+ while (existingTitles.includes(`${lowercaseTitle} ${suffix}`)) {
46
+ suffix++;
47
+ }
48
+ return `${title} ${suffix}`;
49
+ };
50
+
51
+ /**
52
+ * Get a valid slug for a template part.
53
+ * Currently template parts only allow latin chars.
54
+ * The fallback slug will receive suffix by default.
55
+ *
56
+ * @param {string} title The template part title.
57
+ * @return {string} A valid template part slug.
58
+ */
59
+ exports.getUniqueTemplatePartTitle = getUniqueTemplatePartTitle;
60
+ const getCleanTemplatePartSlug = title => {
61
+ return (0, _changeCase.paramCase)(title).replace(/[^\w-]+/g, '') || 'wp-custom-part';
62
+ };
63
+ exports.getCleanTemplatePartSlug = getCleanTemplatePartSlug;
64
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_changeCase","require","_data","_coreData","useExistingTemplateParts","_useSelect","useSelect","select","coreStore","getEntityRecords","per_page","exports","getUniqueTemplatePartTitle","title","templateParts","lowercaseTitle","toLowerCase","existingTitles","map","templatePart","rendered","includes","suffix","getCleanTemplatePartSlug","kebabCase","replace"],"sources":["@wordpress/fields/src/components/create-template-part-modal/utils.js"],"sourcesContent":["/**\n * External dependencies\n */\nimport { paramCase as kebabCase } from 'change-case';\n\n/**\n * WordPress dependencies\n */\nimport { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\n\n/**\n * Internal dependencies\n */\n\nexport const useExistingTemplateParts = () => {\n\treturn (\n\t\tuseSelect(\n\t\t\t( select ) =>\n\t\t\t\tselect( coreStore ).getEntityRecords(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template_part',\n\t\t\t\t\t{\n\t\t\t\t\t\tper_page: -1,\n\t\t\t\t\t}\n\t\t\t\t),\n\t\t\t[]\n\t\t) ?? []\n\t);\n};\n\n/**\n * Return a unique template part title based on\n * the given title and existing template parts.\n *\n * @param {string} title The original template part title.\n * @param {Object} templateParts The array of template part entities.\n * @return {string} A unique template part title.\n */\nexport const getUniqueTemplatePartTitle = ( title, templateParts ) => {\n\tconst lowercaseTitle = title.toLowerCase();\n\tconst existingTitles = templateParts.map( ( templatePart ) =>\n\t\ttemplatePart.title.rendered.toLowerCase()\n\t);\n\n\tif ( ! existingTitles.includes( lowercaseTitle ) ) {\n\t\treturn title;\n\t}\n\n\tlet suffix = 2;\n\twhile ( existingTitles.includes( `${ lowercaseTitle } ${ suffix }` ) ) {\n\t\tsuffix++;\n\t}\n\n\treturn `${ title } ${ suffix }`;\n};\n\n/**\n * Get a valid slug for a template part.\n * Currently template parts only allow latin chars.\n * The fallback slug will receive suffix by default.\n *\n * @param {string} title The template part title.\n * @return {string} A valid template part slug.\n */\nexport const getCleanTemplatePartSlug = ( title ) => {\n\treturn kebabCase( title ).replace( /[^\\w-]+/g, '' ) || 'wp-custom-part';\n};\n"],"mappings":";;;;;;AAGA,IAAAA,WAAA,GAAAC,OAAA;AAKA,IAAAC,KAAA,GAAAD,OAAA;AACA,IAAAE,SAAA,GAAAF,OAAA;AATA;AACA;AACA;;AAGA;AACA;AACA;;AAIA;AACA;AACA;;AAEO,MAAMG,wBAAwB,GAAGA,CAAA,KAAM;EAAA,IAAAC,UAAA;EAC7C,QAAAA,UAAA,GACC,IAAAC,eAAS,EACNC,MAAM,IACPA,MAAM,CAAEC,eAAU,CAAC,CAACC,gBAAgB,CACnC,UAAU,EACV,kBAAkB,EAClB;IACCC,QAAQ,EAAE,CAAC;EACZ,CACD,CAAC,EACF,EACD,CAAC,cAAAL,UAAA,cAAAA,UAAA,GAAI,EAAE;AAET,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAPAM,OAAA,CAAAP,wBAAA,GAAAA,wBAAA;AAQO,MAAMQ,0BAA0B,GAAGA,CAAEC,KAAK,EAAEC,aAAa,KAAM;EACrE,MAAMC,cAAc,GAAGF,KAAK,CAACG,WAAW,CAAC,CAAC;EAC1C,MAAMC,cAAc,GAAGH,aAAa,CAACI,GAAG,CAAIC,YAAY,IACvDA,YAAY,CAACN,KAAK,CAACO,QAAQ,CAACJ,WAAW,CAAC,CACzC,CAAC;EAED,IAAK,CAAEC,cAAc,CAACI,QAAQ,CAAEN,cAAe,CAAC,EAAG;IAClD,OAAOF,KAAK;EACb;EAEA,IAAIS,MAAM,GAAG,CAAC;EACd,OAAQL,cAAc,CAACI,QAAQ,CAAE,GAAIN,cAAc,IAAMO,MAAM,EAAI,CAAC,EAAG;IACtEA,MAAM,EAAE;EACT;EAEA,OAAO,GAAIT,KAAK,IAAMS,MAAM,EAAG;AAChC,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAPAX,OAAA,CAAAC,0BAAA,GAAAA,0BAAA;AAQO,MAAMW,wBAAwB,GAAKV,KAAK,IAAM;EACpD,OAAO,IAAAW,qBAAS,EAAEX,KAAM,CAAC,CAACY,OAAO,CAAE,UAAU,EAAE,EAAG,CAAC,IAAI,gBAAgB;AACxE,CAAC;AAACd,OAAA,CAAAY,wBAAA,GAAAA,wBAAA","ignoreList":[]}
@@ -58,6 +58,12 @@ Object.defineProperty(exports, "statusField", {
58
58
  return _status.default;
59
59
  }
60
60
  });
61
+ Object.defineProperty(exports, "templateField", {
62
+ enumerable: true,
63
+ get: function () {
64
+ return _template.default;
65
+ }
66
+ });
61
67
  Object.defineProperty(exports, "titleField", {
62
68
  enumerable: true,
63
69
  get: function () {
@@ -68,6 +74,7 @@ var _slug = _interopRequireDefault(require("./slug"));
68
74
  var _title = _interopRequireDefault(require("./title"));
69
75
  var _order = _interopRequireDefault(require("./order"));
70
76
  var _featuredImage = _interopRequireDefault(require("./featured-image"));
77
+ var _template = _interopRequireDefault(require("./template"));
71
78
  var _parent = _interopRequireDefault(require("./parent"));
72
79
  var _password = _interopRequireDefault(require("./password"));
73
80
  var _status = _interopRequireDefault(require("./status"));
@@ -1 +1 @@
1
- {"version":3,"names":["_slug","_interopRequireDefault","require","_title","_order","_featuredImage","_parent","_password","_status","_commentStatus","_date","_author"],"sources":["@wordpress/fields/src/fields/index.ts"],"sourcesContent":["export { default as slugField } from './slug';\nexport { default as titleField } from './title';\nexport { default as orderField } from './order';\nexport { default as featuredImageField } from './featured-image';\nexport { default as parentField } from './parent';\nexport { default as passwordField } from './password';\nexport { default as statusField } from './status';\nexport { default as commentStatusField } from './comment-status';\nexport { default as dateField } from './date';\nexport { default as authorField } from './author';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,KAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,MAAA,GAAAF,sBAAA,CAAAC,OAAA;AACA,IAAAE,MAAA,GAAAH,sBAAA,CAAAC,OAAA;AACA,IAAAG,cAAA,GAAAJ,sBAAA,CAAAC,OAAA;AACA,IAAAI,OAAA,GAAAL,sBAAA,CAAAC,OAAA;AACA,IAAAK,SAAA,GAAAN,sBAAA,CAAAC,OAAA;AACA,IAAAM,OAAA,GAAAP,sBAAA,CAAAC,OAAA;AACA,IAAAO,cAAA,GAAAR,sBAAA,CAAAC,OAAA;AACA,IAAAQ,KAAA,GAAAT,sBAAA,CAAAC,OAAA;AACA,IAAAS,OAAA,GAAAV,sBAAA,CAAAC,OAAA","ignoreList":[]}
1
+ {"version":3,"names":["_slug","_interopRequireDefault","require","_title","_order","_featuredImage","_template","_parent","_password","_status","_commentStatus","_date","_author"],"sources":["@wordpress/fields/src/fields/index.ts"],"sourcesContent":["export { default as slugField } from './slug';\nexport { default as titleField } from './title';\nexport { default as orderField } from './order';\nexport { default as featuredImageField } from './featured-image';\nexport { default as templateField } from './template';\nexport { default as parentField } from './parent';\nexport { default as passwordField } from './password';\nexport { default as statusField } from './status';\nexport { default as commentStatusField } from './comment-status';\nexport { default as dateField } from './date';\nexport { default as authorField } from './author';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,KAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,MAAA,GAAAF,sBAAA,CAAAC,OAAA;AACA,IAAAE,MAAA,GAAAH,sBAAA,CAAAC,OAAA;AACA,IAAAG,cAAA,GAAAJ,sBAAA,CAAAC,OAAA;AACA,IAAAI,SAAA,GAAAL,sBAAA,CAAAC,OAAA;AACA,IAAAK,OAAA,GAAAN,sBAAA,CAAAC,OAAA;AACA,IAAAM,SAAA,GAAAP,sBAAA,CAAAC,OAAA;AACA,IAAAO,OAAA,GAAAR,sBAAA,CAAAC,OAAA;AACA,IAAAQ,cAAA,GAAAT,sBAAA,CAAAC,OAAA;AACA,IAAAS,KAAA,GAAAV,sBAAA,CAAAC,OAAA;AACA,IAAAU,OAAA,GAAAX,sBAAA,CAAAC,OAAA","ignoreList":[]}
@@ -17,7 +17,7 @@ var _utils = require("./utils");
17
17
  const SlugView = ({
18
18
  item
19
19
  }) => {
20
- const slug = typeof item === 'object' ? (0, _utils.getSlug)(item) : '';
20
+ const slug = (0, _utils.getSlug)(item);
21
21
  const originalSlugRef = (0, _element.useRef)(slug);
22
22
  (0, _element.useEffect)(() => {
23
23
  if (slug && originalSlugRef.current === undefined) {
@@ -1 +1 @@
1
- {"version":3,"names":["_element","require","_utils","SlugView","item","slug","getSlug","originalSlugRef","useRef","useEffect","current","undefined","slugToDisplay","_default","exports","default"],"sources":["@wordpress/fields/src/fields/slug/slug-view.tsx"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useEffect, useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport type { BasePost } from '../../types';\nimport { getSlug } from './utils';\n\nconst SlugView = ( { item }: { item: BasePost } ) => {\n\tconst slug = typeof item === 'object' ? getSlug( item ) : '';\n\tconst originalSlugRef = useRef( slug );\n\n\tuseEffect( () => {\n\t\tif ( slug && originalSlugRef.current === undefined ) {\n\t\t\toriginalSlugRef.current = slug;\n\t\t}\n\t}, [ slug ] );\n\n\tconst slugToDisplay = slug || originalSlugRef.current;\n\n\treturn `${ slugToDisplay }`;\n};\n\nexport default SlugView;\n"],"mappings":";;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AAMA,IAAAC,MAAA,GAAAD,OAAA;AATA;AACA;AACA;;AAGA;AACA;AACA;;AAIA,MAAME,QAAQ,GAAGA,CAAE;EAAEC;AAAyB,CAAC,KAAM;EACpD,MAAMC,IAAI,GAAG,OAAOD,IAAI,KAAK,QAAQ,GAAG,IAAAE,cAAO,EAAEF,IAAK,CAAC,GAAG,EAAE;EAC5D,MAAMG,eAAe,GAAG,IAAAC,eAAM,EAAEH,IAAK,CAAC;EAEtC,IAAAI,kBAAS,EAAE,MAAM;IAChB,IAAKJ,IAAI,IAAIE,eAAe,CAACG,OAAO,KAAKC,SAAS,EAAG;MACpDJ,eAAe,CAACG,OAAO,GAAGL,IAAI;IAC/B;EACD,CAAC,EAAE,CAAEA,IAAI,CAAG,CAAC;EAEb,MAAMO,aAAa,GAAGP,IAAI,IAAIE,eAAe,CAACG,OAAO;EAErD,OAAO,GAAIE,aAAa,EAAG;AAC5B,CAAC;AAAC,IAAAC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEaZ,QAAQ","ignoreList":[]}
1
+ {"version":3,"names":["_element","require","_utils","SlugView","item","slug","getSlug","originalSlugRef","useRef","useEffect","current","undefined","slugToDisplay","_default","exports","default"],"sources":["@wordpress/fields/src/fields/slug/slug-view.tsx"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useEffect, useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport type { BasePost } from '../../types';\nimport { getSlug } from './utils';\n\nconst SlugView = ( { item }: { item: BasePost } ) => {\n\tconst slug = getSlug( item );\n\tconst originalSlugRef = useRef( slug );\n\n\tuseEffect( () => {\n\t\tif ( slug && originalSlugRef.current === undefined ) {\n\t\t\toriginalSlugRef.current = slug;\n\t\t}\n\t}, [ slug ] );\n\n\tconst slugToDisplay = slug || originalSlugRef.current;\n\n\treturn `${ slugToDisplay }`;\n};\n\nexport default SlugView;\n"],"mappings":";;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AAMA,IAAAC,MAAA,GAAAD,OAAA;AATA;AACA;AACA;;AAGA;AACA;AACA;;AAIA,MAAME,QAAQ,GAAGA,CAAE;EAAEC;AAAyB,CAAC,KAAM;EACpD,MAAMC,IAAI,GAAG,IAAAC,cAAO,EAAEF,IAAK,CAAC;EAC5B,MAAMG,eAAe,GAAG,IAAAC,eAAM,EAAEH,IAAK,CAAC;EAEtC,IAAAI,kBAAS,EAAE,MAAM;IAChB,IAAKJ,IAAI,IAAIE,eAAe,CAACG,OAAO,KAAKC,SAAS,EAAG;MACpDJ,eAAe,CAACG,OAAO,GAAGL,IAAI;IAC/B;EACD,CAAC,EAAE,CAAEA,IAAI,CAAG,CAAC;EAEb,MAAMO,aAAa,GAAGP,IAAI,IAAIE,eAAe,CAACG,OAAO;EAErD,OAAO,GAAIE,aAAa,EAAG;AAC5B,CAAC;AAAC,IAAAC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEaZ,QAAQ","ignoreList":[]}
@@ -15,6 +15,9 @@ var _utils = require("../../actions/utils");
15
15
  */
16
16
 
17
17
  const getSlug = item => {
18
+ if (typeof item !== 'object') {
19
+ return '';
20
+ }
18
21
  return item.slug || (0, _url.cleanForSlug)((0, _utils.getItemTitle)(item)) || item.id.toString();
19
22
  };
20
23
  exports.getSlug = getSlug;
@@ -1 +1 @@
1
- {"version":3,"names":["_url","require","_utils","getSlug","item","slug","cleanForSlug","getItemTitle","id","toString","exports"],"sources":["@wordpress/fields/src/fields/slug/utils.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { cleanForSlug } from '@wordpress/url';\n/**\n * Internal dependencies\n */\nimport type { BasePost } from '../../types';\nimport { getItemTitle } from '../../actions/utils';\n\nexport const getSlug = ( item: BasePost ): string => {\n\treturn (\n\t\titem.slug || cleanForSlug( getItemTitle( item ) ) || item.id.toString()\n\t);\n};\n"],"mappings":";;;;;;AAGA,IAAAA,IAAA,GAAAC,OAAA;AAKA,IAAAC,MAAA,GAAAD,OAAA;AARA;AACA;AACA;;AAEA;AACA;AACA;;AAIO,MAAME,OAAO,GAAKC,IAAc,IAAc;EACpD,OACCA,IAAI,CAACC,IAAI,IAAI,IAAAC,iBAAY,EAAE,IAAAC,mBAAY,EAAEH,IAAK,CAAE,CAAC,IAAIA,IAAI,CAACI,EAAE,CAACC,QAAQ,CAAC,CAAC;AAEzE,CAAC;AAACC,OAAA,CAAAP,OAAA,GAAAA,OAAA","ignoreList":[]}
1
+ {"version":3,"names":["_url","require","_utils","getSlug","item","slug","cleanForSlug","getItemTitle","id","toString","exports"],"sources":["@wordpress/fields/src/fields/slug/utils.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { cleanForSlug } from '@wordpress/url';\n/**\n * Internal dependencies\n */\nimport type { BasePost } from '../../types';\nimport { getItemTitle } from '../../actions/utils';\n\nexport const getSlug = ( item: BasePost ): string => {\n\tif ( typeof item !== 'object' ) {\n\t\treturn '';\n\t}\n\n\treturn (\n\t\titem.slug || cleanForSlug( getItemTitle( item ) ) || item.id.toString()\n\t);\n};\n"],"mappings":";;;;;;AAGA,IAAAA,IAAA,GAAAC,OAAA;AAKA,IAAAC,MAAA,GAAAD,OAAA;AARA;AACA;AACA;;AAEA;AACA;AACA;;AAIO,MAAME,OAAO,GAAKC,IAAc,IAAc;EACpD,IAAK,OAAOA,IAAI,KAAK,QAAQ,EAAG;IAC/B,OAAO,EAAE;EACV;EAEA,OACCA,IAAI,CAACC,IAAI,IAAI,IAAAC,iBAAY,EAAE,IAAAC,mBAAY,EAAEH,IAAK,CAAE,CAAC,IAAIA,IAAI,CAACI,EAAE,CAACC,QAAQ,CAAC,CAAC;AAEzE,CAAC;AAACC,OAAA,CAAAP,OAAA,GAAAA,OAAA","ignoreList":[]}
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _i18n = require("@wordpress/i18n");
8
+ var _templateEdit = require("./template-edit");
9
+ /**
10
+ * WordPress dependencies
11
+ */
12
+
13
+ /**
14
+ * Internal dependencies
15
+ */
16
+
17
+ const templateField = {
18
+ id: 'template',
19
+ type: 'text',
20
+ label: (0, _i18n.__)('Template'),
21
+ getValue: ({
22
+ item
23
+ }) => item.template,
24
+ Edit: _templateEdit.TemplateEdit,
25
+ enableSorting: false
26
+ };
27
+ var _default = exports.default = templateField;
28
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_i18n","require","_templateEdit","templateField","id","type","label","__","getValue","item","template","Edit","TemplateEdit","enableSorting","_default","exports","default"],"sources":["@wordpress/fields/src/fields/template/index.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport type { Field } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport type { BasePost } from '../../types';\nimport { TemplateEdit } from './template-edit';\n\nconst templateField: Field< BasePost > = {\n\tid: 'template',\n\ttype: 'text',\n\tlabel: __( 'Template' ),\n\tgetValue: ( { item } ) => item.template,\n\tEdit: TemplateEdit,\n\tenableSorting: false,\n};\n\nexport default templateField;\n"],"mappings":";;;;;;AAQA,IAAAA,KAAA,GAAAC,OAAA;AAEA,IAAAC,aAAA,GAAAD,OAAA;AAVA;AACA;AACA;;AAGA;AACA;AACA;;AAKA,MAAME,aAAgC,GAAG;EACxCC,EAAE,EAAE,UAAU;EACdC,IAAI,EAAE,MAAM;EACZC,KAAK,EAAE,IAAAC,QAAE,EAAE,UAAW,CAAC;EACvBC,QAAQ,EAAEA,CAAE;IAAEC;EAAK,CAAC,KAAMA,IAAI,CAACC,QAAQ;EACvCC,IAAI,EAAEC,0BAAY;EAClBC,aAAa,EAAE;AAChB,CAAC;AAAC,IAAAC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEab,aAAa","ignoreList":[]}
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.TemplateEdit = void 0;
7
+ var _element = require("@wordpress/element");
8
+ var _blocks = require("@wordpress/blocks");
9
+ var _coreData = require("@wordpress/core-data");
10
+ var _blockEditor = require("@wordpress/block-editor");
11
+ var _components = require("@wordpress/components");
12
+ var _compose = require("@wordpress/compose");
13
+ var _data = require("@wordpress/data");
14
+ var _htmlEntities = require("@wordpress/html-entities");
15
+ var _i18n = require("@wordpress/i18n");
16
+ var _utils = require("../../actions/utils");
17
+ var _lockUnlock = require("../../lock-unlock");
18
+ var _jsxRuntime = require("react/jsx-runtime");
19
+ /**
20
+ * WordPress dependencies
21
+ */
22
+
23
+ // @ts-ignore
24
+
25
+ /**
26
+ * Internal dependencies
27
+ */
28
+ // @ts-expect-error block-editor is not typed correctly.
29
+
30
+ const TemplateEdit = ({
31
+ data,
32
+ field,
33
+ onChange
34
+ }) => {
35
+ const {
36
+ id
37
+ } = field;
38
+ const postType = data.type;
39
+ const postId = typeof data.id === 'number' ? data.id : parseInt(data.id, 10);
40
+ const slug = data.slug;
41
+ const {
42
+ availableTemplates,
43
+ templates
44
+ } = (0, _data.useSelect)(select => {
45
+ var _select$getEntityReco;
46
+ const allTemplates = (_select$getEntityReco = select(_coreData.store).getEntityRecords('postType', 'wp_template', {
47
+ per_page: -1,
48
+ post_type: postType
49
+ })) !== null && _select$getEntityReco !== void 0 ? _select$getEntityReco : [];
50
+ const {
51
+ getHomePage,
52
+ getPostsPageId
53
+ } = (0, _lockUnlock.unlock)(select(_coreData.store));
54
+ const isPostsPage = getPostsPageId() === +postId;
55
+ const isFrontPage = postType === 'page' && getHomePage()?.postId === +postId;
56
+ const allowSwitchingTemplate = !isPostsPage && !isFrontPage;
57
+ return {
58
+ templates: allTemplates,
59
+ availableTemplates: allowSwitchingTemplate ? allTemplates.filter(template => template.is_custom && template.slug !== data.template && !!template.content.raw // Skip empty templates.
60
+ ) : []
61
+ };
62
+ }, [data.template, postId, postType]);
63
+ const templatesAsPatterns = (0, _element.useMemo)(() => availableTemplates.map(template => ({
64
+ name: template.slug,
65
+ blocks: (0, _blocks.parse)(template.content.raw),
66
+ title: (0, _htmlEntities.decodeEntities)(template.title.rendered),
67
+ id: template.id
68
+ })), [availableTemplates]);
69
+ const shownTemplates = (0, _compose.useAsyncList)(templatesAsPatterns);
70
+ const value = field.getValue({
71
+ item: data
72
+ });
73
+ const currentTemplate = (0, _data.useSelect)(select => {
74
+ const foundTemplate = templates?.find(template => template.slug === value);
75
+ if (foundTemplate) {
76
+ return foundTemplate;
77
+ }
78
+ let slugToCheck;
79
+ // In `draft` status we might not have a slug available, so we use the `single`
80
+ // post type templates slug(ex page, single-post, single-product etc..).
81
+ // Pages do not need the `single` prefix in the slug to be prioritized
82
+ // through template hierarchy.
83
+ if (slug) {
84
+ slugToCheck = postType === 'page' ? `${postType}-${slug}` : `single-${postType}-${slug}`;
85
+ } else {
86
+ slugToCheck = postType === 'page' ? 'page' : `single-${postType}`;
87
+ }
88
+ if (postType) {
89
+ const templateId = select(_coreData.store).getDefaultTemplateId({
90
+ slug: slugToCheck
91
+ });
92
+ return select(_coreData.store).getEntityRecord('postType', 'wp_template', templateId);
93
+ }
94
+ }, [postType, slug, templates, value]);
95
+ const [showModal, setShowModal] = (0, _element.useState)(false);
96
+ const onChangeControl = (0, _element.useCallback)(newValue => onChange({
97
+ [id]: newValue
98
+ }), [id, onChange]);
99
+ return /*#__PURE__*/(0, _jsxRuntime.jsxs)("fieldset", {
100
+ className: "fields-controls__template",
101
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Dropdown, {
102
+ popoverProps: {
103
+ placement: 'bottom-start'
104
+ },
105
+ renderToggle: ({
106
+ onToggle
107
+ }) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
108
+ __next40pxDefaultSize: true,
109
+ variant: "tertiary",
110
+ size: "compact",
111
+ onClick: onToggle,
112
+ children: currentTemplate ? (0, _utils.getItemTitle)(currentTemplate) : ''
113
+ }),
114
+ renderContent: ({
115
+ onToggle
116
+ }) => /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.MenuGroup, {
117
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
118
+ onClick: () => {
119
+ setShowModal(true);
120
+ onToggle();
121
+ },
122
+ children: (0, _i18n.__)('Swap template')
123
+ }),
124
+ // The default template in a post is indicated by an empty string
125
+ value !== '' && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.MenuItem, {
126
+ onClick: () => {
127
+ onChangeControl('');
128
+ onToggle();
129
+ },
130
+ children: (0, _i18n.__)('Use default template')
131
+ })]
132
+ })
133
+ }), showModal && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Modal, {
134
+ title: (0, _i18n.__)('Choose a template'),
135
+ onRequestClose: () => setShowModal(false),
136
+ overlayClassName: "fields-controls__template-modal",
137
+ isFullScreen: true,
138
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
139
+ className: "fields-controls__template-content",
140
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.__experimentalBlockPatternsList, {
141
+ label: (0, _i18n.__)('Templates'),
142
+ blockPatterns: templatesAsPatterns,
143
+ shownPatterns: shownTemplates,
144
+ onClickPattern: template => {
145
+ onChangeControl(template.name);
146
+ setShowModal(false);
147
+ }
148
+ })
149
+ })
150
+ })]
151
+ });
152
+ };
153
+ exports.TemplateEdit = TemplateEdit;
154
+ //# sourceMappingURL=template-edit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_element","require","_blocks","_coreData","_blockEditor","_components","_compose","_data","_htmlEntities","_i18n","_utils","_lockUnlock","_jsxRuntime","TemplateEdit","data","field","onChange","id","postType","type","postId","parseInt","slug","availableTemplates","templates","useSelect","select","_select$getEntityReco","allTemplates","coreStore","getEntityRecords","per_page","post_type","getHomePage","getPostsPageId","unlock","isPostsPage","isFrontPage","allowSwitchingTemplate","filter","template","is_custom","content","raw","templatesAsPatterns","useMemo","map","name","blocks","parse","title","decodeEntities","rendered","shownTemplates","useAsyncList","value","getValue","item","currentTemplate","foundTemplate","find","slugToCheck","templateId","getDefaultTemplateId","getEntityRecord","showModal","setShowModal","useState","onChangeControl","useCallback","newValue","jsxs","className","children","jsx","Dropdown","popoverProps","placement","renderToggle","onToggle","Button","__next40pxDefaultSize","variant","size","onClick","getItemTitle","renderContent","MenuGroup","MenuItem","__","Modal","onRequestClose","overlayClassName","isFullScreen","__experimentalBlockPatternsList","label","blockPatterns","shownPatterns","onClickPattern","exports"],"sources":["@wordpress/fields/src/fields/template/template-edit.tsx"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCallback, useMemo, useState } from '@wordpress/element';\n// @ts-ignore\nimport { parse } from '@wordpress/blocks';\nimport type { WpTemplate } from '@wordpress/core-data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { DataFormControlProps } from '@wordpress/dataviews';\n\n/**\n * Internal dependencies\n */\n// @ts-expect-error block-editor is not typed correctly.\nimport { __experimentalBlockPatternsList as BlockPatternsList } from '@wordpress/block-editor';\nimport {\n\tButton,\n\tDropdown,\n\tMenuGroup,\n\tMenuItem,\n\tModal,\n} from '@wordpress/components';\nimport { useAsyncList } from '@wordpress/compose';\nimport { useSelect } from '@wordpress/data';\nimport { decodeEntities } from '@wordpress/html-entities';\nimport { __ } from '@wordpress/i18n';\nimport { getItemTitle } from '../../actions/utils';\nimport type { BasePost } from '../../types';\nimport { unlock } from '../../lock-unlock';\n\nexport const TemplateEdit = ( {\n\tdata,\n\tfield,\n\tonChange,\n}: DataFormControlProps< BasePost > ) => {\n\tconst { id } = field;\n\tconst postType = data.type;\n\tconst postId =\n\t\ttypeof data.id === 'number' ? data.id : parseInt( data.id, 10 );\n\tconst slug = data.slug;\n\n\tconst { availableTemplates, templates } = useSelect(\n\t\t( select ) => {\n\t\t\tconst allTemplates =\n\t\t\t\tselect( coreStore ).getEntityRecords< WpTemplate >(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template',\n\t\t\t\t\t{\n\t\t\t\t\t\tper_page: -1,\n\t\t\t\t\t\tpost_type: postType,\n\t\t\t\t\t}\n\t\t\t\t) ?? [];\n\n\t\t\tconst { getHomePage, getPostsPageId } = unlock(\n\t\t\t\tselect( coreStore )\n\t\t\t);\n\n\t\t\tconst isPostsPage = getPostsPageId() === +postId;\n\t\t\tconst isFrontPage =\n\t\t\t\tpostType === 'page' && getHomePage()?.postId === +postId;\n\n\t\t\tconst allowSwitchingTemplate = ! isPostsPage && ! isFrontPage;\n\n\t\t\treturn {\n\t\t\t\ttemplates: allTemplates,\n\t\t\t\tavailableTemplates: allowSwitchingTemplate\n\t\t\t\t\t? allTemplates.filter(\n\t\t\t\t\t\t\t( template ) =>\n\t\t\t\t\t\t\t\ttemplate.is_custom &&\n\t\t\t\t\t\t\t\ttemplate.slug !== data.template &&\n\t\t\t\t\t\t\t\t!! template.content.raw // Skip empty templates.\n\t\t\t\t\t )\n\t\t\t\t\t: [],\n\t\t\t};\n\t\t},\n\t\t[ data.template, postId, postType ]\n\t);\n\n\tconst templatesAsPatterns = useMemo(\n\t\t() =>\n\t\t\tavailableTemplates.map( ( template ) => ( {\n\t\t\t\tname: template.slug,\n\t\t\t\tblocks: parse( template.content.raw ),\n\t\t\t\ttitle: decodeEntities( template.title.rendered ),\n\t\t\t\tid: template.id,\n\t\t\t} ) ),\n\t\t[ availableTemplates ]\n\t);\n\n\tconst shownTemplates = useAsyncList( templatesAsPatterns );\n\n\tconst value = field.getValue( { item: data } );\n\n\tconst currentTemplate = useSelect(\n\t\t( select ) => {\n\t\t\tconst foundTemplate = templates?.find(\n\t\t\t\t( template ) => template.slug === value\n\t\t\t);\n\n\t\t\tif ( foundTemplate ) {\n\t\t\t\treturn foundTemplate;\n\t\t\t}\n\n\t\t\tlet slugToCheck;\n\t\t\t// In `draft` status we might not have a slug available, so we use the `single`\n\t\t\t// post type templates slug(ex page, single-post, single-product etc..).\n\t\t\t// Pages do not need the `single` prefix in the slug to be prioritized\n\t\t\t// through template hierarchy.\n\t\t\tif ( slug ) {\n\t\t\t\tslugToCheck =\n\t\t\t\t\tpostType === 'page'\n\t\t\t\t\t\t? `${ postType }-${ slug }`\n\t\t\t\t\t\t: `single-${ postType }-${ slug }`;\n\t\t\t} else {\n\t\t\t\tslugToCheck =\n\t\t\t\t\tpostType === 'page' ? 'page' : `single-${ postType }`;\n\t\t\t}\n\n\t\t\tif ( postType ) {\n\t\t\t\tconst templateId = select( coreStore ).getDefaultTemplateId( {\n\t\t\t\t\tslug: slugToCheck,\n\t\t\t\t} );\n\n\t\t\t\treturn select( coreStore ).getEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template',\n\t\t\t\t\ttemplateId\n\t\t\t\t);\n\t\t\t}\n\t\t},\n\t\t[ postType, slug, templates, value ]\n\t);\n\n\tconst [ showModal, setShowModal ] = useState( false );\n\n\tconst onChangeControl = useCallback(\n\t\t( newValue: string ) =>\n\t\t\tonChange( {\n\t\t\t\t[ id ]: newValue,\n\t\t\t} ),\n\t\t[ id, onChange ]\n\t);\n\n\treturn (\n\t\t<fieldset className=\"fields-controls__template\">\n\t\t\t<Dropdown\n\t\t\t\tpopoverProps={ { placement: 'bottom-start' } }\n\t\t\t\trenderToggle={ ( { onToggle } ) => (\n\t\t\t\t\t<Button\n\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\tvariant=\"tertiary\"\n\t\t\t\t\t\tsize=\"compact\"\n\t\t\t\t\t\tonClick={ onToggle }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ currentTemplate\n\t\t\t\t\t\t\t? getItemTitle( currentTemplate )\n\t\t\t\t\t\t\t: '' }\n\t\t\t\t\t</Button>\n\t\t\t\t) }\n\t\t\t\trenderContent={ ( { onToggle } ) => (\n\t\t\t\t\t<MenuGroup>\n\t\t\t\t\t\t<MenuItem\n\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\tsetShowModal( true );\n\t\t\t\t\t\t\t\tonToggle();\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{ __( 'Swap template' ) }\n\t\t\t\t\t\t</MenuItem>\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t// The default template in a post is indicated by an empty string\n\t\t\t\t\t\t\tvalue !== '' && (\n\t\t\t\t\t\t\t\t<MenuItem\n\t\t\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\t\t\tonChangeControl( '' );\n\t\t\t\t\t\t\t\t\t\tonToggle();\n\t\t\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t{ __( 'Use default template' ) }\n\t\t\t\t\t\t\t\t</MenuItem>\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t</MenuGroup>\n\t\t\t\t) }\n\t\t\t/>\n\t\t\t{ showModal && (\n\t\t\t\t<Modal\n\t\t\t\t\ttitle={ __( 'Choose a template' ) }\n\t\t\t\t\tonRequestClose={ () => setShowModal( false ) }\n\t\t\t\t\toverlayClassName=\"fields-controls__template-modal\"\n\t\t\t\t\tisFullScreen\n\t\t\t\t>\n\t\t\t\t\t<div className=\"fields-controls__template-content\">\n\t\t\t\t\t\t<BlockPatternsList\n\t\t\t\t\t\t\tlabel={ __( 'Templates' ) }\n\t\t\t\t\t\t\tblockPatterns={ templatesAsPatterns }\n\t\t\t\t\t\t\tshownPatterns={ shownTemplates }\n\t\t\t\t\t\t\tonClickPattern={ (\n\t\t\t\t\t\t\t\ttemplate: ( typeof templatesAsPatterns )[ 0 ]\n\t\t\t\t\t\t\t) => {\n\t\t\t\t\t\t\t\tonChangeControl( template.name );\n\t\t\t\t\t\t\t\tsetShowModal( false );\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t/>\n\t\t\t\t\t</div>\n\t\t\t\t</Modal>\n\t\t\t) }\n\t\t</fieldset>\n\t);\n};\n"],"mappings":";;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AAEA,IAAAC,OAAA,GAAAD,OAAA;AAEA,IAAAE,SAAA,GAAAF,OAAA;AAOA,IAAAG,YAAA,GAAAH,OAAA;AACA,IAAAI,WAAA,GAAAJ,OAAA;AAOA,IAAAK,QAAA,GAAAL,OAAA;AACA,IAAAM,KAAA,GAAAN,OAAA;AACA,IAAAO,aAAA,GAAAP,OAAA;AACA,IAAAQ,KAAA,GAAAR,OAAA;AACA,IAAAS,MAAA,GAAAT,OAAA;AAEA,IAAAU,WAAA,GAAAV,OAAA;AAA2C,IAAAW,WAAA,GAAAX,OAAA;AA5B3C;AACA;AACA;;AAEA;;AAMA;AACA;AACA;AACA;;AAiBO,MAAMY,YAAY,GAAGA,CAAE;EAC7BC,IAAI;EACJC,KAAK;EACLC;AACiC,CAAC,KAAM;EACxC,MAAM;IAAEC;EAAG,CAAC,GAAGF,KAAK;EACpB,MAAMG,QAAQ,GAAGJ,IAAI,CAACK,IAAI;EAC1B,MAAMC,MAAM,GACX,OAAON,IAAI,CAACG,EAAE,KAAK,QAAQ,GAAGH,IAAI,CAACG,EAAE,GAAGI,QAAQ,CAAEP,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B,eAAe,CAAE,EAAG,CAAC;YACrBW,QAAQ,CAAC,CAAC;UACX,CAAG;UAAAN,QAAA,EAED,IAAAgB,QAAE,EAAE,sBAAuB;QAAC,CACrB,CACV;MAAA,CAEQ;IACT,CACH,CAAC,EACAxB,SAAS,iBACV,IAAArD,WAAA,CAAA8D,GAAA,EAACrE,WAAA,CAAAqF,KAAK;MACLxC,KAAK,EAAG,IAAAuC,QAAE,EAAE,mBAAoB,CAAG;MACnCE,cAAc,EAAGA,CAAA,KAAMzB,YAAY,CAAE,KAAM,CAAG;MAC9C0B,gBAAgB,EAAC,iCAAiC;MAClDC,YAAY;MAAApB,QAAA,eAEZ,IAAA7D,WAAA,CAAA8D,GAAA;QAAKF,SAAS,EAAC,mCAAmC;QAAAC,QAAA,eACjD,IAAA7D,WAAA,CAAA8D,GAAA,EAACtE,YAAA,CAAA0F,+BAAiB;UACjBC,KAAK,EAAG,IAAAN,QAAE,EAAE,WAAY,CAAG;UAC3BO,aAAa,EAAGpD,mBAAqB;UACrCqD,aAAa,EAAG5C,cAAgB;UAChC6C,cAAc,EACb1D,QAA6C,IACzC;YACJ4B,eAAe,CAAE5B,QAAQ,CAACO,IAAK,CAAC;YAChCmB,YAAY,CAAE,KAAM,CAAC;UACtB;QAAG,CACH;MAAC,CACE;IAAC,CACA,CACP;EAAA,CACQ,CAAC;AAEb,CAAC;AAACiC,OAAA,CAAAtF,YAAA,GAAAA,YAAA","ignoreList":[]}
@@ -29,7 +29,7 @@ const TitleView = ({
29
29
  const {
30
30
  getEntityRecord
31
31
  } = select(_coreData.store);
32
- const siteSettings = getEntityRecord('root', 'site', '');
32
+ const siteSettings = getEntityRecord('root', 'site');
33
33
  return {
34
34
  frontPageId: siteSettings?.page_on_front,
35
35
  postsPageId: siteSettings?.page_for_posts