PluginProbe
Gutenberg / 21.2.0
Gutenberg v21.2.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / build / server-side-render / index.js

index.js in Gutenberg 21.2.0, at build/server-side-render/index.js

438 lines 14.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /******/ (() => { // webpackBootstrap
2 /******/ "use strict";
3 /******/ var __webpack_modules__ = ({
4
5 /***/ 7734:
6 /***/ ((module) => {
7
8
9
10 // do not edit .js files directly - edit src/index.jst
11
12
13 var envHasBigInt64Array = typeof BigInt64Array !== 'undefined';
14
15
16 module.exports = function equal(a, b) {
17 if (a === b) return true;
18
19 if (a && b && typeof a == 'object' && typeof b == 'object') {
20 if (a.constructor !== b.constructor) return false;
21
22 var length, i, keys;
23 if (Array.isArray(a)) {
24 length = a.length;
25 if (length != b.length) return false;
26 for (i = length; i-- !== 0;)
27 if (!equal(a[i], b[i])) return false;
28 return true;
29 }
30
31
32 if ((a instanceof Map) && (b instanceof Map)) {
33 if (a.size !== b.size) return false;
34 for (i of a.entries())
35 if (!b.has(i[0])) return false;
36 for (i of a.entries())
37 if (!equal(i[1], b.get(i[0]))) return false;
38 return true;
39 }
40
41 if ((a instanceof Set) && (b instanceof Set)) {
42 if (a.size !== b.size) return false;
43 for (i of a.entries())
44 if (!b.has(i[0])) return false;
45 return true;
46 }
47
48 if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
49 length = a.length;
50 if (length != b.length) return false;
51 for (i = length; i-- !== 0;)
52 if (a[i] !== b[i]) return false;
53 return true;
54 }
55
56
57 if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
58 if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf();
59 if (a.toString !== Object.prototype.toString) return a.toString() === b.toString();
60
61 keys = Object.keys(a);
62 length = keys.length;
63 if (length !== Object.keys(b).length) return false;
64
65 for (i = length; i-- !== 0;)
66 if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
67
68 for (i = length; i-- !== 0;) {
69 var key = keys[i];
70
71 if (!equal(a[key], b[key])) return false;
72 }
73
74 return true;
75 }
76
77 // true if both NaN, false otherwise
78 return a!==a && b!==b;
79 };
80
81
82 /***/ })
83
84 /******/ });
85 /************************************************************************/
86 /******/ // The module cache
87 /******/ var __webpack_module_cache__ = {};
88 /******/
89 /******/ // The require function
90 /******/ function __webpack_require__(moduleId) {
91 /******/ // Check if module is in cache
92 /******/ var cachedModule = __webpack_module_cache__[moduleId];
93 /******/ if (cachedModule !== undefined) {
94 /******/ return cachedModule.exports;
95 /******/ }
96 /******/ // Create a new module (and put it into the cache)
97 /******/ var module = __webpack_module_cache__[moduleId] = {
98 /******/ // no module.id needed
99 /******/ // no module.loaded needed
100 /******/ exports: {}
101 /******/ };
102 /******/
103 /******/ // Execute the module function
104 /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
105 /******/
106 /******/ // Return the exports of the module
107 /******/ return module.exports;
108 /******/ }
109 /******/
110 /************************************************************************/
111 /******/ /* webpack/runtime/compat get default export */
112 /******/ (() => {
113 /******/ // getDefaultExport function for compatibility with non-harmony modules
114 /******/ __webpack_require__.n = (module) => {
115 /******/ var getter = module && module.__esModule ?
116 /******/ () => (module['default']) :
117 /******/ () => (module);
118 /******/ __webpack_require__.d(getter, { a: getter });
119 /******/ return getter;
120 /******/ };
121 /******/ })();
122 /******/
123 /******/ /* webpack/runtime/define property getters */
124 /******/ (() => {
125 /******/ // define getter functions for harmony exports
126 /******/ __webpack_require__.d = (exports, definition) => {
127 /******/ for(var key in definition) {
128 /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
129 /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
130 /******/ }
131 /******/ }
132 /******/ };
133 /******/ })();
134 /******/
135 /******/ /* webpack/runtime/hasOwnProperty shorthand */
136 /******/ (() => {
137 /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
138 /******/ })();
139 /******/
140 /************************************************************************/
141 var __webpack_exports__ = {};
142
143 // EXPORTS
144 __webpack_require__.d(__webpack_exports__, {
145 "default": () => (/* binding */ ExportedServerSideRender)
146 });
147
148 ;// external ["wp","element"]
149 const external_wp_element_namespaceObject = window["wp"]["element"];
150 ;// external ["wp","data"]
151 const external_wp_data_namespaceObject = window["wp"]["data"];
152 // EXTERNAL MODULE: ./node_modules/fast-deep-equal/es6/index.js
153 var es6 = __webpack_require__(7734);
154 var es6_default = /*#__PURE__*/__webpack_require__.n(es6);
155 ;// external ["wp","compose"]
156 const external_wp_compose_namespaceObject = window["wp"]["compose"];
157 ;// external ["wp","i18n"]
158 const external_wp_i18n_namespaceObject = window["wp"]["i18n"];
159 ;// external ["wp","apiFetch"]
160 const external_wp_apiFetch_namespaceObject = window["wp"]["apiFetch"];
161 var external_wp_apiFetch_default = /*#__PURE__*/__webpack_require__.n(external_wp_apiFetch_namespaceObject);
162 ;// external ["wp","url"]
163 const external_wp_url_namespaceObject = window["wp"]["url"];
164 ;// external ["wp","components"]
165 const external_wp_components_namespaceObject = window["wp"]["components"];
166 ;// external ["wp","blocks"]
167 const external_wp_blocks_namespaceObject = window["wp"]["blocks"];
168 ;// external "ReactJSXRuntime"
169 const external_ReactJSXRuntime_namespaceObject = window["ReactJSXRuntime"];
170 ;// ./packages/server-side-render/build-module/server-side-render.js
171 /**
172 * External dependencies
173 */
174
175
176 /**
177 * WordPress dependencies
178 */
179
180
181
182
183
184
185
186
187 const EMPTY_OBJECT = {};
188 function rendererPath(block, attributes = null, urlQueryArgs = {}) {
189 return (0,external_wp_url_namespaceObject.addQueryArgs)(`/wp/v2/block-renderer/${block}`, {
190 context: 'edit',
191 ...(null !== attributes ? {
192 attributes
193 } : {}),
194 ...urlQueryArgs
195 });
196 }
197 function removeBlockSupportAttributes(attributes) {
198 const {
199 backgroundColor,
200 borderColor,
201 fontFamily,
202 fontSize,
203 gradient,
204 textColor,
205 className,
206 ...restAttributes
207 } = attributes;
208 const {
209 border,
210 color,
211 elements,
212 spacing,
213 typography,
214 ...restStyles
215 } = attributes?.style || EMPTY_OBJECT;
216 return {
217 ...restAttributes,
218 style: restStyles
219 };
220 }
221 function DefaultEmptyResponsePlaceholder({
222 className
223 }) {
224 return /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_components_namespaceObject.Placeholder, {
225 className: className,
226 children: (0,external_wp_i18n_namespaceObject.__)('Block rendered as empty.')
227 });
228 }
229 function DefaultErrorResponsePlaceholder({
230 response,
231 className
232 }) {
233 const errorMessage = (0,external_wp_i18n_namespaceObject.sprintf)(
234 // translators: %s: error message describing the problem
235 (0,external_wp_i18n_namespaceObject.__)('Error loading block: %s'), response.errorMsg);
236 return /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_components_namespaceObject.Placeholder, {
237 className: className,
238 children: errorMessage
239 });
240 }
241 function DefaultLoadingResponsePlaceholder({
242 children
243 }) {
244 const [showLoader, setShowLoader] = (0,external_wp_element_namespaceObject.useState)(false);
245 (0,external_wp_element_namespaceObject.useEffect)(() => {
246 // Schedule showing the Spinner after 1 second.
247 const timeout = setTimeout(() => {
248 setShowLoader(true);
249 }, 1000);
250 return () => clearTimeout(timeout);
251 }, []);
252 return /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsxs)("div", {
253 style: {
254 position: 'relative'
255 },
256 children: [showLoader && /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)("div", {
257 style: {
258 position: 'absolute',
259 top: '50%',
260 left: '50%',
261 marginTop: '-9px',
262 marginLeft: '-9px'
263 },
264 children: /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_components_namespaceObject.Spinner, {})
265 }), /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)("div", {
266 style: {
267 opacity: showLoader ? '0.3' : 1
268 },
269 children: children
270 })]
271 });
272 }
273 function ServerSideRender(props) {
274 const {
275 className,
276 EmptyResponsePlaceholder = DefaultEmptyResponsePlaceholder,
277 ErrorResponsePlaceholder = DefaultErrorResponsePlaceholder,
278 LoadingResponsePlaceholder = DefaultLoadingResponsePlaceholder
279 } = props;
280 const isMountedRef = (0,external_wp_element_namespaceObject.useRef)(false);
281 const fetchRequestRef = (0,external_wp_element_namespaceObject.useRef)();
282 const [response, setResponse] = (0,external_wp_element_namespaceObject.useState)(null);
283 const prevProps = (0,external_wp_compose_namespaceObject.usePrevious)(props);
284 const [isLoading, setIsLoading] = (0,external_wp_element_namespaceObject.useState)(false);
285 const latestPropsRef = (0,external_wp_element_namespaceObject.useRef)(props);
286 (0,external_wp_element_namespaceObject.useLayoutEffect)(() => {
287 latestPropsRef.current = props;
288 }, [props]);
289 const fetchData = (0,external_wp_element_namespaceObject.useCallback)(() => {
290 var _sanitizedAttributes, _sanitizedAttributes2;
291 if (!isMountedRef.current) {
292 return;
293 }
294 const {
295 attributes,
296 block,
297 skipBlockSupportAttributes = false,
298 httpMethod = 'GET',
299 urlQueryArgs
300 } = latestPropsRef.current;
301 setIsLoading(true);
302 let sanitizedAttributes = attributes && (0,external_wp_blocks_namespaceObject.__experimentalSanitizeBlockAttributes)(block, attributes);
303 if (skipBlockSupportAttributes) {
304 sanitizedAttributes = removeBlockSupportAttributes(sanitizedAttributes);
305 }
306
307 // If httpMethod is 'POST', send the attributes in the request body instead of the URL.
308 // This allows sending a larger attributes object than in a GET request, where the attributes are in the URL.
309 const isPostRequest = 'POST' === httpMethod;
310 const urlAttributes = isPostRequest ? null : (_sanitizedAttributes = sanitizedAttributes) !== null && _sanitizedAttributes !== void 0 ? _sanitizedAttributes : null;
311 const path = rendererPath(block, urlAttributes, urlQueryArgs);
312 const data = isPostRequest ? {
313 attributes: (_sanitizedAttributes2 = sanitizedAttributes) !== null && _sanitizedAttributes2 !== void 0 ? _sanitizedAttributes2 : null
314 } : null;
315
316 // Store the latest fetch request so that when we process it, we can
317 // check if it is the current request, to avoid race conditions on slow networks.
318 const fetchRequest = fetchRequestRef.current = external_wp_apiFetch_default()({
319 path,
320 data,
321 method: isPostRequest ? 'POST' : 'GET'
322 }).then(fetchResponse => {
323 if (isMountedRef.current && fetchRequest === fetchRequestRef.current && fetchResponse) {
324 setResponse(fetchResponse.rendered);
325 }
326 }).catch(error => {
327 if (isMountedRef.current && fetchRequest === fetchRequestRef.current) {
328 setResponse({
329 error: true,
330 errorMsg: error.message
331 });
332 }
333 }).finally(() => {
334 if (isMountedRef.current && fetchRequest === fetchRequestRef.current) {
335 setIsLoading(false);
336 }
337 });
338 return fetchRequest;
339 }, []);
340 const debouncedFetchData = (0,external_wp_compose_namespaceObject.useDebounce)(fetchData, 500);
341
342 // When the component unmounts, set isMountedRef to false. This will
343 // let the async fetch callbacks know when to stop.
344 (0,external_wp_element_namespaceObject.useEffect)(() => {
345 isMountedRef.current = true;
346 return () => {
347 isMountedRef.current = false;
348 };
349 }, []);
350 (0,external_wp_element_namespaceObject.useEffect)(() => {
351 // Don't debounce the first fetch. This ensures that the first render
352 // shows data as soon as possible.
353 if (prevProps === undefined) {
354 fetchData();
355 } else if (!es6_default()(prevProps, props)) {
356 debouncedFetchData();
357 }
358 });
359 const hasResponse = !!response;
360 const hasEmptyResponse = response === '';
361 const hasError = !!response?.error;
362 if (isLoading) {
363 return /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(LoadingResponsePlaceholder, {
364 ...props,
365 children: hasResponse && !hasError && /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_element_namespaceObject.RawHTML, {
366 className: className,
367 children: response
368 })
369 });
370 }
371 if (hasEmptyResponse || !hasResponse) {
372 return /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(EmptyResponsePlaceholder, {
373 ...props
374 });
375 }
376 if (hasError) {
377 return /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(ErrorResponsePlaceholder, {
378 response: response,
379 ...props
380 });
381 }
382 return /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_element_namespaceObject.RawHTML, {
383 className: className,
384 children: response
385 });
386 }
387
388 ;// ./packages/server-side-render/build-module/index.js
389 /**
390 * WordPress dependencies
391 */
392
393
394
395 /**
396 * Internal dependencies
397 */
398
399
400 /**
401 * Constants
402 */
403
404 const build_module_EMPTY_OBJECT = {};
405 function ExportedServerSideRender({
406 urlQueryArgs = build_module_EMPTY_OBJECT,
407 ...props
408 }) {
409 const currentPostId = (0,external_wp_data_namespaceObject.useSelect)(select => {
410 // FIXME: @wordpress/server-side-render should not depend on @wordpress/editor.
411 // It is used by blocks that can be loaded into a *non-post* block editor.
412 // eslint-disable-next-line @wordpress/data-no-store-string-literals
413 const postId = select('core/editor')?.getCurrentPostId();
414
415 // For templates and template parts we use a custom ID format.
416 // Since they aren't real posts, we don't want to use their ID
417 // for server-side rendering. Since they use a string based ID,
418 // we can assume real post IDs are numbers.
419 return postId && typeof postId === 'number' ? postId : null;
420 }, []);
421 const newUrlQueryArgs = (0,external_wp_element_namespaceObject.useMemo)(() => {
422 if (!currentPostId) {
423 return urlQueryArgs;
424 }
425 return {
426 post_id: currentPostId,
427 ...urlQueryArgs
428 };
429 }, [currentPostId, urlQueryArgs]);
430 return /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(ServerSideRender, {
431 urlQueryArgs: newUrlQueryArgs,
432 ...props
433 });
434 }
435
436 (window.wp = window.wp || {}).serverSideRender = __webpack_exports__["default"];
437 /******/ })()
438 ;